25 * POSSIBILITY OF SUCH DAMAGE. |
25 * POSSIBILITY OF SUCH DAMAGE. |
26 */ |
26 */ |
27 |
27 |
28 #include "ascension/2d.h" |
28 #include "ascension/2d.h" |
29 |
29 |
|
30 #include "ascension/constants.h" |
|
31 #include "ascension/context.h" |
|
32 #include "ascension/error.h" |
|
33 #include "ascension/shader.h" |
|
34 |
|
35 #include <GL/glew.h> |
|
36 |
|
37 typedef struct asc_rectangle_shader_s { |
|
38 AscShaderProgram program; |
|
39 GLint color; |
|
40 GLint size; |
|
41 GLint thickness; |
|
42 } AscRectangleShader; |
|
43 |
|
44 static void *asc_rectangle_shader_create(bool fill) { |
|
45 AscShaderCodes codes; |
|
46 if (asc_shader_load_code_files((AscShaderCodeInfo){ |
|
47 .files.vtx = "sprite_vtx.glsl", |
|
48 .files.frag = "rectangle_frag.glsl", |
|
49 .defines.frag = fill ? "#define FILL" : NULL, |
|
50 }, &codes)) { |
|
51 asc_error("Loading sprite shader failed."); |
|
52 return NULL; |
|
53 } |
|
54 AscRectangleShader *shader = asc_shader_create(codes, sizeof(*shader)); |
|
55 if (asc_has_error()) { |
|
56 asc_shader_free_codes(codes); |
|
57 return NULL; |
|
58 } |
|
59 shader->color = glGetUniformLocation(shader->program.gl_id, "color"); |
|
60 shader->size = glGetUniformLocation(shader->program.gl_id, "size"); |
|
61 if (fill) { |
|
62 shader->thickness = -1; |
|
63 } else { |
|
64 shader->thickness = glGetUniformLocation(shader->program.gl_id, "thickness"); |
|
65 } |
|
66 asc_shader_free_codes(codes); |
|
67 |
|
68 asc_error_catch_all_gl(); |
|
69 |
|
70 return shader; |
|
71 } |
|
72 |
|
73 static AscShaderProgram *asc_rectangle_shader_fill_create() { |
|
74 return asc_rectangle_shader_create(true); |
|
75 } |
|
76 |
|
77 static AscShaderProgram *asc_rectangle_shader_draw_create() { |
|
78 return asc_rectangle_shader_create(false); |
|
79 } |
|
80 |
|
81 static const AscRectangleShader *asc_rectangle_shader_draw(void) { |
|
82 return asc_shader_lookup_or_create(ASC_SHADER_RECTANGLE_DRAW, asc_rectangle_shader_draw_create); |
|
83 } |
|
84 |
|
85 static const AscRectangleShader *asc_rectangle_shader_fill(void) { |
|
86 return asc_shader_lookup_or_create(ASC_SHADER_RECTANGLE_FILL, asc_rectangle_shader_fill_create); |
|
87 } |
|
88 |
|
89 static void asc_rectangle_destroy(AscSceneNode *node) { |
|
90 asc_ptr_cast(AscRectangle, rectangle, node); |
|
91 asc_mesh_destroy(&rectangle->mesh); |
|
92 } |
|
93 |
|
94 static void asc_rectangle_update(AscSceneNode *node) { |
|
95 asc_ptr_cast(AscRectangle, rectangle, node); |
|
96 asc_vec2f size = asc_vec2f_new(rectangle->width, rectangle->height); |
|
97 asc_mesh_plane_2d(&rectangle->mesh, .size = size, .uv_scale = size); |
|
98 } |
|
99 |
|
100 static void asc_rectangle_draw(const AscCamera *camera, const AscSceneNode *node) { |
|
101 asc_ptr_cast(AscRectangle, rectangle, node); |
|
102 bool filled = asc_test_flag(rectangle->flags, ASC_RECTANGLE_FILLED); |
|
103 |
|
104 // Activate shader |
|
105 // TODO: scene should know which shader we are going to activate s.t. it can pre-sort nodes |
|
106 const AscRectangleShader *shader = filled |
|
107 ? asc_rectangle_shader_fill() |
|
108 : asc_rectangle_shader_draw(); |
|
109 asc_shader_use(&shader->program, camera); |
|
110 |
|
111 // Upload uniforms |
|
112 // TODO: uploading model matrix could be a helper function |
|
113 glUniformMatrix4fv(shader->program.model, 1, |
|
114 GL_FALSE, node->world_transform); |
|
115 |
|
116 glUniform4f(shader->color, |
|
117 rectangle->color.red, |
|
118 rectangle->color.green, |
|
119 rectangle->color.blue, |
|
120 rectangle->color.alpha |
|
121 ); |
|
122 glUniform2f(shader->size, (float) rectangle->width, (float) rectangle->height); |
|
123 |
|
124 if (!filled) { |
|
125 // TODO: implement thickness |
|
126 glUniform1f(shader->thickness, 1); |
|
127 } |
|
128 |
|
129 // Draw mesh |
|
130 asc_mesh_draw_triangle_strip(&rectangle->mesh); |
|
131 } |
|
132 |
|
133 AscSceneNode *asc_rectangle_create(struct asc_rectangle_create_args args) { |
|
134 AscRectangle *rectangle = cxZallocDefault(sizeof(AscRectangle)); |
|
135 |
|
136 if (args.bounds.size.width + args.bounds.size.height > 0) { |
|
137 rectangle->node.position.x = (float) args.bounds.pos.x; |
|
138 rectangle->node.position.y = (float) args.bounds.pos.y; |
|
139 rectangle->width = args.bounds.size.width; |
|
140 rectangle->height = args.bounds.size.height; |
|
141 } else { |
|
142 rectangle->node.position.x = (float) args.x; |
|
143 rectangle->node.position.y = (float) args.y; |
|
144 rectangle->width = args.width; |
|
145 rectangle->height = args.height; |
|
146 } |
|
147 |
|
148 rectangle->color = asc_col_itof(asc_context.ink); |
|
149 |
|
150 if (args.filled) { |
|
151 asc_set_flag(rectangle->flags, ASC_RECTANGLE_FILLED); |
|
152 } |
|
153 |
|
154 AscSceneNode *node = &rectangle->node; |
|
155 node->position.z = ASC_SCENE_2D_DEPTH_OFFSET; |
|
156 node->scale = asc_vec3f_one; |
|
157 node->render_group = asc_context.ink.alpha < 255 |
|
158 ? ASC_RENDER_GROUP_2D_BLEND |
|
159 : ASC_RENDER_GROUP_2D_OPAQUE; |
|
160 node->update_func = asc_rectangle_update; |
|
161 node->destroy_func = asc_rectangle_destroy; |
|
162 node->draw_func = asc_rectangle_draw; |
|
163 asc_node_update(node); |
|
164 return node; |
|
165 } |