Changeset 540 for trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp
- Timestamp:
- 01/15/06 04:23:51 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp
r538 r540 16 16 static CGprofile sCgFragmentProfile; 17 17 18 GLuint depthMap; 18 GLuint frontDepthMap; 19 GLuint backDepthMap; 20 19 21 const int depthMapSize = 512; 20 22 static vector<int> sQueries; … … 99 101 } 100 102 103 104 int GlRenderer::GetId(int r, int g, int b) const 105 { 106 return r + (g << 8) + (b << 16); 107 } 108 101 109 void 102 110 GlRenderer::SetupMaterial(Material *m) … … 152 160 glGetOcclusionQueryuivNV = (PFNGLGETOCCLUSIONQUERYUIVNVPROC) 153 161 wglGetProcAddress("glGetOcclusionQueryuivNV"); 154 155 // initialise second depth buffer texture156 glGenTextures(1, &depthMap);157 glBindTexture(GL_TEXTURE_2D, depthMap);158 159 glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, depthMapSize,160 depthMapSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);161 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);162 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);163 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);164 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);165 166 // cg initialization167 cgSetErrorCallback(handleCgError);168 sCgContext = cgCreateContext();169 170 if (cgGLIsProfileSupported(CG_PROFILE_ARBFP1))171 sCgFragmentProfile = CG_PROFILE_ARBFP1;172 else {173 // try FP30174 if (cgGLIsProfileSupported(CG_PROFILE_FP30))175 sCgFragmentProfile = CG_PROFILE_FP30;176 else {177 fprintf(stderr, "Neither arbfp1 or fp30 fragment profiles supported on this system.\n");178 exit(1);179 }180 }181 182 sCgFragmentProgram = cgCreateProgramFromFile(sCgContext,183 CG_SOURCE, "../src/dual_depth.cg",184 sCgFragmentProfile,185 NULL,186 NULL);187 188 if (!cgIsProgramCompiled(sCgFragmentProgram))189 cgCompileProgram(sCgFragmentProgram);190 191 cgGLLoadProgram(sCgFragmentProgram);192 cgGLBindProgram(sCgFragmentProgram);193 // Debug << "LAST LISTING----" << cgGetLastListing(sCgContext) << "----\n";194 195 Debug << "---- PROGRAM BEGIN ----\n" <<196 cgGetProgramString(sCgFragmentProgram, CG_COMPILED_PROGRAM) << "---- PROGRAM END ----\n";197 162 } 198 163 … … 617 582 BeamSampleStatistics &stat) 618 583 { 584 // create beam mesh if not already doen 585 //TODO: should this be done here? 586 587 beam.CreateMesh(2.0f * Magnitude(mSceneGraph->GetBox().Diagonal())); 588 619 589 // TODO: should not be done every time here 620 590 // only back faces are interesting for the depth pass … … 636 606 // would distribute the 'efective viewpoints' of the object surface and thus 637 607 // with a few viewpoints better sample the vipoint space.... 638 639 int viewPointSamples = sqrt((float)desiredSamples); 640 608 //TODO: remove 609 //int viewPointSamples = sqrt((float)desiredSamples); 610 int viewPointSamples = max(desiredSamples / (GetWidth() * GetHeight()), 1); 611 641 612 // the number of direction samples per pass is given by the number of viewpoints 642 int directionalSamples = desiredSamples/viewPointSamples; 643 613 int directionalSamples = desiredSamples / viewPointSamples; 614 615 Debug << "directional samples: " << directionalSamples << endl; 644 616 for (int i = 0; i < viewPointSamples; ++ i) 645 617 { … … 674 646 const Vector3 viewPoint, 675 647 Beam &beam, 676 const int desiredSamples,648 const int samples, 677 649 BeamSampleStatistics &stat) 678 650 { 679 680 glViewport(0, 0, desiredSamples, desiredSamples);651 // 1. setup the view port to match the desired samples 652 glViewport(0, 0, GetWidth(), GetHeight()); 681 653 682 654 // 2. setup the projection matrix and view matrix to match the viewpoint + beam.mDirBox 683 655 SetupProjectionForViewPoint(viewPoint, beam, sourceObject); 684 656 685 657 // 3. reset z-buffer to 0 and render the source object for the beam 686 658 // with glCullFace(Enabled) and glFrontFace(GL_CW) 687 659 // save result to depth map 660 661 // front depth buffer must bé initialised to 0 662 float clearDepth; 663 glGetFloatv(GL_DEPTH_CLEAR_VALUE, &clearDepth); 664 glClearDepth(0.0f); 665 666 688 667 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); 668 //glFrontFace(GL_CW); 689 669 glEnable(GL_CULL_FACE); 690 glEnable(GL_STENCIL_TEST);691 670 glCullFace(GL_FRONT); 692 671 glColorMask(0, 0, 0, 0); 693 672 673 694 674 // stencil is increased where the source object is located 675 glEnable(GL_STENCIL_TEST); 695 676 glStencilFunc(GL_ALWAYS, 0x1, 0x1); 696 glStencilOp(GL_ INCR, GL_INCR, GL_INCR);677 glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); 697 678 698 679 #if 0 … … 713 694 714 695 #endif 696 // reset clear function 697 glClearDepth(clearDepth); 698 699 // copy contents of depth buffer into depth texture 700 // depth buffer holds ray origins 701 glBindTexture(GL_TEXTURE_2D, frontDepthMap); 702 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, depthMapSize, depthMapSize); 703 704 705 #if 0 706 // only the samples which are inside the beam AND on the source object 707 // should be considered => only where stencil == 2 708 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 709 glStencilFunc(GL_ALWAYS, 0x2, 0x2); 710 glStencilOp(GL_INCR, GL_INCR, GL_INCR); 711 712 // render back face of frustum beam 713 RenderMesh(beam.mMesh); 714 #endif 715 715 716 716 717 // 4. set back to normal rendering and clear the ray termination depth buffer 717 718 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 718 719 glColorMask(1, 1, 1, 1); 719 720 glDepthMask(1); 721 glEnable(GL_DEPTH_TEST); 722 glDisable(GL_STENCIL_TEST); 723 glEnable(GL_CULL_FACE); 724 glCullFace(GL_BACK); 725 720 726 // 5. render all objects inside the beam using id based false color 721 727 // (objects can be compiled to a gl list now so that subsequent rendering for … … 743 749 ObjectContainer::const_iterator it, it_end = beam.mObjects.end(); 744 750 for (it = beam.mObjects.begin(); it != it_end; ++ it) 745 RenderIntersectable(*it); 751 { 752 if (*it != sourceObject) 753 RenderIntersectable(*it); 754 } 746 755 747 756 glEndList(); … … 750 759 #else 751 760 ObjectContainer::const_iterator it, it_end = beam.mObjects.end(); 752 for (it = beam.mObjects.begin(); it != it_end; ++ it) 761 for (it = beam.mObjects.begin(); it != it_end; ++ it) 762 { 763 if (*it != sourceObject) 753 764 RenderIntersectable(*it); 765 } 754 766 #endif 755 767 756 // remove objects again 757 if (beam.mFlags & !Beam::STORE_OBJECTS) 758 beam.mObjects.clear(); 759 760 // bind ray origin depth buffer 761 glBindTexture(GL_TEXTURE_2D, depthMap); 762 glEnable(GL_TEXTURE_2D); 763 764 765 //Read the depth buffer into the shadow map texture 766 glBindTexture(GL_TEXTURE_2D, depthMap); 767 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, depthMapSize, depthMapSize); 768 769 770 // 6. Now we have a two depth buffers defining the ray origin and termination 768 769 // 6. Now we have a two depth buffers defining the ray origin and termination 771 770 // only rays which have non-zero entry in the origin buffer are valid since 772 771 // they realy start on the object surface (this can also be tagged by setting a 773 772 // stencil buffer bit at step 3) 773 774 glDisable(GL_STENCIL_TEST); 775 //glEnable(GL_STENCIL_TEST); 774 776 glStencilFunc(GL_EQUAL, 0x1, 0x1); 775 776 //cgGLBindProgram(sCgFragmentProgram); 777 //glStencilFunc(GL_EQUAL, 0x2, 0x2); 778 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); 779 780 // bind depth texture 781 glEnable(GL_TEXTURE_2D); 782 783 // bind depth cull pixel shader 784 cgGLBindProgram(sCgFragmentProgram); 777 785 cgGLEnableProfile(sCgFragmentProfile); 778 786 … … 785 793 786 794 // now check whether any backfacing polygon would pass the depth test? 795 //matt: should check both back /front facing because of dual depth buffer 796 //and danger of cutting the near plane with front facing polys. 787 797 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); 788 798 glDepthMask(GL_FALSE); … … 817 827 GL_PIXEL_COUNT_NV, 818 828 &pixelCount); 819 820 Debug << "visble pixels: " << pixelCount << endl;829 if (pixelCount) 830 Debug << "view cell " << (*vit)->GetId() << " visible pixels: " << pixelCount << endl; 821 831 } 822 832 … … 828 838 // evaluate the contribution entropy for example) 829 839 // However might be an option to extract/store only those the rays which made a contribution 830 // (new viewcell has been discovered) or relative contribution greater than a threashold ... 831 840 // (new viewcell has been discovered) or relative contribution greater than a threshold ... 841 842 ObjectContainer pvsObj; 843 stat.pvsSize = ComputePvs(beam.mObjects, pvsObj); 844 845 846 #if 0 847 // copy contents of back depth buffer into depth texture 848 // depth buffer holds ray origins 849 glBindTexture(GL_TEXTURE_2D, backDepthMap); 850 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, depthMapSize, depthMapSize); 851 #endif 852 // reset state 832 853 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 833 854 glDepthMask(GL_TRUE); 834 855 glEnable(GL_CULL_FACE); 835 836 856 glDisable(GL_STENCIL_TEST); 837 857 cgGLDisableProfile(sCgFragmentProfile); 838 858 glDisable(GL_TEXTURE_2D); 859 860 // remove objects again 861 if (beam.mFlags & !Beam::STORE_OBJECTS) 862 beam.mObjects.clear(); 839 863 } 840 864 … … 845 869 { 846 870 const int n = numQueries - (int)sQueries.size(); 847 int *newQueries = newint[n];848 849 glGenOcclusionQueriesNV(n, (unsigned int *) &newQueries);871 unsigned int *newQueries = new unsigned int[n]; 872 873 glGenOcclusionQueriesNV(n, (unsigned int *)newQueries); 850 874 851 875 for (int i = 0; i < n; ++ i) … … 854 878 } 855 879 856 delete newQueries;880 delete [] newQueries; 857 881 } 858 882 } … … 860 884 861 885 void GlRendererBuffer::SetupProjectionForViewPoint(const Vector3 &viewPoint, 862 const Beam &beam,863 Intersectable *sourceObject)886 const Beam &beam, 887 Intersectable *sourceObject) 864 888 { 865 889 float left, right, bottom, top, znear, zfar; 866 890 867 beam.ComputeFrustum(left, right, bottom, top, znear, zfar, 868 mSceneGraph->GetBox()); 869 891 beam.ComputePerspectiveFrustum(left, right, bottom, top, znear, zfar, 892 mSceneGraph->GetBox()); 893 894 //Debug << left << " " << right << " " << bottom << " " << top << " " << znear << " " << zfar << endl; 895 glMatrixMode(GL_PROJECTION); 896 glLoadIdentity(); 870 897 glFrustum(left, right, bottom, top, znear, zfar); 871 872 const Vector3 eye = viewPoint + beam.GetMainDirection(); 873 const Vector3 up;//AbitraryNormal(eye); 874 875 gluLookAt(eye.x, eye.y, eye.z, 876 viewPoint.x, viewPoint.y, viewPoint.z, 898 //glFrustum(-1, 1, -1, 1, 1, 20000); 899 900 const Vector3 center = viewPoint + beam.GetMainDirection() * (zfar - znear) * 0.3; 901 const Vector3 up = 902 Normalize(CrossProd(beam.mPlanes[0].mNormal, beam.mPlanes[4].mNormal)); 903 904 #ifdef _DEBUG 905 Debug << "view point: " << viewPoint << endl; 906 Debug << "eye: " << center << endl; 907 Debug << "up: " << up << endl; 908 #endif 909 910 glMatrixMode(GL_MODELVIEW); 911 glLoadIdentity(); 912 gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z, 913 center.x, center.y, center.z, 877 914 up.x, up.y, up.z); 878 915 } 879 916 880 917 918 void GlRendererBuffer::InitGL() 919 { 920 GlRenderer::InitGL(); 921 // initialise dual depth buffer textures 922 glGenTextures(1, &frontDepthMap); 923 glBindTexture(GL_TEXTURE_2D, frontDepthMap); 924 925 glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, depthMapSize, 926 depthMapSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); 927 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 928 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 929 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 930 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); 931 932 glGenTextures(1, &backDepthMap); 933 glBindTexture(GL_TEXTURE_2D, backDepthMap); 934 935 glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, depthMapSize, 936 depthMapSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); 937 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 938 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 939 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 940 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); 941 942 // cg initialization 943 cgSetErrorCallback(handleCgError); 944 sCgContext = cgCreateContext(); 945 946 if (cgGLIsProfileSupported(CG_PROFILE_ARBFP1)) 947 sCgFragmentProfile = CG_PROFILE_ARBFP1; 948 else 949 { 950 // try FP30 951 if (cgGLIsProfileSupported(CG_PROFILE_FP30)) 952 sCgFragmentProfile = CG_PROFILE_FP30; 953 else 954 { 955 Debug << "Neither arbfp1 or fp30 fragment profiles supported on this system" << endl; 956 exit(1); 957 } 958 } 959 960 sCgFragmentProgram = cgCreateProgramFromFile(sCgContext, 961 CG_SOURCE, "../src/dual_depth.cg", 962 sCgFragmentProfile, 963 NULL, 964 NULL); 965 966 if (!cgIsProgramCompiled(sCgFragmentProgram)) 967 cgCompileProgram(sCgFragmentProgram); 968 969 cgGLLoadProgram(sCgFragmentProgram); 970 cgGLBindProgram(sCgFragmentProgram); 881 971 882 883 /***************************************************************/ 884 /* GlDebuggerWidget implementation */ 885 /***************************************************************/ 886 887 888 GlDebuggerWidget::GlDebuggerWidget(QWidget *parent, GlRendererBuffer *buf) 972 Debug << "---- PROGRAM BEGIN ----\n" << 973 cgGetProgramString(sCgFragmentProgram, CG_COMPILED_PROGRAM) << "---- PROGRAM END ----\n"; 974 } 975 976 void GlRendererBuffer::ComputeRays(Intersectable *sourceObj, VssRayContainer &rays) 977 { 978 for (int i = 0; i < depthMapSize * depthMapSize; ++ i) 979 { 980 //todo glGetTexImage() 981 } 982 } 983 984 985 986 inline bool ilt(Intersectable *obj1, Intersectable *obj2) 987 { 988 return obj1->mId < obj2->mId; 989 } 990 991 992 int GlRendererBuffer::ComputePvs(ObjectContainer &objects, 993 ObjectContainer &pvs) const 994 { 995 int pvsSize = 0; 996 QImage image = toImage(); 997 Intersectable::NewMail(); 998 999 std::stable_sort(objects.begin(), objects.end(), ilt); 1000 1001 MeshInstance dummy(NULL); 1002 1003 Intersectable *obj = NULL; 1004 1005 for (int x = 0; x < image.width(); ++ x) 1006 { 1007 for (int y = 0; y < image.height(); ++ y) 1008 { 1009 QRgb pix = image.pixel(x, y); 1010 const int id = GetId(qRed(pix), qGreen(pix), qBlue(pix)); 1011 1012 dummy.SetId(id); 1013 1014 ObjectContainer::iterator oit = 1015 lower_bound(objects.begin(), objects.end(), &dummy, ilt); 1016 obj = *oit; 1017 if (!obj->Mailed()) 1018 { 1019 obj->Mail(); 1020 ++ pvsSize; 1021 pvs.push_back(obj); 1022 } 1023 } 1024 } 1025 1026 return pvsSize; 1027 } 1028 1029 /***********************************************************************/ 1030 /* GlDebuggerWidget implementation */ 1031 /***********************************************************************/ 1032 1033 1034 GlDebuggerWidget::GlDebuggerWidget(GlRendererBuffer *buf, QWidget *parent) 889 1035 : QGLWidget(QGLFormat(QGL::SampleBuffers), parent), mRenderBuffer(buf) 890 1036 { … … 932 1078 // draw a spinning cube into the pbuffer.. 933 1079 mRenderBuffer->makeCurrent(); 934 //mRenderBuffer->SampleBeamContributions(); 1080 1081 BeamSampleStatistics stats; 1082 mRenderBuffer->SampleBeamContributions(mSourceObject, mBeam, mSamples, stats); 1083 935 1084 glFlush(); 936 1085 937 1086 // rendering directly to a texture is not supported on X11, unfortunately 938 1087 mRenderBuffer->updateDynamicTexture(dynamicTexture); 939 1088 940 // ..and use the pbuffer contents as a texture when rendering the1089 // and use the pbuffer contents as a texture when rendering the 941 1090 // background and the bouncing cubes 942 1091 makeCurrent(); … … 962 1111 // set up the pbuffer context 963 1112 mRenderBuffer->makeCurrent(); 964 initCommon();1113 /*mRenderBuffer->InitGL(); 965 1114 966 1115 glViewport(0, 0, mRenderBuffer->size().width(), mRenderBuffer->size().height()); … … 972 1121 glLoadIdentity(); 973 1122 974 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); 975 1123 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);*/ 1124 976 1125 // generate a texture that has the same size/format as the pbuffer 977 1126 dynamicTexture = mRenderBuffer->generateDynamicTexture();
Note: See TracChangeset
for help on using the changeset viewer.