Add camera linear rotation

This commit is contained in:
Doyle Thai 2016-06-03 17:05:28 +10:00
parent 472b952408
commit 540b871c58
4 changed files with 133 additions and 21 deletions

BIN
data/blackboard.art Normal file

Binary file not shown.

View File

@ -7,11 +7,13 @@ layout (location = 2) in vec2 texCoord;
out vec3 OurColor; out vec3 OurColor;
out vec2 TexCoord; out vec2 TexCoord;
uniform mat4 transform; uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main() void main()
{ {
gl_Position = transform * vec4(position, 1.0f); gl_Position = projection * view * model * vec4(position, 1.0f);
OurColor = color; OurColor = color;
TexCoord = texCoord; TexCoord = texCoord;
} }

View File

@ -79,16 +79,17 @@ int main()
return -1; return -1;
} }
i32 width, height; i32 screenWidth, screenHeight;
glfwGetFramebufferSize(window, &width, &height); glfwGetFramebufferSize(window, &screenWidth, &screenHeight);
glViewport(0, 0, width, height); glViewport(0, 0, screenWidth, screenHeight);
glfwSetKeyCallback(window, key_callback); glfwSetKeyCallback(window, key_callback);
glEnable(GL_DEPTH_TEST);
/* Initialise shaders */ /* Initialise shaders */
Dengine::Shader shader = Dengine::Shader("data/shaders/default.vert.glsl", Dengine::Shader shader = Dengine::Shader("data/shaders/default.vert.glsl",
"data/shaders/default.frag.glsl"); "data/shaders/default.frag.glsl");
GLuint texture; GLuint texture;
glGenTextures(1, &texture); glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture); glBindTexture(GL_TEXTURE_2D, texture);
@ -123,6 +124,64 @@ int main()
stbi_image_free(image); stbi_image_free(image);
/* Create OGL Vertex objects */ /* Create OGL Vertex objects */
GLfloat vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f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),
glm::vec3( 2.4f, -0.4f, -3.5f),
glm::vec3(-1.7f, 3.0f, -7.5f),
glm::vec3( 1.3f, -2.0f, -2.5f),
glm::vec3( 1.5f, 2.0f, -2.5f),
glm::vec3( 1.5f, 0.2f, -1.5f),
glm::vec3(-1.3f, 1.0f, -1.5f)
};
#if 0
GLfloat vertices[] = { GLfloat vertices[] = {
// Positions Colors Texture Coords // Positions Colors Texture Coords
+0.5f, +0.5f, +0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // Top Right +0.5f, +0.5f, +0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // Top Right
@ -130,16 +189,20 @@ int main()
-0.5f, -0.5f, +0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // Bottom left -0.5f, -0.5f, +0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // Bottom left
-0.5f, +0.5f, +0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, // Top left -0.5f, +0.5f, +0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, // Top left
}; };
#endif
GLuint indices[] = { GLuint indices[] = {
0, 1, 3, // First triangle 0, 1, 3, // First triangle
1, 2, 3, // First triangle 1, 2, 3, // First triangle
}; };
GLuint vbo, vao, ebo;
GLuint vbo, vao;
glGenVertexArrays(1, &vao); glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo); glGenBuffers(1, &vbo);
#if 0
GLuint ebo;
glGenBuffers(1, &ebo); glGenBuffers(1, &ebo);
#endif
// 1. Bind vertex array object // 1. Bind vertex array object
glBindVertexArray(vao); glBindVertexArray(vao);
@ -148,10 +211,10 @@ int main()
glBindBuffer(GL_ARRAY_BUFFER, vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
#if 0
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
const i32 numPos = 3; const i32 numPos = 3;
const i32 numColors = 3; const i32 numColors = 3;
const i32 numTexCoord = 2; const i32 numTexCoord = 2;
@ -172,12 +235,32 @@ int main()
glVertexAttribPointer(2, numTexCoord, GL_FLOAT, GL_FALSE, vertexSize, glVertexAttribPointer(2, numTexCoord, GL_FLOAT, GL_FALSE, vertexSize,
(GLvoid *)texCoordByteOffset); (GLvoid *)texCoordByteOffset);
glEnableVertexAttribArray(2); glEnableVertexAttribArray(2);
#endif
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat),
(GLvoid *)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat),
(GLvoid *)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(2);
// 4. Unbind to prevent mistakes // 4. Unbind to prevent mistakes
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0); glBindVertexArray(0);
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
glm::vec3 cameraTarget = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 cameraDirection = glm::normalize(cameraPos - cameraTarget);
glm::vec3 upVec = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 cameraRight = glm::normalize(glm::cross(upVec, cameraPos));
glm::vec3 cameraUp = glm::cross(cameraDirection, cameraRight);
glm::mat4 view;
// NOTE(doyle): Lookat generates the matrix for camera coordinate axis
view = glm::lookAt(cameraPos, cameraTarget, upVec);
/* Main game loop */ /* Main game loop */
while (!glfwWindowShouldClose(window)) while (!glfwWindowShouldClose(window))
{ {
@ -186,26 +269,48 @@ int main()
/* Rendering commands here*/ /* Rendering commands here*/
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shader.use(); shader.use();
/* Camera */
GLfloat radius = 10.0f;
GLfloat camX = sin(glfwGetTime()) * radius;
GLfloat camZ = cos(glfwGetTime()) * radius;
view = glm::lookAt(glm::vec3(camX, 0.0, camZ), cameraTarget, upVec);
/* Math */ /* Math */
glm::mat4 trans; view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
trans = glm::translate(trans, glm::vec3(0.5f, -0.5f, 0.0f));
GLfloat rotation = glm::radians((GLfloat)glfwGetTime() * 50.0f); glm::mat4 projection;
trans = glm::rotate(trans, rotation, projection =
glm::vec3(0.0, 0.0, 1.0)); glm::perspective(glm::radians(45.0f), ((f32)screenWidth / (f32)screenHeight), 0.1f, 100.0f);
GLuint transformLoc =
glGetUniformLocation(shader.mProgram, "transform");
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(trans)); GLuint modelLoc = glGetUniformLocation(shader.mProgram, "model");
GLuint viewLoc = glGetUniformLocation(shader.mProgram, "view");
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
GLuint projectionLoc = glGetUniformLocation(shader.mProgram, "projection");
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture); glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(glGetUniformLocation(shader.mProgram, "ourTexture"), 0); glUniform1i(glGetUniformLocation(shader.mProgram, "ourTexture"), 0);
glBindVertexArray(vao); glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
for (GLuint i = 0; i < 10; i++)
{
glm::mat4 model;
model = glm::translate(model, cubePositions[i]);
GLfloat angle = glm::radians(20.0f * i);
model = glm::rotate(model, angle, glm::vec3(1.0f, 0.3f, 0.5f));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 36);
}
glBindVertexArray(0); glBindVertexArray(0);
/* Swap the buffers */ /* Swap the buffers */
@ -214,7 +319,9 @@ int main()
glDeleteVertexArrays(1, &vao); glDeleteVertexArrays(1, &vao);
glDeleteBuffers(1, &vbo); glDeleteBuffers(1, &vbo);
#if 0
glDeleteBuffers(1, &ebo); glDeleteBuffers(1, &ebo);
#endif
glfwTerminate(); glfwTerminate();
return 0; return 0;

View File

@ -5,9 +5,12 @@
typedef uint8_t u8; typedef uint8_t u8;
typedef uint32_t u32; typedef uint32_t u32;
typedef int32_t i32; typedef int32_t i32;
typedef i32 b32; typedef i32 b32;
typedef float f32;
#define TRUE 1 #define TRUE 1
#define FALSE 0 #define FALSE 0