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

View file

@ -1,10 +1,26 @@
#define _USE_MATH_DEFINES // for finding things like M_PI in Windows build
#include <math.h>
#include <stdint.h>
#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 {
@ -36,6 +52,37 @@ typedef struct {
};
} 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
@ -44,7 +91,6 @@ typedef struct {
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 {
@ -53,7 +99,17 @@ typedef struct {
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)
@ -65,3 +121,32 @@ 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;
}