Rotating 3D Cube

This commit is contained in:
NaifBanana 2024-01-20 02:03:14 -06:00
parent b42011190e
commit 9bea06d317
10 changed files with 88 additions and 37 deletions

Binary file not shown.

View File

@ -24,11 +24,11 @@
ProcessorClockFrequency="3193"
>
<Testing>
<StartDateTime>Jan 20 00:42 Central Standard Time</StartDateTime>
<StartTestTime>1705732938</StartTestTime>
<StartDateTime>Jan 20 01:04 Central Standard Time</StartDateTime>
<StartTestTime>1705734272</StartTestTime>
<TestList/>
<EndDateTime>Jan 20 00:42 Central Standard Time</EndDateTime>
<EndTestTime>1705732938</EndTestTime>
<EndDateTime>Jan 20 01:04 Central Standard Time</EndDateTime>
<EndTestTime>1705734272</EndTestTime>
<ElapsedMinutes>0</ElapsedMinutes>
</Testing>
</Site>

View File

@ -1,3 +1,3 @@
Start testing: Jan 20 00:42 Central Standard Time
Start testing: Jan 20 01:04 Central Standard Time
----------------------------------------------------------
End testing: Jan 20 00:42 Central Standard Time
End testing: Jan 20 01:04 Central Standard Time

View File

@ -1,12 +1,52 @@
float vertices[] = {
// Positions // Colors // Texture Coords
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
// Positions // Texture Coords
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, 0.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, 1.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, 0.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, 1.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, 0.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, 1.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, 0.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, 1.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, 0.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, 1.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, 0.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, 1.0f, 1.0f
};
/*
unsigned int indices[] = { // note that we start from 0!
0, 1, 3, // first triangle
1, 2, 3
};
*/

View File

@ -45,18 +45,16 @@ int main() {
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8*sizeof(float), (void*)0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8*sizeof(float), (void*)(3*sizeof(float)));
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8*sizeof(float), (void*)(6*sizeof(float)));
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5*sizeof(float), (void*)0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5*sizeof(float), (void*)(3*sizeof(float)));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
// Element Buffers
unsigned int EBO;
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// unsigned int EBO;
// glGenBuffers(1, &EBO);
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
// glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// Use and set shader
Shader myShader("../shad/vert.vs", "../shad/frag.fs");
@ -76,7 +74,7 @@ int main() {
glBindTexture(GL_TEXTURE_2D, texture1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width1, height1, 0, GL_RGB, GL_UNSIGNED_BYTE, data1);
glGenerateMipmap(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE1);
glGenTextures(1, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2);
@ -89,27 +87,43 @@ int main() {
} else {
std::cout << "COULD NOT LOAD TEXTUREs!\n";
}
stbi_image_free(data1);
stbi_image_free(data2);
// Camera Work
glm::mat4 trans = glm::rotate(glm::mat4(1.0f), (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
unsigned int transLoc = glGetUniformLocation(myShader.id, "transform");
glUniformMatrix4fv(transLoc, 1, GL_FALSE, glm::value_ptr(trans));
glm::mat4 model = glm::mat4(1.0f);
model = glm::rotate(model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f));
glm::mat4 view = glm::mat4(1.0f);
view = glm::translate(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 modelLoc, viewLoc, projLoc;
modelLoc = glGetUniformLocation(myShader.id, "model");
viewLoc = glGetUniformLocation(myShader.id, "view");
projLoc = glGetUniformLocation(myShader.id, "projection");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
glEnable(GL_DEPTH_TEST);
// Window loop
while(!glfwWindowShouldClose(window)) {
// Input checking
processInput(window);
// Rotate Model
model = glm::rotate(glm::mat4(1.0f), (float)glfwGetTime() * glm::radians(50.0f), glm::vec3(0.5f, 1.0f, 0.0f));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
// Rendering Loop
trans = glm::rotate(glm::mat4(1.0f), (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
glUniformMatrix4fv(transLoc, 1, GL_FALSE, glm::value_ptr(trans));
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glDrawArrays(GL_TRIANGLES, 0, 36);
// Events and buffer swap
glfwPollEvents();

View File

@ -1,6 +1,5 @@
#version 330 core
in vec4 vertexColor;
in vec2 textureCoord;
out vec4 FragColor;
@ -10,6 +9,5 @@ uniform sampler2D texture2;
void main() {
FragColor = mix(texture(texture1, textureCoord),
texture(texture2, textureCoord), 0.2);
texture(texture2, textureCoord), 0.3);
}

View File

@ -1,16 +1,15 @@
#version 330 core
layout(location=0) in vec3 aPos;
layout(location=1) in vec3 vColor;
layout(location=2) in vec2 tCoord;
layout(location=1) in vec2 tCoord;
out vec4 vertexColor;
out vec2 textureCoord;
uniform mat4 transform;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main() {
gl_Position = transform * vec4(aPos, 1.0);
vertexColor = vec4(vColor, 1.0);
gl_Position = projection * view * model * vec4(aPos, 1.0);
textureCoord = tCoord;
}