#define _USE_MATH_DEFINES // for finding things like M_PI in Windows build #include #include #include "hex_debug.h" /* Types */ typedef float _v2f __attribute__((matrix_type(1,2))); typedef float _v3f __attribute__((matrix_type(1,3))); typedef float _v4f __attribute__((matrix_type(1,4))); typedef float _mat4 __attribute__((matrix_type(4,4))); typedef struct { union { struct { float first; float second; }; _v2f v2f; struct { float x; float y; }; }; } vec2f; typedef struct { union { struct { float first; _v3f tail; }; struct { _v2f first_half; _v2f second_half; }; struct { _v3f v3f; float last; }; _v4f v4f; struct { float r; float g; float b; float a; }; struct { float x; float y; float z; float w; }; }; } vec4f; /* Row-major matrix, make sure to pass it to OpenGL trasposed */ typedef struct { union { _mat4 m4f; struct { float a0; float a1; float a2; float a3; float b0; float b1; float b2; float b3; float c0; float c1; float c2; float c3; float d0; float d1; float d2; float d3; }; struct { vec4f row0; vec4f row1; vec4f row2; vec4f row3; }; }; } mat4f; typedef struct { vec4f position; // center pos float radius; // side length uint32_t vao; uint32_t ebo; uint32_t vbo; float vertices[7*3]; // 7 vert per hex unsigned int indices[6*3]; // 6 triangles to render a hex } hex_t; typedef struct { hex_t *hexes; uint32_t rows; uint32_t columns; } hexgrid_t; typedef struct { float left; float right; float top; float bottom; float near; float far; } camera_t; _Static_assert(sizeof(vec4f) == sizeof(float) * 4, "Bad vector size"); _Static_assert(sizeof(mat4f) == sizeof(float) * 16, "Bad matrix size"); /* Functions */ double sint(double turns) { return sin(turns*2*M_PI); } double cost(double turns) { return cos(turns*2*M_PI); } mat4f ortho_matrix(float left, float right, float top, float bottom, float near, float far, float width, float height) { // Invert Y coord float aux; aux = bottom; bottom = top; top = aux; // Scale and move to [-1,1] left = (left*2 - width)/width; right = (right*2 - width)/width; top = (top*2 - height)/height; bottom = (bottom*2 - height)/height; mat4f matrix = {}; matrix.a0 = 2/(right - left); matrix.a3 = -((right + left)/(right - left)); matrix.b1 = 2/(top - bottom); matrix.b3 = -((top + bottom)/(top - bottom)); matrix.c2 = -2/(far - near); matrix.c3 = -((far + near)/(far - near)); matrix.d3 = 1; return matrix; }