30 #include "ascension/context.h" |
30 #include "ascension/context.h" |
31 #include "ascension/glcontext.h" |
31 #include "ascension/glcontext.h" |
32 |
32 |
33 #include <GL/glew.h> |
33 #include <GL/glew.h> |
34 |
34 |
|
35 static void asc_sprite_free(AscSceneNode *node) { |
|
36 free(node); |
|
37 } |
|
38 |
|
39 AscSceneNode *asc_sprite_create(struct asc_sprite_create_args args) { |
|
40 AscSprite *sprite = calloc(1, sizeof(AscSprite)); |
|
41 |
|
42 // special sprite parameters |
|
43 sprite->tex = args.texture; |
|
44 |
|
45 // basic node parameters |
|
46 AscSceneNode *node = (AscSceneNode *) sprite; |
|
47 node->render_group = args.opaque |
|
48 ? ASC_RENDER_GROUP_SPRITE_OPAQUE |
|
49 : ASC_RENDER_GROUP_SPRITE_BLEND; |
|
50 node->free_func = asc_sprite_free; |
|
51 |
|
52 node->position.x = (float) args.x; |
|
53 node->position.y = (float) args.y; |
|
54 |
|
55 node->scale.x = (float) (args.width == 0 ? args.texture.width : args.width); |
|
56 node->scale.y = (float) (args.height == 0 ? args.texture.height : args.height); |
|
57 |
|
58 return node; |
|
59 } |
|
60 |
35 void asc_sprite_draw(AscSprite const *node) { |
61 void asc_sprite_draw(AscSprite const *node) { |
36 // Obtain shader |
62 // Obtain shader |
37 AscShaderSprite *shader = ASC_SHADER_SPRITE; |
63 AscShaderSprite *shader = &ASC_DEFAULT_SHADER.sprite; |
38 |
64 |
39 // Upload model matrix |
65 // Upload model matrix |
40 glUniformMatrix4fv(shader->program.model, 1, |
66 glUniformMatrix4fv(shader->program.model, 1, |
41 GL_FALSE, node->data.world_transform); |
67 GL_FALSE, node->data.world_transform); |
42 |
68 |
43 // Bind texture |
69 // Bind texture |
44 asc_texture_bind(&node->tex, shader->tex, 0); |
70 if (node->tex.target == GL_TEXTURE_RECTANGLE) { |
|
71 asc_texture_bind(&node->tex, shader->rect_tex, 0); |
|
72 asc_texture_bind(&ASC_DEFAULT_TEXTURES.empty_1x1_2d, shader->uv_tex, 1); |
|
73 } else { |
|
74 asc_texture_bind(&ASC_DEFAULT_TEXTURES.empty_1x1_rect, shader->rect_tex, 0); |
|
75 asc_texture_bind(&node->tex, shader->uv_tex, 1); |
|
76 } |
45 |
77 |
46 // Apply depth |
78 // Apply depth |
47 glUniform1f(shader->depth, (float)(node->data.depth)); |
79 glUniform1f(shader->depth, (float)(node->data.depth)); |
48 |
80 |
49 // Draw mesh |
81 // Draw mesh |
50 asc_mesh_draw_triangle_strip(ASC_PRIMITIVES_PLANE); |
82 asc_mesh_draw_triangle_strip(&ASC_PRIMITIVES.plane); |
51 } |
83 } |