summaryrefslogtreecommitdiff
path: root/height-map-display/src
diff options
context:
space:
mode:
authorSchark <jordan@schark.online>2024-03-17 02:20:30 -0700
committerSchark <jordan@schark.online>2024-03-17 02:20:30 -0700
commit6853e2f83904bafcc4f1131571ba92c7f79f44b3 (patch)
tree1e0074823b3724d46e221f03162616f0fb2664fe /height-map-display/src
parente7a4152899e63182a4cc0dffff6cd331d6dac305 (diff)
downloadgamedev-6853e2f83904bafcc4f1131571ba92c7f79f44b3.tar.gz
gamedev-6853e2f83904bafcc4f1131571ba92c7f79f44b3.zip
Map overhaul complete!
Diffstat (limited to '')
-rw-r--r--height-map-display/src/main.c96
-rw-r--r--height-map-display/src/map/square.c4
2 files changed, 55 insertions, 45 deletions
diff --git a/height-map-display/src/main.c b/height-map-display/src/main.c
index a83384e..4fe377f 100644
--- a/height-map-display/src/main.c
+++ b/height-map-display/src/main.c
@@ -14,23 +14,31 @@
float camera_pos_x = 0.0f;
float camera_pos_y = 0.0f;
float camera_pos_z = 0.0f;
-float camera_yaw = 0.0f; // rotation around y-axis
-float camera_pitch = 0.0f; // rotation aroudn x-axis
+float camera_yaw = 20.0f; // rotation around y-axis
+float camera_pitch = -50.0f; // rotation aroudn x-axis
bool draw_grid = true;
+bool DEBUG = false;
+
+void logger(const char* msg) {
+ if (DEBUG) {
+ printf("%s\n", msg);
+ }
+}
// Renderer
-void renderHeightMap(Node ***nodes) {
- printf("Init colors...\n");
- float colors[3][3] = {
- {0.0f, 1.0f, 0.0f}, // fairway green
+void renderHeightMap(Square **squares) {
+ logger("Init colors...");
+ float colors[4][3] = {
+ {0.0f, 0.0f, 0.0f}, // init
+ {0.0f, 1.0f, 0.0f}, // default green
+ {0.0f, 0.5f, 0.0f}, // rough green
{0.5f, 1.0f, 0.5f}, // light green
- {0.0f, 0.5f, 0.0f}, // fairway green
};
float scale_factor = 1.0f;
- printf("Init perspective...\n");
+ logger("Init perspective...");
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
@@ -42,24 +50,25 @@ void renderHeightMap(Node ***nodes) {
glLoadIdentity();
glOrtho(-10.0, 10.0, -10.0, 10.0, -20.0, 20.0);
- printf("Init rendering...\n");
- for (int x = 0; x < MAP_WIDTH - 1; x++) { // Adjusted loop bounds
+ logger("Init rendering...");
+ for (int x = 0; x < MAP_WIDTH - 1; x++) {
glBegin(GL_TRIANGLE_STRIP);
- for (int y = 0; y < MAP_HEIGHT; y++) {
+ for (int y = 0; y < MAP_HEIGHT - 1; y++) {
float x0 = x;
- float y0 = nodes[x][y]->elevation * scale_factor;
+ float y0 = squares[x][y].nodes[0]->elevation * scale_factor;
float z0 = y;
- float x1 = x + 1; // Adjusted x1
- float y1 = nodes[x+1][y]->elevation * scale_factor; // Adjusted indexing
+ float x1 = x + 1;
+ float y1 = squares[x][y].nodes[2]->elevation * scale_factor;
float z1 = y;
float* color = colors[0];
- if (y0 > 0.5f) { color = colors[2]; }
- if (y0 < -0.5f) { color = colors[1]; }
+ if (squares[x][y].terrain == TERRAIN_FAIRWAY) { color = colors[1]; }
+ if (squares[x][y].terrain == TERRAIN_ROUGH) { color = colors[2]; }
+ if (squares[x][y].terrain == TERRAIN_GREEN) { color = colors[3]; }
glColor3fv(color);
glVertex3f(x0, y0, z0);
@@ -71,33 +80,29 @@ void renderHeightMap(Node ***nodes) {
}
// draw grid
-
- printf("Init grid...\n");
- if(draw_grid) {
+ logger("Init grid...");
+ if (draw_grid) {
glColor3f(0.0f, 0.0f, 0.0f);
glLineWidth(1.0f);
glBegin(GL_LINES);
- // Draw vertical grid lines
- for (int x = 0; x <= MAP_WIDTH - 1; x++) {
- for (int y = 0; y < MAP_HEIGHT - 1; y++) {
- glVertex3f(x, nodes[x][y]->elevation * scale_factor, y);
- glVertex3f(x, nodes[x][y + 1]->elevation * scale_factor, y + 1);
+ for (int x = 0; x <= MAP_WIDTH - 2; x++) {
+ for (int y = 0; y < MAP_HEIGHT - 2; y++) {
+ glVertex3f(x, squares[x][y].nodes[0]->elevation * scale_factor, y);
+ glVertex3f(x, squares[x][y + 1].nodes[0]->elevation * scale_factor, y + 1);
}
}
- // Draw horizontal grid lines
- for (int y = 0; y <= MAP_HEIGHT - 1; y++) {
- for (int x = 0; x < MAP_WIDTH - 1; x++) {
- glVertex3f(x, nodes[x][y]->elevation * scale_factor, y);
- glVertex3f(x + 1, nodes[x + 1][y]->elevation * scale_factor, y);
+ for (int y = 0; y <= MAP_HEIGHT - 2; y++) {
+ for (int x = 0; x < MAP_WIDTH - 2; x++) {
+ glVertex3f(x, squares[x][y].nodes[0]->elevation * scale_factor, y);
+ glVertex3f(x + 1, squares[x + 1][y].nodes[0]->elevation * scale_factor, y);
}
}
}
- printf("Init end...\n");
+ logger("Init end...");
glEnd();
-
}
// Function to initialize nodes
@@ -155,48 +160,53 @@ void free_squares(Square **squares, int X) {
}
int main(int argc, char *argv[]) {
- printf("Initializing window...\n");
+ logger("Initializing window...");
GLFWwindow* window;
// initiate glfw library
if (!glfwInit()){
- printf("Failed to initiate GLFW.\n");
+ logger("Failed to initiate GLFW.");
return -1;
}
// create glfw window
- printf("Creating window...\n");
+ logger("Creating window...");
GLFWmonitor* primary = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(primary);
window = glfwCreateWindow(mode->width, mode->height, "raycaster", primary, NULL);
if (!window){
- printf("Failed to create GLFW window.\n");
+ logger("Failed to create GLFW window.");
glfwTerminate();
return -1;
}
// make window current
- printf("Making window current...\n");
+ logger("Making window current...");
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (err != GLEW_OK) {
- printf("GLEW init error");
+ logger("GLEW init error");
return -1;
}
glEnable(GL_DEPTH_TEST);
// init grid and squares
- printf("Initializing nodes...\n");
+ logger("Initializing nodes...");
Node ***nodes = initialize_nodes(MAP_WIDTH, MAP_HEIGHT);
- printf("Initializing squares...\n");
+ logger("Initializing squares...");
Square **squares = initialize_squares(nodes, MAP_WIDTH, MAP_HEIGHT);
+ // Debug stuff
+ squares[0][0].nodes[0]->elevation = 20.0f;
+ change_square_height(&squares[3][3], 5.0f);
+ change_square_height(&squares[5][5], -5.0f);
+
while (!glfwWindowShouldClose(window)) {
- printf("Entering main loop...\n");
+ logger("Entering main loop...");
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- printf("Rendering...\n");
- renderHeightMap(nodes);
+ logger("Rendering...");
+ renderHeightMap(squares);
glfwSwapBuffers(window); // Swap buffers to display the rendered image
@@ -204,7 +214,7 @@ int main(int argc, char *argv[]) {
glfwWaitEventsTimeout(0.01); // Add a slight delay to reduce CPU usage
}
- printf("Terminating GLFW...\n");
+ logger("Terminating GLFW...");
glfwTerminate();
free_nodes(nodes, MAP_WIDTH, MAP_HEIGHT);
free_squares(squares, MAP_WIDTH);
diff --git a/height-map-display/src/map/square.c b/height-map-display/src/map/square.c
index 707f8b7..1d74ff5 100644
--- a/height-map-display/src/map/square.c
+++ b/height-map-display/src/map/square.c
@@ -15,7 +15,7 @@ Square* init_square(Node *nodes[4], Terrain_Type terrain) {
}
void change_square_height(Square* square, float diff) {
- for (int i; i < 4; i++) {
- square->nodes[i]->elevation = square->nodes[i]->elevation + diff;
+ for (int i = 0; i < 4; i++) {
+ square->nodes[i]->elevation = square->nodes[i]->elevation + diff;
}
}