-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathshaders.c
More file actions
55 lines (42 loc) · 1.83 KB
/
shaders.c
File metadata and controls
55 lines (42 loc) · 1.83 KB
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
#include "three2d.h"
#include "types/matrix4.h"
t2d_pipeline *current_pipeline;
vector3 camera_pos = {-15,00,100};
vector4 default_vert_shader(vector3 pos){
vector4 cam_pos = (vector4){pos.x + camera_pos.x, pos.y - camera_pos.y, pos.z - camera_pos.z, 1};
vector4 out_v = matrix4_mul(cam_pos, current_pipeline->proj_matrix);
return out_v;
}
argbcolor default_frag_shader(vector4 frag_coord, int trig_id){
return (argbcolor){
.alpha = 0xFF,
.red = ((trig_id) % 225) + 30,
.green = ((trig_id + 0) % 225) + 30,
.blue = ((trig_id + 0) % 225) + 30,
};
}
void t2d_make_orthographic_camera(t2d_pipeline *pipeline, float top, float bottom, float left, float right, float near, float far){
pipeline->proj_matrix = matrix_zero();
pipeline->proj_matrix.m[0][0] = 2.0f/(right-left);
pipeline->proj_matrix.m[1][1] = 2.0f/(top-bottom);
pipeline->proj_matrix.m[2][2] = -2.0f/(far-near);
pipeline->proj_matrix.m[3][3] = 1.0f;
pipeline->proj_matrix.m[3][0] = -(right+left)/(right-left);
pipeline->proj_matrix.m[3][1] = -(top+bottom)/(top-bottom);
pipeline->proj_matrix.m[3][2] = -(far+near)/(far-near);
}
void t2d_make_perspective_camera(t2d_pipeline *pipeline, float fov, float aspect, float near, float far){
float tanfov = 0.726542528f;
pipeline->proj_matrix = matrix_zero();
pipeline->proj_matrix.m[0][0] = 1.0f/(tanfov*aspect);
pipeline->proj_matrix.m[1][1] = 1.0f/tanfov;
pipeline->proj_matrix.m[2][2] = -near/(far-near);
pipeline->proj_matrix.m[3][2] = -(far*near)/(far-near);
pipeline->proj_matrix.m[2][3] = -1.0f;
}
void t2d_vert_shader(t2d_pipeline *pipeline, vertex_shader shader){
pipeline->vert_shader = shader;
}
void t2d_frag_shader(t2d_pipeline *pipeline, fragment_shader shader){
pipeline->frag_shader = shader;
}