1 | #include "rectangle.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 sliRectangleFillVAO = 0;
|
19 | #endif
|
20 | static GLuint sliRectangleFillVBOs[1] = {0};
|
21 |
|
22 | #ifndef USE_GLES
|
23 | static GLuint sliRectangleOutlineVAO = 0;
|
24 | #endif
|
25 | static GLuint sliRectangleOutlineVBOs[1] = {0};
|
26 |
|
27 | void sliRectangleInit()
|
28 | {
|
29 | GLfloat outlineVertices[] = {-0.5f, 0.5f,
|
30 | -0.5f, -0.5f,
|
31 | 0.5f, -0.5f,
|
32 | 0.5f, 0.5f};
|
33 |
|
34 | GLfloat fillVertices[] = {-0.5f, -0.5f,
|
35 | -0.5f, 0.5f,
|
36 | 0.5f, -0.5f,
|
37 | 0.5f, 0.5f};
|
38 |
|
39 | // intialize our outline state object
|
40 | #ifndef USE_GLES
|
41 | glGenVertexArrays(1, &sliRectangleOutlineVAO);
|
42 | glBindVertexArray(sliRectangleOutlineVAO);
|
43 | #endif
|
44 | glGenBuffers(1, sliRectangleOutlineVBOs);
|
45 |
|
46 | // configure outline vertex data
|
47 | glBindBuffer(GL_ARRAY_BUFFER, sliRectangleOutlineVBOs[0]);
|
48 | glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 8, outlineVertices, GL_STATIC_DRAW);
|
49 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
|
50 | glEnableVertexAttribArray(0);
|
51 |
|
52 | // initialize our fill state object
|
53 | #ifndef USE_GLES
|
54 | glGenVertexArrays(1, &sliRectangleFillVAO);
|
55 | glBindVertexArray(sliRectangleFillVAO);
|
56 | #endif
|
57 | glGenBuffers(1, sliRectangleFillVBOs);
|
58 |
|
59 | // configure fill vertex data
|
60 | glBindBuffer(GL_ARRAY_BUFFER, sliRectangleFillVBOs[0]);
|
61 | glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 8, fillVertices, GL_STATIC_DRAW);
|
62 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
|
63 | glEnableVertexAttribArray(0);
|
64 | }
|
65 |
|
66 | void sliRectangleDestroy()
|
67 | {
|
68 | glDeleteBuffers(1, sliRectangleOutlineVBOs);
|
69 | #ifndef USE_GLES
|
70 | glDeleteVertexArrays(1, &sliRectangleOutlineVAO);
|
71 | #endif
|
72 |
|
73 | glDeleteBuffers(1, sliRectangleFillVBOs);
|
74 | #ifndef USE_GLES
|
75 | glDeleteVertexArrays(1, &sliRectangleFillVAO);
|
76 | #endif
|
77 | }
|
78 |
|
79 | void sliRectangleOutline(Mat4 *modelview, Vec4 *color)
|
80 | {
|
81 | // prepare our shader object
|
82 | shaderBind(sliBasicShader);
|
83 | shaderUniformMatrix4fv(sliBasicShader, "u_Modelview" , 1, (float*)modelview);
|
84 | shaderUniform4f(sliBasicShader, "u_Color" , color -> x, color -> y, color -> z, color -> w);
|
85 |
|
86 | // bind appropriate object state and render the object
|
87 | #ifndef USE_GLES
|
88 | glBindVertexArray(sliRectangleOutlineVAO);
|
89 | #endif
|
90 | glBindBuffer(GL_ARRAY_BUFFER, sliRectangleOutlineVBOs[0]);
|
91 | #ifdef USE_GLES
|
92 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
|
93 | #endif
|
94 | glDrawArrays(GL_LINE_LOOP, 0, 4);
|
95 | }
|
96 |
|
97 | void sliRectangleFill(Mat4 *modelview, Vec4 *color)
|
98 | {
|
99 | // prepare our shader object
|
100 | shaderBind(sliBasicShader);
|
101 | shaderUniformMatrix4fv(sliBasicShader, "u_Modelview" , 1, (float*)modelview);
|
102 | shaderUniform4f(sliBasicShader, "u_Color" , color -> x, color -> y, color -> z, color -> w);
|
103 |
|
104 | // bind appropriate object state and render the object
|
105 | #ifndef USE_GLES
|
106 | glBindVertexArray(sliRectangleFillVAO);
|
107 | #endif
|
108 | glBindBuffer(GL_ARRAY_BUFFER, sliRectangleFillVBOs[0]);
|
109 | #ifdef USE_GLES
|
110 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
|
111 | #endif
|
112 | glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
113 | }
|
114 | |