-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.h
114 lines (81 loc) · 2.61 KB
/
common.h
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
#define _CRT_SECURE_NO_WARNINGS
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <SDL2/SDL.h>
#include "GL.h"
#define WINDOW_WIDTH 1024
#define WINDOW_HEIGHT 768
#define DEBUG_GRAPHICS true
#define PI ((float)M_PI)
#define TO_RADIANS (PI / 180.0f)
#define FRAME_TIME (1 / 60.0f)
#define UNUSED(var) (void)(var)
#define COUNTOF(a) (sizeof(a) / sizeof(a[0]))
typedef struct Vector3
{
float x, y, z;
} Vector3;
typedef struct Vector4
{
float x, y, z, w;
} Vector4;
typedef struct Matrix4
{
float e[16];
} Matrix4;
typedef struct Color
{
float r, g, b, a;
} Color;
typedef struct PackedColor
{
uint8_t r, g, b, a;
} PackedColor;
typedef struct BasicVertex
{
Vector3 position;
float padding;
Vector3 normal;
PackedColor color;
} BasicVertex;
typedef struct Mesh
{
GLuint vao, vertexBuffer, indexBuffer;
size_t primitiveCount;
} Mesh;
//=============================================================================================
// Basics
//=============================================================================================
void check(bool condition, char *message);
void *xalloc(size_t size);
char *readTextFile(char *path);
//=============================================================================================
// GL
//=============================================================================================
GLuint compileShaderProgram(char *vertexShaderSource, char *fragmentShaderSource);
void createMesh(Mesh *mesh);
void setMeshData(
Mesh *mesh,
size_t vertexCount, BasicVertex *vertexData,
size_t indexCount, uint16_t *indexData);
//=============================================================================================
// Matrices
//=============================================================================================
Matrix4 matrixPixelPerfect();
Matrix4 matrixPerspective(float near, float fov);
Matrix4 matrixMultiply(Matrix4 left, Matrix4 right);
void matrixConcat(Matrix4 *left, Matrix4 right);
Matrix4 matrixTranslation(Vector3 v);
Matrix4 matrixTranslationF(float x, float y, float z);
Matrix4 matrixRotationX(float radians);
Matrix4 matrixRotationY(float radians);
Matrix4 matrixRotationZ(float radians);
Matrix4 matrixScaleF(float x, float y, float z);
Matrix4 matrixScaleUniform(float s);
Vector3 matrixTransformPoint(Matrix4 transform, Vector3 point);
//=============================================================================================
// Modes
//=============================================================================================
void screensaverCube();
void screensaverCheckers();