48 .defines.frag = rect ? "#define USE_RECT" : NULL, |
48 .defines.frag = rect ? "#define USE_RECT" : NULL, |
49 }, &codes)) { |
49 }, &codes)) { |
50 asc_error("Loading sprite shader failed."); |
50 asc_error("Loading sprite shader failed."); |
51 return NULL; |
51 return NULL; |
52 } |
52 } |
53 // TODO: find better way to deal with inheritance (&shader->program just looks bad) |
53 AscShaderProgram *shader = asc_shader_create(codes, sizeof(*shader)); |
54 AscSpriteShader *shader = asc_shader_create(codes, sizeof(*shader)); |
54 if (asc_shader_invalid(shader)) { |
55 if (asc_shader_invalid(&shader->program)) { |
|
56 asc_shader_free_codes(codes); |
55 asc_shader_free_codes(codes); |
57 return (AscShaderProgram*) shader; |
56 return shader; |
58 } |
57 } |
59 shader->tex = asc_shader_get_uniform_loc(&shader->program, "tex"); |
58 asc_ptr_cast(AscSpriteShader, sprite_shader, shader); |
|
59 sprite_shader->tex = asc_shader_get_uniform_loc(shader, "tex"); |
60 asc_shader_free_codes(codes); |
60 asc_shader_free_codes(codes); |
61 |
61 |
62 asc_error_catch_all_gl(); |
62 asc_error_catch_all_gl(); |
63 |
63 |
64 return (AscShaderProgram*) shader; |
64 return shader; |
65 } |
65 } |
66 |
66 |
67 static void asc_sprite_destroy(AscSceneNode *node) { |
67 static void asc_sprite_destroy(AscSceneNode *node) { |
68 AscSprite *sprite = (AscSprite *) node; |
68 asc_ptr_cast(AscSprite, sprite, node); |
69 asc_mesh_destroy(&sprite->mesh); |
69 asc_mesh_destroy(&sprite->mesh); |
70 sprite->texture->refcount--; |
70 sprite->texture->refcount--; |
71 } |
71 } |
72 |
72 |
73 static void asc_sprite_update(AscSceneNode *node) { |
73 static void asc_sprite_update(AscSceneNode *node) { |
74 AscSprite *sprite = (AscSprite *) node; |
74 asc_ptr_cast(AscSprite, sprite, node); |
75 |
75 |
76 // calculate texture parameters |
76 // calculate texture parameters |
77 asc_vec2f uv_scale; |
77 asc_vec2f uv_scale; |
78 if (sprite->texture_scale_mode == ASC_TEXTURE_SCALE_REPEAT) { |
78 if (sprite->texture_scale_mode == ASC_TEXTURE_SCALE_REPEAT) { |
79 uv_scale = asc_texture_calculate_uv_scale(sprite->texture, |
79 uv_scale = asc_texture_calculate_uv_scale(sprite->texture, |
88 .uv_scale = uv_scale |
88 .uv_scale = uv_scale |
89 ); |
89 ); |
90 } |
90 } |
91 |
91 |
92 void asc_sprite_draw(const AscCamera *camera, const AscSceneNode *node) { |
92 void asc_sprite_draw(const AscCamera *camera, const AscSceneNode *node) { |
93 asc_ptr_cast(const AscSprite, sprite, node); |
93 asc_cptr_cast(AscSprite, sprite, node); |
94 |
94 |
95 // Activate shader |
95 // Activate shader |
96 // TODO: scene should know which shader we are going to activate s.t. it can pre-sort nodes |
96 // TODO: scene should know which shader we are going to activate s.t. it can pre-sort nodes |
97 const AscSpriteShader *shader = |
97 const AscShaderProgram *shader = |
98 asc_texture_is_uv_normalized(sprite->texture) |
98 asc_texture_is_uv_normalized(sprite->texture) |
99 ? asc_shader_lookup_or_create(ASC_SHADER_SPRITE_UV, asc_sprite_shader_create, 0) |
99 ? asc_shader_lookup_or_create(ASC_SHADER_SPRITE_UV, asc_sprite_shader_create, 0) |
100 : asc_shader_lookup_or_create(ASC_SHADER_SPRITE_RECT, asc_sprite_shader_create, 1); |
100 : asc_shader_lookup_or_create(ASC_SHADER_SPRITE_RECT, asc_sprite_shader_create, 1); |
101 // TODO: how to remove the following cast? |
101 if (asc_shader_use(shader, camera)) return; |
102 if (asc_shader_invalid((AscShaderProgram*)shader)) return; |
102 asc_cptr_cast(AscSpriteShader, sprite_shader, shader); |
103 asc_shader_use(&shader->program, camera); |
|
104 |
103 |
105 // Upload model matrix |
104 // Upload model matrix |
106 asc_shader_upload_model_matrix(&shader->program, node); |
105 asc_shader_upload_model_matrix(shader, node); |
107 |
106 |
108 // Bind texture |
107 // Bind texture |
109 asc_texture_bind(sprite->texture, shader->tex, 0); |
108 asc_texture_bind(sprite->texture, sprite_shader->tex, 0); |
110 |
109 |
111 // Draw mesh |
110 // Draw mesh |
112 asc_mesh_draw_triangle_strip(&sprite->mesh); |
111 asc_mesh_draw_triangle_strip(&sprite->mesh); |
113 } |
112 } |
114 |
113 |