- Timestamp:
- 07/13/08 23:51:02 (16 years ago)
- Location:
- GTP/trunk/App/Demos/Vis/FriendlyCulling
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj
r2827 r2834 716 716 </Filter> 717 717 </Filter> 718 <Filter 719 Name="shaders" 720 > 721 <File 722 RelativePath=".\src\shaders\deferred.cg" 723 > 724 </File> 725 <File 726 RelativePath=".\src\shaders\mrt.cg" 727 > 728 </File> 729 </Filter> 718 730 <File 719 731 RelativePath=".\ReadMe.txt" -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp
r2833 r2834 96 96 97 97 matProjectionView = matViewing * matProjectionView; 98 //matProjectionView = matProjectionView * matViewing; 99 98 100 99 float planes[6][4]; 101 100 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/PerformanceGraph.cpp
r2829 r2834 51 51 glLineWidth(2); 52 52 glDisable(GL_DEPTH_TEST); 53 //glDepthMask(true);54 53 55 54 glDisable(GL_LIGHTING); … … 74 73 DrawGrid(); 75 74 76 glPopMatrix();77 78 75 // restore the projection matrix 79 76 glMatrixMode(GL_PROJECTION); … … 81 78 82 79 glMatrixMode(GL_MODELVIEW); 80 glPopMatrix(); 83 81 84 82 glPopAttrib(); -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneQuery.cpp
r2809 r2834 149 149 { 150 150 glViewport(0, 0, texWidth, texHeight); 151 glPushAttrib(GL_VIEWPORT_BIT); 151 152 152 153 glClearColor(1, 1, 1, 1); … … 162 163 163 164 glMatrixMode(GL_PROJECTION); 164 gl LoadIdentity();165 glPushMatrix(); 165 166 166 167 glOrtho(-xlen, xlen, -ylen, ylen, 0.0f, mSceneBox.Size().z); 167 168 168 169 glMatrixMode(GL_MODELVIEW); 170 glPushMatrix(); 169 171 170 172 orthoCam->SetupCameraView(); … … 174 176 //renderer->SetCamera(orthoCam); 175 177 renderer->RenderScene(); 178 179 glPopMatrix(); 180 glPopMatrix(); 181 182 glPopAttrib(); 176 183 } 177 184 depthTexture->EndCapture(); -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
r2833 r2834 39 39 40 40 ///////////// 41 //-- textures 41 //-- fbos 42 42 43 GLuint fbo; 44 GLuint fbo1; 45 GLuint fbo2; 46 47 bool isFirstTexture = true; 48 49 ///////////// 50 //-- renderbuffers 51 43 52 44 53 GLuint depthBuffer; … … 46 55 GLuint positionsBuffer; 47 56 GLuint colorsBuffer; 57 GLuint colorsBuffer1; 58 GLuint colorsBuffer2; 48 59 GLuint normalsBuffer; 60 61 62 ///////////// 63 //-- textures 64 49 65 50 66 GLuint positionsTex; 51 67 GLuint colorsTex; 68 GLuint colorsTex1; 69 GLuint colorsTex2; 70 52 71 GLuint normalsTex; 53 72 … … 169 188 170 189 // ssao number of samples 171 #define NUM_SAMPLES 8 190 //#define NUM_SAMPLES 8 191 #define NUM_SAMPLES 16 172 192 173 193 // ssao random spherical samples 174 194 static float samples[NUM_SAMPLES * 2]; 195 196 static Matrix4x4 matProjectionView = IdentityMatrix(); 175 197 176 198 … … 249 271 250 272 static CGparameter sModelViewProjMatrixParam; 273 static CGparameter sOldModelViewProjMatrixParam; 251 274 static CGparameter sMaxDepthParam; 252 275 static CGparameter sMaxDepthParamTex; 253 276 254 277 static CGparameter sSamplesParamSsao; 255 //static CGparameter sNoiseMultiplierParam;; 256 257 278 279 static CGparameter sOldTexParam; 280 static CGparameter sOldTexParamSsao; 281 282 static CGparameter sNoiseMultiplierParam; 258 283 259 284 static void cgErrorCallback() … … 439 464 void InitCg(void) 440 465 { 441 GenerateSamples();442 443 466 // Setup Cg 444 467 cgSetErrorCallback(cgErrorCallback); … … 488 511 489 512 cgGLSetParameter1f(sMaxDepthParamTex, 10.0f / myfar); 513 Debug << "maxdepth: " << 10.0f / myfar << endl; 490 514 } 491 515 else … … 535 559 sNormalsTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "normals"); 536 560 sNoiseTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "noiseTexture"); 537 //sNoiseMultiplierParam = cgGetNamedParameter(sCgSsaoProgram, "noiseMultiplier"); 561 sNoiseMultiplierParam = cgGetNamedParameter(sCgSsaoProgram, "noiseMultiplier"); 562 sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "oldModelViewProj"); 538 563 539 564 cgGLDisableTextureParameter(sNoiseTexParamSsao); 540 565 541 566 sSamplesParamSsao = cgGetNamedParameter(sCgSsaoProgram, "samples"); 567 sOldTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "oldTex"); 568 569 GenerateSamples(); 542 570 cgGLSetParameterArray2f(sSamplesParamSsao, 0, NUM_SAMPLES, (const float *)samples); 543 571 } … … 561 589 sPositionsTexParam = cgGetNamedParameter(sCgDeferredProgram, "positions"); 562 590 sColorsTexParam = cgGetNamedParameter(sCgDeferredProgram, "colors"); 563 sNormalsTexParam = cgGetNamedParameter(sCgDeferredProgram, "normals"); 591 sNormalsTexParam = cgGetNamedParameter(sCgDeferredProgram, "normals"); 592 593 sOldTexParam = cgGetNamedParameter(sCgDeferredProgram, "oldTex"); 564 594 } 565 595 else … … 569 599 PrintGLerror("init"); 570 600 571 cout << "cg initiali sed" << endl;601 cout << "cg initialization successful" << endl; 572 602 } 573 603 … … 619 649 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, colorsBuffer); 620 650 621 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA 8, texWidth, texHeight);651 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA16F_ARB, texWidth, texHeight); 622 652 int samples = 8; 623 653 //glEnable(GL_MULTISAMPLE_ARB); … … 628 658 glBindTexture(GL_TEXTURE_2D, colorsTex); 629 659 630 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA 8, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);660 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL); 631 661 glGenerateMipmapEXT(GL_TEXTURE_2D); 632 662 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorsTex, 0); … … 651 681 glGenTextures(1, &positionsTex); 652 682 glBindTexture(GL_TEXTURE_2D, positionsTex); 653 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, 683 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL); 654 684 //glGenerateMipmapEXT(GL_TEXTURE_2D); 655 685 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D, positionsTex, 0); … … 675 705 glGenTextures(1, &normalsTex); 676 706 glBindTexture(GL_TEXTURE_2D, normalsTex); 677 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); 678 //glGenerateMipmapEXT(GL_TEXTURE_2D); 707 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); 679 708 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_TEXTURE_2D, normalsTex, 0); 680 709 … … 699 728 PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)); 700 729 730 731 732 ////////////////////////////////// 733 734 735 736 glGenFramebuffersEXT(1, &fbo1); 737 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo1); 738 739 740 //////////// 741 //-- colors buffer 742 743 glGenRenderbuffersEXT(1, &colorsBuffer1); 744 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, colorsBuffer1); 745 746 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA16F_ARB, texWidth, texHeight); 747 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, colorsBuffer1); 748 749 glGenTextures(1, &colorsTex1); 750 glBindTexture(GL_TEXTURE_2D, colorsTex1); 751 752 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL); 753 glGenerateMipmapEXT(GL_TEXTURE_2D); 754 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorsTex1, 0); 755 756 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 757 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 758 759 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 760 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 761 762 PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)); 763 764 765 766 ///////////////////////// 767 768 glGenFramebuffersEXT(1, &fbo2); 769 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo2); 770 771 772 //////////// 773 //-- colors buffer 774 775 glGenRenderbuffersEXT(1, &colorsBuffer2); 776 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, colorsBuffer2); 777 778 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA16F_ARB, texWidth, texHeight); 779 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, colorsBuffer2); 780 781 glGenTextures(1, &colorsTex2); 782 glBindTexture(GL_TEXTURE_2D, colorsTex2); 783 784 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL); 785 glGenerateMipmapEXT(GL_TEXTURE_2D); 786 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorsTex2, 0); 787 788 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 789 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 790 791 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 792 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 793 794 PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)); 795 701 796 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 702 797 … … 917 1012 void SetupEyeView() 918 1013 { 1014 Matrix4x4 matViewing, matProjection; 1015 1016 if (renderType == RenderState::DEFERRED) 1017 { 1018 cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)matProjectionView.x); 1019 } 1020 919 1021 glMatrixMode(GL_PROJECTION); 920 1022 glLoadIdentity(); … … 932 1034 glLightfv(GL_LIGHT1, GL_POSITION, position1); 933 1035 1036 1037 camera->GetModelViewMatrix(matViewing); 1038 camera->GetProjectionMatrix(matProjection); 1039 1040 matProjectionView = matViewing * matProjection; 934 1041 935 1042 if (renderType == RenderState::DEFERRED) … … 942 1049 static Matrix4x4 identity = IdentityMatrix(); 943 1050 cgGLSetMatrixParameterfc(SceneEntity::sModelMatrixParam, (const float *)identity.x); 944 } 1051 } 945 1052 } 946 1053 … … 1124 1231 perfGraph->AddData(algTime); 1125 1232 1126 /*float col[3];1127 switch (renderMode)1128 {1129 case RenderTraverser::CHCPLUSPLUS:1130 col[0] = 1.0f; col[1] = 0.0f; col[2] = 0.0f;1131 break;1132 case RenderTraverser::CHC:1133 col[0] = 0.0f; col[1] = 1.0f; col[2] = 1.0f;1134 break;1135 case RenderTraverser::STOP_AND_WAIT:1136 col[0] = 1.0f; col[1] = 1.0f; col[2] = 0.0f;1137 break;1138 case RenderTraverser::CULL_FRUSTUM:1139 col[0] = 1.0f; col[1] = 0.0f; col[2] = 1.0f;1140 break;1141 default:1142 col[0] = 1.0f; col[1] = 0.0f; col[2] = 0.0f;1143 }1144 perfGraph->SetGraphColor(col);1145 */1146 1233 perfGraph->Draw(); 1147 1234 } … … 1152 1239 1153 1240 DisplayStats(); 1241 1242 isFirstTexture = !isFirstTexture; 1154 1243 1155 1244 glutSwapBuffers(); … … 1530 1619 glPushMatrix(); 1531 1620 glLoadIdentity(); 1621 1532 1622 gluOrtho2D(0, winWidth, 0, winHeight); 1533 1623 … … 1540 1630 void End2D() 1541 1631 { 1632 glMatrixMode(GL_PROJECTION); 1542 1633 glPopMatrix(); 1634 1543 1635 glMatrixMode(GL_MODELVIEW); 1544 1636 glPopMatrix(); … … 1570 1662 1571 1663 const float offs = box.Size().x * 0.3f; 1572 //const float yoffs = box.Size().y * 0.5f; 1573 1664 1574 1665 Vector3 pos = camera->GetPosition(); 1575 1666 … … 1578 1669 visCamera->SetPosition(vizpos); 1579 1670 1671 glPushAttrib(GL_VIEWPORT_BIT); 1580 1672 glViewport(winWidth - winWidth / 3, winHeight - winHeight / 3, winWidth / 3, winHeight / 3); 1581 1673 1582 1674 glMatrixMode(GL_PROJECTION); 1675 glPushMatrix(); 1676 1583 1677 glLoadIdentity(); 1584 1678 … … 1586 1680 1587 1681 glMatrixMode(GL_MODELVIEW); 1682 glPushMatrix(); 1588 1683 1589 1684 visCamera->SetupCameraView(); … … 1609 1704 visualization->Render(); 1610 1705 1611 // reset vp 1612 glViewport(0, 0, winWidth, winHeight); 1706 1707 // reset previous settings 1708 glPopAttrib(); 1709 1710 glMatrixMode(GL_PROJECTION); 1711 glPopMatrix(); 1712 glMatrixMode(GL_MODELVIEW); 1713 glPopMatrix(); 1613 1714 } 1614 1715 … … 1880 1981 1881 1982 1983 void DisplayRenderTexture3() 1984 { 1985 glEnable(GL_TEXTURE_2D); 1986 1987 if (isFirstTexture) 1988 glBindTexture(GL_TEXTURE_2D, colorsTex1); 1989 else 1990 glBindTexture(GL_TEXTURE_2D, colorsTex2); 1991 1992 glDisable(GL_LIGHTING); 1993 1994 glMatrixMode(GL_PROJECTION); 1995 glPushMatrix(); 1996 glLoadIdentity(); 1997 1998 glMatrixMode(GL_MODELVIEW); 1999 glPushMatrix(); 2000 glLoadIdentity(); 2001 2002 const float offs = 0.5f; 2003 glOrtho(-offs, offs, -offs, offs, 0, 1); 2004 2005 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 2006 2007 glColor4f(1.0f, 1.0f, 1.0f, 1.0f); 2008 glBegin(GL_QUADS); 2009 2010 glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f); 2011 glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f); 2012 glTexCoord2f(1, 1); glVertex3f( offs, offs, -0.5f); 2013 glTexCoord2f(0, 1); glVertex3f(-offs, offs, -0.5f); 2014 2015 glEnd(); 2016 2017 glEnable(GL_LIGHTING); 2018 glDisable(GL_TEXTURE_2D); 2019 2020 glMatrixMode(GL_PROJECTION); 2021 glPopMatrix(); 2022 2023 glMatrixMode(GL_MODELVIEW); 2024 glPopMatrix(); 2025 2026 PrintGLerror("displaytexture3"); 2027 } 2028 2029 1882 2030 void DisplayRenderTexture() 1883 2031 { 2032 GLuint oldTex; 2033 2034 if (isFirstTexture) 2035 { 2036 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo1); 2037 oldTex = colorsTex2; 2038 } 2039 else 2040 { 2041 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo2); 2042 oldTex = colorsTex1; 2043 } 2044 2045 glPushAttrib(GL_VIEWPORT_BIT); 2046 glViewport(0, 0, texWidth, texHeight); 2047 2048 glDrawBuffers(1, mrt); 2049 1884 2050 glDisable(GL_ALPHA_TEST); 1885 2051 glDisable(GL_TEXTURE_2D); … … 1906 2072 cgGLBindProgram(sCgSsaoProgram); 1907 2073 2074 cgGLSetTextureParameter(sPositionsTexParamSsao, positionsTex); 1908 2075 cgGLEnableTextureParameter(sPositionsTexParamSsao); 1909 cgGLSetTextureParameter(sPositionsTexParamSsao, positionsTex); 1910 2076 2077 cgGLSetTextureParameter(sColorsTexParamSsao, colorsTex); 1911 2078 cgGLEnableTextureParameter(sColorsTexParamSsao); 1912 cgGLSetTextureParameter(sColorsTexParamSsao, colorsTex); 1913 2079 2080 cgGLSetTextureParameter(sNormalsTexParamSsao, normalsTex); 1914 2081 cgGLEnableTextureParameter(sNormalsTexParamSsao); 1915 cgGLSetTextureParameter(sNormalsTexParamSsao, normalsTex); 1916 2082 2083 cgGLSetTextureParameter(sNoiseTexParamSsao, noiseTex); 1917 2084 cgGLEnableTextureParameter(sNoiseTexParamSsao); 1918 cgGLSetTextureParameter(sNoiseTexParamSsao, noiseTex); 1919 1920 //cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 7.0f)); 2085 2086 cgGLSetTextureParameter(sOldTexParamSsao, oldTex); 2087 cgGLEnableTextureParameter(sOldTexParamSsao); 2088 2089 cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f)); 2090 2091 //GenerateSamples(); cgGLSetParameterArray2f(sSamplesParamSsao, 0, NUM_SAMPLES, (const float *)samples); 1921 2092 } 1922 2093 else … … 1924 2095 cgGLBindProgram(sCgDeferredProgram); 1925 2096 2097 cgGLSetTextureParameter(sPositionsTexParam, positionsTex); 1926 2098 cgGLEnableTextureParameter(sPositionsTexParam); 1927 cgGLSetTextureParameter(sPositionsTexParam, positionsTex); 1928 2099 2100 cgGLSetTextureParameter(sColorsTexParam, colorsTex); 1929 2101 cgGLEnableTextureParameter(sColorsTexParam); 1930 cgGLSetTextureParameter(sColorsTexParam, colorsTex); 1931 2102 2103 cgGLSetTextureParameter(sNormalsTexParam, normalsTex); 1932 2104 cgGLEnableTextureParameter(sNormalsTexParam); 1933 cgGLSetTextureParameter(sNormalsTexParam, normalsTex); 2105 2106 cgGLSetTextureParameter(sOldTexParam, oldTex); 2107 cgGLEnableTextureParameter(sOldTexParam); 1934 2108 } 1935 2109 … … 1952 2126 glEnd(); 1953 2127 1954 cgGLDisableTextureParameter(sColorsTexParamSsao);1955 cgGLDisableTextureParameter(sPositionsTexParamSsao);1956 cgGLDisableTextureParameter(sNormalsTexParamSsao);1957 2128 1958 2129 if (useSsao) 2130 { 2131 cgGLDisableTextureParameter(sColorsTexParamSsao); 2132 cgGLDisableTextureParameter(sPositionsTexParamSsao); 2133 cgGLDisableTextureParameter(sNormalsTexParamSsao); 1959 2134 cgGLDisableTextureParameter(sNoiseTexParamSsao); 2135 cgGLDisableTextureParameter(sOldTexParamSsao); 2136 } 2137 else 2138 { 2139 cgGLDisableTextureParameter(sColorsTexParam); 2140 cgGLDisableTextureParameter(sPositionsTexParam); 2141 cgGLDisableTextureParameter(sNormalsTexParam); 2142 cgGLDisableTextureParameter(sOldTexParam); 2143 } 1960 2144 1961 2145 cgGLDisableProfile(RenderState::sCgFragmentProfile); … … 1970 2154 glPopMatrix(); 1971 2155 1972 2156 glPopAttrib(); 2157 1973 2158 PrintGLerror("displaytexture"); 2159 2160 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 2161 2162 DisplayRenderTexture3(); 1974 2163 } 1975 2164 … … 2014 2203 #else 2015 2204 2016 /*void GenerateSamples()2017 {2018 float scale = 1.0f / (float)NUM_SAMPLES;2019 2020 ////////2021 //-- generate uniformly distributed spherical samples2022 2023 for (int i = 0; i < NUM_SAMPLES; ++ i)2024 {2025 // create stratified samples over sphere2026 const float rx = ((float)i + RandomValue(0, 1)) * scale;2027 const float ry = ((float)i + RandomValue(0, 1)) * scale;2028 2029 const float theta = 2.0f * acos(sqrt(1.0f - rx));2030 const float phi = 2.0f * M_PI * ry;2031 2032 Vector3 unit = Vector3(sin(theta) * cos(phi), sin(theta) * sin(phi), cos(theta));2033 2034 //////2035 //-- we sample in texture space => store 2D component2036 2037 // q: project to plane or just drop z?2038 Vector3 proj = unit / (1.0f + fabs(unit.z));2039 //Vector3 proj = unit;2040 2041 samples[i + 0] = proj.x;2042 samples[i + 1] = proj.y;2043 //cout << samples[i] << " " << samples[i + 1] << endl;2044 }2045 }*/2046 2205 2047 2206 void GenerateSamples() 2048 2207 { 2049 2208 // generates poisson distribution on disc 2050 //float factor = 5.0f; 2051 float minDist = 2.0f;//factor / sqrt((float)NUM_SAMPLES); 2052 2053 cout << "minDist= " << minDist << endl; 2209 float minDist = 2.0f / sqrt((float)NUM_SAMPLES); 2210 2211 //cout << "minDist before= " << minDist << endl; 2054 2212 2055 2213 for (int i = 0; i < NUM_SAMPLES * 2; i += 2) … … 2096 2254 } 2097 2255 } 2098 cout << "sample: " << samples[i] << " " << i /2 << " " << samples[i + 1] << " r: " << sqrt(samples[i] * samples[i] + samples[i + 1] * samples[i + 1]) << " tries: " << totalTries << endl;2099 } 2100 2101 cout << "minDist= " << minDist << endl;2256 //cout << "sample: " << samples[i] << " " << i /2 << " " << samples[i + 1] << " r: " << sqrt(samples[i] * samples[i] + samples[i + 1] * samples[i + 1]) << " tries: " << totalTries << endl; 2257 } 2258 2259 //cout << "minDist after= " << minDist << endl; 2102 2260 } 2103 2261 … … 2118 2276 float t2 = w_far * 0.5f; 2119 2277 2278 #if 0 2120 2279 bl = Normalize(-Vector3(-t1, -t2, 1.0f)); 2121 2280 br = Normalize(-Vector3( t1, -t2, 1.0f)); 2122 2281 tl = Normalize(-Vector3(-t1, t2, 1.0f)); 2123 2282 tr = Normalize(-Vector3( t1, t2, 1.0f)); 2124 2283 #else 2284 bl = -Normalize(camera->GetDirection()); 2285 br = -Normalize(camera->GetDirection()); 2286 tl = -Normalize(camera->GetDirection()); 2287 tr = -Normalize(camera->GetDirection()); 2288 #endif 2125 2289 // normalize to 0 .. 1 2126 2290 bl = bl * 0.5f + 0.5f; -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
r2833 r2834 1 1 //////////////////// 2 2 // Screen Spaced Ambient Occlusion shader 3 // mainly based on Kustls shader 4 5 #define NUM_SAMPLES 8 6 //#define SAMPLE_INTENSITY 0.5f 7 //#define SAMPLE_INTENSITY 1.1f 8 #define SAMPLE_INTENSITY 0.25f 3 // mainly based on shader of Alexander Kusternig 4 5 //#define NUM_SAMPLES 8 6 #define NUM_SAMPLES 16 7 //#define SAMPLE_INTENSITY 0.125f 8 // rule of thumb: approx 1 / NUM_SAMPLES 9 #define SAMPLE_INTENSITY 0.2 //0.0625f 10 9 11 #define AREA_SIZE 5e-1f 10 12 … … 30 32 // reflect around plane 31 33 float2 rpt = pt - d * 2.0f * n; 32 34 33 35 //return pt; 34 36 return rpt; … … 38 40 float2 rotate(float2 pt, float2 n) 39 41 { 40 float2 ptTransformed; 41 ptTransformed.x = n.r * pt.x - n.g * pt.y; 42 ptTransformed.y = n.g * pt.x + n.r * pt.y; 43 44 return ptTransformed; 45 } 46 47 48 //based on kustls shader 42 float2 ptTransformed; 43 ptTransformed.x = n.r * pt.x - n.g * pt.y; 44 ptTransformed.y = n.g * pt.x + n.r * pt.y; 45 46 return ptTransformed; 47 } 48 49 49 50 float ssao(fragment IN, 50 uniform sampler2D positions, 51 uniform sampler2D noiseTexture, 52 uniform float2 samples[NUM_SAMPLES], 53 uniform float3 currentNormal, 54 uniform float3 currentViewDir) 55 { 56 // the current world position 57 float4 centerPosition = tex2D(positions, IN.texCoord.xy); 58 // the w coordinate from the persp. projection 59 float w = centerPosition.w; 60 61 // Check in a circular area around the current position. 62 // Shoot vectors to the positions there, and check the angle to these positions. 63 // Summing up these angles gives an estimation of the occlusion at the current position. 64 65 float total_ao = 0.0; 66 67 const float areaSize = 5e-1f; 68 //const float areaSize = 3e-1f; 69 //const float sampleIntensity = 0.2f; 70 71 for (int i = 0; i < NUM_SAMPLES; i ++) { 72 float2 offset = samples[i]; 73 74 //sample noisetex; r stores costheta, g stores sintheta 75 float2 noise = tex2D(noiseTexture, IN.texCoord.xy * 7.1f).xy * 2.0f - 1.0f; 51 uniform sampler2D positions, 52 uniform sampler2D noiseTexture, 53 uniform float2 samples[NUM_SAMPLES], 54 uniform float3 currentNormal, 55 uniform float3 currentViewDir, 56 uniform float noiseMultiplier, 57 uniform float4 centerPosition 58 ) 59 { 60 // the w coordinate from the persp. projection 61 float w = centerPosition.w; 62 63 // Check in a circular area around the current position. 64 // Shoot vectors to the positions there, and check the angle to these positions. 65 // Summing up these angles gives an estimation of the occlusion at the current position. 66 67 float total_ao = 0.0; 68 69 const float areaSize = 5e-1f; 70 //const float areaSize = 3e-1f; 71 //const float sampleIntensity = 0.2f; 72 73 for (int i = 0; i < NUM_SAMPLES; i ++) 74 { 75 float2 offset = samples[i]; 76 77 //sample noisetex; r stores costheta, g stores sintheta 78 float2 noise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f; 79 80 // rotation 81 //float2 offsetTransformed = offset; 82 float2 offsetTransformed = rotate(offset, noise); 83 //float2 offsetTransformed = reflect(offset, noise); 84 85 // weight with projected coordinate to reach similar kernel size for near and far 86 float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w; 87 88 float3 sample_position = tex2D(positions, texcoord).xyz; 89 90 float3 vector_to_sample = sample_position - centerPosition.xyz; 91 float length_to_sample = length(vector_to_sample); 92 float3 direction_to_sample = vector_to_sample / length_to_sample; 93 94 // Angle between current normal and direction to sample controls AO intensity. 95 float cos_angle = dot(direction_to_sample, currentNormal); 96 cos_angle = max(cos_angle, 0.0f); 97 // take quadratic influence to sharpen contrast 98 //cos_angle *= cos_angle; 99 100 // distance between current position and sample position controls AO intensity. 101 //const float maxdist = 2e-1f; 102 //const float maxdist = 5e-1f; 103 const float distanceScale = 1e-6f; 104 105 float distance_intensity = 106 (SAMPLE_INTENSITY * distanceScale) / (distanceScale + length_to_sample * length_to_sample); 107 108 // if normal perpenticular to view dir, only half of the samples count 109 float view_correction = (2.0f - dot(currentViewDir, currentNormal)); 110 view_correction *= view_correction; 111 112 total_ao += cos_angle * distance_intensity * view_correction; 113 } 114 115 return (1.0f - total_ao); 116 } 117 118 119 float4 globIllum(fragment IN, 120 uniform sampler2D colors, 121 uniform sampler2D positions, 122 uniform sampler2D noiseTexture, 123 uniform float2 samples[NUM_SAMPLES], 124 uniform float3 currentNormal, 125 uniform float3 currentViewDir, 126 uniform float noiseMultiplier, 127 uniform float4 centerPosition) 128 { 129 // the w coordinate from the persp. projection 130 float w = centerPosition.w; 131 132 // Check in a circular area around the current position. 133 // Shoot vectors to the positions there, and check the angle to these positions. 134 // Summing up these angles gives an estimation of the occlusion at the current position. 135 136 float total_ao = 0.0; 137 float3 total_color = float3(0.0f); 138 139 const float areaSize = 5e-1f; 140 141 for (int i = 0; i < NUM_SAMPLES; i ++) 142 { 143 float2 offset = samples[i]; 144 145 //sample noisetex; r stores costheta, g stores sintheta 146 float2 noise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f; 147 148 // rotation 149 float2 offsetTransformed = rotate(offset, noise); 150 151 // weight with projected coordinate to reach similar kernel size for near and far 152 float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w; 153 154 float3 sample_position = tex2D(positions, texcoord).xyz; 155 float3 sample_color = tex2D(colors, texcoord).xyz; 156 157 float3 vector_to_sample = sample_position - centerPosition.xyz; 158 float length_to_sample = length(vector_to_sample); 159 float3 direction_to_sample = vector_to_sample / length_to_sample; 160 161 // Angle between current normal and direction to sample controls AO intensity. 162 float cos_angle = dot(direction_to_sample, currentNormal); 163 cos_angle = max(cos_angle, 0.0f); 164 165 // distance between current position and sample position controls AO intensity. 166 const float distanceScale = 1e-6f; 167 168 float distance_intensity = 169 (SAMPLE_INTENSITY * distanceScale) / (distanceScale + length_to_sample * length_to_sample); 170 171 // if normal perpenticular to view dir, only half of the samples count 172 float view_correction = 1.0f + 0.5f * (1.0f - dot(currentViewDir, currentNormal)); 173 //view_correction *= view_correction; 174 175 total_ao += cos_angle * distance_intensity * view_correction; 176 total_color += cos_angle * distance_intensity * view_correction * sample_color; 177 } 178 179 return float4(total_color, 1.0f - total_ao); 180 } 181 182 183 float4 shade(fragment IN, 184 uniform sampler2D colors, 185 uniform sampler2D positions, 186 uniform float3 normal, 187 uniform float amb) 188 { 189 float4 lightDir = float4(0.8f, -1.0f, 0.7f, 0.0f); 190 float4 lightDir2 = float4(-0.5f, 0.5f, 0.4f, 0.0f); 191 192 float4 color = tex2D(colors, IN.texCoord.xy); 193 194 float4 position = tex2D(positions, IN.texCoord.xy); 195 196 float4 ambient = 0.3f; 197 198 // float3 L = normalize(lightPosition - position); 199 float3 light = normalize(lightDir.xyz); 200 float3 light2 = normalize(lightDir2.xyz); 201 202 float diffuseLight = max(dot(normal, light), 0.0f); 203 float diffuseLight2 = max(dot(normal, light2), 0.0f); 204 205 float diffuse = diffuseLight + diffuseLight2; 206 //float diffuse = diffuseLight; 207 208 return (ambient + diffuse) * color * (1.0f - amb) + amb * color; 209 } 210 211 212 pixel main_ssao(fragment IN, 213 uniform sampler2D colors, 214 uniform sampler2D positions, 215 uniform sampler2D normals, 216 uniform sampler2D noiseTexture, 217 uniform float2 samples[NUM_SAMPLES], 218 uniform float noiseMultiplier, 219 uniform sampler2D oldTex, 220 const uniform float4x4 oldModelViewProj 221 ) 222 { 223 pixel OUT; 224 225 float4 normal = tex2D(normals, IN.texCoord.xy); 226 float amb = normal.w; 227 228 // expand normal 229 normal = normalize(normal * 2.0f - 1.0f); 230 float3 viewDir = normalize(IN.view * 2.0f - float3(1.0f)); 231 232 // the current world position 233 float4 centerPosition = tex2D(positions, IN.texCoord.xy); 76 234 77 // rotation 78 //float2 offsetTransformed = offset; 79 float2 offsetTransformed = rotate(offset, noise); 80 //float2 offsetTransformed = reflect(offset, noise); 81 82 // weight with projected coordinate to reach similar kernel size for near and far 83 float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w; 84 85 float3 sample_position = tex2D(positions, texcoord).xyz; 86 87 float3 vector_to_sample = sample_position - centerPosition.xyz; 88 float length_to_sample = length(vector_to_sample); 89 float3 direction_to_sample = vector_to_sample / length_to_sample; 90 91 // Angle between current normal and direction to sample controls AO intensity. 92 float cos_angle = dot(direction_to_sample, currentNormal); 93 cos_angle = max(cos_angle, 0.0f); 94 // take quadratic influence to sharpen contrast 95 //cos_angle *= cos_angle; 96 97 // distance between current position and sample position controls AO intensity. 98 //const float maxdist = 2e-1f; 99 //const float maxdist = 5e-1f; 100 const float distanceScale = 1e-6f; 101 102 float distance_intensity = 103 (SAMPLE_INTENSITY * distanceScale) / (distanceScale + length_to_sample * length_to_sample); 104 105 // if normal perpenticular to view dir, only half of the samples count 106 float view_correction = (2.0f - dot(currentViewDir, currentNormal)); 107 108 total_ao += cos_angle * distance_intensity * view_correction; 109 } 110 111 return (1.0f - total_ao); 112 } 113 114 115 float4 shade(fragment IN, 116 uniform sampler2D colors, 117 uniform sampler2D positions, 118 uniform float3 normal, 119 uniform float amb) 120 { 121 float4 lightDir = float4(0.8f, -1.0f, 0.7f, 0.0f); 122 float4 lightDir2 = float4(-0.5f, 0.5f, 0.4f, 0.0f); 123 124 float4 color = tex2D(colors, IN.texCoord.xy); 235 float4 col = shade(IN, colors, positions, normal.xyz, amb); 236 237 //float ao = ssao(IN, positions, noiseTexture, samples, normal.xyz, viewDir, noiseMultiplier, centerPosition); 238 float4 new_col = globIllum(IN, colors, positions, noiseTexture, samples, normal.xyz, viewDir, noiseMultiplier, centerPosition); 239 float ao = new_col.w; 240 241 //float4 attenuated_color = ao * col + new_col; 242 float4 attenuated_color = ao * col; 243 244 //OUT.color = ao; 245 //OUT.color = ao * col; 246 float4 dummy = centerPosition / 0.000487676f; 247 dummy.w = 1.0f; 248 float4 oldPos = mul(oldModelViewProj, dummy); 249 250 float newDepth = oldPos.z / oldPos.w; 125 251 126 float4 position = tex2D(positions, IN.texCoord.xy); 127 128 float4 ambient = 0.3f; 129 130 // float3 L = normalize(lightPosition - position); 131 float3 light = normalize(lightDir.xyz); 132 float3 light2 = normalize(lightDir2.xyz); 133 134 float diffuseLight = max(dot(normal, light), 0.0f); 135 float diffuseLight2 = max(dot(normal, light2), 0.0f); 136 137 //float diffuse = diffuseLight + diffuseLight2; 138 float diffuse = diffuseLight; 139 140 return (ambient + diffuse) * color * (1.0f - amb) + amb * color; 141 } 142 143 144 pixel main_ssao(fragment IN, 145 uniform sampler2D colors, 146 uniform sampler2D positions, 147 uniform sampler2D normals, 148 uniform sampler2D noiseTexture, 149 uniform float2 samples[NUM_SAMPLES]) 150 { 151 pixel OUT; 152 153 float4 normal = tex2D(normals, IN.texCoord.xy); 154 float amb = normal.w; 155 156 // expand normal 157 normal = normalize(normal * 2.0f - 1.0f); 158 float3 viewDir = normalize(IN.view * 2.0f - float(1.0f)); 159 160 float4 col = shade(IN, colors, positions, normal, amb); 161 float ao = ssao(IN, positions, noiseTexture, samples, normal, viewDir); 162 163 //OUT.color = ao; 164 OUT.color = ao * col; 165 166 return OUT; 252 const float x = 0.05f; 253 254 float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f; 255 float4 col1 = tex2D(oldTex, tex); 256 257 float oldDepth = col1.w; 258 259 if ((tex.x >= 0) && (tex.x <= 1) && (tex.y >= 0) && (tex.y <= 1) && (abs(newDepth - oldDepth) < 5e-4f)) 260 OUT.color = attenuated_color * x + col1 * float4(1.0f - x); 261 else OUT.color = attenuated_color;// * x + col1 * float4(1.0f - x); 262 263 //OUT.color = float4(centerPosition.w); 264 //OUT.color = float4(oldDepth); 265 //OUT.color = float4(newDepth); 266 //OUT.color = float4(abs(newDepth - oldDepth)); 267 OUT.color.w = tex2D(colors, IN.texCoord.xy).w; 268 269 return OUT; 167 270 } 168 271 169 272 170 273 pixel main(fragment IN, 171 uniform sampler2D colors, 172 uniform sampler2D positions, 173 uniform sampler2D normals) 174 { 175 pixel OUT; 176 177 float4 normal = tex2D(normals, IN.texCoord.xy); 178 float amb = normal.w; 179 180 //OUT.color.xyz = IN.view; 181 // expand normal 182 normal = normalize(normal * 2.0f - float4(1.0f)); 183 184 float4 col = shade(IN, colors, positions, normal.xyz, amb); 185 OUT.color = col; 186 187 return OUT; 188 } 274 uniform sampler2D colors, 275 uniform sampler2D positions, 276 uniform sampler2D normals, 277 uniform sampler2D oldTex 278 ) 279 { 280 pixel OUT; 281 282 float4 normal = tex2D(normals, IN.texCoord.xy); 283 float amb = normal.w; 284 285 //OUT.color.xyz = IN.view; 286 // expand normal 287 normal = normalize(normal * 2.0f - float4(1.0f)); 288 289 float4 col = shade(IN, colors, positions, normal.xyz, amb); 290 //OUT.color = col; 291 292 float4 col1 = tex2D(oldTex, IN.texCoord.xy); 293 294 const float x = 0.1f; 295 OUT.color = col * x + col1 * float4(1.0f - x); 296 297 return OUT; 298 } -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg
r2833 r2834 17 17 float4 worldPos: TEXCOORD1; // world position 18 18 float3 normal: TEXCOORD2; 19 float4 mypos: TEXCOORD3; 19 20 }; 20 21 … … 30 31 float4 worldPos: TEXCOORD1; // world position 31 32 float3 normal: TEXCOORD2; 33 34 float4 mypos: TEXCOORD3; 35 //float DEPTH; 32 36 }; 33 37 … … 42 46 43 47 vtxout vtx(vtxin IN, 44 const uniform float4x4 ModelViewProj,45 uniform float4x4 ModelMatrix)48 const uniform float4x4 ModelViewProj, 49 uniform float4x4 ModelMatrix) 46 50 { 47 51 vtxout OUT; … … 58 62 OUT.normal = IN.normal; 59 63 64 OUT.mypos = OUT.position; 60 65 return OUT; 61 66 } … … 63 68 64 69 pixel fragtex(fragin IN, 65 66 67 68 70 uniform sampler2D tex, 71 uniform float maxDepth, 72 uniform float4 ambient, 73 uniform float4 diffuse) 69 74 { 70 75 pixel pix; … … 77 82 // hack: store projection coordinate for scaling ssao 78 83 pix.pos.w = IN.projPos.w; 84 pix.col.w = IN.mypos.z / IN.mypos.w; 79 85 80 86 return pix; … … 91 97 pix.col = diffuse; 92 98 pix.pos = IN.worldPos * maxDepth; 93 pix.norm.xyz = IN.normal * 0.5f + float 4(0.5f);99 pix.norm.xyz = IN.normal * 0.5f + float3(0.5f); 94 100 // hack: squeeze some information about ambient into the texture 95 101 pix.norm.w = ambient.x; 96 pix.pos.w = IN.projPos.w; 102 pix.pos.w = IN.mypos.w; //IN.projPos.w; 103 pix.col.w = IN.mypos.z / IN.mypos.w; 97 104 98 105 return pix;
Note: See TracChangeset
for help on using the changeset viewer.