diff --git a/assets/shaders/basicFragment.frag b/assets/shaders/basicFragment.frag index ad71e9a..88d9bd3 100644 --- a/assets/shaders/basicFragment.frag +++ b/assets/shaders/basicFragment.frag @@ -8,5 +8,9 @@ uniform sampler2D decal; void main() { - FragColor = mix(texture(ourTexture, TexCoord), texture(decal, TexCoord), 0.2f); -} \ No newline at end of file + vec4 tex1 = texture(ourTexture, TexCoord); + vec4 tex2 = texture(decal, TexCoord); + vec4 textures = mix(tex1, tex2, 0.2f); + + FragColor = textures; +} diff --git a/assets/shaders/basicVertex.vert b/assets/shaders/basicVertex.vert index 953b8d4..5b81207 100644 --- a/assets/shaders/basicVertex.vert +++ b/assets/shaders/basicVertex.vert @@ -1,8 +1,9 @@ #version 330 core layout (location = 0) in vec3 aPos; -layout (location = 1) in vec3 aColor; +layout (location = 1) in vec3 aNormal; layout (location = 2) in vec2 aTexCoord; +out vec3 Normal; out vec2 TexCoord; uniform mat4 model; @@ -11,7 +12,8 @@ uniform mat4 projection; void main() { - //gl_Position = vec4(aPos, 1.0); gl_Position = projection * view * model * vec4(aPos, 1.0f); - TexCoord = vec2(aTexCoord.x, aTexCoord.y); -} \ No newline at end of file + + Normal = aNormal; + TexCoord = aTexCoord; +} diff --git a/engine/mesh.h b/engine/mesh.h index 348aa1c..acf1a11 100644 --- a/engine/mesh.h +++ b/engine/mesh.h @@ -5,9 +5,8 @@ struct Vertex { float x, y, z; // position - float r, g, b; // color + float nX, nY, zY; // normal float u, v; // texcoord - // = 8 floats = 32 bytes }; class Mesh @@ -27,10 +26,6 @@ public: glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); glGenBuffers(1, &EBO); - - std::cout << VAO << std::endl; - std::cout << VBO << std::endl; - std::cout << EBO << std::endl; glBindVertexArray(VAO); @@ -40,13 +35,15 @@ public: glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW); - // position attribute + // Position glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, x)); glEnableVertexAttribArray(0); - // color attribute - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, r)); + + // Normals + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, nX)); glEnableVertexAttribArray(1); - // texture coord attribute + + // Texcoords glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, u)); glEnableVertexAttribArray(2); } @@ -58,9 +55,9 @@ public: void Draw() const { - glDrawArrays(GL_TRIANGLES, 0, 36); - //glBindVertexArray(VAO); - //glDrawElements(GL_TRIANGLES, static_cast(indexCount), - // GL_UNSIGNED_INT, nullptr); + //glDrawArrays(GL_TRIANGLES, 0, 36); + glBindVertexArray(VAO); + glDrawElements(GL_TRIANGLES, static_cast(indexCount), + GL_UNSIGNED_INT, 0); } }; diff --git a/main.cpp b/main.cpp index 536ae17..55058d4 100644 --- a/main.cpp +++ b/main.cpp @@ -76,56 +76,72 @@ int main(int argc, char* argv[]) // Load Shader Shader shaderTest("assets/shaders/basicVertex.vert", "assets/shaders/basicFragment.frag"); std::vector vertices = { - { -0.5f, -0.5f, -0.5f, 1.0f, 0.2f, 0.2f, 0.0f, 0.0f }, - { 0.5f, -0.5f, -0.5f, 1.0f, 0.2f, 0.2f, 1.0f, 0.0f }, - { 0.5f, 0.5f, -0.5f, 1.0f, 0.2f, 0.2f, 1.0f, 1.0f }, - { 0.5f, 0.5f, -0.5f, 1.0f, 0.2f, 0.2f, 1.0f, 1.0f }, - { -0.5f, 0.5f, -0.5f, 1.0f, 0.2f, 0.2f, 0.0f, 1.0f }, - { -0.5f, -0.5f, -0.5f, 1.0f, 0.2f, 0.2f, 0.0f, 0.0f }, + // Front face (-z) – normal: (0, 0, -1) + { -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f }, + { 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f }, + { 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f }, + { 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f }, + { -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f }, + { -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f }, - // Back face (+z) – green-ish - { -0.5f, -0.5f, 0.5f, 0.2f, 1.0f, 0.2f, 0.0f, 0.0f }, - { 0.5f, -0.5f, 0.5f, 0.2f, 1.0f, 0.2f, 1.0f, 0.0f }, - { 0.5f, 0.5f, 0.5f, 0.2f, 1.0f, 0.2f, 1.0f, 1.0f }, - { 0.5f, 0.5f, 0.5f, 0.2f, 1.0f, 0.2f, 1.0f, 1.0f }, - { -0.5f, 0.5f, 0.5f, 0.2f, 1.0f, 0.2f, 0.0f, 1.0f }, - { -0.5f, -0.5f, 0.5f, 0.2f, 1.0f, 0.2f, 0.0f, 0.0f }, + // Back face (+z) – normal: (0, 0, +1) + { -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }, + { 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f }, + { 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f }, + { 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f }, + { -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f }, + { -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }, - // Left face (-x) – blue-ish - { -0.5f, 0.5f, 0.5f, 0.2f, 0.2f, 1.0f, 1.0f, 0.0f }, - { -0.5f, 0.5f, -0.5f, 0.2f, 0.2f, 1.0f, 1.0f, 1.0f }, - { -0.5f, -0.5f, -0.5f, 0.2f, 0.2f, 1.0f, 0.0f, 1.0f }, - { -0.5f, -0.5f, -0.5f, 0.2f, 0.2f, 1.0f, 0.0f, 1.0f }, - { -0.5f, -0.5f, 0.5f, 0.2f, 0.2f, 1.0f, 0.0f, 0.0f }, - { -0.5f, 0.5f, 0.5f, 0.2f, 0.2f, 1.0f, 1.0f, 0.0f }, + // Left face (-x) – normal: (-1, 0, 0) + { -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f }, + { -0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f }, + { -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f }, + { -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f }, + { -0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f }, + { -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f }, - // Right face (+x) – yellow-ish - { 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.2f, 1.0f, 0.0f }, - { 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.2f, 1.0f, 1.0f }, - { 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.2f, 0.0f, 1.0f }, - { 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.2f, 0.0f, 1.0f }, - { 0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 0.2f, 0.0f, 0.0f }, - { 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.2f, 1.0f, 0.0f }, + // Right face (+x) – normal: (+1, 0, 0) + { 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f }, + { 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f }, + { 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f }, + { 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f }, + { 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f }, + { 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f }, - // Bottom face (-y) – magenta-ish - { -0.5f, -0.5f, -0.5f, 1.0f, 0.2f, 1.0f, 0.0f, 1.0f }, - { 0.5f, -0.5f, -0.5f, 1.0f, 0.2f, 1.0f, 1.0f, 1.0f }, - { 0.5f, -0.5f, 0.5f, 1.0f, 0.2f, 1.0f, 1.0f, 0.0f }, - { 0.5f, -0.5f, 0.5f, 1.0f, 0.2f, 1.0f, 1.0f, 0.0f }, - { -0.5f, -0.5f, 0.5f, 1.0f, 0.2f, 1.0f, 0.0f, 0.0f }, - { -0.5f, -0.5f, -0.5f, 1.0f, 0.2f, 1.0f, 0.0f, 1.0f }, + // Bottom face (-y) – normal: (0, -1, 0) + { -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f }, + { 0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f }, + { 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f }, + { 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f }, + { -0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f }, + { -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f }, - // Top face (+y) – cyan-ish - { -0.5f, 0.5f, -0.5f, 0.2f, 1.0f, 1.0f, 0.0f, 1.0f }, - { 0.5f, 0.5f, -0.5f, 0.2f, 1.0f, 1.0f, 1.0f, 1.0f }, - { 0.5f, 0.5f, 0.5f, 0.2f, 1.0f, 1.0f, 1.0f, 0.0f }, - { 0.5f, 0.5f, 0.5f, 0.2f, 1.0f, 1.0f, 1.0f, 0.0f }, - { -0.5f, 0.5f, 0.5f, 0.2f, 1.0f, 1.0f, 0.0f, 0.0f }, - { -0.5f, 0.5f, -0.5f, 0.2f, 1.0f, 1.0f, 0.0f, 1.0f } + // Top face (+y) – normal: (0, +1, 0) + { -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f }, + { 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f }, + { 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f }, + { 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f }, + { -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f }, + { -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f } }; std::vector indices = { - 0, 1, 3, - 1, 2, 3 + // Front (-z) ── outer view CCW + 0, 1, 2, 0, 2, 5, + + // Back (+z) ── outer view CCW + 6, 7, 8, 6, 8, 11, + + // Left (-x) ── outer view CCW + 12, 13, 14, 12, 14, 17, + + // Right (+x) ── outer view CCW + 18, 19, 20, 18, 20, 23, + + // Bottom (-y) ── outer view CCW + 24, 25, 26, 24, 26, 29, + + // Top (+y) ── outer view CCW + 30, 31, 32, 30, 32, 35 }; Mesh testMesh(vertices, indices); @@ -137,20 +153,6 @@ int main(int argc, char* argv[]) shaderTest.setInt("ourTexture", 0); shaderTest.setInt("decal", 1); - glm::vec3 cubePositions[] = { - glm::vec3( 0.0f, 0.0f, 0.0f), - glm::vec3( 2.0f, 5.0f, -15.0f), - glm::vec3(-1.5f, -2.2f, -2.5f), - glm::vec3(-3.8f, -2.0f, -12.3f), - glm::vec3( 2.4f, -0.4f, -3.5f), - glm::vec3(-1.7f, 3.0f, -7.5f), - glm::vec3( 1.3f, -2.0f, -2.5f), - glm::vec3( 1.5f, 2.0f, -2.5f), - glm::vec3( 1.5f, 0.2f, -1.5f), - glm::vec3(-1.3f, 1.0f, -1.5f) - }; - - // Render Loop float lastUpdate = 0; while (!glfwWindowShouldClose(window)) @@ -190,20 +192,10 @@ int main(int argc, char* argv[]) glm::mat4 view = camera.GetViewMatrix(); shaderTest.setMat4("view", view); + glm::mat4 model = glm::mat4(1.0f); + shaderTest.setMat4("model", model); - //testMesh.Draw(); - - glBindVertexArray(testMesh.VAO); - for(unsigned int i = 0; i < 10; i++) - { - glm::mat4 model = glm::mat4(1.0f); - model = glm::translate(model, cubePositions[i]); - float angle = 20.0f * i; - model = glm::rotate(model, (float)glfwGetTime() * glm::radians(50.0f), glm::vec3(0.5f, 1.0f, 0.0f)); - shaderTest.setMat4("model", model); - - glDrawArrays(GL_TRIANGLES, 0, 36); - } + testMesh.Draw(); // Call events & swap buffers glfwSwapBuffers(window);