src/sprite.c

changeset 158
f650994ec543
parent 151
42960d0c879b
child 161
4c8b9c6d241f
equal deleted inserted replaced
157:d6c2f028d8c9 158:f650994ec543
40 typedef struct asc_sprite_shader_s { 40 typedef struct asc_sprite_shader_s {
41 AscShaderProgram program; 41 AscShaderProgram program;
42 GLint tex; 42 GLint tex;
43 } AscSpriteShader; 43 } AscSpriteShader;
44 44
45 static void *asc_sprite_shader_create(bool rect) { 45 static AscShaderProgram *asc_sprite_shader_create(int rect) {
46 AscShaderCodes codes; 46 AscShaderCodes codes;
47 if (asc_shader_load_code_files((AscShaderCodeInfo){ 47 if (asc_shader_load_code_files((AscShaderCodeInfo){
48 .files.vtx = "sprite_vtx.glsl", 48 .files.vtx = "sprite_vtx.glsl",
49 .files.frag = "sprite_frag.glsl", 49 .files.frag = "sprite_frag.glsl",
50 .defines.frag = rect ? "#define USE_RECT" : NULL, 50 .defines.frag = rect ? "#define USE_RECT" : NULL,
60 shader->tex = glGetUniformLocation(shader->program.gl_id, "tex"); 60 shader->tex = glGetUniformLocation(shader->program.gl_id, "tex");
61 asc_shader_free_codes(codes); 61 asc_shader_free_codes(codes);
62 62
63 asc_error_catch_all_gl(); 63 asc_error_catch_all_gl();
64 64
65 return shader; 65 return (AscShaderProgram*) shader;
66 }
67
68 static AscShaderProgram *asc_sprite_shader_rect_create() {
69 return asc_sprite_shader_create(true);
70 }
71
72 static AscShaderProgram *asc_sprite_shader_uv_create() {
73 return asc_sprite_shader_create(false);
74 }
75
76 static const AscSpriteShader *asc_sprite_shader_rect(void) {
77 return asc_shader_lookup_or_create(ASC_SHADER_SPRITE_RECT, asc_sprite_shader_rect_create);
78 }
79
80 static const AscSpriteShader *asc_sprite_shader_uv(void) {
81 return asc_shader_lookup_or_create(ASC_SHADER_SPRITE_UV, asc_sprite_shader_uv_create);
82 } 66 }
83 67
84 static void asc_sprite_destroy(AscSceneNode *node) { 68 static void asc_sprite_destroy(AscSceneNode *node) {
85 AscSprite *sprite = (AscSprite *) node; 69 AscSprite *sprite = (AscSprite *) node;
86 asc_mesh_destroy(&sprite->mesh); 70 asc_mesh_destroy(&sprite->mesh);
111 95
112 // Activate shader 96 // Activate shader
113 // TODO: scene should know which shader we are going to activate s.t. it can pre-sort nodes 97 // TODO: scene should know which shader we are going to activate s.t. it can pre-sort nodes
114 const AscSpriteShader *shader = 98 const AscSpriteShader *shader =
115 sprite->texture->target == GL_TEXTURE_RECTANGLE 99 sprite->texture->target == GL_TEXTURE_RECTANGLE
116 ? asc_sprite_shader_rect() 100 ? asc_shader_lookup_or_create(ASC_SHADER_SPRITE_RECT, asc_sprite_shader_create, 1)
117 : asc_sprite_shader_uv(); 101 : asc_shader_lookup_or_create(ASC_SHADER_SPRITE_UV, asc_sprite_shader_create, 0);
118 asc_shader_use(&shader->program, camera); 102 asc_shader_use(&shader->program, camera);
119 103
120 // Upload model matrix 104 // Upload model matrix
121 glUniformMatrix4fv(shader->program.model, 1, 105 glUniformMatrix4fv(shader->program.model, 1,
122 GL_FALSE, node->world_transform); 106 GL_FALSE, node->world_transform);

mercurial