Add ortographic camera

This commit is contained in:
Phireh 2023-04-30 17:18:15 +02:00
commit 8c4b7bff18
5 changed files with 202 additions and 14 deletions

105
main.c
View file

@ -8,9 +8,8 @@
#include <stdlib.h>
#include "hex_math.h"
#include "hex_debug.h"
#define log_err(str, ...) do { fprintf(stderr, "[ERROR] (%s:%d): " str "\n", __FILE__, __LINE__, ##__VA_ARGS__); } while (0)
#define log_debug(str, ...) do { fprintf(stderr, "[DEBUG] (%s:%d): " str "\n", __FILE__, __LINE__, ##__VA_ARGS__); } while (0)
char *read_entire_file(char *path)
{
@ -20,7 +19,7 @@ char *read_entire_file(char *path)
if (f)
{
fseek(f, 0, SEEK_END);
length = ftell(f);
length = (size_t)ftell(f);
fseek(f, 0, SEEK_SET);
if (length)
{
@ -37,12 +36,31 @@ static void glfw_error_callback(int errcode, const char *description)
fprintf(stderr, "[GLFW ERROR][ERRCODE %d] %s\n", errcode, description);
}
void set_uniform(GLuint program_id, const char *name, vec4f value)
void set_uniform4f(GLuint program_id, const char *name, vec4f value)
{
GLint location = glGetUniformLocation(program_id, name);
glUniform4f(location, value.x, value.y, value.z, value.w);
}
void set_uniform2f(GLuint program_id, const char *name, vec2f value)
{
GLint location = glGetUniformLocation(program_id, name);
glUniform2f(location, value.x, value.y);
}
void set_uniform4x4f(GLuint program_id, const char *name, mat4f value)
{
GLint location = glGetUniformLocation(program_id, name);
glUniformMatrix4fv(location, 1, GL_TRUE, &value.a0);
}
#define set_uniform(id, name, value) _Generic((value), \
vec4f: set_uniform4f, \
vec2f: set_uniform2f, \
mat4f: set_uniform4x4f)(id, name, value)
int main(void)
{
GLFWwindow *window;
@ -112,12 +130,12 @@ int main(void)
background_color.a = 1.0f;
hexgrid_t grid;
grid.rows = 10;
grid.columns = 10;
grid.rows = 30;
grid.columns = 30;
grid.hexes = calloc(1, sizeof(hex_t) * grid.rows * grid.columns);
// Scale of hexagon relative to screen. A 1:1 hexagon would reach the entire horizontal space
float scale = 1.0f / 5.0f;
float scale = 1.0f / 1.0f;
for (size_t i = 0; i < grid.columns; ++i)
{
@ -126,13 +144,14 @@ int main(void)
// Offset the X and Y coordinates of adjacent hexagons so they do not cut each other
float offset = 0.0f;
if (j % 2)
offset = sint(1.0f/6);
offset = (float)sint(1.0f/6);
grid.hexes[i*grid.rows+j].position.x = (0.0f + j*(1 + cost(1.0f/6)));
grid.hexes[i*grid.rows+j].position.y = (0.0f + i*(sint(1.0f/6))*2.0f) + offset;
grid.hexes[i*grid.rows+j].position.z = 0.0f;
grid.hexes[i*grid.rows+j].position.w = 1.0f;
fprintf(stdout, "Position of hex %ld %ld is %f %f\n", i, j, grid.hexes[i*grid.rows+j].position.x, grid.hexes[i*grid.rows+j].position.y);
log_debug("Position of hex %ld %ld is %f %f\n", i, j, grid.hexes[i*grid.rows+j].position.x, grid.hexes[i*grid.rows+j].position.y);
}
}
@ -184,8 +203,63 @@ int main(void)
//glBindBuffer(GL_ARRAY_BUFFER, 0);
//glBindVertexArray(0);
camera_t camera = {};
camera.near = -0.1f;
camera.far = 1.0f;
camera.left = 0.0f;
camera.right = 600.0f;
camera.top = 0.0f;
camera.bottom = 600.0f;
while (!glfwWindowShouldClose(window))
{
// Camera logic
if (glfwGetKey(window, GLFW_KEY_A))
{
camera.right -= (camera.right - camera.left) * 0.01f;
camera.left -= (camera.right - camera.left) * 0.01f;
}
if (glfwGetKey(window, GLFW_KEY_D))
{
camera.right += (camera.right - camera.left) * 0.01f;
camera.left += (camera.right - camera.left) * 0.01f;
}
if (glfwGetKey(window, GLFW_KEY_W))
{
camera.top += (camera.bottom - camera.top) * 0.01f;
camera.bottom += (camera.bottom - camera.top) * 0.01f;
}
if (glfwGetKey(window, GLFW_KEY_S))
{
camera.top -= (camera.bottom - camera.top) * 0.01f;
camera.bottom -= (camera.bottom - camera.top) * 0.01f;
}
if (glfwGetKey(window, GLFW_KEY_Q))
{
float offset = (camera.right - camera.left)*0.02f;
camera.right -= offset;
camera.left += offset;
camera.top += offset;
camera.bottom -= offset;
}
if (glfwGetKey(window, GLFW_KEY_E))
{
float offset = (camera.right - camera.left)*0.05f;
camera.right += offset;
camera.left -= offset;
camera.top -= offset;
camera.bottom += offset;
}
if (glfwGetKey(window, GLFW_KEY_R))
{
camera.near = -0.1f;
camera.far = 1.0f;
camera.left = 0.0f;
camera.right = 400.0f;
camera.top = 0.0f;
camera.bottom = 400.0f;
}
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
@ -194,10 +268,19 @@ int main(void)
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shader_program);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
mat4f camera_matrix = ortho_matrix(camera.left, camera.right, camera.top, camera.bottom, camera.near, camera.far, width, height);
set_uniform(shader_program, "camera", camera_matrix);
vec2f window_size = { .x = (float)width, .y = (float)height };
set_uniform(shader_program, "window_size", window_size);
// For debugging
float aux;
glGetUniformfv(shader_program, glGetUniformLocation(shader_program, "window_size"), &aux);
for (size_t i = 0; i < grid.rows * grid.columns; ++i)
{
@ -207,6 +290,7 @@ int main(void)
hex_color.g = (float)i / (grid.rows * grid.columns);
hex_color.b = (float)i / (grid.rows * grid.columns);
hex_color.a = 1.0f;
set_uniform(shader_program, "uniform_color", hex_color);
glBindVertexArray(grid.hexes[i].vao);
@ -215,6 +299,7 @@ int main(void)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, grid.hexes[i].ebo);
glDrawElements(GL_TRIANGLES, 3*6, GL_UNSIGNED_INT, 0);
}
glfwPollEvents();
glfwSwapBuffers(window);
}