Added some transformations
This commit is contained in:
@@ -1,14 +1,18 @@
|
|||||||
#version 150 core
|
#version 150 core
|
||||||
|
|
||||||
in vec2 inPos;
|
in vec3 inPos;
|
||||||
in vec3 inColor;
|
in vec3 inColor;
|
||||||
in vec2 inCoord;
|
in vec2 inCoord;
|
||||||
|
|
||||||
out vec3 color;
|
out vec3 color;
|
||||||
out vec2 coord;
|
out vec2 coord;
|
||||||
|
|
||||||
|
uniform mat4 model;
|
||||||
|
uniform mat4 view;
|
||||||
|
uniform mat4 proj;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
color = inColor;
|
color = inColor;
|
||||||
coord = inCoord;
|
coord = inCoord;
|
||||||
gl_Position = vec4(inPos, 0.0, 1.0);
|
gl_Position = proj * view * model * vec4(inPos, 1.0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,9 @@
|
|||||||
|
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
#include <GLFW/glfw3.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 "LogUtils.hpp"
|
||||||
#include "ShaderProgramBuilder.hpp"
|
#include "ShaderProgramBuilder.hpp"
|
||||||
@@ -59,14 +62,14 @@ App::App(GLFWwindow *window):
|
|||||||
glfwSetKeyCallback(mpWindow, key_callback_s);
|
glfwSetKeyCallback(mpWindow, key_callback_s);
|
||||||
|
|
||||||
float vertices[] = {
|
float vertices[] = {
|
||||||
// Position(3), Color(2), TexCoord(2)
|
// Position(3), Color(3), TexCoord(2)
|
||||||
-0.75f, 0.75f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Top-left
|
-1.0f, 1.0f, 0.0f, 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
|
1.0f, 1.0f, 0.0f, 0.f, 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
|
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
|
1.0f, -1.0f, 0.0f, 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
|
-1.0f, -1.0f, 0.0f, 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, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f // Top-left
|
||||||
};
|
};
|
||||||
|
|
||||||
GLuint vbo;
|
GLuint vbo;
|
||||||
@@ -100,9 +103,9 @@ App::App(GLFWwindow *window):
|
|||||||
GLint posAttrib = glGetAttribLocation(shaderProgram, "inPos");
|
GLint posAttrib = glGetAttribLocation(shaderProgram, "inPos");
|
||||||
GLint colorAttrib = glGetAttribLocation(shaderProgram, "inColor");
|
GLint colorAttrib = glGetAttribLocation(shaderProgram, "inColor");
|
||||||
GLint coordAttrib = glGetAttribLocation(shaderProgram, "inCoord");
|
GLint coordAttrib = glGetAttribLocation(shaderProgram, "inCoord");
|
||||||
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 7*sizeof(float), 0);
|
glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 8*sizeof(float), 0);
|
||||||
glVertexAttribPointer(colorAttrib, 3, GL_FLOAT, GL_FALSE, 7*sizeof(float), (void*)(2*sizeof(float)));
|
glVertexAttribPointer(colorAttrib, 3, GL_FLOAT, GL_FALSE, 8*sizeof(float), (void*)(3*sizeof(float)));
|
||||||
glVertexAttribPointer(coordAttrib, 2, GL_FLOAT, GL_FALSE, 7*sizeof(float), (void*)(5*sizeof(float)));
|
glVertexAttribPointer(coordAttrib, 2, GL_FLOAT, GL_FALSE, 8*sizeof(float), (void*)(6*sizeof(float)));
|
||||||
glEnableVertexAttribArray(posAttrib);
|
glEnableVertexAttribArray(posAttrib);
|
||||||
glEnableVertexAttribArray(colorAttrib);
|
glEnableVertexAttribArray(colorAttrib);
|
||||||
glEnableVertexAttribArray(coordAttrib);
|
glEnableVertexAttribArray(coordAttrib);
|
||||||
@@ -111,15 +114,44 @@ App::App(GLFWwindow *window):
|
|||||||
|
|
||||||
void App::game_loop() {
|
void App::game_loop() {
|
||||||
GLint timeUniform = glGetUniformLocation(mShaderProgram, "time");
|
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)) {
|
while(!glfwWindowShouldClose(mpWindow)) {
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
|
||||||
glUniform1f(timeUniform, glfwGetTime() - startTime);
|
auto now = glfwGetTime();
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
auto dt = now - prevTime;
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
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);
|
glfwSwapBuffers(mpWindow);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user