Skip to content

Commit

Permalink
rotating 3D cube!
Browse files Browse the repository at this point in the history
  • Loading branch information
Electron7-7 committed Nov 7, 2024
1 parent 98a225a commit 993880a
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 46 deletions.
109 changes: 71 additions & 38 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,49 @@ int main(int argc, char** argv)

Shader simple_shader("src/shaders/vertex_shader.glsl", "src/shaders/fragment_shader.glsl");

float vertices[]
{
// positions // colors // texture coords
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.3f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.3f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f // top left
};

unsigned int indices[]
{
0, 1, 3, // first triangle
1, 2, 3 // second triangle
float vertices[] = {
// positions // texture coords
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,

-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,

-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,

0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,

-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,

-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
};

unsigned int EBO;
Expand All @@ -58,26 +88,14 @@ int main(int argc, char** argv)

glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

/*
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
*/

// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);

// color attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);

// texture coordinates attribute
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);


/*
Expand All @@ -87,14 +105,6 @@ int main(int argc, char** argv)
// glBindBuffer(GL_ARRAY_BUFFER, VAO);
// glBindVertexArray(0);


float texture_coordinates[]
{
0.0f, 0.0f,
1.0f, 0.0f,
0.5f, 1.0f
};

unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
Expand All @@ -121,23 +131,46 @@ int main(int argc, char** argv)

// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // Wireframe mode

glEnable(GL_DEPTH_TEST);

while(!glfwWindowShouldClose(window))
{
processInput(window);

glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glm::mat4 trans = glm::mat4(1.0f);
trans = glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));

unsigned int transform_location = glGetUniformLocation(simple_shader.ID, "transform");
glUniformMatrix4fv(transform_location, 1, GL_FALSE, glm::value_ptr(trans));


glm::mat4 model = glm::mat4(1.0f);
model = glm::rotate(model, (float)glfwGetTime() * glm::radians(50.0f), glm::vec3(1.0f, 0.5f, 0.2f));

glm::mat4 backwards_view = glm::mat4(1.0f);
backwards_view = glm::translate(backwards_view, glm::vec3(0.0f, 0.0f, -3.0f));

glm::mat4 projection;
projection = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f);


int model_location = glGetUniformLocation(simple_shader.ID, "model");
glUniformMatrix4fv(model_location, 1, GL_FALSE, glm::value_ptr(model));

int view_location = glGetUniformLocation(simple_shader.ID, "backwards_view");
glUniformMatrix4fv(view_location, 1, GL_FALSE, glm::value_ptr(backwards_view));

int projection_location = glGetUniformLocation(simple_shader.ID, "projection");
glUniformMatrix4fv(projection_location, 1, GL_FALSE, glm::value_ptr(projection));

simple_shader.use();
glBindTexture(GL_TEXTURE_2D, texture);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glDrawArrays(GL_TRIANGLES, 0, 36);
// glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

glfwSwapBuffers(window);
glfwPollEvents();
Expand Down
7 changes: 4 additions & 3 deletions src/shaders/fragment_shader.glsl
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#version 460 core
out vec4 FragColor;

in vec3 vertexColor;
// in vec3 vertexColor;
in vec2 textureCoordinates;

uniform sampler2D newTexture2D;

void main()
{
FragColor = vec4(vertexColor, 1.0);
FragColor = texture(newTexture2D, textureCoordinates) * vec4(vertexColor, 1.0);
// FragColor = vec4(vertexColor, 1.0);
// FragColor = texture(newTexture2D, textureCoordinates) * vec4(vertexColor, 1.0);
FragColor = texture(newTexture2D, textureCoordinates);
};
15 changes: 10 additions & 5 deletions src/shaders/vertex_shader.glsl
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
#version 460 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;
layout (location = 1) in vec2 aTexCoord;
// layout (location = 1) in vec3 aColor; // 2D plane
// layout (location = 2) in vec2 aTexCoord; // 2D plane

out vec3 vertexColor;
// out vec3 vertexColor;
out vec2 textureCoordinates;

uniform mat4 transform;
uniform mat4 model;
uniform mat4 backwards_view;
uniform mat4 projection;

void main()
{
gl_Position = transform * vec4(aPos, 1.0);
vertexColor = aColor;
// gl_Position = transform * vec4(aPos, 1.0);
gl_Position = projection * backwards_view * model * vec4(aPos, 1.0);
// vertexColor = aColor;
textureCoordinates = aTexCoord;
};

0 comments on commit 993880a

Please sign in to comment.