1 | #include "sprite.h"
|
2 | #include "shaders.h"
|
3 |
|
4 | #include "../util/shader.h"
|
5 |
|
6 | #include "config.h"
|
7 |
|
8 | #ifdef USE_GLES
|
9 | #include <GLES2/gl2.h>
|
10 | #include <GLES2/gl2ext.h>
|
11 | #else
|
12 | #include <GL/glew.h>
|
13 | #endif
|
14 |
|
15 | #include <stddef.h>
|
16 |
|
17 | #ifndef USE_GLES
|
18 | static GLuint sliSpriteVAO = 0;
|
19 | #endif
|
20 | static GLuint sliSpriteVBOs[2] = {0, 0};
|
21 |
|
22 | void sliSpriteInit()
|
23 | {
|
24 | GLfloat vertices[] = {-0.5f, 0.5f,
|
25 | 0.5f, 0.5f,
|
26 | -0.5f, -0.5f,
|
27 | 0.5f, -0.5f};
|
28 |
|
29 | GLfloat texCoords[] = {0.0f, 1.0f,
|
30 | 1.0f, 1.0f,
|
31 | 0.0f, 0.0f,
|
32 | 1.0f, 0.0f};
|
33 |
|
34 | // initialize our state object
|
35 | #ifndef USE_GLES
|
36 | glGenVertexArrays(1, &sliSpriteVAO);
|
37 | glBindVertexArray(sliSpriteVAO);
|
38 | #endif
|
39 | glGenBuffers(2, sliSpriteVBOs);
|
40 |
|
41 | // configure vertex data
|
42 | glBindBuffer(GL_ARRAY_BUFFER, sliSpriteVBOs[0]);
|
43 | glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 8, vertices, GL_STATIC_DRAW);
|
44 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
|
45 | glEnableVertexAttribArray(0);
|
46 |
|
47 | // configure texture coord data
|
48 | glBindBuffer(GL_ARRAY_BUFFER, sliSpriteVBOs[1]);
|
49 | glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 8, texCoords, GL_STATIC_DRAW);
|
50 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
|
51 | glEnableVertexAttribArray(1);
|
52 | }
|
53 |
|
54 | void sliSpriteDestroy()
|
55 | {
|
56 | glDeleteBuffers(2, sliSpriteVBOs);
|
57 | #ifndef USE_GLES
|
58 | glDeleteVertexArrays(1, &sliSpriteVAO);
|
59 | #endif
|
60 | }
|
61 |
|
62 | void sliSprite(Mat4 *modelview, Vec4 *color, GLuint texture, Vec2 *tiling, Vec2 *scroll)
|
63 | {
|
64 | // prepare our shader object
|
65 | shaderBind(sliTextureShader);
|
66 | shaderUniformMatrix4fv(sliTextureShader, "u_Modelview" , 1, (float*)modelview);
|
67 | shaderUniform4f(sliTextureShader, "u_Color" , (float)color -> x, (float)color -> y, (float)color -> z, (float)color -> w);
|
68 | shaderUniform2f(sliTextureShader, "u_Tiling" , (float)tiling -> x, (float)tiling -> y);
|
69 | shaderUniform2f(sliTextureShader, "u_Scroll" , (float)scroll -> x, (float)scroll -> y);
|
70 |
|
71 | // bind appropriate object state and render the object
|
72 | #ifndef USE_GLES
|
73 | glBindVertexArray(sliSpriteVAO);
|
74 | #else
|
75 | glBindBuffer(GL_ARRAY_BUFFER, sliSpriteVBOs[0]);
|
76 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
|
77 | glBindBuffer(GL_ARRAY_BUFFER, sliSpriteVBOs[1]);
|
78 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
|
79 | #endif
|
80 | glBindTexture(GL_TEXTURE_2D, texture);
|
81 | glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
82 | }
|
83 | |