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