diff options
Diffstat (limited to 'height-map-display/src')
-rw-r--r-- | height-map-display/src/main.c | 168 | ||||
-rw-r--r-- | height-map-display/src/map/node.c | 20 | ||||
-rw-r--r-- | height-map-display/src/map/node.h | 13 | ||||
-rw-r--r-- | height-map-display/src/map/square.c | 21 | ||||
-rw-r--r-- | height-map-display/src/map/square.h | 24 |
5 files changed, 166 insertions, 80 deletions
diff --git a/height-map-display/src/main.c b/height-map-display/src/main.c index 5979ed4..a83384e 100644 --- a/height-map-display/src/main.c +++ b/height-map-display/src/main.c @@ -1,77 +1,36 @@ #include <GL/glew.h> // must come before glfw3 #include <GLFW/glfw3.h> +#include <stdlib.h> #include <stdio.h> #include <math.h> #include <stdbool.h> +#include "map/node.h" +#include "map/square.h" + #define MAP_HEIGHT 10 #define MAP_WIDTH 10 -//int height_map[MAP_WIDTH][MAP_HEIGHT] = { -// {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, -// {0, 1, 1, 1, 1, 1, 1, 1, 1, 0}, -// {0, 1, 3, 3, 3, 3, 3, 3, 1, 0}, -// {0, 1, 3, 5, 5, 5, 5, 3, 1, 0}, -// {0, 1, 3, 5, 8, 8, 5, 3, 1, 0}, -// {0, 1, 3, 5, 8, 8, 5, 3, 1, 0}, -// {0, 1, 3, 5, 5, 5, 5, 3, 1, 0}, -// {0, 1, 3, 3, 3, 3, 3, 3, 1, 0}, -// {0, 1, 1, 1, 1, 1, 1, 1, 1, 0}, -// {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, -//}; - -bool draw_grid = true; - -int height_map[MAP_WIDTH][MAP_HEIGHT] = { - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 1, 1, 0, 0, 0, 0, 0, 0, 0}, - {0, 1, 0, 0, 0, 0, -1, -2, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, -1, 0, 0}, - {0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, - {0, 0, 0, 1, 2, 1, 0, 0, 0, 0}, - {0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, -}; - -// TODO: move into dedicated camera struct -// Define the perspective projection matrix -void perspective(float fov, float aspect, float near, float far, float *mat) { - float f = 1.0f / tan(fov * 0.5f); - mat[0] = f / aspect; - mat[1] = 0.0f; - mat[2] = 0.0f; - mat[3] = 0.0f; - mat[4] = 0.0f; - mat[5] = f; - mat[6] = 0.0f; - mat[7] = 0.0f; - mat[8] = 0.0f; - mat[9] = 0.0f; - mat[10] = (far + near) / (near - far); - mat[11] = -1.0f; - mat[12] = 0.0f; - mat[13] = 0.0f; - mat[14] = (2.0f * far * near) / (near - far); - mat[15] = 0.0f; -} - 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 -void renderHeightMap() { +bool draw_grid = true; + +// Renderer +void renderHeightMap(Node ***nodes) { + printf("Init colors...\n"); float colors[3][3] = { - {0.0f, 1.0f, 0.0f}, // default green - {0.0f, 0.0f, 1.0f}, // blue - {0.5f, 1.0f, 0.5f} // light green + {0.0f, 1.0f, 0.0f}, // fairway 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"); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); @@ -83,6 +42,7 @@ void renderHeightMap() { 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 glBegin(GL_TRIANGLE_STRIP); @@ -90,11 +50,11 @@ void renderHeightMap() { for (int y = 0; y < MAP_HEIGHT; y++) { float x0 = x; - float y0 = height_map[x][y] * scale_factor; + float y0 = nodes[x][y]->elevation * scale_factor; float z0 = y; float x1 = x + 1; // Adjusted x1 - float y1 = height_map[x+1][y] * scale_factor; // Adjusted indexing + float y1 = nodes[x+1][y]->elevation * scale_factor; // Adjusted indexing float z1 = y; float* color = colors[0]; @@ -112,49 +72,90 @@ void renderHeightMap() { // draw grid + printf("Init grid...\n"); 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; x++) { - for (int y = 0; y < MAP_HEIGHT; y++) { - glVertex3f(x, height_map[x][y] * scale_factor, y); - glVertex3f(x, height_map[x][y + 1] * scale_factor, y + 1); + 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); } } // Draw horizontal grid lines - for (int y = 0; y <= MAP_HEIGHT; y++) { - for (int x = 0; x < MAP_WIDTH; x++) { - glVertex3f(x, height_map[x][y] * scale_factor, y); - glVertex3f(x + 1, height_map[x + 1][y] * scale_factor, y); + 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); } } } + printf("Init end...\n"); glEnd(); } -void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) { +// Function to initialize nodes +Node*** initialize_nodes(int X, int Y) { + Node ***nodes = (Node ***)malloc(X * sizeof(Node **)); + for (int i = 0; i < X; i++) { + nodes[i] = (Node **)malloc(Y * sizeof(Node *)); + for (int j = 0; j < Y; j++) { + // Allocate memory for each Node + nodes[i][j] = (Node *)malloc(sizeof(Node)); + nodes[i][j]->x = i; + nodes[i][j]->y = j; + nodes[i][j]->elevation = 0.0f; + } + } + return nodes; +} - const float camera_speed = 0.5f; +// Function to initialize squares +Square** initialize_squares(Node ***nodes, int X, int Y) { + Square **squares = (Square **)malloc((X - 1) * sizeof(Square *)); + for (int i = 0; i < X - 1; i++) { + squares[i] = (Square *)malloc((Y - 1) * sizeof(Square)); + for (int j = 0; j < Y - 1; j++) { + // Assign pointers to nodes to create the Square + squares[i][j].nodes[0] = nodes[i][j]; + squares[i][j].nodes[1] = nodes[i][j+1]; + squares[i][j].nodes[2] = nodes[i+1][j]; + squares[i][j].nodes[3] = nodes[i+1][j+1]; + squares[i][j].terrain = TERRAIN_ROUGH; + } + } + return squares; +} - if (key == GLFW_KEY_W && (action == GLFW_PRESS || action == GLFW_REPEAT)) { height_map[5][4] += 1; } - else if (key == GLFW_KEY_S && (action == GLFW_PRESS || action == GLFW_REPEAT)) { height_map[5][4] -= 1; } - else if (key == GLFW_KEY_A && (action == GLFW_PRESS || action == GLFW_REPEAT)) { camera_pos_x -= camera_speed; } - else if (key == GLFW_KEY_D && (action == GLFW_PRESS || action == GLFW_REPEAT)) { camera_pos_x += camera_speed; } - else if (key == GLFW_KEY_Q && (action == GLFW_PRESS || action == GLFW_REPEAT)) { camera_pitch -= camera_speed; } - else if (key == GLFW_KEY_E && (action == GLFW_PRESS || action == GLFW_REPEAT)) { camera_pitch += camera_speed; } - else if (key == GLFW_KEY_Z && (action == GLFW_PRESS || action == GLFW_REPEAT)) { camera_yaw -= camera_speed; } - else if (key == GLFW_KEY_C && (action == GLFW_PRESS || action == GLFW_REPEAT)) { camera_yaw += camera_speed; } - else if (key == GLFW_KEY_X && (action == GLFW_PRESS || action == GLFW_REPEAT)) { draw_grid = !draw_grid; } +// TODO: Move into dedicated cleaner header +// Function to free memory allocated for nodes +void free_nodes(Node ***nodes, int X, int Y) { + for (int i = 0; i < X; i++) { + for (int j = 0; j < Y; j++) { + free(nodes[i][j]); + } + free(nodes[i]); + } + free(nodes); +} +// TODO: Move into dedicated cleaner header +// Function to free memory allocated for squares +void free_squares(Square **squares, int X) { + for (int i = 0; i < X - 1; i++) { + free(squares[i]); + } + free(squares); } int main(int argc, char *argv[]) { + printf("Initializing window...\n"); GLFWwindow* window; // initiate glfw library @@ -164,6 +165,7 @@ int main(int argc, char *argv[]) { } // create glfw window + printf("Creating window...\n"); GLFWmonitor* primary = glfwGetPrimaryMonitor(); const GLFWvidmode* mode = glfwGetVideoMode(primary); window = glfwCreateWindow(mode->width, mode->height, "raycaster", primary, NULL); @@ -172,8 +174,8 @@ int main(int argc, char *argv[]) { glfwTerminate(); return -1; } - // make window current + printf("Making window current...\n"); glfwMakeContextCurrent(window); glewExperimental = GL_TRUE; GLenum err = glewInit(); @@ -181,16 +183,20 @@ int main(int argc, char *argv[]) { printf("GLEW init error"); return -1; } - - // enable depth testing glEnable(GL_DEPTH_TEST); - glfwSetKeyCallback(window, key_callback); + // init grid and squares + printf("Initializing nodes...\n"); + Node ***nodes = initialize_nodes(MAP_WIDTH, MAP_HEIGHT); + printf("Initializing squares...\n"); + Square **squares = initialize_squares(nodes, MAP_WIDTH, MAP_HEIGHT); while (!glfwWindowShouldClose(window)) { + printf("Entering main loop...\n"); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - renderHeightMap(); + printf("Rendering...\n"); + renderHeightMap(nodes); glfwSwapBuffers(window); // Swap buffers to display the rendered image @@ -200,5 +206,7 @@ int main(int argc, char *argv[]) { printf("Terminating GLFW...\n"); glfwTerminate(); + free_nodes(nodes, MAP_WIDTH, MAP_HEIGHT); + free_squares(squares, MAP_WIDTH); return 0; } diff --git a/height-map-display/src/map/node.c b/height-map-display/src/map/node.c new file mode 100644 index 0000000..eaf2877 --- /dev/null +++ b/height-map-display/src/map/node.c @@ -0,0 +1,20 @@ +#include "node.h" + +#include <stdio.h> +#include <stdlib.h> +#include <malloc.h> + +Node* create_node(int x, int y, float elevation) { + Node* node = (Node*)malloc(sizeof(Node)); + if (node != NULL) { + node->x = x; + node->y = y; + node->elevation = elevation; + } + return node; +} + +void change_node_height(Node* node, float diff) { + if (node != NULL) { node->elevation = node->elevation + diff; } +} + diff --git a/height-map-display/src/map/node.h b/height-map-display/src/map/node.h new file mode 100644 index 0000000..d12accf --- /dev/null +++ b/height-map-display/src/map/node.h @@ -0,0 +1,13 @@ +#ifndef NODE_H +#define NODE_H + +typedef struct Node { + int x; + int y; + float elevation; +} Node; + +void change_node_height(Node* node, float diff); + +#endif + diff --git a/height-map-display/src/map/square.c b/height-map-display/src/map/square.c new file mode 100644 index 0000000..707f8b7 --- /dev/null +++ b/height-map-display/src/map/square.c @@ -0,0 +1,21 @@ +#include "square.h" +#include "node.h" + +#include <stdio.h> +#include <stdlib.h> +#include <malloc.h> + +Square* init_square(Node *nodes[4], Terrain_Type terrain) { + Square* square; + for (int i; i < 4; i++) { + square->nodes[i] = nodes[i]; + } + square->terrain = terrain; + return square; +} + +void change_square_height(Square* square, float diff) { + for (int i; i < 4; i++) { + square->nodes[i]->elevation = square->nodes[i]->elevation + diff; + } +} diff --git a/height-map-display/src/map/square.h b/height-map-display/src/map/square.h new file mode 100644 index 0000000..d40e7d1 --- /dev/null +++ b/height-map-display/src/map/square.h @@ -0,0 +1,24 @@ +#ifndef SQUARE_H +#define SQUARE_H + +#include "node.h" // Include node.h to use Node type. + +// Define the Terrain_Type enum +typedef enum { + TERRAIN_FAIRWAY, + TERRAIN_ROUGH, + TERRAIN_GREEN, +} Terrain_Type; + +// Define the Square struct +typedef struct Square { + Terrain_Type terrain; + Node *nodes[4]; // Array of Node pointers +} Square; + +// Function prototypes +Square* init_square(Node *nodes[4], Terrain_Type terrain); +void change_square_height(Square* square, float diff); + +#endif + |