From 9568583cdd23497602cf8d1c34f08663489203b2 Mon Sep 17 00:00:00 2001 From: 0x01FE <0x01fe@0x01fe.net> Date: Mon, 10 Nov 2025 19:51:50 -0600 Subject: [PATCH] working on some bugs --- build.ps1 | 1 + full-build.ps1 | 1 + shaders/cube_frag.glsl | 11 +++++----- shaders/cube_vert.glsl | 5 +++++ src/main.cpp | 47 +++++++++++++++++++++++++++++++++--------- 5 files changed, 50 insertions(+), 15 deletions(-) diff --git a/build.ps1 b/build.ps1 index 3a462cd..18e88c3 100644 --- a/build.ps1 +++ b/build.ps1 @@ -9,3 +9,4 @@ cp -r objs/ build/Debug cd build/Debug .\VoxelEngine.exe +cd ../.. diff --git a/full-build.ps1 b/full-build.ps1 index aa642d7..1df16a8 100644 --- a/full-build.ps1 +++ b/full-build.ps1 @@ -10,3 +10,4 @@ cp -r objs/ build/Debug cd build/Debug .\VoxelEngine.exe +cd ../.. diff --git a/shaders/cube_frag.glsl b/shaders/cube_frag.glsl index e7a951e..4efa73e 100644 --- a/shaders/cube_frag.glsl +++ b/shaders/cube_frag.glsl @@ -1,11 +1,12 @@ #version 330 core -// The final color output for the pixel. out vec4 FragColor; +// [NEW] This variable will be set from your C++ code +uniform vec3 u_Color; + void main() { - // Hard-code the color to an obnoxious bright orange. - // The values are (Red, Green, Blue, Alpha). - FragColor = vec4(1.0, 1.0, 1.0, 1.0); -} + // Use the color from C++ + FragColor = vec4(u_Color, 1.0); +} \ No newline at end of file diff --git a/shaders/cube_vert.glsl b/shaders/cube_vert.glsl index 0a83ed5..c34ce1b 100644 --- a/shaders/cube_vert.glsl +++ b/shaders/cube_vert.glsl @@ -3,14 +3,19 @@ // Input vertex attribute (e.g., from your VBO) // 'layout (location = 0)' matches the first attribute you enable. layout (location = 0) in vec3 aPos; +layout (location = 1) in vec3 aNormal; // A "uniform" is a global variable you set from your C++/Java/etc. code. // This matrix combines your model, view, and projection matrices. uniform mat4 u_mvp; +flat out vec3 v_Normal; + void main() { // gl_Position is a special built-in variable. // It's the final clip-space position of the vertex. gl_Position = u_mvp * vec4(aPos, 1.0); + + v_Normal = aNormal; } diff --git a/src/main.cpp b/src/main.cpp index 3dbed2a..45b93e9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,8 +11,8 @@ #include "World.h" // Window dimensions -unsigned int SCR_WIDTH = 800 * 1.5; -unsigned int SCR_HEIGHT = 600 * 1.5; +unsigned int SCR_WIDTH = 1920; +unsigned int SCR_HEIGHT = 1080; // Jump const float JUMP_COOLDOWN = 0.0f; @@ -26,8 +26,8 @@ const float LOOK_SENSITIVITY = 0.1f; const float Z_FAR = 1000.0f; const float Z_NEAR = 0.1f; -bool WIREFRAME_MODE = true; -const float FOV = 90.0f; +bool WIREFRAME_MODE = !true; +const float FOV = 110.0f; bool W_pressed, S_pressed, A_pressed, D_pressed = false; @@ -172,7 +172,8 @@ void processInput(GLFWwindow *window) } // Draw a cube at some x, y, z -void draw_cube(glm::ivec3 coords, Shader& shader, unsigned int VAO, size_t indexCount, const glm::mat4& view, const glm::mat4& projection) { +// [UPDATED] Added a new 'color' parameter +void draw_cube(glm::ivec3 coords, Shader& shader, unsigned int VAO, size_t indexCount, const glm::mat4& view, const glm::mat4& projection, const glm::vec3& color) { // Create model matrix for positioning the cube glm::mat4 model = glm::mat4(1.0f); model = glm::translate(model, glm::vec3(coords)); @@ -183,10 +184,12 @@ void draw_cube(glm::ivec3 coords, Shader& shader, unsigned int VAO, size_t index // Set the uniform and draw shader.use(); shader.setMat4("u_mvp", mvp); + // [NEW] Set the color uniform in the fragment shader + shader.setVec3("u_Color", color); + glBindVertexArray(VAO); glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, nullptr); } - int main() { // 1. --- Initialize GLFW --- if (!glfwInit()) { @@ -213,6 +216,8 @@ int main() { return -1; } + glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT); + // 4. Shader Setup Shader shader("shaders/cube_vert.glsl", "shaders/cube_frag.glsl"); @@ -245,11 +250,11 @@ int main() { // 6. Tell OpenGL how to interpret the vertex data // Position Attribute - glVertexAttribPointer(0, 3, GL_FLOAT, GL_TRUE, 6 * sizeof(float), (void*)0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); // Enable the vertex attribute (location 0) // Normal Attribute - glVertexAttribPointer(1, 3, GL_FLOAT, GL_TRUE, 6 * sizeof(float), (void*)(3 * sizeof(float))); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float))); glEnableVertexAttribArray(1); // 6.5 Setup EBO @@ -274,6 +279,8 @@ int main() { double start_time = glfwGetTime(); double move_time = glfwGetTime(); + glEnable(GL_DEPTH_TEST); + // 6. --- The Render Loop --- while (!glfwWindowShouldClose(window)) { // Input (e.g., close window on ESC) @@ -284,11 +291,31 @@ int main() { view = camera.getView(); - // Draw the World + // ---- PASS 1: Draw solid grey cubes ---- + glEnable(GL_POLYGON_OFFSET_FILL); + glPolygonOffset(1.0, 1.0); // Push solid faces "back" + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + + glm::vec3 solidColor = glm::vec3(0.5, 0.5, 0.5); // Grey for (glm::ivec3 voxel_pos : world.voxels) { - draw_cube(voxel_pos, shader, VAO, cube.EBO_buffer.size(), view, projection); + // [UPDATED] Pass the grey color + draw_cube(voxel_pos, shader, VAO, cube.EBO_buffer.size(), view, projection, solidColor); } + glDisable(GL_POLYGON_OFFSET_FILL); + + // ---- PASS 2: Draw wireframe on top ---- + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // Just the lines + + glm::vec3 wireframeColor = glm::vec3(1.0, 1.0, 1.0); // White + for (glm::ivec3 voxel_pos : world.voxels) { + // [UPDATED] Pass the white wireframe color + draw_cube(voxel_pos, shader, VAO, cube.EBO_buffer.size(), view, projection, wireframeColor); + } + + // --- Reset polygon mode for next frame (good practice) --- + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + double end_time = glfwGetTime(); float time_since_last_move = end_time - move_time; if (time_since_last_move >= 0.017)