git.haldean.org ana / be425b7
pack all node data into a single buffer Haldean Brown 1 year, 9 months ago
1 changed file(s) with 23 addition(s) and 48 deletion(s). Raw diff Collapse all Expand all
1010
1111 static const std::string node_vert_src = R"(
1212 #version 330 core
13 layout (location = 0) in vec2 pos;
14 layout (location = 1) in vec2 size;
13 layout (location = 0) in vec4 data;
1514 out NODE_DATA {
1615 vec2 size;
1716 } node_data;
1817
1918 void main(void) {
20 gl_Position = vec4(pos, 0.0, 1.0);
21 node_data.size = size;
19 gl_Position = vec4(data[0], data[1], 0.0, 1.0);
20 node_data.size = vec2(data[2], data[3]);
2221 }
2322 )";
2423
5655 )";
5756
5857 static GLuint node_program_id = 0;
59 static GLint node_program_pos = -1;
60 static GLint node_program_size = -1;
61 static GLuint node_loc_buf = 0;
62 static GLuint node_size_buf = 0;
58 static GLint node_data_attr = -1;
59 static GLuint node_data_buf = 0;
6360
6461 bool init(void) {
6562 GLuint vs, fs, gs;
9289 return false;
9390 }
9491
95 node_program_pos = glGetAttribLocation(node_program_id, "pos");
96 if (node_program_pos == -1) {
97 std::cerr << "[E] glGetAttribLocation failed for pos" << std::endl;
98 return false;
99 }
100 node_program_size = glGetAttribLocation(node_program_id, "size");
101 if (node_program_pos == -1) {
102 std::cerr << "[E] glGetAttribLocation failed for pos" << std::endl;
92 node_data_attr = glGetAttribLocation(node_program_id, "data");
93 if (node_data_attr == -1) {
94 std::cerr << "[E] glGetAttribLocation failed for node data" << std::endl;
10395 return false;
10496 }
10597
109101 glBindVertexArray(vao);
110102 INSERT_GL_ERROR_CHECK;
111103
112 GLuint bufs[2];
113 glGenBuffers(2, bufs);
104 glGenBuffers(1, &node_data_buf);
114105 INSERT_GL_ERROR_CHECK;
115 node_loc_buf = bufs[0];
116 node_size_buf = bufs[1];
117106
118107 return true;
119108 }
120109
121 GLuint node_program(void) {
122 return node_program_id;
123 }
124
125110 void draw(const ana::nodeset &nodes) {
126 size_t elems = nodes.size() * 2;
127 GLfloat *pos = (GLfloat*) calloc(sizeof(GLfloat), elems);
128 GLfloat *size = (GLfloat*) calloc(sizeof(GLfloat), elems);
111 size_t elems = nodes.size() * 4;
112 GLfloat *data = (GLfloat*) calloc(sizeof(GLfloat), elems);
129113 int i = 0;
130114 for (const ana::node &n : nodes.nodes) {
131 pos[2 * i + 0] = n.loc.x;
132 pos[2 * i + 1] = n.loc.y;
133 size[2 * i + 0] = n.size.x;
134 size[2 * i + 1] = n.size.y;
115 data[4 * i + 0] = n.loc.x;
116 data[4 * i + 1] = n.loc.y;
117 data[4 * i + 2] = n.size.x;
118 data[4 * i + 3] = n.size.y;
135119 i++;
136120 }
137121
138 glBindBuffer(GL_ARRAY_BUFFER, node_loc_buf);
122 glBindBuffer(GL_ARRAY_BUFFER, node_data_buf);
139123 INSERT_GL_ERROR_CHECK;
140 glBufferData(GL_ARRAY_BUFFER, elems * sizeof(GLfloat), pos, GL_DYNAMIC_DRAW);
141 INSERT_GL_ERROR_CHECK;
142 glBindBuffer(GL_ARRAY_BUFFER, node_size_buf);
143 INSERT_GL_ERROR_CHECK;
144 glBufferData(GL_ARRAY_BUFFER, elems * sizeof(GLfloat), size, GL_DYNAMIC_DRAW);
124 glBufferData(GL_ARRAY_BUFFER, elems * sizeof(GLfloat), data, GL_DYNAMIC_DRAW);
145125 INSERT_GL_ERROR_CHECK;
146126
147 free(pos);
148 free(size);
127 free(data);
149128
150129 glUseProgram(node_program_id); INSERT_GL_ERROR_CHECK;
151 glEnableVertexAttribArray(node_program_pos); INSERT_GL_ERROR_CHECK;
152 glEnableVertexAttribArray(node_program_size); INSERT_GL_ERROR_CHECK;
153 glBindBuffer(GL_ARRAY_BUFFER, node_loc_buf); INSERT_GL_ERROR_CHECK;
154 glVertexAttribPointer(node_program_pos, 2, GL_FLOAT, GL_FALSE, 0, 0); INSERT_GL_ERROR_CHECK;
155 glBindBuffer(GL_ARRAY_BUFFER, node_size_buf); INSERT_GL_ERROR_CHECK;
156 glVertexAttribPointer(node_program_size, 2, GL_FLOAT, GL_FALSE, 0, 0); INSERT_GL_ERROR_CHECK;
130 glEnableVertexAttribArray(node_data_attr); INSERT_GL_ERROR_CHECK;
131 glBindBuffer(GL_ARRAY_BUFFER, node_data_buf); INSERT_GL_ERROR_CHECK;
132 glVertexAttribPointer(node_data_attr, 4, GL_FLOAT, GL_FALSE, 0, 0); INSERT_GL_ERROR_CHECK;
157133 glDrawArrays(GL_POINTS, 0, nodes.size()); INSERT_GL_ERROR_CHECK;
158 glDisableVertexAttribArray(node_program_pos); INSERT_GL_ERROR_CHECK;
159 glDisableVertexAttribArray(node_program_size); INSERT_GL_ERROR_CHECK;
134 glDisableVertexAttribArray(node_data_attr); INSERT_GL_ERROR_CHECK;
160135 }
161136
162137 void destroy(void) {
163138 glDeleteProgram(node_program_id);
164 glDeleteBuffers(1, &node_loc_buf);
139 glDeleteBuffers(1, &node_data_buf);
165140 }
166141
167142 }