summaryrefslogtreecommitdiff
path: root/height-map-display/src/main.c
blob: 4fe377f25d9e8aa927acbbf5a4a15bd8e99fac59 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#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

float camera_pos_x = 0.0f;
float camera_pos_y = 0.0f;
float camera_pos_z = 0.0f;
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(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
    };

    float scale_factor = 1.0f;

    logger("Init perspective...");
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glTranslatef(-camera_pos_x, -camera_pos_y, -camera_pos_z);
    glRotatef(-camera_pitch, 1.0f, 0.0f, 0.0f);
    glRotatef(-camera_yaw, 0.0f, 1.0f, 0.0f);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-10.0, 10.0, -10.0, 10.0, -20.0, 20.0);

    logger("Init rendering...");
    for (int x = 0; x < MAP_WIDTH - 1; x++) {

        glBegin(GL_TRIANGLE_STRIP);

        for (int y = 0; y < MAP_HEIGHT - 1; y++) {

            float x0 = x;
            float y0 = squares[x][y].nodes[0]->elevation * scale_factor;
            float z0 = y;

            float x1 = x + 1;
            float y1 = squares[x][y].nodes[2]->elevation * scale_factor;
            float z1 = y;

            float* color = colors[0];
            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);
            glVertex3f(x1, y1, z1);

        }
        glEnd();

    }

    // draw grid
    logger("Init grid...");
    if (draw_grid) {
        glColor3f(0.0f, 0.0f, 0.0f);
        glLineWidth(1.0f);
        glBegin(GL_LINES);

        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);
            }
        }

        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);
            }
        }
    }

    logger("Init end...");
    glEnd();
}

// 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;
}

// 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;
}

// 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[]) {
    logger("Initializing window...");
    GLFWwindow* window;

    // initiate glfw library
    if (!glfwInit()){
        logger("Failed to initiate GLFW.");
        return -1;
    }

    // create glfw window
    logger("Creating window...");
    GLFWmonitor* primary = glfwGetPrimaryMonitor();
    const GLFWvidmode* mode = glfwGetVideoMode(primary);
    window = glfwCreateWindow(mode->width, mode->height, "raycaster", primary, NULL);
    if (!window){
        logger("Failed to create GLFW window.");
        glfwTerminate();
        return -1;
    }
    // make window current
    logger("Making window current...");
    glfwMakeContextCurrent(window);
    glewExperimental = GL_TRUE;
    GLenum err = glewInit();
    if (err != GLEW_OK) {
        logger("GLEW init error");
        return -1;
    }
    glEnable(GL_DEPTH_TEST);

    // init grid and squares
    logger("Initializing nodes...");
    Node ***nodes = initialize_nodes(MAP_WIDTH, MAP_HEIGHT);
    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)) {
        logger("Entering main loop...");
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        logger("Rendering...");
        renderHeightMap(squares);

        glfwSwapBuffers(window); // Swap buffers to display the rendered image

        // Poll for and process events, wait for events if none are pending
        glfwWaitEventsTimeout(0.01); // Add a slight delay to reduce CPU usage
    }

    logger("Terminating GLFW...");
    glfwTerminate();
    free_nodes(nodes, MAP_WIDTH, MAP_HEIGHT);
    free_squares(squares, MAP_WIDTH);
    return 0;
}