Added some transformations
This commit is contained in:
@@ -4,6 +4,9 @@
|
||||
|
||||
#include <GL/glew.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include "LogUtils.hpp"
|
||||
#include "ShaderProgramBuilder.hpp"
|
||||
@@ -59,14 +62,14 @@ App::App(GLFWwindow *window):
|
||||
glfwSetKeyCallback(mpWindow, key_callback_s);
|
||||
|
||||
float vertices[] = {
|
||||
// Position(3), Color(2), TexCoord(2)
|
||||
-0.75f, 0.75f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Top-left
|
||||
0.75f, 0.75f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Top-right
|
||||
0.75f, -0.75f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // Bottom-right
|
||||
// Position(3), Color(3), TexCoord(2)
|
||||
-1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Top-left
|
||||
1.0f, 1.0f, 0.0f, 0.f, 1.0f, 0.0f, 1.0f, 0.0f, // Top-right
|
||||
1.0f, -1.0f, 0.0f, 0.f, 0.0f, 1.0f, 1.0f, 1.0f, // Bottom-right
|
||||
|
||||
0.75f, -0.75f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // Bottom-right
|
||||
-0.75f, -0.75f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, // Bottom-left
|
||||
-0.75f, 0.75f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f // Top-left
|
||||
1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // Bottom-right
|
||||
-1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, // Bottom-left
|
||||
-1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f // Top-left
|
||||
};
|
||||
|
||||
GLuint vbo;
|
||||
@@ -100,9 +103,9 @@ App::App(GLFWwindow *window):
|
||||
GLint posAttrib = glGetAttribLocation(shaderProgram, "inPos");
|
||||
GLint colorAttrib = glGetAttribLocation(shaderProgram, "inColor");
|
||||
GLint coordAttrib = glGetAttribLocation(shaderProgram, "inCoord");
|
||||
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 7*sizeof(float), 0);
|
||||
glVertexAttribPointer(colorAttrib, 3, GL_FLOAT, GL_FALSE, 7*sizeof(float), (void*)(2*sizeof(float)));
|
||||
glVertexAttribPointer(coordAttrib, 2, GL_FLOAT, GL_FALSE, 7*sizeof(float), (void*)(5*sizeof(float)));
|
||||
glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 8*sizeof(float), 0);
|
||||
glVertexAttribPointer(colorAttrib, 3, GL_FLOAT, GL_FALSE, 8*sizeof(float), (void*)(3*sizeof(float)));
|
||||
glVertexAttribPointer(coordAttrib, 2, GL_FLOAT, GL_FALSE, 8*sizeof(float), (void*)(6*sizeof(float)));
|
||||
glEnableVertexAttribArray(posAttrib);
|
||||
glEnableVertexAttribArray(colorAttrib);
|
||||
glEnableVertexAttribArray(coordAttrib);
|
||||
@@ -111,15 +114,44 @@ App::App(GLFWwindow *window):
|
||||
|
||||
void App::game_loop() {
|
||||
GLint timeUniform = glGetUniformLocation(mShaderProgram, "time");
|
||||
GLint modelUniform = glGetUniformLocation(mShaderProgram, "model");
|
||||
GLint viewUniform = glGetUniformLocation(mShaderProgram, "view");
|
||||
GLint projUniform = glGetUniformLocation(mShaderProgram, "proj");
|
||||
|
||||
auto startTime = glfwGetTime();
|
||||
int width, height;
|
||||
glfwGetWindowSize(mpWindow, &width, &height);
|
||||
auto projMatrix = glm::perspective(60.0f, float(width) / height, 0.01f, 1000.f);
|
||||
|
||||
auto viewMatrix = glm::lookAt(
|
||||
glm::vec3{0.f, 0.f, 0.f},
|
||||
glm::vec3{0.f, 0.f, -1.f},
|
||||
glm::vec3{0.f, -1.f, 0.f}
|
||||
);
|
||||
|
||||
auto modelMatrix = glm::identity<glm::mat4>();
|
||||
modelMatrix = glm::translate(modelMatrix, glm::vec3{0.f, 0.f, -.2f});
|
||||
|
||||
glUniformMatrix4fv(viewUniform, 1, GL_FALSE, glm::value_ptr(viewMatrix));
|
||||
glUniformMatrix4fv(projUniform, 1, GL_FALSE, glm::value_ptr(projMatrix));
|
||||
glUniformMatrix4fv(modelUniform, 1, GL_FALSE, glm::value_ptr(modelMatrix));
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
auto prevTime = glfwGetTime();
|
||||
auto elapsed = 0.0;
|
||||
while(!glfwWindowShouldClose(mpWindow)) {
|
||||
glfwPollEvents();
|
||||
|
||||
glUniform1f(timeUniform, glfwGetTime() - startTime);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
auto now = glfwGetTime();
|
||||
auto dt = now - prevTime;
|
||||
elapsed += dt;
|
||||
prevTime = now;
|
||||
|
||||
modelMatrix = glm::rotate(modelMatrix, glm::radians(float(dt) * 18.0f), glm::vec3{0.08f, 0.05f, 1.f});
|
||||
glUniformMatrix4fv(modelUniform, 1, GL_FALSE, glm::value_ptr(modelMatrix));
|
||||
glUniform1f(timeUniform, 4.f * elapsed);
|
||||
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
glfwSwapBuffers(mpWindow);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user