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
1919 #include <PerfStat.h>
2020 #include <OctalCode.h>
2121 #include <pthread.h>
22+#include "Application.h"
2223 #include "Log.h"
2324 #include "VoxelConstants.h"
2425 #include "InterfaceConfig.h"
@@ -37,14 +38,15 @@ GLfloat identityNormals[] = { 0,0,-1, 0,0,-1, 0,0,-1, 0,0,-1, · 12 unmodified lines
3738 -1,0,0, +1,0,0, +1,0,0, -1,0,0,
3839 -1,0,0, +1,0,0, +1,0,0, -1,0,0 };
3940
40−GLubyte identityIndices[] = { 0,2,1, 0,3,2, // Z- .
41+GLubyte identityIndices[] = { 0,2,1, 0,3,2, // Z-
4142 8,9,13, 8,13,12, // Y-
4243 16,23,19, 16,20,23, // X-
4344 17,18,22, 17,22,21, // X+
4445 10,11,15, 10,15,14, // Y+
45− 4,5,6, 4,6,7 }; // Z+ .
46+ 4,5,6, 4,6,7 }; // Z+
4647
47−VoxelSystem::VoxelSystem() : AgentData(NULL) {
48+VoxelSystem::VoxelSystem(float treeScale, int maxVoxels) :
49+ AgentData(NULL), _treeScale(treeScale), _maxVoxels(maxVoxels) {
4850 _voxelsInReadArrays = _voxelsInWriteArrays = _voxelsUpdated = 0;
4951 _writeRenderFullVBO = true;
5052 _readRenderFullVBO = true;
@@ -312,12 +314,11 @@ void VoxelSystem::copyWrittenDataToReadArrays(bool fullVBOs) { · 261 unmodified lines
312314 }
313315
314316 int VoxelSystem::newTreeToArrays(VoxelNode* node) {
315− assert(_viewFrustum); // you must set up _viewFrustum before calling this
316317 int voxelsUpdated = 0;
317318 bool shouldRender = false; // assume we don't need to render it
318319 // if it's colored, we might need to render it!
319320 if (node->isColored()) {
320− float distanceToNode = node->distanceToCamera(*_viewFrustum);
321+ float distanceToNode = node->distanceToCamera(*Application::getInstance()->getViewFrustum());
321322 float boundary = boundaryDistanceForRenderLevel(node->getLevel());
322323 float childBoundary = boundaryDistanceForRenderLevel(node->getLevel() + 1);
323324 bool inBoundary = (distanceToNode <= boundary);
@@ -352,7 +353,7 @@ int VoxelSystem::newTreeToArrays(VoxelNode* node) { · 28 unmodified lines
352353
353354 int VoxelSystem::updateNodeInArraysAsFullVBO(VoxelNode* node) {
354355 // If we've run out of room, then just bail...
355− if (_voxelsInWriteArrays >= MAX_VOXELS_PER_SYSTEM) {
356+ if (_voxelsInWriteArrays >= _maxVoxels) {
356357 return 0;
357358 }
358359
@@ -382,7 +383,7 @@ int VoxelSystem::updateNodeInArraysAsFullVBO(VoxelNode* node) { · 23 unmodified lines
382383
383384 int VoxelSystem::updateNodeInArraysAsPartialVBO(VoxelNode* node) {
384385 // If we've run out of room, then just bail...
385− if (_voxelsInWriteArrays >= MAX_VOXELS_PER_SYSTEM) {
386+ if (_voxelsInWriteArrays >= _maxVoxels) {
386387 return 0;
387388 }
388389
@@ -425,6 +426,9 @@ int VoxelSystem::updateNodeInArraysAsPartialVBO(VoxelNode* node) { · 36 unmodified lines
425426 return 0; // not-updated
426427 }
427428
429+ProgramObject* VoxelSystem::_perlinModulateProgram = 0;
430+GLuint VoxelSystem::_permutationNormalTextureID = 0;
431+
428432 void VoxelSystem::init() {
429433
430434 _renderWarningsOn = false;
@@ -440,23 +444,23 @@ void VoxelSystem::init() { · 9 unmodified lines
440444 _unusedArraySpace = 0;
441445
442446 // we will track individual dirty sections with these arrays of bools
443− _writeVoxelDirtyArray = new bool[MAX_VOXELS_PER_SYSTEM];
444− memset(_writeVoxelDirtyArray, false, MAX_VOXELS_PER_SYSTEM * sizeof(bool));
445− _readVoxelDirtyArray = new bool[MAX_VOXELS_PER_SYSTEM];
446− memset(_readVoxelDirtyArray, false, MAX_VOXELS_PER_SYSTEM * sizeof(bool));
447+ _writeVoxelDirtyArray = new bool[_maxVoxels];
448+ memset(_writeVoxelDirtyArray, false, _maxVoxels * sizeof(bool));
449+ _readVoxelDirtyArray = new bool[_maxVoxels];
450+ memset(_readVoxelDirtyArray, false, _maxVoxels * sizeof(bool));
447451
448452 // prep the data structures for incoming voxel data
449− _writeVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];
450− _readVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];
453+ _writeVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
454+ _readVerticesArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
451455
452− _writeColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];
453− _readColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];
456+ _writeColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
457+ _readColorsArray = new GLubyte[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
454458
455− GLuint* indicesArray = new GLuint[INDICES_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];
459+ GLuint* indicesArray = new GLuint[INDICES_PER_VOXEL * _maxVoxels];
456460
457461 // populate the indicesArray
458462 // 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++) {
460464 // fill the indices array
461465 int voxelIndexOffset = n * INDICES_PER_VOXEL;
462466 GLuint* currentIndicesPos = indicesArray + voxelIndexOffset;
@@ -468,11 +472,11 @@ void VoxelSystem::init() { · 5 unmodified lines
468472 }
469473 }
470474
471− GLfloat* normalsArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * MAX_VOXELS_PER_SYSTEM];
475+ GLfloat* normalsArray = new GLfloat[VERTEX_POINTS_PER_VOXEL * _maxVoxels];
472476 GLfloat* normalsArrayEndPointer = normalsArray;
473477
474478 // populate the normalsArray
475− for (int n = 0; n < MAX_VOXELS_PER_SYSTEM; n++) {
479+ for (int n = 0; n < _maxVoxels; n++) {
476480 for (int i = 0; i < VERTEX_POINTS_PER_VOXEL; i++) {
477481 *(normalsArrayEndPointer++) = identityNormals[i];
478482 }
@@ -481,32 +485,35 @@ void VoxelSystem::init() { · 2 unmodified lines
481485 // VBO for the verticesArray
482486 glGenBuffers(1, &_vboVerticesID);
483487 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);
485489
486490 // VBO for the normalsArray
487491 glGenBuffers(1, &_vboNormalsID);
488492 glBindBuffer(GL_ARRAY_BUFFER, _vboNormalsID);
489493 glBufferData(GL_ARRAY_BUFFER,
490− VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * MAX_VOXELS_PER_SYSTEM,
494+ VERTEX_POINTS_PER_VOXEL * sizeof(GLfloat) * _maxVoxels,
491495 normalsArray, GL_STATIC_DRAW);
492496
493497 // VBO for colorsArray
494498 glGenBuffers(1, &_vboColorsID);
495499 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);
497501
498502 // VBO for the indicesArray
499503 glGenBuffers(1, &_vboIndicesID);
500504 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID);
501505 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
502− INDICES_PER_VOXEL * sizeof(GLuint) * MAX_VOXELS_PER_SYSTEM,
506+ INDICES_PER_VOXEL * sizeof(GLuint) * _maxVoxels,
503507 indicesArray, GL_STATIC_DRAW);
504508
505509 // delete the indices and normals arrays that are no longer needed
506510 delete[] indicesArray;
507511 delete[] normalsArray;
508512
509− // create our simple fragment shader
513+ // create our simple fragment shader if we're the first system to init
514+ if (_perlinModulateProgram != 0) {
515+ return;
516+ }
510517 switchToResourcesParentIfRequired();
511518 _perlinModulateProgram = new ProgramObject();
512519 _perlinModulateProgram->addShaderFromSourceFile(QGLShader::Vertex, "resources/shaders/perlin_modulate.vert");
@@ -661,7 +668,7 @@ void VoxelSystem::render(bool texture) { · 148 unmodified lines
661668
662669 // draw the number of voxels we have
663670 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndicesID);
664− glScalef(TREE_SCALE, TREE_SCALE, TREE_SCALE);
671+ glScalef(_treeScale, _treeScale, _treeScale);
665672 glDrawRangeElementsEXT(GL_TRIANGLES, 0, VERTICES_PER_VOXEL * _voxelsInReadArrays - 1,
666673 36 * _voxelsInReadArrays, GL_UNSIGNED_INT, 0);
667674
@@ -855,7 +862,7 @@ bool VoxelSystem::removeOutOfViewOperation(VoxelNode* node, void* extraData) { · 187 unmodified lines
855862 for (int i = 0; i < NUMBER_OF_CHILDREN; i++) {
856863 VoxelNode* childNode = node->getChildAtIndex(i);
857864 if (childNode) {
858− ViewFrustum::location inFrustum = childNode->inFrustum(*thisVoxelSystem->_viewFrustum);
865+ ViewFrustum::location inFrustum = childNode->inFrustum(*Application::getInstance()->getViewFrustum());
859866 switch (inFrustum) {
860867 case ViewFrustum::OUTSIDE: {
861868 args->nodesOutside++;
@@ -907,9 +914,9 @@ bool VoxelSystem::hasViewChanged() { · 45 unmodified lines
907914 }
908915
909916 // If our viewFrustum has changed since our _lastKnowViewFrustum
910− if (_viewFrustum && !_lastStableViewFrustum.matches(_viewFrustum)) {
917+ if (!_lastStableViewFrustum.matches(Application::getInstance()->getViewFrustum())) {
911918 result = true;
912− _lastStableViewFrustum = *_viewFrustum; // save last stable
919+ _lastStableViewFrustum = *Application::getInstance()->getViewFrustum(); // save last stable
913920 }
914921 return result;
915922 }

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