u05-2 but boring
This commit is contained in:
parent
83b313919b
commit
46f5c48493
|
@ -1,5 +1,5 @@
|
||||||
#version 330 core
|
#version 330 core
|
||||||
in vec3 color;
|
uniform vec3 color;
|
||||||
void main() {
|
void main() {
|
||||||
gl_FragColor = vec4(color, 1.0);
|
gl_FragColor = vec4(color, 1.0);
|
||||||
}
|
}
|
106
u05-2/main.c
106
u05-2/main.c
|
@ -12,6 +12,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#define RESTART 345678
|
#define RESTART 345678
|
||||||
|
|
||||||
|
@ -19,11 +20,14 @@ GLuint program;
|
||||||
GLuint vao;
|
GLuint vao;
|
||||||
GLuint cubeIndicesBufferObject;
|
GLuint cubeIndicesBufferObject;
|
||||||
|
|
||||||
|
bool exitRequested = false;
|
||||||
|
|
||||||
GLFWwindow* window;
|
GLFWwindow* window;
|
||||||
|
|
||||||
GLfloat aspectRatio = 1.0f;
|
GLfloat aspectRatio = 1.0f;
|
||||||
|
|
||||||
GLfloat step = 0.0f;
|
GLfloat step = 0.0f;
|
||||||
|
const GLfloat pi = 3.14159f;
|
||||||
|
|
||||||
GLfloat cameraPosition[3] = {0.0f, 0.0f, 2.0f};
|
GLfloat cameraPosition[3] = {0.0f, 0.0f, 2.0f};
|
||||||
|
|
||||||
|
@ -77,18 +81,20 @@ void handleInputs(void) {
|
||||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
|
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
|
||||||
cameraPosition[2] -= 0.02f;
|
cameraPosition[2] -= 0.02f;
|
||||||
}
|
}
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
|
||||||
|
cameraPosition[1] += 0.02f;
|
||||||
|
}
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) {
|
||||||
|
cameraPosition[1] -= 0.02f;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void keyboardHandler(GLFWwindow* window, int key, int scancode, int action, int mods) {
|
void keyboardHandler(GLFWwindow* window, int key, int scancode, int action, int mods) {
|
||||||
// if (action == GLFW_PRESS) {
|
if (action == GLFW_PRESS) {
|
||||||
// if (key == GLFW_KEY_W) {
|
if (key == GLFW_KEY_ESCAPE) {
|
||||||
// cameraVelocity[0] += 0.1f;
|
exitRequested = true;
|
||||||
// }
|
}
|
||||||
// if (key == GLFW_KEY_S) {
|
}
|
||||||
// cameraVelocity[0] -= 0.1f;
|
|
||||||
// }
|
|
||||||
// if (key == GLFW_KEY_S)
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void init(void) {
|
void init(void) {
|
||||||
|
@ -191,9 +197,6 @@ void init(void) {
|
||||||
glEnable(GL_PRIMITIVE_RESTART);
|
glEnable(GL_PRIMITIVE_RESTART);
|
||||||
glPrimitiveRestartIndex(RESTART);
|
glPrimitiveRestartIndex(RESTART);
|
||||||
|
|
||||||
// ENABLE DEPTH TESTING
|
|
||||||
// glEnable(GL_DEPTH_TEST);
|
|
||||||
|
|
||||||
// DEFINE INDEX ARRAY FOR ELEMENT DRAWING
|
// DEFINE INDEX ARRAY FOR ELEMENT DRAWING
|
||||||
glGenBuffers(1, &cubeIndicesBufferObject);
|
glGenBuffers(1, &cubeIndicesBufferObject);
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cubeIndicesBufferObject);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cubeIndicesBufferObject);
|
||||||
|
@ -203,6 +206,41 @@ void init(void) {
|
||||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void drawCube(GLfloat* position, GLfloat* scaleVec, GLfloat* rotateVec) {
|
||||||
|
GLfloat modelTransformation[16];
|
||||||
|
|
||||||
|
identity(modelTransformation);
|
||||||
|
|
||||||
|
scale(modelTransformation, modelTransformation, scaleVec);
|
||||||
|
|
||||||
|
rotateX(modelTransformation, modelTransformation, rotateVec[0]);
|
||||||
|
rotateY(modelTransformation, modelTransformation, rotateVec[1]);
|
||||||
|
rotateZ(modelTransformation, modelTransformation, rotateVec[2]);
|
||||||
|
|
||||||
|
translate(modelTransformation, modelTransformation, position);
|
||||||
|
|
||||||
|
|
||||||
|
glUniformMatrix4fv(glGetUniformLocation(program, "modelTransformation"), 1, GL_FALSE, modelTransformation);
|
||||||
|
|
||||||
|
glUniform3f(glGetUniformLocation(program, "color"), 1.0f, 0.0f, 0.0f);
|
||||||
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)(0 * sizeof(GLuint)));
|
||||||
|
|
||||||
|
glUniform3f(glGetUniformLocation(program, "color"), 1.0f, 1.0f, 0.0f);
|
||||||
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)(6 * sizeof(GLuint)));
|
||||||
|
|
||||||
|
glUniform3f(glGetUniformLocation(program, "color"), 1.0f, 1.0f, 1.0f);
|
||||||
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)(12 * sizeof(GLuint)));
|
||||||
|
|
||||||
|
glUniform3f(glGetUniformLocation(program, "color"), 0.0f, 1.0f, 1.0f);
|
||||||
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)(18 * sizeof(GLuint)));
|
||||||
|
|
||||||
|
glUniform3f(glGetUniformLocation(program, "color"), 0.0f, 0.0f, 1.0f);
|
||||||
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)(24 * sizeof(GLuint)));
|
||||||
|
|
||||||
|
glUniform3f(glGetUniformLocation(program, "color"), 1.0f, 0.0f, 1.0f);
|
||||||
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)(30 * sizeof(GLuint)));
|
||||||
|
}
|
||||||
|
|
||||||
void draw(void) {
|
void draw(void) {
|
||||||
|
|
||||||
handleInputs();
|
handleInputs();
|
||||||
|
@ -214,26 +252,24 @@ void draw(void) {
|
||||||
|
|
||||||
step += 0.002f;
|
step += 0.002f;
|
||||||
if (step > 1.0f) step -= 1.0f;
|
if (step > 1.0f) step -= 1.0f;
|
||||||
GLfloat stepi = step * 3.14159 * 2;
|
GLfloat stepi = step * pi * 2;
|
||||||
|
|
||||||
|
|
||||||
// ------------- MODEL TRANSFORMATION ---------------------
|
// ------------- MODEL TRANSFORMATION ---------------------
|
||||||
// SCALE -> ROTATE -> TRANSLATE
|
// SCALE -> ROTATE -> TRANSLATE
|
||||||
|
|
||||||
GLfloat scaleFactor = 0.6f;
|
// GLfloat cubePosition[3] = {0.0f, -2.0f, 0.0f};
|
||||||
// GLfloat cubePosition[3] = {0.0f, sin(3.14159f * 2 * step), 0.0f};
|
// GLfloat cubeScale[3] = {4.0f, 0.2f, 4.0f};
|
||||||
GLfloat cubePosition[3] = {0.0f, 0.0f, 0.0f};
|
|
||||||
GLfloat cubeScale[3] = {scaleFactor, scaleFactor, scaleFactor};
|
|
||||||
|
|
||||||
GLfloat modelTransformation[16];
|
// GLfloat modelTransformation[16];
|
||||||
identity(modelTransformation);
|
// identity(modelTransformation);
|
||||||
scale(modelTransformation, modelTransformation, cubeScale);
|
// scale(modelTransformation, modelTransformation, cubeScale);
|
||||||
|
|
||||||
rotateY(modelTransformation, modelTransformation, stepi);
|
// rotateY(modelTransformation, modelTransformation, stepi);
|
||||||
rotateX(modelTransformation, modelTransformation, stepi + 1.0f);
|
// rotateX(modelTransformation, modelTransformation, stepi + 1.0f);
|
||||||
rotateZ(modelTransformation, modelTransformation, stepi + 0.5f);
|
// rotateZ(modelTransformation, modelTransformation, stepi + 0.5f);
|
||||||
|
|
||||||
translate(modelTransformation, modelTransformation, cubePosition);
|
// translate(modelTransformation, modelTransformation, cubePosition);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -279,8 +315,24 @@ void draw(void) {
|
||||||
|
|
||||||
glUniformMatrix4fv(glGetUniformLocation(program, "globalTransformation"), 1, GL_FALSE, globalTransformation);
|
glUniformMatrix4fv(glGetUniformLocation(program, "globalTransformation"), 1, GL_FALSE, globalTransformation);
|
||||||
|
|
||||||
glUniformMatrix4fv(glGetUniformLocation(program, "modelTransformation"), 1, GL_FALSE, modelTransformation);
|
GLfloat position[3] = {0.0f, -3.0f, 0.0f};
|
||||||
glDrawElements(GL_TRIANGLES, sizeof(cubeIndices) / sizeof(GLuint), GL_UNSIGNED_INT, NULL);
|
GLfloat scaleVec[3] = {3.0f, 0.1f, 3.0f};
|
||||||
|
GLfloat rotateVec[3] = {0.0f, 0.0f, 0.0f};
|
||||||
|
drawCube(position, scaleVec, rotateVec);
|
||||||
|
|
||||||
|
position[1] = -2.8f;
|
||||||
|
scaleVec[0] = 2.0f;
|
||||||
|
scaleVec[2] = 2.0f;
|
||||||
|
rotateVec[0] = pi;
|
||||||
|
drawCube(position, scaleVec, rotateVec);
|
||||||
|
|
||||||
|
position[1] = -2.4f;
|
||||||
|
scaleVec[0] = 1.0f;
|
||||||
|
scaleVec[1] = 0.3f;
|
||||||
|
scaleVec[2] = 1.0f;
|
||||||
|
rotateVec[0] = 0.0f;
|
||||||
|
rotateVec[1] = pi / 4;
|
||||||
|
drawCube(position, scaleVec, rotateVec);
|
||||||
}
|
}
|
||||||
|
|
||||||
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
|
||||||
|
@ -338,7 +390,7 @@ int main(void) {
|
||||||
|
|
||||||
init();
|
init();
|
||||||
|
|
||||||
while (!glfwWindowShouldClose(window)) {
|
while (!glfwWindowShouldClose(window) && !exitRequested) {
|
||||||
draw();
|
draw();
|
||||||
|
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
|
|
|
@ -2,8 +2,7 @@
|
||||||
layout (location = 0) in vec3 aPosition;
|
layout (location = 0) in vec3 aPosition;
|
||||||
uniform mat4 globalTransformation;
|
uniform mat4 globalTransformation;
|
||||||
uniform mat4 modelTransformation;
|
uniform mat4 modelTransformation;
|
||||||
out vec3 color;
|
|
||||||
void main() {
|
void main() {
|
||||||
color = aPosition / 2 + vec3(0.5, 0.5, 0.5);
|
// color = aPosition / 2 + vec3(0.5, 0.5, 0.5);
|
||||||
gl_Position = globalTransformation * modelTransformation * vec4(aPosition, 1.0);
|
gl_Position = globalTransformation * modelTransformation * vec4(aPosition, 1.0);
|
||||||
}
|
}
|
Loading…
Reference in New Issue