summaryrefslogtreecommitdiff
path: root/height-map-display/src/main.c
blob: 5ad5c7814e0702391003a748d707b7ca215629eb (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
#include <GL/glew.h> // must come before glfw3
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <math.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},
};

// 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() {
    float colors[3][3] = {
        {0.0f, 1.0f, 0.0f}, // Green
        {0.5f, 0.5f, 0.5f}, // Gray
        {1.0f, 1.0f, 1.0f}  // White
    };

    float scale_factor = 1.0f;

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

    for (int x = 0; x < MAP_WIDTH - 1; x++) { // Adjusted loop bounds

        glBegin(GL_TRIANGLE_STRIP);

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

            float x0 = x;
            float y0 = height_map[x][y] * scale_factor;
            float z0 = y;

            float x1 = x + 1; // Adjusted x1
            float y1 = height_map[x+1][y] * scale_factor; // Adjusted indexing
            float z1 = y;

            float* color = colors[0];
            if (y0 < 1.0f) { color = colors[1]; }
            if (y0 < 0.5f) { color = colors[2]; }
            glColor3fv(color);

            glVertex3f(x0, y0, z0);
            glVertex3f(x1, y1, z1);

        }
        glEnd();

    }

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

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

    glEnd();

}

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {

    const float camera_speed = 0.5f;

    if (key == GLFW_KEY_W && action == GLFW_PRESS) { camera_pos_z -= camera_speed; }
    else if (key == GLFW_KEY_S && action == GLFW_PRESS) { camera_pos_z += camera_speed; }
    else if (key == GLFW_KEY_A && action == GLFW_PRESS) { camera_pos_x -= camera_speed; }
    else if (key == GLFW_KEY_D && action == GLFW_PRESS) { camera_pos_x += camera_speed; }
    else if (key == GLFW_KEY_Q && action == GLFW_PRESS) { camera_pitch -= camera_speed; }
    else if (key == GLFW_KEY_E && action == GLFW_PRESS) { camera_pitch += camera_speed; }
    else if (key == GLFW_KEY_Z && action == GLFW_PRESS) { camera_yaw -= camera_speed; }
    else if (key == GLFW_KEY_C && action == GLFW_PRESS) { camera_yaw += camera_speed; }

}

int main(int argc, char *argv[]) {
    GLFWwindow* window;

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

    // create glfw 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");
        glfwTerminate();
        return -1;
    }

    // make window current
    glfwMakeContextCurrent(window);
    glewExperimental = GL_TRUE;
    GLenum err = glewInit();
    if (err != GLEW_OK) {
        printf("GLEW init error");
        return -1;
    }

    // enable depth testing
    glEnable(GL_DEPTH_TEST);

    glfwSetKeyCallback(window, key_callback);

    while (!glfwWindowShouldClose(window)) {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        renderHeightMap();

        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
    }

    printf("Terminating GLFW...\n");
    glfwTerminate();
    return 0;
}