- Timestamp:
- 03/06/07 14:26:14 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/buffers.cpp
r2127 r2194 7 7 #include "../include/global.h" 8 8 9 namespace VMI 10 { 11 Vertex_ *buf_vertices = NULL; 12 Color *buf_colors = NULL; 13 14 Vertex_ *pbuf_vertices; 15 Color *pbuf_colors; 16 17 GLuint vertex_buf = 0, 18 color_buf = 0; 19 20 } 21 22 using namespace VMI; 23 24 void VMI::saveVertexBuffer(Change *c, VertexIL *dest) { 25 int j, t, n = 0, m; 9 using namespace VMI; 10 11 12 13 VertexIL *VMI::saveVertexBuffer(Change *c, Vertex_ *verts, Color *cols) { 14 int i, j, t, n = 0, m; 15 VertexIL *dest; 16 17 dest = (VertexIL *)malloc((c->numDel + c->numMod + 1) * 3 * sizeof(VertexIL)); 26 18 27 19 // Save vertex coordinates and color for deleted triangles … … 32 24 m = 3 * t; 33 25 34 dest[n].x = buf_vertices[m + 0].x; 35 dest[n].y = buf_vertices[m + 0].y; 36 dest[n].z = buf_vertices[m + 0].z; 37 dest[n].a = buf_colors[m + 0].a; 38 dest[n].b = buf_colors[m + 0].b; 39 dest[n].r = buf_colors[m + 0].r; 40 dest[n].g = buf_colors[m + 0].g; 41 n++; 42 43 dest[n].x = buf_vertices[m + 1].x; 44 dest[n].y = buf_vertices[m + 1].y; 45 dest[n].z = buf_vertices[m + 1].z; 46 dest[n].a = buf_colors[m + 1].a; 47 dest[n].b = buf_colors[m + 1].b; 48 dest[n].r = buf_colors[m + 1].r; 49 dest[n].g = buf_colors[m + 1].g; 50 n++; 51 52 dest[n].x = buf_vertices[m + 2].x; 53 dest[n].y = buf_vertices[m + 2].y; 54 dest[n].z = buf_vertices[m + 2].z; 55 dest[n].a = buf_colors[m + 2].a; 56 dest[n].b = buf_colors[m + 2].b; 57 dest[n].r = buf_colors[m + 2].r; 58 dest[n].g = buf_colors[m + 2].g; 59 n++; 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 } 60 36 } 61 37 // Save vertex coordinates for modified triangles … … 66 42 m = 3 * t; 67 43 68 dest[n].x = buf_vertices[m + 0].x; 69 dest[n].y = buf_vertices[m + 0].y; 70 dest[n].z = buf_vertices[m + 0].z; 71 n++; 72 73 dest[n].x = buf_vertices[m + 1].x; 74 dest[n].y = buf_vertices[m + 1].y; 75 dest[n].z = buf_vertices[m + 1].z; 76 n++; 77 78 dest[n].x = buf_vertices[m + 2].x; 79 dest[n].y = buf_vertices[m + 2].y; 80 dest[n].z = buf_vertices[m + 2].z; 81 n++; 82 } 83 } 84 85 void VMI::loadVertexBuffer(VertexIL *src, Change *c) { 86 int j, t, n = 0, m; 87 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 } 50 } 51 52 return dest; 53 } 54 55 VertexIL *VMI::saveVertexBufferGPU(Change *c) { 56 Vertex_ *verts; 57 Color *cols; 58 VertexIL *dest; 59 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 88 87 // Load vertex coordinates and color for deleted triangles 89 88 for (j=0; j<c->numDel; j++) { … … 92 91 93 92 m = 3 * t; 94 95 buf_vertices[m + 0].x = src[n].x; 96 buf_vertices[m + 0].y = src[n].y; 97 buf_vertices[m + 0].z = src[n].z; 98 buf_colors[m + 0].a = src[n].a; 99 buf_colors[m + 0].b = src[n].b; 100 buf_colors[m + 0].r = src[n].r; 101 buf_colors[m + 0].g = src[n].g; 102 n++; 103 104 buf_vertices[m + 1].x = src[n].x; 105 buf_vertices[m + 1].y = src[n].y; 106 buf_vertices[m + 1].z = src[n].z; 107 buf_colors[m + 1].a = src[n].a; 108 buf_colors[m + 1].b = src[n].b; 109 buf_colors[m + 1].r = src[n].r; 110 buf_colors[m + 1].g = src[n].g; 111 n++; 112 113 buf_vertices[m + 2].x = src[n].x; 114 buf_vertices[m + 2].y = src[n].y; 115 buf_vertices[m + 2].z = src[n].z; 116 buf_colors[m + 2].a = src[n].a; 117 buf_colors[m + 2].b = src[n].b; 118 buf_colors[m + 2].r = src[n].r; 119 buf_colors[m + 2].g = src[n].g; 120 n++; 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 } 121 104 } 122 105 // Load vertex coordinates for modified triangles … … 126 109 127 110 m = 3 * t; 128 129 buf_vertices[m + 0].x = src[n].x; 130 buf_vertices[m + 0].y = src[n].y; 131 buf_vertices[m + 0].z = src[n].z; 132 n++; 133 134 buf_vertices[m + 1].x = src[n].x; 135 buf_vertices[m + 1].y = src[n].y; 136 buf_vertices[m + 1].z = src[n].z; 137 n++; 138 139 buf_vertices[m + 2].x = src[n].x; 140 buf_vertices[m + 2].y = src[n].y; 141 buf_vertices[m + 2].z = src[n].z; 142 n++; 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 } 120 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 140 } 141 glBindBuffer(GL_ARRAY_BUFFER_ARB, color_buf); 142 if (!glUnmapBufferARB(GL_ARRAY_BUFFER_ARB)) { 143 // Handle error case 143 144 } 144 145 } … … 146 147 void VMI::setupVertexArray(Mesh *mesh, Color *colors) 147 148 { 148 int i, v1, v2, v3, n = 0;149 int i, j, v, n = 0; 149 150 GLubyte r, g, b, a; 150 151 … … 153 154 154 155 buf_vertices = (Vertex_ *)malloc(mesh->numTriangles * 3 * sizeof(Vertex_)); 155 156 156 buf_colors = (Color *)malloc(mesh->numTriangles * 3 * sizeof(Color)); 157 157 … … 167 167 168 168 for (i=0; i<mesh->numTriangles; i++) { 169 170 v1= mesh->triangles[i].indices[0]; 171 v2= mesh->triangles[i].indices[1]; 172 v3= mesh->triangles[i].indices[2]; 173 174 if (mesh->triangles[i].enable == TRUE) { 175 176 r = colors[i].r; 177 g = colors[i].g; 178 b = colors[i].b; 179 a = colors[i].a; 180 181 buf_colors[n].r = r; 182 buf_colors[n].g = g; 183 buf_colors[n].b = b; 184 buf_colors[n].a = a; 185 buf_vertices[n].x = mesh->vertices[v1].x; 186 buf_vertices[n].y = mesh->vertices[v1].y; 187 buf_vertices[n].z = mesh->vertices[v1].z; 188 n++; 189 190 buf_colors[n].r = r; 191 buf_colors[n].g = g; 192 buf_colors[n].b = b; 193 buf_colors[n].a = a; 194 buf_vertices[n].x = mesh->vertices[v2].x; 195 buf_vertices[n].y = mesh->vertices[v2].y; 196 buf_vertices[n].z = mesh->vertices[v2].z; 197 n++; 198 199 buf_colors[n].r = r; 200 buf_colors[n].g = g; 201 buf_colors[n].b = b; 202 buf_colors[n].a = a; 203 buf_vertices[n].x = mesh->vertices[v3].x; 204 buf_vertices[n].y = mesh->vertices[v3].y; 205 buf_vertices[n].z = mesh->vertices[v3].z; 206 n++; 207 } else { 208 209 buf_colors[n].r = 0; 210 buf_colors[n].g = 0; 211 buf_colors[n].b = 0; 212 buf_colors[n].a = 0; 213 buf_vertices[n].x = 0; 214 buf_vertices[n].y = 0; 215 buf_vertices[n].z = 0; 216 n++; 217 218 buf_colors[n].r = 0; 219 buf_colors[n].g = 0; 220 buf_colors[n].b = 0; 221 buf_colors[n].a = 0; 222 buf_vertices[n].x = 0; 223 buf_vertices[n].y = 0; 224 buf_vertices[n].z = 0; 225 n++; 226 227 buf_colors[n].r = 0; 228 buf_colors[n].g = 0; 229 buf_colors[n].b = 0; 230 buf_colors[n].a = 0; 231 buf_vertices[n].x = 0; 232 buf_vertices[n].y = 0; 233 buf_vertices[n].z = 0; 234 n++; 235 } 169 if (mesh->triangles[i].enable == TRUE) { 170 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 } 236 202 } 237 203 #ifdef VERTEX_ARRAY … … 243 209 glVertexPointer(3, GL_FLOAT, 0, buf_vertices); 244 210 #endif 245 246 pbuf_colors = buf_colors;247 pbuf_vertices = buf_vertices;248 211 } 249 212 … … 253 216 254 217 glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertex_buf); 255 glBufferDataARB(GL_ARRAY_BUFFER_ARB, mesh->numTriangles * 3 * 3 * sizeof(float), buf_vertices, GL_STATIC_DRAW_ARB); //upload data218 glBufferDataARB(GL_ARRAY_BUFFER_ARB, mesh->numTriangles * 3 * sizeof(Vertex_), buf_vertices, GL_STATIC_DRAW_ARB); //upload data 256 219 257 220 glVertexPointer(3, GL_FLOAT, 0,0); … … 265 228 glEnableClientState(GL_VERTEX_ARRAY); 266 229 glEnableClientState(GL_COLOR_ARRAY); 267 268 //free(buf_vertices);269 //buf_vertices = NULL;270 271 //free(buf_colors);272 //buf_colors =NULL;273 } 274 275 void VMI::updateVertexArray(Mesh *mesh, Change *c )276 { 277 int i, v1, v2, v3, t, n;230 231 free(buf_vertices); 232 buf_vertices = NULL; 233 234 free(buf_colors); 235 buf_colors =NULL; 236 } 237 238 void VMI::updateVertexArray(Mesh *mesh, Change *c, Vertex_ *verts, Color * cols) 239 { 240 int i, j, v, t, n; 278 241 279 242 // The deleted triangles are now in background color 280 243 for (i=0; i<c->numDel; i++) { 281 244 t = c->deleted[i].id; 282 245 283 246 n = t * 3; 284 285 buf_colors[n + 0].r = 0; 286 buf_colors[n + 0].g = 0; 287 buf_colors[n + 0].b = 0; 288 buf_colors[n + 0].a = 0; 289 290 buf_colors[n + 1].r = 0; 291 buf_colors[n + 1].g = 0; 292 buf_colors[n + 1].b = 0; 293 buf_colors[n + 1].a = 0; 294 295 buf_colors[n + 2].r = 0; 296 buf_colors[n + 2].g = 0; 297 buf_colors[n + 2].b = 0; 298 buf_colors[n + 2].a = 0; 299 300 buf_vertices[n + 0].x = 0; 301 buf_vertices[n + 0].y = 0; 302 buf_vertices[n + 0].z = 0; 303 304 buf_vertices[n + 1].x = 0; 305 buf_vertices[n + 1].y = 0; 306 buf_vertices[n + 1].z = 0; 307 308 buf_vertices[n + 2].x = 0; 309 buf_vertices[n + 2].y = 0; 310 buf_vertices[n + 2].z = 0; 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 } 311 258 } 312 259 … … 317 264 n = t * 3; 318 265 319 v1= mesh->triangles[t].indices[0]; 320 v2= mesh->triangles[t].indices[1]; 321 v3= mesh->triangles[t].indices[2]; 322 323 buf_vertices[n + 0].x = mesh->vertices[v1].x; 324 buf_vertices[n + 0].y = mesh->vertices[v1].y; 325 buf_vertices[n + 0].z = mesh->vertices[v1].z; 326 327 buf_vertices[n + 1].x = mesh->vertices[v2].x; 328 buf_vertices[n + 1].y = mesh->vertices[v2].y; 329 buf_vertices[n + 1].z = mesh->vertices[v2].z; 330 331 buf_vertices[n + 2].x = mesh->vertices[v3].x; 332 buf_vertices[n + 2].y = mesh->vertices[v3].y; 333 buf_vertices[n + 2].z = mesh->vertices[v3].z; 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 } 334 274 } 335 275 } 336 276 337 277 void VMI::updateVertexBufferObjects(Mesh *mesh, Change *c) { 278 Vertex_ *verts; 279 Color *cols; 338 280 339 281 glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertex_buf); 340 282 // Map the buffer object 341 buf_vertices = (Vertex_ *)glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY);283 verts = (Vertex_ *)glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY); 342 284 343 285 glBindBufferARB(GL_ARRAY_BUFFER_ARB, color_buf); 344 286 // Map the buffer object 345 buf_colors = (Color *)glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY); 346 347 updateVertexArray(mesh, c); 287 cols = (Color *)glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY); 288 289 // Update GPU buffers 290 updateVertexArray(mesh, c, verts, cols); 348 291 349 292 // Unmap buffers … … 360 303 VertexIL *VMI::setupInterleave(Mesh *mesh, Color *colors) 361 304 { 362 int i, v1, v2, v3, n = 0;305 int i, j, v, n = 0; 363 306 GLubyte r, g, b, a; 364 307 VertexIL *interleave = NULL; … … 374 317 375 318 if (mesh->triangles[i].enable == TRUE) { 376 v1= mesh->triangles[i].indices[0]; 377 v2= mesh->triangles[i].indices[1]; 378 v3= mesh->triangles[i].indices[2]; 379 380 r = colors[i].r; 319 320 r = colors[i].r; 381 321 g = colors[i].g; 382 322 b = colors[i].b; 383 323 a = colors[i].a; 384 385 interleave[n].tx = 0.0f; 386 interleave[n].ty = 0.0f; 387 interleave[n].r = r; 388 interleave[n].g = g; 389 interleave[n].b = b; 390 interleave[n].a = a; 391 interleave[n].x = mesh->vertices[v1].x; 392 interleave[n].y = mesh->vertices[v1].y; 393 interleave[n].z = mesh->vertices[v1].z; 394 n++; 395 396 interleave[n].tx = 0.0f; 397 interleave[n].ty = 0.0f; 398 interleave[n].r = r; 399 interleave[n].g = g; 400 interleave[n].b = b; 401 interleave[n].a = a; 402 interleave[n].x = mesh->vertices[v2].x; 403 interleave[n].y = mesh->vertices[v2].y; 404 interleave[n].z = mesh->vertices[v2].z; 405 n++; 406 407 interleave[n].tx = 0.0f; 408 interleave[n].ty = 0.0f; 409 interleave[n].r = r; 410 interleave[n].g = g; 411 interleave[n].b = b; 412 interleave[n].a = a; 413 interleave[n].x = mesh->vertices[v3].x; 414 interleave[n].y = mesh->vertices[v3].y; 415 interleave[n].z = mesh->vertices[v3].z; 416 n++; 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 } 417 340 } 418 341 } … … 435 358 void VMI::updateAllColorInterleave(VertexIL *interleave, int num, Color *colors) 436 359 { 437 int i, n = 0;360 int i, j, n = 0; 438 361 GLubyte r, g, b, a; 439 362 440 363 for (i=1; i<=num; i++) { // Triangles start at 1 441 364 442 365 r = colors[i].r; 443 366 g = colors[i].g; 444 367 b = colors[i].b; 445 368 a = colors[i].a; 446 447 interleave[n].r = r; 448 interleave[n].g = g; 449 interleave[n].b = b; 450 interleave[n].a = a; 451 n++; 452 453 interleave[n].r = r; 454 interleave[n].g = g; 455 interleave[n].b = b; 456 interleave[n].a = a; 457 n++; 458 459 interleave[n].r = r; 460 interleave[n].g = g; 461 interleave[n].b = b; 462 interleave[n].a = a; 463 n++; 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 } 464 377 } 465 378 }
Note: See TracChangeset
for help on using the changeset viewer.