src/sprite.c

changeset 168
f70569c49c24
parent 167
8e6a661c87db
equal deleted inserted replaced
167:8e6a661c87db 168:f70569c49c24
32 #include "ascension/error.h" 32 #include "ascension/error.h"
33 #include "ascension/mesh.h" 33 #include "ascension/mesh.h"
34 #include "ascension/shader.h" 34 #include "ascension/shader.h"
35 #include "ascension/constants.h" 35 #include "ascension/constants.h"
36 36
37 #include <GL/glew.h>
38
39 37
40 typedef struct asc_sprite_shader_s { 38 typedef struct asc_sprite_shader_s {
41 AscShaderProgram program; 39 AscShaderProgram program;
42 GLint tex; 40 asc_uniform_loc tex;
43 } AscSpriteShader; 41 } AscSpriteShader;
44 42
45 static AscShaderProgram *asc_sprite_shader_create(int rect) { 43 static AscShaderProgram *asc_sprite_shader_create(int rect) {
46 AscShaderCodes codes; 44 AscShaderCodes codes;
47 if (asc_shader_load_code_files((AscShaderCodeInfo){ 45 if (asc_shader_load_code_files((AscShaderCodeInfo){
56 AscSpriteShader *shader = asc_shader_create(codes, sizeof(*shader)); 54 AscSpriteShader *shader = asc_shader_create(codes, sizeof(*shader));
57 if (asc_shader_invalid(&shader->program)) { 55 if (asc_shader_invalid(&shader->program)) {
58 asc_shader_free_codes(codes); 56 asc_shader_free_codes(codes);
59 return (AscShaderProgram*) shader; 57 return (AscShaderProgram*) shader;
60 } 58 }
61 shader->tex = glGetUniformLocation(shader->program.gl_id, "tex"); 59 shader->tex = asc_shader_get_uniform_loc(&shader->program, "tex");
62 asc_shader_free_codes(codes); 60 asc_shader_free_codes(codes);
63 61
64 asc_error_catch_all_gl(); 62 asc_error_catch_all_gl();
65 63
66 return (AscShaderProgram*) shader; 64 return (AscShaderProgram*) shader;
95 asc_ptr_cast(const AscSprite, sprite, node); 93 asc_ptr_cast(const AscSprite, sprite, node);
96 94
97 // Activate shader 95 // Activate shader
98 // 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
99 const AscSpriteShader *shader = 97 const AscSpriteShader *shader =
100 sprite->texture->target == GL_TEXTURE_RECTANGLE 98 asc_texture_is_uv_normalized(sprite->texture)
101 ? asc_shader_lookup_or_create(ASC_SHADER_SPRITE_RECT, asc_sprite_shader_create, 1) 99 ? asc_shader_lookup_or_create(ASC_SHADER_SPRITE_UV, asc_sprite_shader_create, 0)
102 : 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);
103 // TODO: how to remove the following cast? 101 // TODO: how to remove the following cast?
104 if (asc_shader_invalid((AscShaderProgram*)shader)) return; 102 if (asc_shader_invalid((AscShaderProgram*)shader)) return;
105 asc_shader_use(&shader->program, camera); 103 asc_shader_use(&shader->program, camera);
106 104
107 // Upload model matrix 105 // Upload model matrix

mercurial