Changeset 2543 for GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp
- Timestamp:
- 08/21/07 19:37:28 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/Lib/Vis/Preprocessing/src/GlRenderer.cpp
r2539 r2543 5 5 #include "ViewCellsManager.h" 6 6 #include "SceneGraph.h" 7 //#include "ObjectPvs.h"8 7 #include "Viewcell.h" 9 8 #include "Beam.h" … … 71 70 ViewCellsManager *viewCellsManager, 72 71 KdTree *tree): 73 Renderer(sceneGraph, viewCellsManager), 74 mKdTree(tree), 75 mUseFalseColors(false), 76 mVboId(-1), 77 mData(NULL), 78 mIndices(NULL), 79 mUseVbos(true) 80 //mUseVbos(false) 81 { 82 mSceneGraph->CollectObjects(&mObjects); 83 84 // mViewCellsManager->GetViewPoint(mViewPoint); 85 86 viewCellsManager->GetViewPoint(mViewPoint); 87 //mSceneGraph->GetBox().Center(); 88 mViewDirection = Vector3(0,0,1); 89 90 // mViewPoint = Vector3(991.7, 187.8, -271); 91 // mViewDirection = Vector3(0.9, 0, -0.4); 92 93 // timerId = startTimer(10); 94 // debug coords for atlanta 95 // mViewPoint = Vector3(3473, 6.778, -1699); 96 // mViewDirection = Vector3(-0.2432, 0, 0.97); 97 98 mFrame = 0; 99 mWireFrame = false; 100 Environment::GetSingleton()->GetBoolValue("Preprocessor.detectEmptyViewSpace", 101 mDetectEmptyViewSpace); 102 mSnapErrorFrames = true; 103 mSnapPrefix = "snap/"; 104 mUseForcedColors = false; 105 mRenderBoxes = false; 106 mUseGlLists = true; 107 //mUseGlLists = false; 108 109 if (mViewCellsManager->GetViewCellPoints()->size()) 110 mPvsStatFrames = (int)mViewCellsManager->GetViewCellPoints()->size(); 111 else 112 Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", mPvsStatFrames); 113 114 115 mPvsErrorBuffer.resize(mPvsStatFrames); 116 ClearErrorBuffer(); 72 Renderer(sceneGraph, viewCellsManager), 73 mKdTree(tree), 74 mUseFalseColors(false), 75 mVboId(-1), 76 mData(NULL), 77 mIndices(NULL), 78 mUseVbos(true), 79 mCurrentFrame(-1) 80 { 81 mSceneGraph->CollectObjects(&mObjects); 82 83 viewCellsManager->GetViewPoint(mViewPoint); 84 85 mViewDirection = Vector3(0,0,1); 86 87 mFrame = 0; 88 mWireFrame = false; 89 Environment::GetSingleton()->GetBoolValue("Preprocessor.detectEmptyViewSpace", 90 mDetectEmptyViewSpace); 91 mSnapErrorFrames = true; 92 mSnapPrefix = "snap/"; 93 mUseForcedColors = false; 94 mRenderBoxes = false; 95 //mUseGlLists = true; 96 mUseGlLists = false; 97 98 if (mViewCellsManager->GetViewCellPoints()->size()) 99 mPvsStatFrames = (int)mViewCellsManager->GetViewCellPoints()->size(); 100 else 101 Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", mPvsStatFrames); 102 103 104 mPvsErrorBuffer.resize(mPvsStatFrames); 105 ClearErrorBuffer(); 117 106 } 118 107 … … 120 109 GlRenderer::~GlRenderer() 121 110 { 122 cerr<<"gl renderer destructor..\n"; 123 124 CLEAR_CONTAINER(mOcclusionQueries); 125 126 DeleteVbos(); 127 128 if (mData) delete [] mData; 129 if (mIndices) delete [] mIndices; 130 131 glDeleteBuffersARB(1, &mVboId); 132 cerr<<"done."<<endl; 133 } 134 135 136 void 137 GlRenderer::RenderTriangle(TriangleIntersectable *object) 138 { 139 Triangle3 &t = object->GetItem(); 140 141 glBegin(GL_TRIANGLES); 142 Vector3 normal = t.GetNormal(); 143 glNormal3f(normal.x, normal.y, normal.z); 144 glVertex3f(t.mVertices[0].x, t.mVertices[0].y, t.mVertices[0].z); 145 glVertex3f(t.mVertices[1].x, t.mVertices[1].y, t.mVertices[1].z); 146 glVertex3f(t.mVertices[2].x, t.mVertices[2].y, t.mVertices[2].z); 147 glEnd(); 148 } 149 150 void 151 GlRenderer::RenderIntersectable(Intersectable *object) 152 { 153 if (!object) 154 return; 155 156 if (object->Mailed()) 157 return; 158 159 object->Mail(); 160 161 glPushAttrib(GL_CURRENT_BIT); 162 if (mUseFalseColors) 163 SetupFalseColor(object->mId); 164 165 switch (object->Type()) { 166 case Intersectable::MESH_INSTANCE: 167 RenderMeshInstance((MeshInstance *)object); 168 break; 169 case Intersectable::VIEW_CELL: 170 RenderViewCell(static_cast<ViewCell *>(object)); 171 break; 172 case Intersectable::TRANSFORMED_MESH_INSTANCE: 173 RenderTransformedMeshInstance(static_cast<TransformedMeshInstance *>(object)); 174 break; 175 case Intersectable::TRIANGLE_INTERSECTABLE: 176 RenderTriangle(static_cast<TriangleIntersectable *>(object)); 177 break; 178 case Intersectable::BVH_INTERSECTABLE: { 179 180 BvhNode *node = static_cast<BvhNode *>(object); 181 182 if (mRenderBoxes) 183 RenderBox(node->GetBoundingBox()); 184 else 185 RenderBvhNode(node); 186 break; 187 } 188 case Intersectable::KD_INTERSECTABLE: { 189 KdNode *node = (static_cast<KdIntersectable *>(object))->GetItem(); 190 191 if (mRenderBoxes) 192 RenderBox(mKdTree->GetBox(node)); 193 else 194 RenderKdNode(node); 195 break; 196 } 197 198 default: 199 cerr<<"Rendering this object not yet implemented\n"; 200 break; 201 } 202 203 glPopAttrib(); 204 } 205 206 void 207 GlRenderer::RenderRays(const VssRayContainer &rays) 208 { 209 VssRayContainer::const_iterator it = rays.begin(), it_end = rays.end(); 210 211 glBegin(GL_LINES); 212 for (; it != it_end; ++it) { 213 VssRay *ray = *it; 214 float importance = log10(1e3*ray->mWeightedPvsContribution)/3.0f; 215 // cout<<"w="<<ray->mWeightedPvsContribution<<" r="<<ray->mWeightedPvsContribution; 216 glColor3f(importance, importance, importance); 217 glVertex3fv(&ray->mOrigin.x); 218 glVertex3fv(&ray->mTermination.x); 219 } 220 glEnd(); 221 } 222 223 224 225 void 226 GlRenderer::RenderViewCell(ViewCell *vc) 227 { 228 if (vc->GetMesh()) { 229 230 if (!mUseFalseColors) { 231 if (vc->GetValid()) 232 glColor3f(0,1,0); 233 else 234 glColor3f(0,0,1); 235 } 236 237 RenderMesh(vc->GetMesh()); 238 } else { 239 // render viewcells in the subtree 240 if (!vc->IsLeaf()) { 241 ViewCellInterior *vci = (ViewCellInterior *) vc; 242 243 ViewCellContainer::iterator it = vci->mChildren.begin(); 244 for (; it != vci->mChildren.end(); ++it) { 245 RenderViewCell(*it); 246 } 247 } else { 248 // cerr<<"Empty viewcell mesh\n"; 249 } 250 } 111 cerr<<"gl renderer destructor..\n"; 112 113 CLEAR_CONTAINER(mOcclusionQueries); 114 115 DeleteVbos(); 116 117 if (mData) delete [] mData; 118 if (mIndices) delete [] mIndices; 119 120 glDeleteBuffersARB(1, &mVboId); 121 cerr<<"done."<<endl; 122 } 123 124 125 void GlRenderer::RenderTriangle(TriangleIntersectable *object) 126 { 127 Triangle3 &t = object->GetItem(); 128 129 glBegin(GL_TRIANGLES); 130 Vector3 normal = t.GetNormal(); 131 glNormal3f(normal.x, normal.y, normal.z); 132 glVertex3f(t.mVertices[0].x, t.mVertices[0].y, t.mVertices[0].z); 133 glVertex3f(t.mVertices[1].x, t.mVertices[1].y, t.mVertices[1].z); 134 glVertex3f(t.mVertices[2].x, t.mVertices[2].y, t.mVertices[2].z); 135 glEnd(); 136 } 137 138 139 void GlRenderer::RenderIntersectable(Intersectable *object) 140 { 141 if (!object) 142 return; 143 144 if (object->mRenderedFrame == mCurrentFrame) 145 return; 146 147 object->mRenderedFrame = mCurrentFrame; 148 149 //if (object->Mailed()) return; 150 //object->Mail(); 151 152 glPushAttrib(GL_CURRENT_BIT); 153 154 if (mUseFalseColors) 155 SetupFalseColor(object->mId); 156 157 switch (object->Type()) 158 { 159 case Intersectable::MESH_INSTANCE: 160 RenderMeshInstance((MeshInstance *)object); 161 break; 162 case Intersectable::VIEW_CELL: 163 RenderViewCell(static_cast<ViewCell *>(object)); 164 break; 165 case Intersectable::TRANSFORMED_MESH_INSTANCE: 166 RenderTransformedMeshInstance(static_cast<TransformedMeshInstance *>(object)); 167 break; 168 case Intersectable::TRIANGLE_INTERSECTABLE: 169 RenderTriangle(static_cast<TriangleIntersectable *>(object)); 170 break; 171 case Intersectable::BVH_INTERSECTABLE: 172 { 173 BvhNode *node = static_cast<BvhNode *>(object); 174 175 if (mRenderBoxes) 176 RenderBox(node->GetBoundingBox()); 177 else 178 RenderBvhNode(node); 179 break; 180 } 181 case Intersectable::KD_INTERSECTABLE: 182 { 183 KdNode *node = (static_cast<KdIntersectable *>(object))->GetItem(); 184 185 if (mRenderBoxes) 186 RenderBox(mKdTree->GetBox(node)); 187 else 188 RenderKdNode(node); 189 break; 190 } 191 192 default: 193 cerr<<"Rendering this object not yet implemented\n"; 194 break; 195 } 196 197 glPopAttrib(); 198 } 199 200 201 void GlRenderer::RenderRays(const VssRayContainer &rays) 202 { 203 VssRayContainer::const_iterator it = rays.begin(), it_end = rays.end(); 204 205 glBegin(GL_LINES); 206 207 for (; it != it_end; ++it) 208 { 209 VssRay *ray = *it; 210 const float importance = log10(1e3*ray->mWeightedPvsContribution)/3.0f; 211 212 glColor3f(importance, importance, importance); 213 glVertex3fv(&ray->mOrigin.x); 214 glVertex3fv(&ray->mTermination.x); 215 } 216 217 glEnd(); 218 } 219 220 221 void GlRenderer::RenderViewCell(ViewCell *vc) 222 { 223 if (vc->GetMesh()) 224 { 225 if (!mUseFalseColors) 226 { 227 if (vc->GetValid()) 228 glColor3f(0,1,0); 229 else 230 glColor3f(0,0,1); 231 } 232 233 RenderMesh(vc->GetMesh()); 234 } 235 else 236 { 237 // render viewcells in the subtree 238 if (!vc->IsLeaf()) 239 { 240 ViewCellInterior *vci = (ViewCellInterior *) vc; 241 242 ViewCellContainer::iterator it = vci->mChildren.begin(); 243 for (; it != vci->mChildren.end(); ++it) 244 { 245 RenderViewCell(*it); 246 } 247 } 248 else 249 { 250 // cerr<<"Empty viewcell mesh\n"; 251 } 252 } 251 253 } 252 254 … … 266 268 267 269 glPushMatrix(); 268 /* cout << "\n";269 for (int i = 0; i < 4; ++ i)270 for (int j = 0; j < 4; ++ j)271 cout << m.x[i][j] << " "; cout << "\n"*/272 270 273 271 glMultMatrixf((float *)m.x); 274 272 275 /*GLfloat dummy[16];276 glGetFloatv(GL_MODELVIEW_MATRIX, dummy);277 for (int i = 0; i < 16; ++ i)278 cout << dummy[i] << " ";279 cout << endl;*/280 273 RenderMesh(mi->GetMesh()); 281 274 … … 308 301 309 302 310 void 311 GlRenderer::RenderMesh(Mesh *mesh) 312 { 313 int i = 0; 314 315 if (!mUseFalseColors && !mUseForcedColors) 316 SetupMaterial(mesh->mMaterial); 317 318 for (i=0; i < mesh->mFaces.size(); i++){319 if (mWireFrame)320 321 else322 323 324 Face *face = mesh->mFaces[i];325 for (int j = 0; j < face->mVertexIndices.size(); j++) {326 327 }328 glEnd();329 303 void GlRenderer::RenderMesh(Mesh *mesh) 304 { 305 int i = 0; 306 307 if (!mUseFalseColors && !mUseForcedColors) 308 SetupMaterial(mesh->mMaterial); 309 310 for (i=0; i < mesh->mFaces.size(); i++) 311 { 312 if (mWireFrame) 313 glBegin(GL_LINE_LOOP); 314 else 315 glBegin(GL_POLYGON); 316 317 Face *face = mesh->mFaces[i]; 318 for (int j = 0; j < face->mVertexIndices.size(); j++) { 319 glVertex3fv(&mesh->mVertices[face->mVertexIndices[j]].x); 320 } 321 glEnd(); 322 } 330 323 } 331 324 … … 758 751 ) 759 752 { 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 { 782 783 784 } 785 786 787 788 789 790 791 792 753 // choose a random view point 754 mViewCellsManager->GetViewPoint(mViewPoint); 755 sample.mPosition = mViewPoint; 756 //cout << "viewpoint: " << mViewPoint << endl; 757 758 // take a render cost sample by rendering a cube 759 Vector3 directions[6]; 760 761 directions[0] = Vector3(1,0,0); 762 directions[1] = Vector3(0,1,0); 763 directions[2] = Vector3(0,0,1); 764 directions[3] = Vector3(-1,0,0); 765 directions[4] = Vector3(0,-1,0); 766 directions[5] = Vector3(0,0,-1); 767 768 sample.mVisibleObjects = 0; 769 770 // reset object counters 771 ObjectContainer::const_iterator it, it_end = mObjects.end(); 772 773 for (it = mObjects.begin(); it != it_end; ++ it) 774 { 775 (*it)->mCounter = 0; 776 777 } 778 779 ++ mFrame; 780 781 //glCullFace(GL_FRONT); 782 glCullFace(GL_BACK); 783 glDisable(GL_CULL_FACE); 784 785 793 786 // query all 6 directions for a full point sample 794 795 { 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 787 for (int i = 0; i < 6; ++ i) 788 { 789 mViewDirection = directions[i]; 790 SetupCamera(); 791 792 glClearColor(1.0f, 1.0f, 1.0f, 1.0f); 793 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 794 //glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); glDepthMask(GL_TRUE); 795 glDepthFunc(GL_LESS); 796 797 mUseFalseColors = true; 798 799 // the actual scene rendering fills the depth (for occlusion queries) 800 // and the frame buffer (for item buffer) 801 RenderScene(); 802 803 804 if (0) 812 805 { 813 814 815 816 817 } 818 819 820 806 char filename[256]; 807 sprintf(filename, "snap/frame-%04d-%d.png", mFrame, i); 808 // QImage im = toImage(); 809 // im.save(filename, "PNG"); 810 } 811 812 // evaluate the sample 813 if (useOcclusionQueries) 821 814 { 822 823 } 824 815 EvalQueryWithOcclusionQueries(); 816 } 817 else 825 818 { 826 819 EvalQueryWithItemBuffer(); 827 820 } 828 821 } 829 830 831 832 833 834 835 { 836 837 822 823 // now evaluate the statistics over that sample 824 // currently only the number of visible objects is taken into account 825 sample.Reset(); 826 827 for (it = mObjects.begin(); it != it_end; ++ it) 828 { 829 Intersectable *obj = *it; 830 if (obj->mCounter >= threshold) 838 831 { 839 840 841 } 842 } 843 844 832 ++ sample.mVisibleObjects; 833 sample.mVisiblePixels += obj->mCounter; 834 } 835 } 836 837 //cout << "RS=" << sample.mVisibleObjects << " "; 845 838 } 846 839 … … 856 849 #endif 857 850 #endif 858 851 859 852 } 860 853 … … 867 860 ) 868 861 { 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 } 900 862 MakeCurrent(); 863 864 if (mPixelBuffer == NULL) 865 mPixelBuffer = new unsigned int[GetWidth()*GetHeight()]; 866 867 // using 90 degree projection to capture 360 view with 6 samples 868 SetupProjection(GetHeight(), GetHeight(), 90.0f); 869 870 //samples.resize(numSamples); 871 halton.Reset(); 872 873 // the number of queries queried in batch mode 874 const int numQ = 500; 875 876 //const int numQ = (int)mObjects.size(); 877 if (useOcclusionQueries) 878 { 879 cout << "\ngenerating " << numQ << " queries ... "; 880 OcclusionQuery::GenQueries(mOcclusionQueries, numQ); 881 cout << "finished" << endl; 882 } 883 884 // sampling queries 885 for (int i = 0; i < numSamples; ++ i) 886 { 887 cout << "."; 888 EvalRenderCostSample(samples[i], useOcclusionQueries, threshold); 889 } 890 891 DoneCurrent(); 892 } 893 901 894 902 895 … … 1500 1493 GlRenderer::ValidViewPoint() 1501 1494 { 1502 1495 //cout<<"VV4 "; 1503 1496 if (!mDetectEmptyViewSpace) 1504 1497 return true; 1505 1498 //cout << "vp: " << mViewPoint << " dir: " << mViewDirection << endl; 1506 1499 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 return false; // backfacing polygon found -> not a valid viewspace sample1549 1550 1551 } 1552 1553 float 1554 GlRenderer::GetPixelError(int &pvsSize)1500 OcclusionQuery *query = mOcclusionQueries[0]; 1501 1502 // now check whether any backfacing polygon would pass the depth test 1503 SetupCamera(); 1504 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 1505 glEnable( GL_CULL_FACE ); 1506 glCullFace(GL_BACK); 1507 1508 //cout<<"VV1 "; 1509 RenderScene(); 1510 1511 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); 1512 glDepthMask(GL_FALSE); 1513 glDisable( GL_CULL_FACE ); 1514 1515 query->BeginQuery(); 1516 1517 // cout<<"VV2 "; 1518 RenderScene(); 1519 // cout<<"VV3 "; 1520 1521 query->EndQuery(); 1522 1523 // at this point, if possible, go and do some other computation 1524 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 1525 glDepthMask(GL_TRUE); 1526 glEnable( GL_CULL_FACE ); 1527 1528 // int wait = 0; 1529 // while (!query.ResultAvailable()) { 1530 // wait++; 1531 // } 1532 1533 // reenable other state 1534 unsigned int pixelCount = query->GetQueryResult(); 1535 // cout<<"VV4 "; 1536 1537 //cout<<"count: " << pixelCount<<endl; 1538 1539 if (pixelCount > 0) 1540 { 1541 return false; // backfacing polygon found -> not a valid viewspace sample 1542 } 1543 return true; 1544 } 1545 1546 1547 float GlRenderer::GetPixelError(int &pvsSize) 1555 1548 { 1556 1549 return -1.0f; 1557 1550 } 1558 1551 1559 void 1560 GlRenderer::RenderViewPoint()1552 1553 void GlRenderer::RenderViewPoint() 1561 1554 { 1562 1555 mWireFrame = true;
Note: See TracChangeset
for help on using the changeset viewer.