Add GLM and make the triangle move
This commit is contained in:
parent
27452c30a5
commit
56cf609849
@ -5,6 +5,7 @@ set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
find_package(OpenGL REQUIRED)
|
||||
find_package(GLEW REQUIRED)
|
||||
find_package(glm REQUIRED)
|
||||
find_package(glfw3 REQUIRED)
|
||||
|
||||
add_executable(opengl_test src/main.cpp)
|
||||
@ -12,5 +13,6 @@ add_executable(opengl_test src/main.cpp)
|
||||
target_link_libraries(opengl_test
|
||||
OpenGL::GL
|
||||
GLEW::GLEW
|
||||
glm::glm
|
||||
glfw
|
||||
)
|
||||
56
src/main.cpp
56
src/main.cpp
@ -2,28 +2,31 @@
|
||||
#include <string.h>
|
||||
#include <GL/glew.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <cmath>
|
||||
|
||||
// Window dimensions
|
||||
const GLint WIDTH = 800, HEIGHT = 600;
|
||||
|
||||
// Vertex Shader
|
||||
static const char *vertex_shader = " \n\
|
||||
#version 330 \n\
|
||||
\n\
|
||||
layout (location = 0) in vec3 pos; \n\
|
||||
\n\
|
||||
void main() { \n\
|
||||
gl_Position = vec4(pos.x, pos.y, pos.z, 1.0); \n\
|
||||
static const char *vertex_shader = " \n\
|
||||
#version 330 \n\
|
||||
\n\
|
||||
layout (location = 0) in vec3 pos; \n\
|
||||
\n\
|
||||
uniform float x_move; \n\
|
||||
\n\
|
||||
void main() { \n\
|
||||
gl_Position = vec4(pos.x + x_move, pos.y, pos.z, 1.0); \n\
|
||||
}";
|
||||
|
||||
// Fragment shader
|
||||
static const char *fragment_shader = " \n\
|
||||
#version 330 \n\
|
||||
\n\
|
||||
out vec4 color; \n\
|
||||
\n\
|
||||
void main() { \n\
|
||||
color = vec4(1.0, 0.0, 0.0, 1.0); \n\
|
||||
static const char *fragment_shader = " \n\
|
||||
#version 330 \n\
|
||||
\n\
|
||||
out vec4 color; \n\
|
||||
\n\
|
||||
void main() { \n\
|
||||
color = vec4(1.0, 0.0, 0.0, 1.0); \n\
|
||||
}";
|
||||
|
||||
bool add_shader(GLuint *program, const char *shader_code, GLenum shader_type) {
|
||||
@ -51,7 +54,7 @@ bool add_shader(GLuint *program, const char *shader_code, GLenum shader_type) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool compile_shaders(GLuint *shader_program) {
|
||||
bool compile_shaders(GLuint *shader_program, GLuint *x_move) {
|
||||
*shader_program = glCreateProgram();
|
||||
|
||||
if (!shader_program) {
|
||||
@ -83,6 +86,8 @@ bool compile_shaders(GLuint *shader_program) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*x_move = glGetUniformLocation(*shader_program, "x_move");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -109,7 +114,11 @@ void create_triangle(GLuint *VAO, GLuint *VBO) {
|
||||
}
|
||||
|
||||
int main() {
|
||||
GLuint VAO, VBO, shader_program;
|
||||
GLuint VAO, VBO, shader_program, x_move;
|
||||
bool direction = true;
|
||||
float tri_offset = 0.0f;
|
||||
float tri_max_offset = 0.7f;
|
||||
float tri_increment = 0.005f;
|
||||
|
||||
// Initialize GLFW
|
||||
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_WAYLAND);
|
||||
@ -168,18 +177,31 @@ int main() {
|
||||
|
||||
// Create our triangle
|
||||
create_triangle(&VAO, &VBO);
|
||||
compile_shaders(&shader_program);
|
||||
compile_shaders(&shader_program, &x_move);
|
||||
|
||||
// Loop until window is closed
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
// Get and handle user input events
|
||||
glfwPollEvents();
|
||||
|
||||
if (direction) {
|
||||
tri_offset += tri_increment;
|
||||
} else {
|
||||
tri_offset -= tri_increment;
|
||||
}
|
||||
|
||||
if (std::abs(tri_offset) >= tri_max_offset) {
|
||||
direction = !direction;
|
||||
}
|
||||
|
||||
// Clear window
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glUseProgram(shader_program);
|
||||
|
||||
glUniform1f(x_move, tri_offset);
|
||||
|
||||
glBindVertexArray(VAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user