Complete transformation and coordinate system chapters

This commit is contained in:
Abdelrahman Said 2024-12-01 01:09:47 +00:00
parent efa4b7a6a8
commit 318c000391
3 changed files with 72 additions and 30 deletions

View File

@ -1,6 +1,5 @@
#version 330 core #version 330 core
in vec3 vert_color;
in vec2 tex_coords; in vec2 tex_coords;
out vec4 color; out vec4 color;

View File

@ -1,16 +1,15 @@
#version 330 core #version 330 core
layout(location=0) in vec3 position; layout(location=0) in vec3 position;
layout(location=1) in vec3 color; layout(location=1) in vec2 uv;
layout(location=2) in vec2 uv;
uniform mat4 matrix; uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
out vec3 vert_color;
out vec2 tex_coords; out vec2 tex_coords;
void main() { void main() {
tex_coords = uv; tex_coords = uv;
vert_color = color; gl_Position = projection * view * model * vec4(position, 1.0);
gl_Position = matrix * vec4(position, 1.0);
}; };

View File

@ -1,4 +1,5 @@
// GLAD // GLAD
#include "SDL_timer.h"
#include "glad/glad.h" #include "glad/glad.h"
// GLM // GLM
@ -90,22 +91,42 @@ int main() {
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glEnable(GL_DEPTH_TEST);
std::vector<GLfloat> vertices = { std::vector<GLfloat> vertices = {
-0.5f, -0.5f, 0.0f, // position 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // position, uv
1.0f, 0.0f, 0.0f, // colour -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // position, uv
0.0f, 0.0f, // uv 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // position, uv
0.5f, -0.5f, 0.0f, // position 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, // position, uv
0.0f, 0.0f, 1.0f, // colour -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // position, uv
1.0f, 0.0f, // uv 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, // position, uv
-0.5f, 0.5f, 0.0f, // position -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // position, uv
0.0f, 1.0f, 0.0f, // colour -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, // position, uv
0.0f, 1.0f, // uv -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, // position, uv
0.5f, 0.5f, 0.0f, // position 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // position, uv
0.5f, 0.5f, 0.5f, // colour 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, // position, uv
1.0f, 1.0f, // uv -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, // position, uv
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, // position, uv
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // position, uv
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, // position, uv
0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // position, uv
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f // position, uv
}; };
std::vector<GLuint> indices = {0, 1, 2, 2, 1, 3}; std::vector<GLuint> indices = {
14, 5, 9,
9, 7, 14,
4, 3, 12,
12, 8, 4,
1, 6, 13,
13, 4, 1,
2, 9, 15,
15, 0, 2,
13, 10, 3,
3, 4, 13,
7, 9, 2,
2, 11, 16
};
GLuint vao = 0; GLuint vao = 0;
glGenVertexArrays(1, &vao); glGenVertexArrays(1, &vao);
@ -121,12 +142,10 @@ int main() {
glBindBuffer(GL_ARRAY_BUFFER, vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), vertices.data(), GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), vertices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void *)0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (void *)0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void *)(3 * sizeof(GLfloat))); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (void *)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void *)(6 * sizeof(GLfloat)));
glEnableVertexAttribArray(2);
glBindVertexArray(0); glBindVertexArray(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
@ -175,11 +194,28 @@ int main() {
float texture_mix_factor = 0.2f; float texture_mix_factor = 0.2f;
glm::mat4 mat = glm::mat4(1.0f); glm::vec3 cube_positions[] = {
mat = glm::rotate(mat, glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f)); glm::vec3( 0.0f, 0.0f, 0.0f),
mat = glm::scale(mat, glm::vec3(0.5f, 0.5f, 0.5f)); glm::vec3( 5.0f, 8.0f, -25.0f),
glm::vec3(-4.5f, -5.2f, -12.5f),
glm::vec3(-6.8f, -5.0f, -22.3f),
glm::vec3( 5.4f, -3.4f, -13.5f),
glm::vec3(-4.7f, 6.0f, -17.5f),
glm::vec3( 4.3f, -5.0f, -12.5f),
glm::vec3( 4.5f, 5.0f, -12.5f),
glm::vec3( 4.5f, 3.2f, -11.5f),
glm::vec3(-4.3f, 4.0f, -11.5f)
};
glUniformMatrix4fv(glGetUniformLocation(main_shader.program, "matrix"), 1, GL_FALSE, glm::value_ptr(mat)); glm::mat4 model = glm::mat4(1.0f);
glm::mat4 view = glm::mat4(1.0f);
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -4.0f));
glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)WINDOW_WIDTH / (float)WINDOW_HEIGHT, 0.1f, 100.0f);
glUniformMatrix4fv(glGetUniformLocation(main_shader.program, "view"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(main_shader.program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
bool running = true; bool running = true;
SDL_Event event = {}; SDL_Event event = {};
@ -212,12 +248,13 @@ int main() {
} }
} }
texture_mix_factor = texture_mix_factor < 0.0f ? texture_mix_factor = texture_mix_factor < 0.0f ?
0.0f : texture_mix_factor > 1.0f ? 0.0f : texture_mix_factor > 1.0f ?
1.0f : texture_mix_factor; 1.0f : texture_mix_factor;
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);
main_shader.activate(); main_shader.activate();
glUniform1f(glGetUniformLocation(main_shader.program, "mix_factor"), texture_mix_factor); glUniform1f(glGetUniformLocation(main_shader.program, "mix_factor"), texture_mix_factor);
@ -226,7 +263,14 @@ int main() {
glActiveTexture(GL_TEXTURE1); glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, textures[1]); glBindTexture(GL_TEXTURE_2D, textures[1]);
glBindVertexArray(vao); glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, NULL);
for (int i = 0; i < sizeof(cube_positions) / sizeof(cube_positions[0]); ++i) {
float angle = 20.0f * i;
model = glm::translate(glm::mat4(1.0f), cube_positions[i]);
model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
glUniformMatrix4fv(glGetUniformLocation(main_shader.program, "model"), 1, GL_FALSE, glm::value_ptr(model));
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, (void *)0);
}
SDL_GL_SwapWindow(window); SDL_GL_SwapWindow(window);
} }