working on some bugs
This commit is contained in:
parent
54a01e3a05
commit
9568583cdd
@ -9,3 +9,4 @@ cp -r objs/ build/Debug
|
||||
|
||||
cd build/Debug
|
||||
.\VoxelEngine.exe
|
||||
cd ../..
|
||||
|
||||
@ -10,3 +10,4 @@ cp -r objs/ build/Debug
|
||||
|
||||
cd build/Debug
|
||||
.\VoxelEngine.exe
|
||||
cd ../..
|
||||
|
||||
@ -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);
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
|
||||
47
src/main.cpp
47
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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user