summaryrefslogtreecommitdiff
path: root/height-map-display
diff options
context:
space:
mode:
authorSchark <jordan@schark.online>2024-03-06 02:00:59 -0800
committerSchark <jordan@schark.online>2024-03-06 02:00:59 -0800
commitc356e96932b5d1a23bf41914934cf83e8b535994 (patch)
tree9ee15f23c0ae584443d8af63b473d5c2eee094ba /height-map-display
parentce811be0799bfee8355aa56d3ab566fb5b5dab55 (diff)
downloadgamedev-c356e96932b5d1a23bf41914934cf83e8b535994.tar.gz
gamedev-c356e96932b5d1a23bf41914934cf83e8b535994.zip
Working barebones heightmap renderer
Diffstat (limited to '')
-rw-r--r--height-map-display/CMakeLists.txt14
-rw-r--r--height-map-display/README.md (renamed from README.md)0
-rwxr-xr-xheight-map-display/height-map-displaybin0 -> 17512 bytes
-rw-r--r--height-map-display/src/main.c185
4 files changed, 199 insertions, 0 deletions
diff --git a/height-map-display/CMakeLists.txt b/height-map-display/CMakeLists.txt
new file mode 100644
index 0000000..5861290
--- /dev/null
+++ b/height-map-display/CMakeLists.txt
@@ -0,0 +1,14 @@
+cmake_minimum_required(VERSION 3.10)
+project(raycaster C)
+
+set(CMAKE_C_STANDARD 99)
+
+find_package(OpenGL REQUIRED)
+find_package(GLEW REQUIRED)
+find_package(glfw3 REQUIRED)
+find_package(Freetype REQUIRED)
+
+file(GLOB SOURCES "src/*.c")
+
+add_executable(height-map-display ${SOURCES})
+target_link_libraries(height-map-display ${OPENGL_gl_LIBRARY} ${OPENGL_INCLUDE_DIRS} ${FREETYPE_LIBARIES} ${GLEW_LIBRARIES} glfw m)
diff --git a/README.md b/height-map-display/README.md
index 84a96cc..84a96cc 100644
--- a/README.md
+++ b/height-map-display/README.md
diff --git a/height-map-display/height-map-display b/height-map-display/height-map-display
new file mode 100755
index 0000000..b67544a
--- /dev/null
+++ b/height-map-display/height-map-display
Binary files differ
diff --git a/height-map-display/src/main.c b/height-map-display/src/main.c
new file mode 100644
index 0000000..5ad5c78
--- /dev/null
+++ b/height-map-display/src/main.c
@@ -0,0 +1,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;
+}