History

Working on avatar voxels.

Andrzej Kapolka committed 13 years ago

Commit e4bc7af1 parent f5e7d6e6 files · +68 −43Browse files

Showing the changes to interface/src/VoxelSystem.cpp (1 of 6 files). Show all

Unified · Split

interface/src/VoxelSystem.cpp+35−28
@@ -19,6 +19,7 @@ · 18 unmodified lines
19#include <PerfStat.h>19#include <PerfStat.h>
20#include <OctalCode.h>20#include <OctalCode.h>
21#include <pthread.h>21#include <pthread.h>
22#include "Application.h"
22#include "Log.h"23#include "Log.h"
23#include "VoxelConstants.h"24#include "VoxelConstants.h"
24#include "InterfaceConfig.h"25#include "InterfaceConfig.h"
@@ -37,14 +38,15 @@ GLfloat identityNormals[] = { 0,0,-1, 0,0,-1, 0,0,-1, 0,0,-1, · 12 unmodified lines
37 -1,0,0, +1,0,0, +1,0,0, -1,0,0,38 -1,0,0, +1,0,0, +1,0,0, -1,0,0,
38 -1,0,0, +1,0,0, +1,0,0, -1,0,0 };39 -1,0,0, +1,0,0, +1,0,0, -1,0,0 };
39 40
40GLubyte identityIndices[] = { 0,2,1, 0,3,2, // Z- .41GLubyte identityIndices[] = { 0,2,1, 0,3,2, // Z-
41 8,9,13, 8,13,12, // Y-42 8,9,13, 8,13,12, // Y-
42 16,23,19, 16,20,23, // X-43 16,23,19, 16,20,23, // X-
43 17,18,22, 17,22,21, // X+44 17,18,22, 17,22,21, // X+
44 10,11,15, 10,15,14, // Y+45 10,11,15, 10,15,14, // Y+
45 4,5,6, 4,6,7 }; // Z+ .46 4,5,6, 4,6,7 }; // Z+
46 47
47VoxelSystem::VoxelSystem() : AgentData(NULL) {48VoxelSystem::VoxelSystem(float treeScale, int maxVoxels) :
49 AgentData(NULL), _treeScale(treeScale), _maxVoxels(maxVoxels) {
48 _voxelsInReadArrays = _voxelsInWriteArrays = _voxelsUpdated = 0;50 _voxelsInReadArrays = _voxelsInWriteArrays = _voxelsUpdated = 0;
49 _writeRenderFullVBO = true;51 _writeRenderFullVBO = true;
50 _readRenderFullVBO = true;52 _readRenderFullVBO = true;
@@ -312,12 +314,11 @@ void VoxelSystem::copyWrittenDataToReadArrays(bool fullVBOs) { · 261 unmodified lines
312}314}
313 315
314int VoxelSystem::newTreeToArrays(VoxelNode* node) {316int VoxelSystem::newTreeToArrays(VoxelNode* node) {
315 assert(_viewFrustum); // you must set up _viewFrustum before calling this
316 int voxelsUpdated = 0;317 int voxelsUpdated = 0;
317 bool shouldRender = false; // assume we don't need to render it318 bool shouldRender = false; // assume we don't need to render it
318 // if it's colored, we might need to render it!319 // if it's colored, we might need to render it!
319 if (node->isColored()) {320 if (node->isColored()) {
320 float distanceToNode = node->distanceToCamera(*_viewFrustum);321 float distanceToNode = node->distanceToCamera(*Application::getInstance()->getViewFrustum());
321 float boundary = boundaryDistanceForRenderLevel(node->getLevel());322 float boundary = boundaryDistanceForRenderLevel(node->getLevel());
322 float childBoundary = boundaryDistanceForRenderLevel(node->getLevel() + 1);323 float childBoundary = boundaryDistanceForRenderLevel(node->getLevel() + 1);
323 bool inBoundary = (distanceToNode <= boundary);324 bool inBoundary = (distanceToNode <= boundary);
@@ -352,7 +353,7 @@ int VoxelSystem::newTreeToArrays(VoxelNode* node) { · 28 unmodified lines
352 353
353int VoxelSystem::updateNodeInArraysAsFullVBO(VoxelNode* node) {354int VoxelSystem::updateNodeInArraysAsFullVBO(VoxelNode* node) {
354 // If we've run out of room, then just bail...355 // If we've run out of room, then just bail...
355 if (_voxelsInWriteArrays >= MAX_VOXELS_PER_SYSTEM) {356 if (_voxelsInWriteArrays >= _maxVoxels) {
356 return 0;357 return 0;
357 }358 }
358 359
@@ -382,7 +383,7 @@ int VoxelSystem::updateNodeInArraysAsFullVBO(VoxelNode* node) { · 23 unmodified lines
382 383
383int VoxelSystem::updateNodeInArraysAsPartialVBO(VoxelNode* node) {384int VoxelSystem::updateNodeInArraysAsPartialVBO(VoxelNode* node) {
384 // If we've run out of room, then just bail...385 // If we've run out of room, then just bail...
385 if (_voxelsInWriteArrays >= MAX_VOXELS_PER_SYSTEM) {386 if (_voxelsInWriteArrays >= _maxVoxels) {
386 return 0;387 return 0;
387 }388 }
388 389
@@ -425,6 +426,9 @@ int VoxelSystem::updateNodeInArraysAsPartialVBO(VoxelNode* node) { · 36 unmodified lines
425 return 0; // not-updated426 return 0; // not-updated
426}427}
427 428
429ProgramObject* VoxelSystem::_perlinModulateProgram = 0;
430GLuint VoxelSystem::_permutationNormalTextureID = 0;
431
428void VoxelSystem::init() {432void VoxelSystem::init() {
429 433
430 _renderWarningsOn = false;434 _renderWarningsOn = false;
@@ -440,23 +444,23 @@ void VoxelSystem::init() { · 9 unmodified lines
440 _unusedArraySpace = 0;444 _unusedArraySpace = 0;
441 445
442 // we will track individual dirty sections with these arrays of bools446 // we will track individual dirty sections with these arrays of bools
443 _writeVoxelDirtyArray = new bool[MAX_VOXELS_PER_SYSTEM];447 _writeVoxelDirtyArray = new bool[_maxVoxels];
444 memset(_writeVoxelDirtyArray, false, MAX_VOXELS_PER_SYSTEM * sizeof(bool));448 memset(_writeVoxelDirtyArray, false, _maxVoxels * sizeof(bool));
445 _readVoxelDirtyArray = new bool[MAX_VOXELS_PER_SYSTEM];449 _readVoxelDirtyArray = new bool[_maxVoxels];
446 memset(_readVoxelDirtyArray, false, MAX_VOXELS_PER_SYSTEM * sizeof(bool));450 memset(_readVoxelDirtyArray, false, _maxVoxels * sizeof(bool));
447 451
448 // prep the data structures for incoming voxel data452 // prep the data structures for incoming voxel data
449 _writeVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];453 _writeVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
450 _readVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];454 _readVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
451 455
452 _writeColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];456 _writeColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
453 _readColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];457 _readColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
454 458
455 GLuint* indicesArray = new GLuint[INDICES_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];459 GLuint* indicesArray = new GLuint[INDICES_PER_VOXEL * _maxVoxels];
456 460
457 // populate the indicesArray461 // populate the indicesArray
458 // this will not change given new voxels, so we can set it all up now462 // this will not change given new voxels, so we can set it all up now
459 for (int n = 0; n < MAX_VOXELS_PER_SYSTEM; n++) {463 for (int n = 0; n < _maxVoxels; n++) {
460 // fill the indices array464 // fill the indices array
461 int voxelIndexOffset = n * INDICES_PER_VOXEL;465 int voxelIndexOffset = n * INDICES_PER_VOXEL;
462 GLuint* currentIndicesPos = indicesArray + voxelIndexOffset;466 GLuint* currentIndicesPos = indicesArray + voxelIndexOffset;
@@ -468,11 +472,11 @@ void VoxelSystem::init() { · 5 unmodified lines
468 }472 }
469 }473 }
470 474
471 GLfloat* normalsArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];475 GLfloat* normalsArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
472 GLfloat* normalsArrayEndPointer = normalsArray;476 GLfloat* normalsArrayEndPointer = normalsArray;
473 477
474 // populate the normalsArray478 // populate the normalsArray
475 for (int n = 0; n < MAX_VOXELS_PER_SYSTEM; n++) {479 for (int n = 0; n < _maxVoxels; n++) {
476 for (int i = 0; i < VERTEX_POINTS_PER_VOXEL; i++) {480 for (int i = 0; i < VERTEX_POINTS_PER_VOXEL; i++) {
477 *(normalsArrayEndPointer++) = identityNormals[i];481 *(normalsArrayEndPointer++) = identityNormals[i];
478 }482 }
@@ -481,32 +485,35 @@ void VoxelSystem::init() { · 2 unmodified lines
481 // VBO for the verticesArray485 // VBO for the verticesArray
482 glGenBuffers(1, &_vboVerticesID);486 glGenBuffers(1, &_vboVerticesID);
483 glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID);487 glBindBuffer(GL_ARRAY_BUFFER, _vboVerticesID);
484 glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * MAX_VOXELS_PER_SYSTEM, NULL, GL_DYNAMIC_DRAW);488 glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels, NULL, GL_DYNAMIC_DRAW);
485 489
486 // VBO for the normalsArray490 // VBO for the normalsArray
487 glGenBuffers(1, &_vboNormalsID);491 glGenBuffers(1, &_vboNormalsID);
488 glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID);492 glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID);
489 glBufferData(GL_ARRAY_BUFFER,493 glBufferData(GL_ARRAY_BUFFER,
490 VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * MAX_VOXELS_PER_SYSTEM,494 VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels,
491 normalsArray, GL_STATIC_DRAW);495 normalsArray, GL_STATIC_DRAW);
492 496
493 // VBO for colorsArray497 // VBO for colorsArray
494 glGenBuffers(1, &_vboColorsID);498 glGenBuffers(1, &_vboColorsID);
495 glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID);499 glBindBuffer(GL_ARRAY_BUFFER, _vboColorsID);
496 glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte) * MAX_VOXELS_PER_SYSTEM, NULL, GL_DYNAMIC_DRAW);500 glBufferData(GL_ARRAY_BUFFER, VERTEX_POINTS_PER_VOXEL * sizeof(GLubyte) * _maxVoxels, NULL, GL_DYNAMIC_DRAW);
497 501
498 // VBO for the indicesArray502 // VBO for the indicesArray
499 glGenBuffers(1, &_vboIndicesID);503 glGenBuffers(1, &_vboIndicesID);
500 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID);504 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID);
501 glBufferData(GL_ELEMENT_ARRAY_BUFFER,505 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
502 INDICES_PER_VOXEL * sizeof(GLuint) * MAX_VOXELS_PER_SYSTEM,506 INDICES_PER_VOXEL * sizeof(GLuint) * _maxVoxels,
503 indicesArray, GL_STATIC_DRAW);507 indicesArray, GL_STATIC_DRAW);
504 508
505 // delete the indices and normals arrays that are no longer needed509 // delete the indices and normals arrays that are no longer needed
506 delete[] indicesArray;510 delete[] indicesArray;
507 delete[] normalsArray;511 delete[] normalsArray;
508 512
509 // create our simple fragment shader513 // create our simple fragment shader if we're the first system to init
514 if (_perlinModulateProgram != 0) {
515 return;
516 }
510 switchToResourcesParentIfRequired();517 switchToResourcesParentIfRequired();
511 _perlinModulateProgram = new ProgramObject();518 _perlinModulateProgram = new ProgramObject();
512 _perlinModulateProgram->addShaderFromSourceFile(QGLShader::Vertex, "resources/shaders/perlin_modulate.vert");519 _perlinModulateProgram->addShaderFromSourceFile(QGLShader::Vertex, "resources/shaders/perlin_modulate.vert");
@@ -661,7 +668,7 @@ void VoxelSystem::render(bool texture) { · 148 unmodified lines
661 668
662 // draw the number of voxels we have669 // draw the number of voxels we have
663 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID);670 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID);
664 glScalef(TREE_SCALE, TREE_SCALE, TREE_SCALE);671 glScalef(_treeScale, _treeScale, _treeScale);
665 glDrawRangeElementsEXT(GL_TRIANGLES, 0, VERTICES_PER_VOXEL * _voxelsInReadArrays - 1,672 glDrawRangeElementsEXT(GL_TRIANGLES, 0, VERTICES_PER_VOXEL * _voxelsInReadArrays - 1,
666 36 * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);673 36 * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
667 674
@@ -855,7 +862,7 @@ bool VoxelSystem::removeOutOfViewOperation(VoxelNode* node, void* extraData) { · 187 unmodified lines
855 for (int i = 0; i < NUMBER_OF_CHILDREN; i++) {862 for (int i = 0; i < NUMBER_OF_CHILDREN; i++) {
856 VoxelNode* childNode = node->getChildAtIndex(i);863 VoxelNode* childNode = node->getChildAtIndex(i);
857 if (childNode) {864 if (childNode) {
858 ViewFrustum::location inFrustum = childNode->inFrustum(*thisVoxelSystem->_viewFrustum);865 ViewFrustum::location inFrustum = childNode->inFrustum(*Application::getInstance()->getViewFrustum());
859 switch (inFrustum) {866 switch (inFrustum) {
860 case ViewFrustum::OUTSIDE: {867 case ViewFrustum::OUTSIDE: {
861 args->nodesOutside++;868 args->nodesOutside++;
@@ -907,9 +914,9 @@ bool VoxelSystem::hasViewChanged() { · 45 unmodified lines
907 }914 }
908 915
909 // If our viewFrustum has changed since our _lastKnowViewFrustum916 // If our viewFrustum has changed since our _lastKnowViewFrustum
910 if (_viewFrustum && !_lastStableViewFrustum.matches(_viewFrustum)) {917 if (!_lastStableViewFrustum.matches(Application::getInstance()->getViewFrustum())) {
911 result = true;918 result = true;
912 _lastStableViewFrustum = *_viewFrustum; // save last stable919 _lastStableViewFrustum = *Application::getInstance()->getViewFrustum(); // save last stable
913 }920 }
914 return result;921 return result;
915}922}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t