1 | #include "point.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 <stdlib.h>
|
16 |
|
17 | #define MAX_POINT_GROUP_SIZE 1024
|
18 |
|
19 | #ifndef USE_GLES
|
20 | static GLuint sliPointVAO = 0;
|
21 | #endif
|
22 | static GLuint sliPointVBOs[1] = {0};
|
23 |
|
24 | static GLfloat *sliPointAttributes;
|
25 |
|
26 | static int sliPointCount;
|
27 | static GLfloat *sliPointAttributesPtr;
|
28 |
|
29 | void sliPointInit()
|
30 | {
|
31 | // initialize our state object
|
32 | #ifndef USE_GLES
|
33 | glGenVertexArrays(1, &sliPointVAO);
|
34 | glBindVertexArray(sliPointVAO);
|
35 | #endif
|
36 | glGenBuffers(1, sliPointVBOs);
|
37 |
|
38 | // configure vertex attribute data
|
39 | glBindBuffer(GL_ARRAY_BUFFER, sliPointVBOs[0]);
|
40 | glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * MAX_POINT_GROUP_SIZE * 6, NULL, GL_DYNAMIC_DRAW);
|
41 |
|
42 | // configure vertex position attribute
|
43 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 6, NULL);
|
44 | glEnableVertexAttribArray(0);
|
45 |
|
46 | // configure vertex color attribute
|
47 | glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 6, (GLvoid*)(sizeof(GLfloat) * 2));
|
48 | glEnableVertexAttribArray(1);
|
49 |
|
50 | // initialize our vertex data buffer
|
51 | sliPointAttributes = (GLfloat*)malloc(sizeof(GLfloat) * MAX_POINT_GROUP_SIZE * 6);
|
52 | sliPointAttributesPtr = sliPointAttributes;
|
53 |
|
54 | // no points buffered yet
|
55 | sliPointCount = 0;
|
56 | }
|
57 |
|
58 | void sliPointDestroy()
|
59 | {
|
60 | // free up dynamically-allocated vertex and color data
|
61 | free(sliPointAttributes);
|
62 |
|
63 | // free OpenGL objects
|
64 | glDeleteBuffers(1, sliPointVBOs);
|
65 | #ifndef USE_GLES
|
66 | glDeleteVertexArrays(1, &sliPointVAO);
|
67 | #endif
|
68 | }
|
69 |
|
70 | void sliPoint(Mat4 *modelview, Vec4 *color)
|
71 | {
|
72 | // assign vertex position for the incoming point
|
73 | *(sliPointAttributesPtr++) = (float)modelview -> cols[3].x;
|
74 | *(sliPointAttributesPtr++) = (float)modelview -> cols[3].y;
|
75 |
|
76 | // assign color values for the incoming point
|
77 | *(sliPointAttributesPtr++) = (float)color -> x;
|
78 | *(sliPointAttributesPtr++) = (float)color -> y;
|
79 | *(sliPointAttributesPtr++) = (float)color -> z;
|
80 | *(sliPointAttributesPtr++) = (float)color -> w;
|
81 |
|
82 | // track the number of points we've cached; render if the buffer is full
|
83 | sliPointCount ++;
|
84 | if(sliPointCount >= MAX_POINT_GROUP_SIZE)
|
85 | {
|
86 | sliPointsFlush();
|
87 | }
|
88 | }
|
89 |
|
90 | void sliPointsFlush()
|
91 | {
|
92 | // nothing to do if no points are waiting to be drawn
|
93 | if(sliPointCount > 0)
|
94 | {
|
95 | // activate our state object
|
96 | #ifndef USE_GLES
|
97 | glBindVertexArray(sliPointVAO);
|
98 | #endif
|
99 |
|
100 | // send vertex positions to graphics card
|
101 | glBindBuffer(GL_ARRAY_BUFFER, sliPointVBOs[0]);
|
102 | glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLfloat) * sliPointCount * 6, sliPointAttributes);
|
103 | #ifdef USE_GLES
|
104 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 6, NULL);
|
105 | glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 6, (GLvoid*)(sizeof(GLfloat) * 2));
|
106 | #endif
|
107 |
|
108 | // prepare our shader object
|
109 | shaderBind(sliPointShader);
|
110 |
|
111 | // render the points
|
112 | #ifndef USE_GLES
|
113 | glPointSize(1.0);
|
114 | #endif
|
115 | glDrawArrays(GL_POINTS, 0, sliPointCount);
|
116 |
|
117 | // reset the points buffer
|
118 | sliPointAttributesPtr = sliPointAttributes;
|
119 | sliPointCount = 0;
|
120 | }
|
121 | }
|
122 | |