[2127] | 1 | #include <stdio.h> |
---|
| 2 | #include <stdlib.h> |
---|
| 3 | |
---|
| 4 | #include "../include/GL/glew.h" |
---|
| 5 | |
---|
| 6 | #include "../include/buffers.h" |
---|
| 7 | #include "../include/global.h" |
---|
| 8 | |
---|
[2194] | 9 | using namespace VMI; |
---|
[2127] | 10 | |
---|
| 11 | |
---|
| 12 | |
---|
[2194] | 13 | VertexIL *VMI::saveVertexBuffer(Change *c, Vertex_ *verts, Color *cols) { |
---|
| 14 | int i, j, t, n = 0, m; |
---|
| 15 | VertexIL *dest; |
---|
[2127] | 16 | |
---|
[2194] | 17 | dest = (VertexIL *)malloc((c->numDel + c->numMod + 1) * 3 * sizeof(VertexIL)); |
---|
| 18 | |
---|
[2127] | 19 | // Save vertex coordinates and color for deleted triangles |
---|
| 20 | for (j=0; j<c->numDel; j++) { |
---|
| 21 | |
---|
| 22 | t = c->deleted[j].id; |
---|
| 23 | |
---|
| 24 | m = 3 * t; |
---|
| 25 | |
---|
[2194] | 26 | for (i=0; i<3; i++) { |
---|
| 27 | dest[n].x = verts[m + i].x; |
---|
| 28 | dest[n].y = verts[m + i].y; |
---|
| 29 | dest[n].z = verts[m + i].z; |
---|
| 30 | dest[n].a = cols[m + i].a; |
---|
| 31 | dest[n].b = cols[m + i].b; |
---|
| 32 | dest[n].r = cols[m + i].r; |
---|
| 33 | dest[n].g = cols[m + i].g; |
---|
| 34 | n++; |
---|
| 35 | } |
---|
[2127] | 36 | } |
---|
| 37 | // Save vertex coordinates for modified triangles |
---|
| 38 | for (j=0; j<c->numMod; j++) { |
---|
| 39 | |
---|
| 40 | t = c->modified[j].id; |
---|
| 41 | |
---|
| 42 | m = 3 * t; |
---|
| 43 | |
---|
[2194] | 44 | for (i=0; i<3; i++) { |
---|
| 45 | dest[n].x = verts[m + i].x; |
---|
| 46 | dest[n].y = verts[m + i].y; |
---|
| 47 | dest[n].z = verts[m + i].z; |
---|
| 48 | n++; |
---|
| 49 | } |
---|
[2127] | 50 | } |
---|
[2194] | 51 | |
---|
| 52 | return dest; |
---|
[2127] | 53 | } |
---|
| 54 | |
---|
[2194] | 55 | VertexIL *VMI::saveVertexBufferGPU(Change *c) { |
---|
| 56 | Vertex_ *verts; |
---|
| 57 | Color *cols; |
---|
| 58 | VertexIL *dest; |
---|
[2127] | 59 | |
---|
[2194] | 60 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertex_buf); |
---|
| 61 | // Map the buffer object |
---|
| 62 | verts = (Vertex_ *)glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_READ_ONLY); |
---|
| 63 | |
---|
| 64 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, color_buf); |
---|
| 65 | // Map the buffer object |
---|
| 66 | cols = (Color *)glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_READ_ONLY); |
---|
| 67 | |
---|
| 68 | // Update GPU buffers |
---|
| 69 | dest = saveVertexBuffer(c, verts, cols); |
---|
| 70 | |
---|
| 71 | // Unmap buffers |
---|
| 72 | glBindBuffer(GL_ARRAY_BUFFER_ARB, vertex_buf); |
---|
| 73 | if (!glUnmapBufferARB(GL_ARRAY_BUFFER_ARB)) { |
---|
| 74 | // Handle error case |
---|
| 75 | } |
---|
| 76 | glBindBuffer(GL_ARRAY_BUFFER_ARB, color_buf); |
---|
| 77 | if (!glUnmapBufferARB(GL_ARRAY_BUFFER_ARB)) { |
---|
| 78 | // Handle error case |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | return dest; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | void VMI::loadVertexBuffer(VertexIL *src, Change *c, Vertex_ *verts, Color *cols) { |
---|
| 85 | int i, j, t, n = 0, m; |
---|
| 86 | |
---|
[2127] | 87 | // Load vertex coordinates and color for deleted triangles |
---|
| 88 | for (j=0; j<c->numDel; j++) { |
---|
| 89 | |
---|
| 90 | t = c->deleted[j].id; |
---|
| 91 | |
---|
| 92 | m = 3 * t; |
---|
[2194] | 93 | |
---|
| 94 | for (i=0; i<3; i++) { |
---|
| 95 | verts[m + i].x = src[n].x; |
---|
| 96 | verts[m + i].y = src[n].y; |
---|
| 97 | verts[m + i].z = src[n].z; |
---|
| 98 | cols[m + i].a = src[n].a; |
---|
| 99 | cols[m + i].b = src[n].b; |
---|
| 100 | cols[m + i].r = src[n].r; |
---|
| 101 | cols[m + i].g = src[n].g; |
---|
| 102 | n++; |
---|
| 103 | } |
---|
[2127] | 104 | } |
---|
| 105 | // Load vertex coordinates for modified triangles |
---|
| 106 | for (j=0; j<c->numMod; j++) { |
---|
| 107 | |
---|
| 108 | t = c->modified[j].id; |
---|
| 109 | |
---|
| 110 | m = 3 * t; |
---|
[2194] | 111 | |
---|
| 112 | for (i=0; i<3; i++) { |
---|
| 113 | verts[m + i].x = src[n].x; |
---|
| 114 | verts[m + i].y = src[n].y; |
---|
| 115 | verts[m + i].z = src[n].z; |
---|
| 116 | n++; |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | } |
---|
[2127] | 120 | |
---|
[2194] | 121 | void VMI::loadVertexBufferGPU(VertexIL *src, Change *c) { |
---|
| 122 | Vertex_ *verts; |
---|
| 123 | Color *cols; |
---|
| 124 | |
---|
| 125 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertex_buf); |
---|
| 126 | // Map the buffer object |
---|
| 127 | verts = (Vertex_ *)glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY); |
---|
| 128 | |
---|
| 129 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, color_buf); |
---|
| 130 | // Map the buffer object |
---|
| 131 | cols = (Color *)glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY); |
---|
| 132 | |
---|
| 133 | // Update GPU buffers |
---|
| 134 | loadVertexBuffer(src, c, verts, cols); |
---|
| 135 | |
---|
| 136 | // Unmap buffers |
---|
| 137 | glBindBuffer(GL_ARRAY_BUFFER_ARB, vertex_buf); |
---|
| 138 | if (!glUnmapBufferARB(GL_ARRAY_BUFFER_ARB)) { |
---|
| 139 | // Handle error case |
---|
[2127] | 140 | } |
---|
[2194] | 141 | glBindBuffer(GL_ARRAY_BUFFER_ARB, color_buf); |
---|
| 142 | if (!glUnmapBufferARB(GL_ARRAY_BUFFER_ARB)) { |
---|
| 143 | // Handle error case |
---|
| 144 | } |
---|
[2127] | 145 | } |
---|
| 146 | |
---|
| 147 | void VMI::setupVertexArray(Mesh *mesh, Color *colors) |
---|
| 148 | { |
---|
[2194] | 149 | int i, j, v, n = 0; |
---|
[2127] | 150 | GLubyte r, g, b, a; |
---|
| 151 | |
---|
| 152 | if (buf_vertices != NULL) free(buf_vertices); |
---|
| 153 | if (buf_colors != NULL) free(buf_colors); |
---|
| 154 | |
---|
| 155 | buf_vertices = (Vertex_ *)malloc(mesh->numTriangles * 3 * sizeof(Vertex_)); |
---|
| 156 | buf_colors = (Color *)malloc(mesh->numTriangles * 3 * sizeof(Color)); |
---|
| 157 | |
---|
| 158 | if (buf_vertices == NULL) { |
---|
| 159 | fprintf(stderr, "Error allocating memory\n"); |
---|
| 160 | exit(1); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | if (buf_colors == NULL) { |
---|
| 164 | fprintf(stderr, "Error allocating memory\n"); |
---|
| 165 | exit(1); |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | for (i=0; i<mesh->numTriangles; i++) { |
---|
[2194] | 169 | if (mesh->triangles[i].enable == TRUE) { |
---|
[2127] | 170 | |
---|
[2194] | 171 | r = colors[i].r; |
---|
| 172 | g = colors[i].g; |
---|
| 173 | b = colors[i].b; |
---|
| 174 | a = colors[i].a; |
---|
| 175 | |
---|
| 176 | for (j=0; j<3; j++) { |
---|
| 177 | |
---|
| 178 | v = mesh->triangles[i].indices[j]; |
---|
| 179 | |
---|
| 180 | buf_colors[n].r = r; |
---|
| 181 | buf_colors[n].g = g; |
---|
| 182 | buf_colors[n].b = b; |
---|
| 183 | buf_colors[n].a = a; |
---|
| 184 | buf_vertices[n].x = mesh->vertices[v].x; |
---|
| 185 | buf_vertices[n].y = mesh->vertices[v].y; |
---|
| 186 | buf_vertices[n].z = mesh->vertices[v].z; |
---|
| 187 | n++; |
---|
| 188 | } |
---|
| 189 | } else { |
---|
| 190 | for (j=0; j<3; j++) { |
---|
| 191 | |
---|
| 192 | buf_colors[n].r = 0; |
---|
| 193 | buf_colors[n].g = 0; |
---|
| 194 | buf_colors[n].b = 0; |
---|
| 195 | buf_colors[n].a = 0; |
---|
| 196 | buf_vertices[n].x = 0; |
---|
| 197 | buf_vertices[n].y = 0; |
---|
| 198 | buf_vertices[n].z = 0; |
---|
| 199 | n++; |
---|
| 200 | } |
---|
| 201 | } |
---|
[2127] | 202 | } |
---|
| 203 | #ifdef VERTEX_ARRAY |
---|
| 204 | // Enable the buf_vertices vector |
---|
| 205 | glEnableClientState(GL_COLOR_ARRAY); |
---|
| 206 | glEnableClientState(GL_VERTEX_ARRAY); |
---|
| 207 | |
---|
| 208 | glColorPointer(4, GL_UNSIGNED_BYTE, 0, buf_colors); |
---|
| 209 | glVertexPointer(3, GL_FLOAT, 0, buf_vertices); |
---|
| 210 | #endif |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | void VMI::setupVertexBufferObjects(Mesh *mesh, Color *colors) { |
---|
| 214 | |
---|
| 215 | setupVertexArray(mesh, colors); |
---|
| 216 | |
---|
| 217 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertex_buf); |
---|
[2194] | 218 | glBufferDataARB(GL_ARRAY_BUFFER_ARB, mesh->numTriangles * 3 * sizeof(Vertex_), buf_vertices, GL_STATIC_DRAW_ARB); //upload data |
---|
[2127] | 219 | |
---|
| 220 | glVertexPointer(3, GL_FLOAT, 0,0); |
---|
| 221 | |
---|
| 222 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, color_buf); |
---|
| 223 | glBufferDataARB(GL_ARRAY_BUFFER_ARB, mesh->numTriangles * 3 * sizeof(Color), buf_colors, GL_STATIC_DRAW_ARB); //upload data |
---|
| 224 | |
---|
| 225 | glColorPointer(4, GL_UNSIGNED_BYTE, 0, 0); |
---|
| 226 | |
---|
| 227 | // Enable the vertex array functionality: |
---|
| 228 | glEnableClientState(GL_VERTEX_ARRAY); |
---|
| 229 | glEnableClientState(GL_COLOR_ARRAY); |
---|
[2194] | 230 | |
---|
| 231 | free(buf_vertices); |
---|
| 232 | buf_vertices = NULL; |
---|
| 233 | |
---|
| 234 | free(buf_colors); |
---|
| 235 | buf_colors =NULL; |
---|
[2127] | 236 | } |
---|
| 237 | |
---|
[2194] | 238 | void VMI::updateVertexArray(Mesh *mesh, Change *c, Vertex_ *verts, Color * cols) |
---|
[2127] | 239 | { |
---|
[2194] | 240 | int i, j, v, t, n; |
---|
[2127] | 241 | |
---|
| 242 | // The deleted triangles are now in background color |
---|
| 243 | for (i=0; i<c->numDel; i++) { |
---|
| 244 | t = c->deleted[i].id; |
---|
[2194] | 245 | |
---|
[2127] | 246 | n = t * 3; |
---|
[2194] | 247 | |
---|
| 248 | for (j=0; j<3; j++) { |
---|
| 249 | cols[n + j].r = 0; |
---|
| 250 | cols[n + j].g = 0; |
---|
| 251 | cols[n + j].b = 0; |
---|
| 252 | cols[n + j].a = 0; |
---|
| 253 | |
---|
| 254 | verts[n + j].x = 0; |
---|
| 255 | verts[n + j].y = 0; |
---|
| 256 | verts[n + j].z = 0; |
---|
| 257 | } |
---|
[2127] | 258 | } |
---|
| 259 | |
---|
| 260 | // Update vertex coordinates for modified triangles |
---|
| 261 | for (i=0; i<c->numMod; i++) { |
---|
| 262 | t = c->modified[i].id; |
---|
| 263 | |
---|
| 264 | n = t * 3; |
---|
| 265 | |
---|
[2194] | 266 | for (j=0; j<3; j++) { |
---|
| 267 | |
---|
| 268 | v= mesh->triangles[t].indices[j]; |
---|
| 269 | |
---|
| 270 | verts[n + j].x = mesh->vertices[v].x; |
---|
| 271 | verts[n + j].y = mesh->vertices[v].y; |
---|
| 272 | verts[n + j].z = mesh->vertices[v].z; |
---|
| 273 | } |
---|
[2127] | 274 | } |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | void VMI::updateVertexBufferObjects(Mesh *mesh, Change *c) { |
---|
[2194] | 278 | Vertex_ *verts; |
---|
| 279 | Color *cols; |
---|
[2127] | 280 | |
---|
| 281 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertex_buf); |
---|
| 282 | // Map the buffer object |
---|
[2194] | 283 | verts = (Vertex_ *)glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY); |
---|
[2127] | 284 | |
---|
| 285 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, color_buf); |
---|
| 286 | // Map the buffer object |
---|
[2194] | 287 | cols = (Color *)glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY); |
---|
[2127] | 288 | |
---|
[2194] | 289 | // Update GPU buffers |
---|
| 290 | updateVertexArray(mesh, c, verts, cols); |
---|
[2127] | 291 | |
---|
| 292 | // Unmap buffers |
---|
| 293 | glBindBuffer(GL_ARRAY_BUFFER_ARB, vertex_buf); |
---|
| 294 | if (!glUnmapBufferARB(GL_ARRAY_BUFFER_ARB)) { |
---|
| 295 | // Handle error case |
---|
| 296 | } |
---|
| 297 | glBindBuffer(GL_ARRAY_BUFFER_ARB, color_buf); |
---|
| 298 | if (!glUnmapBufferARB(GL_ARRAY_BUFFER_ARB)) { |
---|
| 299 | // Handle error case |
---|
| 300 | } |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | VertexIL *VMI::setupInterleave(Mesh *mesh, Color *colors) |
---|
| 304 | { |
---|
[2194] | 305 | int i, j, v, n = 0; |
---|
[2127] | 306 | GLubyte r, g, b, a; |
---|
| 307 | VertexIL *interleave = NULL; |
---|
| 308 | |
---|
| 309 | interleave = (VertexIL *)malloc(mesh->currentNumTriangles * 3 * sizeof(VertexIL)); |
---|
| 310 | |
---|
| 311 | if (interleave == NULL) { |
---|
| 312 | fprintf(stderr, "Error allocating memory\n"); |
---|
| 313 | exit(1); |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | for (i=0; i<mesh->numTriangles; i++) { |
---|
| 317 | |
---|
| 318 | if (mesh->triangles[i].enable == TRUE) { |
---|
[2194] | 319 | |
---|
| 320 | r = colors[i].r; |
---|
[2127] | 321 | g = colors[i].g; |
---|
| 322 | b = colors[i].b; |
---|
| 323 | a = colors[i].a; |
---|
[2194] | 324 | |
---|
| 325 | for (j=0; j<3; j++) { |
---|
| 326 | |
---|
| 327 | v= mesh->triangles[i].indices[j]; |
---|
| 328 | |
---|
| 329 | interleave[n].tx = 0.0f; |
---|
| 330 | interleave[n].ty = 0.0f; |
---|
| 331 | interleave[n].r = r; |
---|
| 332 | interleave[n].g = g; |
---|
| 333 | interleave[n].b = b; |
---|
| 334 | interleave[n].a = a; |
---|
| 335 | interleave[n].x = mesh->vertices[v].x; |
---|
| 336 | interleave[n].y = mesh->vertices[v].y; |
---|
| 337 | interleave[n].z = mesh->vertices[v].z; |
---|
| 338 | n++; |
---|
| 339 | } |
---|
[2127] | 340 | } |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | return interleave; |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | void VMI::printInterleave(VertexIL *interleave, int num) |
---|
| 347 | { |
---|
| 348 | int i; |
---|
| 349 | |
---|
| 350 | printf("\n"); |
---|
| 351 | for (i=0; i<num * 3; i++) { |
---|
| 352 | printf("Interleave%d (%d, %d, %d ,%d, %f, %f ,%f)\n", i, |
---|
| 353 | interleave[i].r, interleave[i].g, interleave[i].b, interleave[i].a, |
---|
| 354 | interleave[i].x , interleave[i].y, interleave[i].z); |
---|
| 355 | } |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | void VMI::updateAllColorInterleave(VertexIL *interleave, int num, Color *colors) |
---|
| 359 | { |
---|
[2194] | 360 | int i, j, n = 0; |
---|
[2127] | 361 | GLubyte r, g, b, a; |
---|
[2194] | 362 | |
---|
[2127] | 363 | for (i=1; i<=num; i++) { // Triangles start at 1 |
---|
[2194] | 364 | |
---|
[2127] | 365 | r = colors[i].r; |
---|
| 366 | g = colors[i].g; |
---|
| 367 | b = colors[i].b; |
---|
| 368 | a = colors[i].a; |
---|
[2194] | 369 | |
---|
| 370 | for (j=0; j<3; j++) { |
---|
| 371 | interleave[n].r = r; |
---|
| 372 | interleave[n].g = g; |
---|
| 373 | interleave[n].b = b; |
---|
| 374 | interleave[n].a = a; |
---|
| 375 | n++; |
---|
| 376 | } |
---|
[2127] | 377 | } |
---|
| 378 | } |
---|
| 379 | |
---|
| 380 | void VMI::updateColorInterleave(VertexIL *interleave, int num, Color *colors, int begin, int end) |
---|
| 381 | { |
---|
| 382 | int i, n; |
---|
| 383 | GLubyte r, g, b, a; |
---|
| 384 | |
---|
| 385 | if (end > num) return; |
---|
| 386 | |
---|
| 387 | for (i=begin; i<end; i++) { |
---|
| 388 | |
---|
| 389 | n = i * 3; |
---|
| 390 | |
---|
| 391 | r = colors[i].r; |
---|
| 392 | g = colors[i].g; |
---|
| 393 | b = colors[i].b; |
---|
| 394 | a = colors[i].a; |
---|
| 395 | |
---|
| 396 | interleave[n].r = r; |
---|
| 397 | interleave[n].g = g; |
---|
| 398 | interleave[n].b = b; |
---|
| 399 | interleave[n].a = a; |
---|
| 400 | |
---|
| 401 | n++; |
---|
| 402 | interleave[n].r = r; |
---|
| 403 | interleave[n].g = g; |
---|
| 404 | interleave[n].b = b; |
---|
| 405 | interleave[n].a = a; |
---|
| 406 | |
---|
| 407 | n++; |
---|
| 408 | interleave[n].r = r; |
---|
| 409 | interleave[n].g = g; |
---|
| 410 | interleave[n].b = b; |
---|
| 411 | interleave[n].a = a; |
---|
| 412 | } |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | void VMI::fillColorInterleave(VertexIL *interleave, int num, int begin, int end, GLubyte color) |
---|
| 416 | { |
---|
| 417 | int i, n; |
---|
| 418 | |
---|
| 419 | if (end > num) return; |
---|
| 420 | |
---|
| 421 | for (i=begin; i<end; i++) { |
---|
| 422 | |
---|
| 423 | n = i * 3; |
---|
| 424 | interleave[n].r = color; |
---|
| 425 | interleave[n].g = color; |
---|
| 426 | interleave[n].b = color; |
---|
| 427 | interleave[n].a = color; |
---|
| 428 | |
---|
| 429 | n++; |
---|
| 430 | interleave[n].r = color; |
---|
| 431 | interleave[n].g = color; |
---|
| 432 | interleave[n].b = color; |
---|
| 433 | interleave[n].a = color; |
---|
| 434 | |
---|
| 435 | n++; |
---|
| 436 | interleave[n].r = color; |
---|
| 437 | interleave[n].g = color; |
---|
| 438 | interleave[n].b = color; |
---|
| 439 | interleave[n].a = color; |
---|
| 440 | } |
---|
| 441 | } |
---|