Compare commits
2 Commits
54a01e3a05
...
90c99734bf
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
90c99734bf | ||
|
|
9568583cdd |
@ -9,3 +9,4 @@ cp -r objs/ build/Debug
|
|||||||
|
|
||||||
cd build/Debug
|
cd build/Debug
|
||||||
.\VoxelEngine.exe
|
.\VoxelEngine.exe
|
||||||
|
cd ../..
|
||||||
|
|||||||
@ -10,3 +10,4 @@ cp -r objs/ build/Debug
|
|||||||
|
|
||||||
cd build/Debug
|
cd build/Debug
|
||||||
.\VoxelEngine.exe
|
.\VoxelEngine.exe
|
||||||
|
cd ../..
|
||||||
|
|||||||
@ -1,11 +1,12 @@
|
|||||||
#version 330 core
|
#version 330 core
|
||||||
|
|
||||||
// The final color output for the pixel.
|
|
||||||
out vec4 FragColor;
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
// [NEW] This variable will be set from your C++ code
|
||||||
|
uniform vec3 u_Color;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
// Hard-code the color to an obnoxious bright orange.
|
// Use the color from C++
|
||||||
// The values are (Red, Green, Blue, Alpha).
|
FragColor = vec4(u_Color, 1.0);
|
||||||
FragColor = vec4(1.0, 1.0, 1.0, 1.0);
|
}
|
||||||
}
|
|
||||||
@ -3,14 +3,19 @@
|
|||||||
// Input vertex attribute (e.g., from your VBO)
|
// Input vertex attribute (e.g., from your VBO)
|
||||||
// 'layout (location = 0)' matches the first attribute you enable.
|
// 'layout (location = 0)' matches the first attribute you enable.
|
||||||
layout (location = 0) in vec3 aPos;
|
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.
|
// A "uniform" is a global variable you set from your C++/Java/etc. code.
|
||||||
// This matrix combines your model, view, and projection matrices.
|
// This matrix combines your model, view, and projection matrices.
|
||||||
uniform mat4 u_mvp;
|
uniform mat4 u_mvp;
|
||||||
|
|
||||||
|
flat out vec3 v_Normal;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
// gl_Position is a special built-in variable.
|
// gl_Position is a special built-in variable.
|
||||||
// It's the final clip-space position of the vertex.
|
// It's the final clip-space position of the vertex.
|
||||||
gl_Position = u_mvp * vec4(aPos, 1.0);
|
gl_Position = u_mvp * vec4(aPos, 1.0);
|
||||||
|
|
||||||
|
v_Normal = aNormal;
|
||||||
}
|
}
|
||||||
|
|||||||
48
src/main.cpp
48
src/main.cpp
@ -11,8 +11,8 @@
|
|||||||
#include "World.h"
|
#include "World.h"
|
||||||
|
|
||||||
// Window dimensions
|
// Window dimensions
|
||||||
unsigned int SCR_WIDTH = 800 * 1.5;
|
unsigned int SCR_WIDTH = 1920;
|
||||||
unsigned int SCR_HEIGHT = 600 * 1.5;
|
unsigned int SCR_HEIGHT = 1080;
|
||||||
|
|
||||||
// Jump
|
// Jump
|
||||||
const float JUMP_COOLDOWN = 0.0f;
|
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_FAR = 1000.0f;
|
||||||
const float Z_NEAR = 0.1f;
|
const float Z_NEAR = 0.1f;
|
||||||
bool WIREFRAME_MODE = true;
|
bool WIREFRAME_MODE = !true;
|
||||||
const float FOV = 90.0f;
|
const float FOV = 110.0f;
|
||||||
|
|
||||||
bool W_pressed, S_pressed, A_pressed, D_pressed = false;
|
bool W_pressed, S_pressed, A_pressed, D_pressed = false;
|
||||||
|
|
||||||
@ -172,17 +172,23 @@ void processInput(GLFWwindow *window)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Draw a cube at some x, y, z
|
// 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
|
// Create model matrix for positioning the cube
|
||||||
glm::mat4 model = glm::mat4(1.0f);
|
glm::mat4 model = glm::mat4(1.0f);
|
||||||
model = glm::translate(model, glm::vec3(coords));
|
model = glm::translate(model, glm::vec3(coords));
|
||||||
|
|
||||||
|
model = glm::scale(model, glm::vec3(0.5f, 0.5f, 0.5f));
|
||||||
|
|
||||||
// Calculate MVP matrix
|
// Calculate MVP matrix
|
||||||
glm::mat4 mvp = projection * view * model;
|
glm::mat4 mvp = projection * view * model;
|
||||||
|
|
||||||
// Set the uniform and draw
|
// Set the uniform and draw
|
||||||
shader.use();
|
shader.use();
|
||||||
shader.setMat4("u_mvp", mvp);
|
shader.setMat4("u_mvp", mvp);
|
||||||
|
// [NEW] Set the color uniform in the fragment shader
|
||||||
|
shader.setVec3("u_Color", color);
|
||||||
|
|
||||||
glBindVertexArray(VAO);
|
glBindVertexArray(VAO);
|
||||||
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, nullptr);
|
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, nullptr);
|
||||||
}
|
}
|
||||||
@ -213,6 +219,8 @@ int main() {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT);
|
||||||
|
|
||||||
// 4. Shader Setup
|
// 4. Shader Setup
|
||||||
Shader shader("shaders/cube_vert.glsl", "shaders/cube_frag.glsl");
|
Shader shader("shaders/cube_vert.glsl", "shaders/cube_frag.glsl");
|
||||||
|
|
||||||
@ -245,11 +253,11 @@ int main() {
|
|||||||
// 6. Tell OpenGL how to interpret the vertex data
|
// 6. Tell OpenGL how to interpret the vertex data
|
||||||
|
|
||||||
// Position Attribute
|
// 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)
|
glEnableVertexAttribArray(0); // Enable the vertex attribute (location 0)
|
||||||
|
|
||||||
// Normal Attribute
|
// 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);
|
glEnableVertexAttribArray(1);
|
||||||
|
|
||||||
// 6.5 Setup EBO
|
// 6.5 Setup EBO
|
||||||
@ -274,6 +282,8 @@ int main() {
|
|||||||
double start_time = glfwGetTime();
|
double start_time = glfwGetTime();
|
||||||
double move_time = glfwGetTime();
|
double move_time = glfwGetTime();
|
||||||
|
|
||||||
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
|
||||||
// 6. --- The Render Loop ---
|
// 6. --- The Render Loop ---
|
||||||
while (!glfwWindowShouldClose(window)) {
|
while (!glfwWindowShouldClose(window)) {
|
||||||
// Input (e.g., close window on ESC)
|
// Input (e.g., close window on ESC)
|
||||||
@ -284,11 +294,31 @@ int main() {
|
|||||||
|
|
||||||
view = camera.getView();
|
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) {
|
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();
|
double end_time = glfwGetTime();
|
||||||
float time_since_last_move = end_time - move_time;
|
float time_since_last_move = end_time - move_time;
|
||||||
if (time_since_last_move >= 0.017)
|
if (time_since_last_move >= 0.017)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user