Showing the changes to interface/src/VoxelSystem.cpp (1 of 5 files). Show all
Unified · Split
| @@ -33,9 +33,11 @@ · 32 unmodified lines | ||
| 33 | 33 | #include "VoxelConstants.h" |
| 34 | 34 | #include "VoxelSystem.h" |
| 35 | 35 | |
| 36 | −float identityVertices[] = { 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1, | |
| 37 | − 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1, | |
| 38 | − 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1 }; | |
| 36 | +float identityVerticesGlobalNormals[] = { 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1 }; | |
| 37 | + | |
| 38 | +float identityVertices[] = { 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1, //0-7 | |
| 39 | + 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1, //8-15 | |
| 40 | + 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1 }; // 16-23 | |
| 39 | 41 | |
| 40 | 42 | GLfloat identityNormals[] = { 0,0,-1, 0,0,-1, 0,0,-1, 0,0,-1, |
| 41 | 43 | 0,0,+1, 0,0,+1, 0,0,+1, 0,0,+1, |
| @@ -51,6 +53,13 @@ GLubyte identityIndices[] = { 0,2,1, 0,3,2, // Z- · 9 unmodified lines | ||
| 51 | 53 | 10,11,15, 10,15,14, // Y+ |
| 52 | 54 | 4,5,6, 4,6,7 }; // Z+ |
| 53 | 55 | |
| 56 | +GLubyte identityIndicesTop[] = { 2, 3, 7, 2, 7, 6 }; | |
| 57 | +GLubyte identityIndicesBottom[] = { 0, 1, 5, 0, 5, 4 }; | |
| 58 | +GLubyte identityIndicesLeft[] = { 0, 7, 3, 0, 4, 7 }; | |
| 59 | +GLubyte identityIndicesRight[] = { 1, 2, 6, 1, 6, 5 }; | |
| 60 | +GLubyte identityIndicesFront[] = { 0, 2, 1, 0, 3, 2 }; | |
| 61 | +GLubyte identityIndicesBack[] = { 4, 5, 6, 4, 6, 7 }; | |
| 62 | + | |
| 54 | 63 | VoxelSystem::VoxelSystem(float treeScale, int maxVoxels) |
| 55 | 64 | : NodeData(NULL), |
| 56 | 65 | _treeScale(treeScale), |
| @@ -77,6 +86,7 @@ VoxelSystem::VoxelSystem(float treeScale, int maxVoxels) · 20 unmodified lines | ||
| 77 | 86 | |
| 78 | 87 | _useVoxelShader = false; |
| 79 | 88 | _useByteNormals = false; |
| 89 | + _useGlobalNormals = false; | |
| 80 | 90 | |
| 81 | 91 | _writeVoxelShaderData = NULL; |
| 82 | 92 | _readVoxelShaderData = NULL; |
| @@ -145,6 +155,7 @@ void VoxelSystem::setUseByteNormals(bool useByteNormals) { · 62 unmodified lines | ||
| 145 | 155 | pthread_mutex_lock(&_bufferWriteLock); |
| 146 | 156 | bool wasInitialized = _initialized; |
| 147 | 157 | if (wasInitialized) { |
| 158 | + clearAllNodesBufferIndex(); | |
| 148 | 159 | cleanupVoxelMemory(); |
| 149 | 160 | } |
| 150 | 161 | _useByteNormals = useByteNormals; |
| @@ -158,11 +169,29 @@ void VoxelSystem::setUseByteNormals(bool useByteNormals) { · 7 unmodified lines | ||
| 158 | 169 | } |
| 159 | 170 | } |
| 160 | 171 | |
| 172 | +void VoxelSystem::setUseGlobalNormals(bool useGlobalNormals) { | |
| 173 | + pthread_mutex_lock(&_bufferWriteLock); | |
| 174 | + bool wasInitialized = _initialized; | |
| 175 | + if (wasInitialized) { | |
| 176 | + clearAllNodesBufferIndex(); | |
| 177 | + cleanupVoxelMemory(); | |
| 178 | + } | |
| 179 | + _useGlobalNormals = useGlobalNormals; | |
| 180 | + if (wasInitialized) { | |
| 181 | + init(); | |
| 182 | + } | |
| 183 | + pthread_mutex_unlock(&_bufferWriteLock); | |
| 184 | + | |
| 185 | + if (wasInitialized) { | |
| 186 | + forceRedrawEntireTree(); | |
| 187 | + } | |
| 188 | +} | |
| 161 | 189 | |
| 162 | 190 | void VoxelSystem::setMaxVoxels(int maxVoxels) { |
| 163 | 191 | pthread_mutex_lock(&_bufferWriteLock); |
| 164 | 192 | bool wasInitialized = _initialized; |
| 165 | 193 | if (wasInitialized) { |
| 194 | + clearAllNodesBufferIndex(); | |
| 166 | 195 | cleanupVoxelMemory(); |
| 167 | 196 | } |
| 168 | 197 | _maxVoxels = maxVoxels; |
| @@ -206,9 +235,19 @@ void VoxelSystem::cleanupVoxelMemory() { · 37 unmodified lines | ||
| 206 | 235 | } else { |
| 207 | 236 | // Destroy glBuffers |
| 208 | 237 | glDeleteBuffers(1, &_vboVerticesID); |
| 209 | − glDeleteBuffers(1, &_vboNormalsID); | |
| 210 | 238 | glDeleteBuffers(1, &_vboColorsID); |
| 211 | − glDeleteBuffers(1, &_vboIndicesID); | |
| 239 | + | |
| 240 | + if (!_useGlobalNormals) { | |
| 241 | + glDeleteBuffers(1, &_vboNormalsID); | |
| 242 | + glDeleteBuffers(1, &_vboIndicesID); | |
| 243 | + } else { | |
| 244 | + glDeleteBuffers(1, &_vboIndicesTop); | |
| 245 | + glDeleteBuffers(1, &_vboIndicesBottom); | |
| 246 | + glDeleteBuffers(1, &_vboIndicesLeft); | |
| 247 | + glDeleteBuffers(1, &_vboIndicesRight); | |
| 248 | + glDeleteBuffers(1, &_vboIndicesFront); | |
| 249 | + glDeleteBuffers(1, &_vboIndicesBack); | |
| 250 | + } | |
| 212 | 251 | |
| 213 | 252 | delete[] _readVerticesArray; |
| 214 | 253 | delete[] _writeVerticesArray; |
| @@ -221,6 +260,34 @@ void VoxelSystem::cleanupVoxelMemory() { · 6 unmodified lines | ||
| 221 | 260 | _initialized = false; // no longer initialized |
| 222 | 261 | } |
| 223 | 262 | |
| 263 | +void VoxelSystem::setupFaceIndices(GLuint& faceVBOID, GLubyte faceIdentityIndices[]) { | |
| 264 | + GLuint* indicesArray = new GLuint[INDICES_PER_FACE * _maxVoxels]; | |
| 265 | + | |
| 266 | + // populate the indicesArray | |
| 267 | + // this will not change given new voxels, so we can set it all up now | |
| 268 | + for (int n = 0; n < _maxVoxels; n++) { | |
| 269 | + // fill the indices array | |
| 270 | + int voxelIndexOffset = n * INDICES_PER_FACE; | |
| 271 | + GLuint* currentIndicesPos = indicesArray + voxelIndexOffset; | |
| 272 | + int startIndex = (n * GLOBAL_NORMALS_VERTICES_PER_VOXEL); | |
| 273 | + | |
| 274 | + for (int i = 0; i < INDICES_PER_FACE; i++) { | |
| 275 | + // add indices for this side of the cube | |
| 276 | + currentIndicesPos[i] = startIndex + faceIdentityIndices[i]; | |
| 277 | + } | |
| 278 | + } | |
| 279 | + | |
| 280 | + glGenBuffers(1, &faceVBOID); | |
| 281 | + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, faceVBOID); | |
| 282 | + glBufferData(GL_ELEMENT_ARRAY_BUFFER, | |
| 283 | + INDICES_PER_FACE * sizeof(GLuint) * _maxVoxels, | |
| 284 | + indicesArray, GL_STATIC_DRAW); | |
| 285 | + _memoryUsageVBO += INDICES_PER_FACE * sizeof(GLuint) * _maxVoxels; | |
| 286 | + | |
| 287 | + // delete the indices and normals arrays that are no longer needed | |
| 288 | + delete[] indicesArray; | |
| 289 | +} | |
| 290 | + | |
| 224 | 291 | void VoxelSystem::initVoxelMemory() { |
| 225 | 292 | _initialMemoryUsageGPU = getFreeMemoryGPU(); |
| 226 | 293 | _memoryUsageRAM = 0; |
| @@ -265,91 +332,104 @@ void VoxelSystem::initVoxelMemory() { · 38 unmodified lines | ||
| 265 | 332 | _readVoxelShaderData = new VoxelShaderVBOData[_maxVoxels]; |
| 266 | 333 | _memoryUsageRAM += (sizeof(VoxelShaderVBOData) * _maxVoxels); |
| 267 | 334 | } else { |
| 268 | − GLuint* indicesArray = new GLuint[INDICES_PER_VOXEL * _maxVoxels]; | |
| 335 | + if (_useGlobalNormals) { | |
| 336 | + // Global Normals mode uses a technique of not including normals on any voxel vertices, and instead | |
| 337 | + // rendering the voxel faces in 6 passes that use a global call to glNormal3f() | |
| 338 | + qDebug("Using Global Normals...\n"); | |
| 339 | + setupFaceIndices(_vboIndicesTop, identityIndicesTop); | |
| 340 | + setupFaceIndices(_vboIndicesBottom, identityIndicesBottom); | |
| 341 | + setupFaceIndices(_vboIndicesLeft, identityIndicesLeft); | |
| 342 | + setupFaceIndices(_vboIndicesRight, identityIndicesRight); | |
| 343 | + setupFaceIndices(_vboIndicesFront, identityIndicesFront); | |
| 344 | + setupFaceIndices(_vboIndicesBack, identityIndicesBack); | |
| 345 | + } else { | |
| 346 | + if (_useByteNormals) { | |
| 347 | + qDebug("Using Byte Normals...\n"); | |
| 348 | + GLbyte* normalsArray = new GLbyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | |
| 349 | + GLbyte* normalsArrayEndPointer = normalsArray; | |
| 350 | + | |
| 351 | + // populate the normalsArray | |
| 352 | + for (int n = 0; n < _maxVoxels; n++) { | |
| 353 | + for (int i = 0; i < VERTEX_POINTS_PER_VOXEL; i++) { | |
| 354 | + *(normalsArrayEndPointer++) = (identityNormals[i] * CHAR_MAX); | |
| 355 | + } | |
| 356 | + } | |
| 269 | 357 | |
| 270 | − // populate the indicesArray | |
| 271 | − // this will not change given new voxels, so we can set it all up now | |
| 272 | − for (int n = 0; n < _maxVoxels; n++) { | |
| 273 | − // fill the indices array | |
| 274 | − int voxelIndexOffset = n * INDICES_PER_VOXEL; | |
| 275 | − GLuint* currentIndicesPos = indicesArray + voxelIndexOffset; | |
| 276 | − int startIndex = (n * VERTICES_PER_VOXEL); | |
| 277 | − | |
| 278 | − for (int i = 0; i < INDICES_PER_VOXEL; i++) { | |
| 279 | − // add indices for this side of the cube | |
| 280 | − currentIndicesPos[i] = startIndex + identityIndices[i]; | |
| 358 | + // VBO for the normalsArray | |
| 359 | + glGenBuffers(1, &_vboNormalsID); | |
| 360 | + glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID); | |
| 361 | + glBufferData(GL_ARRAY_BUFFER, | |
| 362 | + VERTEX_POINTS_PER_VOXEL * sizeof(GLbyte) * _maxVoxels, | |
| 363 | + normalsArray, GL_STATIC_DRAW); | |
| 364 | + _memoryUsageVBO += VERTEX_POINTS_PER_VOXEL * sizeof(GLbyte) * _maxVoxels; | |
| 365 | + | |
| 366 | + // delete the indices and normals arrays that are no longer needed | |
| 367 | + delete[] normalsArray; | |
| 368 | + } else { | |
| 369 | + qDebug("Using Float Normals...\n"); | |
| 370 | + GLfloat* normalsArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | |
| 371 | + GLfloat* normalsArrayEndPointer = normalsArray; | |
| 372 | + | |
| 373 | + // populate the normalsArray | |
| 374 | + for (int n = 0; n < _maxVoxels; n++) { | |
| 375 | + for (int i = 0; i < VERTEX_POINTS_PER_VOXEL; i++) { | |
| 376 | + *(normalsArrayEndPointer++) = identityNormals[i]; | |
| 377 | + } | |
| 378 | + } | |
| 379 | + | |
| 380 | + // VBO for the normalsArray | |
| 381 | + glGenBuffers(1, &_vboNormalsID); | |
| 382 | + glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID); | |
| 383 | + glBufferData(GL_ARRAY_BUFFER, | |
| 384 | + VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels, | |
| 385 | + normalsArray, GL_STATIC_DRAW); | |
| 386 | + _memoryUsageVBO += VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels; | |
| 387 | + | |
| 388 | + // delete the indices and normals arrays that are no longer needed | |
| 389 | + delete[] normalsArray; | |
| 281 | 390 | } |
| 282 | − } | |
| 283 | 391 | |
| 284 | − if (_useByteNormals) { | |
| 285 | − qDebug("Using Byte Normals...\n"); | |
| 286 | − GLbyte* normalsArray = new GLbyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | |
| 287 | − GLbyte* normalsArrayEndPointer = normalsArray; | |
| 392 | + GLuint* indicesArray = new GLuint[INDICES_PER_VOXEL * _maxVoxels]; | |
| 288 | 393 | |
| 289 | − // populate the normalsArray | |
| 394 | + // populate the indicesArray | |
| 395 | + // this will not change given new voxels, so we can set it all up now | |
| 290 | 396 | for (int n = 0; n < _maxVoxels; n++) { |
| 291 | − for (int i = 0; i < VERTEX_POINTS_PER_VOXEL; i++) { | |
| 292 | − *(normalsArrayEndPointer++) = (identityNormals[i] * CHAR_MAX); | |
| 397 | + // fill the indices array | |
| 398 | + int voxelIndexOffset = n * INDICES_PER_VOXEL; | |
| 399 | + GLuint* currentIndicesPos = indicesArray + voxelIndexOffset; | |
| 400 | + int startIndex = (n * VERTICES_PER_VOXEL); | |
| 401 | + | |
| 402 | + for (int i = 0; i < INDICES_PER_VOXEL; i++) { | |
| 403 | + // add indices for this side of the cube | |
| 404 | + currentIndicesPos[i] = startIndex + identityIndices[i]; | |
| 293 | 405 | } |
| 294 | 406 | } |
| 295 | 407 | |
| 296 | − // VBO for the normalsArray | |
| 297 | − glGenBuffers(1, &_vboNormalsID); | |
| 298 | − glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID); | |
| 299 | − glBufferData(GL_ARRAY_BUFFER, | |
| 300 | − VERTEX_POINTS_PER_VOXEL * sizeof(GLbyte) * _maxVoxels, | |
| 301 | − normalsArray, GL_STATIC_DRAW); | |
| 302 | − _memoryUsageVBO += VERTEX_POINTS_PER_VOXEL * sizeof(GLbyte) * _maxVoxels; | |
| 408 | + // VBO for the indicesArray | |
| 409 | + glGenBuffers(1, &_vboIndicesID); | |
| 410 | + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID); | |
| 411 | + glBufferData(GL_ELEMENT_ARRAY_BUFFER, | |
| 412 | + INDICES_PER_VOXEL * sizeof(GLuint) * _maxVoxels, | |
| 413 | + indicesArray, GL_STATIC_DRAW); | |
| 414 | + _memoryUsageVBO += INDICES_PER_VOXEL * sizeof(GLuint) * _maxVoxels; | |
| 303 | 415 | |
| 304 | 416 | // delete the indices and normals arrays that are no longer needed |
| 305 | − delete[] normalsArray; | |
| 306 | − } else { | |
| 307 | − qDebug("Using Float Normals...\n"); | |
| 308 | − GLfloat* normalsArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | |
| 309 | − GLfloat* normalsArrayEndPointer = normalsArray; | |
| 417 | + delete[] indicesArray; | |
| 310 | 418 | |
| 311 | − // populate the normalsArray | |
| 312 | − for (int n = 0; n < _maxVoxels; n++) { | |
| 313 | − for (int i = 0; i < VERTEX_POINTS_PER_VOXEL; i++) { | |
| 314 | − *(normalsArrayEndPointer++) = identityNormals[i]; | |
| 315 | − } | |
| 316 | − } | |
| 317 | − | |
| 318 | − // VBO for the normalsArray | |
| 319 | − glGenBuffers(1, &_vboNormalsID); | |
| 320 | − glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID); | |
| 321 | − glBufferData(GL_ARRAY_BUFFER, | |
| 322 | − VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels, | |
| 323 | − normalsArray, GL_STATIC_DRAW); | |
| 324 | − _memoryUsageVBO += VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels; | |
| 325 | − | |
| 326 | − // delete the indices and normals arrays that are no longer needed | |
| 327 | − delete[] normalsArray; | |
| 328 | 419 | } |
| 329 | − | |
| 330 | − | |
| 420 | + | |
| 421 | + // Depending on if we're using per vertex normals, we will need more or less vertex points per voxel | |
| 422 | + int vertexPointsPerVoxel = _useGlobalNormals ? GLOBAL_NORMALS_VERTEX_POINTS_PER_VOXEL : VERTEX_POINTS_PER_VOXEL; | |
| 331 | 423 | glGenBuffers(1, &_vboVerticesID); |
| 332 | 424 | glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID); |
| 333 | − glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels, NULL, GL_DYNAMIC_DRAW); | |
| 334 | − _memoryUsageVBO += VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels; | |
| 425 | + glBufferData(GL_ARRAY_BUFFER, vertexPointsPerVoxel * sizeof(GLfloat) * _maxVoxels, NULL, GL_DYNAMIC_DRAW); | |
| 426 | + _memoryUsageVBO += vertexPointsPerVoxel * sizeof(GLfloat) * _maxVoxels; | |
| 335 | 427 | |
| 336 | 428 | // VBO for colorsArray |
| 337 | 429 | glGenBuffers(1, &_vboColorsID); |
| 338 | 430 | glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID); |
| 339 | − glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte) * _maxVoxels, NULL, GL_DYNAMIC_DRAW); | |
| 340 | − _memoryUsageVBO += VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte) * _maxVoxels; | |
| 341 | − | |
| 342 | − // VBO for the indicesArray | |
| 343 | − glGenBuffers(1, &_vboIndicesID); | |
| 344 | − glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID); | |
| 345 | − glBufferData(GL_ELEMENT_ARRAY_BUFFER, | |
| 346 | − INDICES_PER_VOXEL * sizeof(GLuint) * _maxVoxels, | |
| 347 | − indicesArray, GL_STATIC_DRAW); | |
| 348 | − _memoryUsageVBO += INDICES_PER_VOXEL * sizeof(GLuint) * _maxVoxels; | |
| 349 | − | |
| 350 | − // delete the indices and normals arrays that are no longer needed | |
| 351 | − delete[] indicesArray; | |
| 352 | − | |
| 431 | + glBufferData(GL_ARRAY_BUFFER, vertexPointsPerVoxel * sizeof(GLubyte) * _maxVoxels, NULL, GL_DYNAMIC_DRAW); | |
| 432 | + _memoryUsageVBO += vertexPointsPerVoxel * sizeof(GLubyte) * _maxVoxels; | |
| 353 | 433 | |
| 354 | 434 | // we will track individual dirty sections with these arrays of bools |
| 355 | 435 | _writeVoxelDirtyArray = new bool[_maxVoxels]; |
| @@ -361,15 +441,15 @@ void VoxelSystem::initVoxelMemory() { · 5 unmodified lines | ||
| 361 | 441 | _memoryUsageRAM += (sizeof(bool) * _maxVoxels); |
| 362 | 442 | |
| 363 | 443 | // prep the data structures for incoming voxel data |
| 364 | − _writeVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | |
| 365 | − _memoryUsageRAM += (sizeof(GLfloat) * VERTEX_POINTS_PER_VOXEL * _maxVoxels); | |
| 366 | − _readVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | |
| 367 | − _memoryUsageRAM += (sizeof(GLfloat) * VERTEX_POINTS_PER_VOXEL * _maxVoxels); | |
| 444 | + _writeVerticesArray = new GLfloat[vertexPointsPerVoxel * _maxVoxels]; | |
| 445 | + _memoryUsageRAM += (sizeof(GLfloat) * vertexPointsPerVoxel * _maxVoxels); | |
| 446 | + _readVerticesArray = new GLfloat[vertexPointsPerVoxel * _maxVoxels]; | |
| 447 | + _memoryUsageRAM += (sizeof(GLfloat) * vertexPointsPerVoxel * _maxVoxels); | |
| 368 | 448 | |
| 369 | − _writeColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | |
| 370 | − _memoryUsageRAM += (sizeof(GLubyte) * VERTEX_POINTS_PER_VOXEL * _maxVoxels); | |
| 371 | − _readColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels]; | |
| 372 | − _memoryUsageRAM += (sizeof(GLubyte) * VERTEX_POINTS_PER_VOXEL * _maxVoxels); | |
| 449 | + _writeColorsArray = new GLubyte[vertexPointsPerVoxel * _maxVoxels]; | |
| 450 | + _memoryUsageRAM += (sizeof(GLubyte) * vertexPointsPerVoxel * _maxVoxels); | |
| 451 | + _readColorsArray = new GLubyte[vertexPointsPerVoxel * _maxVoxels]; | |
| 452 | + _memoryUsageRAM += (sizeof(GLubyte) * vertexPointsPerVoxel * _maxVoxels); | |
| 373 | 453 | |
| 374 | 454 | |
| 375 | 455 | // create our simple fragment shader if we're the first system to init |
| @@ -649,16 +729,19 @@ void VoxelSystem::copyWrittenDataSegmentToReadArrays(glBufferIndex segmentStart, · 273 unmodified lines | ||
| 649 | 729 | void* writeDataAt = &_writeVoxelShaderData[segmentStart]; |
| 650 | 730 | memcpy(readDataAt, writeDataAt, segmentSizeBytes); |
| 651 | 731 | } else { |
| 652 | − GLintptr segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat); | |
| 653 | − GLsizeiptr segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat); | |
| 654 | − GLfloat* readVerticesAt = _readVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | |
| 655 | − GLfloat* writeVerticesAt = _writeVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | |
| 732 | + // Depending on if we're using per vertex normals, we will need more or less vertex points per voxel | |
| 733 | + int vertexPointsPerVoxel = _useGlobalNormals ? GLOBAL_NORMALS_VERTEX_POINTS_PER_VOXEL : VERTEX_POINTS_PER_VOXEL; | |
| 734 | + | |
| 735 | + GLintptr segmentStartAt = segmentStart * vertexPointsPerVoxel * sizeof(GLfloat); | |
| 736 | + GLsizeiptr segmentSizeBytes = segmentLength * vertexPointsPerVoxel * sizeof(GLfloat); | |
| 737 | + GLfloat* readVerticesAt = _readVerticesArray + (segmentStart * vertexPointsPerVoxel); | |
| 738 | + GLfloat* writeVerticesAt = _writeVerticesArray + (segmentStart * vertexPointsPerVoxel); | |
| 656 | 739 | memcpy(readVerticesAt, writeVerticesAt, segmentSizeBytes); |
| 657 | 740 | |
| 658 | − segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte); | |
| 659 | − segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte); | |
| 660 | − GLubyte* readColorsAt = _readColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | |
| 661 | − GLubyte* writeColorsAt = _writeColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | |
| 741 | + segmentStartAt = segmentStart * vertexPointsPerVoxel * sizeof(GLubyte); | |
| 742 | + segmentSizeBytes = segmentLength * vertexPointsPerVoxel * sizeof(GLubyte); | |
| 743 | + GLubyte* readColorsAt = _readColorsArray + (segmentStart * vertexPointsPerVoxel); | |
| 744 | + GLubyte* writeColorsAt = _writeColorsArray + (segmentStart * vertexPointsPerVoxel); | |
| 662 | 745 | memcpy(readColorsAt, writeColorsAt, segmentSizeBytes); |
| 663 | 746 | } |
| 664 | 747 | } |
| @@ -797,10 +880,17 @@ void VoxelSystem::updateNodeInArrays(glBufferIndex nodeIndex, const glm::vec3& s · 132 unmodified lines | ||
| 797 | 880 | } |
| 798 | 881 | } else { |
| 799 | 882 | if (_writeVerticesArray && _writeColorsArray) { |
| 800 | − for (int j = 0; j < VERTEX_POINTS_PER_VOXEL; j++ ) { | |
| 801 | − GLfloat* writeVerticesAt = _writeVerticesArray + (nodeIndex * VERTEX_POINTS_PER_VOXEL); | |
| 802 | − GLubyte* writeColorsAt = _writeColorsArray + (nodeIndex * VERTEX_POINTS_PER_VOXEL); | |
| 803 | − *(writeVerticesAt+j) = startVertex[j % 3] + (identityVertices[j] * voxelScale); | |
| 883 | + int vertexPointsPerVoxel = _useGlobalNormals ? GLOBAL_NORMALS_VERTEX_POINTS_PER_VOXEL : VERTEX_POINTS_PER_VOXEL; | |
| 884 | + for (int j = 0; j < vertexPointsPerVoxel; j++ ) { | |
| 885 | + GLfloat* writeVerticesAt = _writeVerticesArray + (nodeIndex * vertexPointsPerVoxel); | |
| 886 | + GLubyte* writeColorsAt = _writeColorsArray + (nodeIndex * vertexPointsPerVoxel); | |
| 887 | + | |
| 888 | + if (_useGlobalNormals) { | |
| 889 | + *(writeVerticesAt+j) = startVertex[j % 3] + (identityVerticesGlobalNormals[j] * voxelScale); | |
| 890 | + } else { | |
| 891 | + *(writeVerticesAt+j) = startVertex[j % 3] + (identityVertices[j] * voxelScale); | |
| 892 | + } | |
| 893 | + | |
| 804 | 894 | *(writeColorsAt +j) = color[j % 3]; |
| 805 | 895 | } |
| 806 | 896 | } |
| @@ -912,15 +1002,16 @@ void VoxelSystem::updateVBOSegment(glBufferIndex segmentStart, glBufferIndex seg · 105 unmodified lines | ||
| 912 | 1002 | glBindBuffer(GL_ARRAY_BUFFER, _vboVoxelsID); |
| 913 | 1003 | glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readVerticesFrom); |
| 914 | 1004 | } else { |
| 1005 | + int vertexPointsPerVoxel = _useGlobalNormals ? GLOBAL_NORMALS_VERTEX_POINTS_PER_VOXEL : VERTEX_POINTS_PER_VOXEL; | |
| 915 | 1006 | int segmentLength = (segmentEnd - segmentStart) + 1; |
| 916 | − GLintptr segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat); | |
| 917 | − GLsizeiptr segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat); | |
| 918 | − GLfloat* readVerticesFrom = _readVerticesArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | |
| 1007 | + GLintptr segmentStartAt = segmentStart * vertexPointsPerVoxel * sizeof(GLfloat); | |
| 1008 | + GLsizeiptr segmentSizeBytes = segmentLength * vertexPointsPerVoxel * sizeof(GLfloat); | |
| 1009 | + GLfloat* readVerticesFrom = _readVerticesArray + (segmentStart * vertexPointsPerVoxel); | |
| 919 | 1010 | glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID); |
| 920 | 1011 | glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readVerticesFrom); |
| 921 | − segmentStartAt = segmentStart * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte); | |
| 922 | − segmentSizeBytes = segmentLength * VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte); | |
| 923 | − GLubyte* readColorsFrom = _readColorsArray + (segmentStart * VERTEX_POINTS_PER_VOXEL); | |
| 1012 | + segmentStartAt = segmentStart * vertexPointsPerVoxel * sizeof(GLubyte); | |
| 1013 | + segmentSizeBytes = segmentLength * vertexPointsPerVoxel * sizeof(GLubyte); | |
| 1014 | + GLubyte* readColorsFrom = _readColorsArray + (segmentStart * vertexPointsPerVoxel); | |
| 924 | 1015 | glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID); |
| 925 | 1016 | glBufferSubData(GL_ARRAY_BUFFER, segmentStartAt, segmentSizeBytes, readColorsFrom); |
| 926 | 1017 | } |
| @@ -966,27 +1057,66 @@ void VoxelSystem::render(bool texture) { · 39 unmodified lines | ||
| 966 | 1057 | } else { |
| 967 | 1058 | // tell OpenGL where to find vertex and color information |
| 968 | 1059 | glEnableClientState(GL_VERTEX_ARRAY); |
| 969 | − glEnableClientState(GL_NORMAL_ARRAY); | |
| 970 | 1060 | glEnableClientState(GL_COLOR_ARRAY); |
| 971 | 1061 | |
| 972 | 1062 | glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID); |
| 973 | 1063 | glVertexPointer(3, GL_FLOAT, 0, 0); |
| 974 | 1064 | |
| 975 | − glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID); | |
| 976 | − glNormalPointer((_useByteNormals ? GL_BYTE : GL_FLOAT), 0, 0); | |
| 977 | − | |
| 978 | 1065 | glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID); |
| 979 | 1066 | glColorPointer(3, GL_UNSIGNED_BYTE, 0, 0); |
| 980 | 1067 | |
| 1068 | + if (!_useGlobalNormals) { | |
| 1069 | + glEnableClientState(GL_NORMAL_ARRAY); | |
| 1070 | + glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID); | |
| 1071 | + glNormalPointer((_useByteNormals ? GL_BYTE : GL_FLOAT), 0, 0); | |
| 1072 | + } else { | |
| 1073 | + glNormal3f(0,1.0f,0); // hack for now | |
| 1074 | + } | |
| 1075 | + | |
| 981 | 1076 | applyScaleAndBindProgram(texture); |
| 982 | 1077 | |
| 983 | 1078 | // for performance, enable backface culling |
| 984 | 1079 | glEnable(GL_CULL_FACE); |
| 985 | 1080 | |
| 986 | − // draw the number of voxels we have | |
| 987 | − glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID); | |
| 988 | − glDrawRangeElementsEXT(GL_TRIANGLES, 0, VERTICES_PER_VOXEL * _voxelsInReadArrays - 1, | |
| 989 | − 36 * _voxelsInReadArrays, GL_UNSIGNED_INT, 0); | |
| 1081 | + if (!_useGlobalNormals) { | |
| 1082 | + // draw the number of voxels we have | |
| 1083 | + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID); | |
| 1084 | + glDrawRangeElementsEXT(GL_TRIANGLES, 0, VERTICES_PER_VOXEL * _voxelsInReadArrays - 1, | |
| 1085 | + 36 * _voxelsInReadArrays, GL_UNSIGNED_INT, 0); | |
| 1086 | + } else { | |
| 1087 | + // draw voxels in 6 passes | |
| 1088 | + | |
| 1089 | + glNormal3f(0,1.0f,0); | |
| 1090 | + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesTop); | |
| 1091 | + glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1, | |
| 1092 | + INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0); | |
| 1093 | + | |
| 1094 | + glNormal3f(0,-1.0f,0); | |
| 1095 | + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesBottom); | |
| 1096 | + glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1, | |
| 1097 | + INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0); | |
| 1098 | + | |
| 1099 | + glNormal3f(-1.0f,0,0); | |
| 1100 | + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesLeft); | |
| 1101 | + glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1, | |
| 1102 | + INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0); | |
| 1103 | + | |
| 1104 | + glNormal3f(1.0f,0,0); | |
| 1105 | + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesRight); | |
| 1106 | + glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1, | |
| 1107 | + INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0); | |
| 1108 | + | |
| 1109 | + glNormal3f(0,0,-1.0f); | |
| 1110 | + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesFront); | |
| 1111 | + glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1, | |
| 1112 | + INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0); | |
| 1113 | + | |
| 1114 | + glNormal3f(0,0,1.0f); | |
| 1115 | + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesBack); | |
| 1116 | + glDrawRangeElementsEXT(GL_TRIANGLES, 0, INDICES_PER_FACE * _voxelsInReadArrays - 1, | |
| 1117 | + INDICES_PER_FACE * _voxelsInReadArrays, GL_UNSIGNED_INT, 0); | |
| 1118 | + } | |
| 1119 | + | |
| 990 | 1120 | |
| 991 | 1121 | glDisable(GL_CULL_FACE); |
| 992 | 1122 | |
| @@ -994,9 +1124,12 @@ void VoxelSystem::render(bool texture) { · 1 unmodified line | ||
| 994 | 1124 | |
| 995 | 1125 | // deactivate vertex and color arrays after drawing |
| 996 | 1126 | glDisableClientState(GL_VERTEX_ARRAY); |
| 997 | − glDisableClientState(GL_NORMAL_ARRAY); | |
| 998 | 1127 | glDisableClientState(GL_COLOR_ARRAY); |
| 999 | 1128 | |
| 1129 | + if (!_useGlobalNormals) { | |
| 1130 | + glDisableClientState(GL_NORMAL_ARRAY); | |
| 1131 | + } | |
| 1132 | + | |
| 1000 | 1133 | // bind with 0 to switch back to normal operation |
| 1001 | 1134 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 1002 | 1135 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |