├── LICENSE ├── README.md ├── bin ├── PerlinTerrainRaycasting.exe ├── cg.dll ├── cgGL.dll ├── cudart32_30_14.dll ├── glew32.dll ├── glut32.dll ├── msvcp71.dll ├── msvcr71.dll └── raycasting_shader.cg └── src ├── Bmp.cpp ├── Bmp.h ├── Core.cpp ├── Core.h ├── Cuda_Main.cu ├── GLee.c ├── GLee.h ├── Jelly ball.rc ├── Quad 5.ico ├── SSDM.sdf ├── SSDM.sln ├── SSDM.vcproj ├── SSDM.vcxproj ├── SSDM.vcxproj.filters ├── SSDM.vcxproj.user ├── VecMath.cpp ├── VecMath.h ├── Vector3.h ├── fbo.h ├── fbo_2.h ├── main.cpp ├── raycasting_shader.cg └── resource.h /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Spacerat 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Voxel-Terrain-Raycasting-with-SSDM 2 | Voxel Terrain Raycaster with Hitpoint Refinement and Screen Space Displacement Mapping 3 | 4 | The demo is an experiment to combine terrain raycasting of perlin noise generated terrain with screen space ambient occlusions (SSAO) and screen space displacement mapping (SSDM). 5 | 6 | The terrain is generated at the beginning of the demo as a 1024x256x1024 sized perlin noise volume data in CUDA, which is raycasted in the demo using distance functions for acceleration. To improve the quality, third-order texture filtering and hitpoint refinement using binary search are included. 7 | 8 | After the first hit is found, the screen-space normal vector is computed for shading and for the SSDM. The SSDM is not achieved using a stack of textures as this leads to problems in case of overlapping areas for large displacements. Instead, the SSDM is achieved in a post-process using a high-resolution triangle mesh with two triangles per pixel. That is the reason why sometimes articafts near the screen-boundary can be observed. 9 | 10 | The ones of you adept in CG may play around with the shader thats included. 11 | 12 | (Edit) Note: As the program generates the terrain as a 256MB PBO that is copied to the final texture, at least 768MB of GPU memory are recommended. 13 | 14 | Youtube Vid below 15 | 16 | 17 | [![HVOX Engine](http://img.youtube.com/vi/f4bYYWnQbSU/0.jpg)](http://www.youtube.com/watch?v=f4bYYWnQbSU) 18 | -------------------------------------------------------------------------------- /bin/PerlinTerrainRaycasting.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sp4cerat/Voxel-Terrain-Raycasting-with-SSDM/9e50a069b3c6384e68b49fb5f3fdac7cca45d251/bin/PerlinTerrainRaycasting.exe -------------------------------------------------------------------------------- /bin/cg.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sp4cerat/Voxel-Terrain-Raycasting-with-SSDM/9e50a069b3c6384e68b49fb5f3fdac7cca45d251/bin/cg.dll -------------------------------------------------------------------------------- /bin/cgGL.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sp4cerat/Voxel-Terrain-Raycasting-with-SSDM/9e50a069b3c6384e68b49fb5f3fdac7cca45d251/bin/cgGL.dll -------------------------------------------------------------------------------- /bin/cudart32_30_14.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sp4cerat/Voxel-Terrain-Raycasting-with-SSDM/9e50a069b3c6384e68b49fb5f3fdac7cca45d251/bin/cudart32_30_14.dll -------------------------------------------------------------------------------- /bin/glew32.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sp4cerat/Voxel-Terrain-Raycasting-with-SSDM/9e50a069b3c6384e68b49fb5f3fdac7cca45d251/bin/glew32.dll -------------------------------------------------------------------------------- /bin/glut32.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sp4cerat/Voxel-Terrain-Raycasting-with-SSDM/9e50a069b3c6384e68b49fb5f3fdac7cca45d251/bin/glut32.dll -------------------------------------------------------------------------------- /bin/msvcp71.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sp4cerat/Voxel-Terrain-Raycasting-with-SSDM/9e50a069b3c6384e68b49fb5f3fdac7cca45d251/bin/msvcp71.dll -------------------------------------------------------------------------------- /bin/msvcr71.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sp4cerat/Voxel-Terrain-Raycasting-with-SSDM/9e50a069b3c6384e68b49fb5f3fdac7cca45d251/bin/msvcr71.dll -------------------------------------------------------------------------------- /bin/raycasting_shader.cg: -------------------------------------------------------------------------------- 1 | struct app_vertex 2 | { 3 | float4 Position : POSITION; 4 | float4 TexCoord : TEXCOORD1; 5 | float4 Color : COLOR0; 6 | }; 7 | 8 | struct vertex_fragment 9 | { 10 | float4 Position : POSITION; // For the rasterizer 11 | float4 TexCoord : TEXCOORD0; 12 | float4 Color : TEXCOORD1; 13 | float4 Pos : TEXCOORD2; 14 | }; 15 | 16 | struct fragment_out 17 | { 18 | float4 Color : COLOR0; 19 | float Depth : DEPTH; 20 | }; 21 | 22 | float bspline_3d_fp( sampler3D texin, float3 vecin) 23 | { 24 | //return tex3D(texin, vecin).x; 25 | // transform the coordinate from [0,extent] to [-0.5, extent-0.5] 26 | float3 tsize = float3(1024.0,256.0,1024.0); 27 | float3 one_tsize = float3(1.0/tsize.x,1.0/tsize.y,1.0/tsize.z); 28 | float3 coord_grid = vecin*tsize - float3(0.5,0.5,0.5); 29 | float3 fraction = frac(coord_grid); 30 | float3 one_frac = 1.0 - fraction; 31 | float3 one_frac2 = one_frac * one_frac; 32 | float3 fraction2 = fraction * fraction; 33 | float3 w0 = 1.0/6.0 * one_frac2 * one_frac; 34 | float3 w1 = 2.0/3.0 - 0.5 * fraction2 * (2.0-fraction); 35 | float3 w2 = 2.0/3.0 - 0.5 * one_frac2 * (2.0-one_frac); 36 | float3 w3 = 1.0/6.0 * fraction2 * fraction; 37 | float3 g0 = w0 + w1; 38 | float3 g1 = w2 + w3; 39 | float3 index = coord_grid-fraction; 40 | float3 h0 = ((w1 / g0) - 0.5 + index)*one_tsize; 41 | float3 h1 = ((w3 / g1) + 1.5 + index)*one_tsize; 42 | 43 | // fetch the four linear interpolations 44 | float tex000 = tex3D(texin, float3(h0.x, h0.y, h0.z)); 45 | float tex001 = tex3D(texin, float3(h0.x, h1.y, h0.z)); 46 | tex000 = lerp(tex001, tex000, g0.y); 47 | float tex010 = tex3D(texin, float3(h1.x, h0.y, h0.z)); 48 | float tex011 = tex3D(texin, float3(h1.x, h1.y, h0.z)); 49 | tex010 = lerp(tex011, tex010, g0.y); 50 | 51 | float tex100 = tex3D(texin, float3(h0.x, h0.y, h1.z)); 52 | float tex101 = tex3D(texin, float3(h0.x, h1.y, h1.z)); 53 | tex100 = lerp(tex101, tex100, g0.y); 54 | float tex110 = tex3D(texin, float3(h1.x, h0.y, h1.z)); 55 | float tex111 = tex3D(texin, float3(h1.x, h1.y, h1.z)); 56 | tex110 = lerp(tex111, tex110, g0.y); 57 | 58 | tex100 = lerp(tex110, tex100, g0.x); 59 | tex000 = lerp(tex010, tex000, g0.x); 60 | 61 | return lerp(tex100, tex000, g0.z); 62 | } 63 | 64 | float bspline_3d_fp_col( sampler3D texin, float3 vecin) 65 | { 66 | //return tex3D(texin, vecin).x; 67 | // transform the coordinate from [0,extent] to [-0.5, extent-0.5] 68 | float3 tsize = float3(128.0,128.0,128.0); 69 | float3 one_tsize = float3(1.0/tsize.x,1.0/tsize.y,1.0/tsize.z); 70 | float3 coord_grid = vecin*tsize - float3(0.5,0.5,0.5); 71 | float3 fraction = frac(coord_grid); 72 | float3 one_frac = 1.0 - fraction; 73 | float3 one_frac2 = one_frac * one_frac; 74 | float3 fraction2 = fraction * fraction; 75 | float3 w0 = 1.0/6.0 * one_frac2 * one_frac; 76 | float3 w1 = 2.0/3.0 - 0.5 * fraction2 * (2.0-fraction); 77 | float3 w2 = 2.0/3.0 - 0.5 * one_frac2 * (2.0-one_frac); 78 | float3 w3 = 1.0/6.0 * fraction2 * fraction; 79 | float3 g0 = w0 + w1; 80 | float3 g1 = w2 + w3; 81 | float3 index = coord_grid-fraction; 82 | float3 h0 = ((w1 / g0) - 0.5 + index)*one_tsize; 83 | float3 h1 = ((w3 / g1) + 1.5 + index)*one_tsize; 84 | 85 | // fetch the four linear interpolations 86 | float tex000 = tex3D(texin, float3(h0.x, h0.y, h0.z)); 87 | float tex001 = tex3D(texin, float3(h0.x, h1.y, h0.z)); 88 | tex000 = lerp(tex001, tex000, g0.y); 89 | float tex010 = tex3D(texin, float3(h1.x, h0.y, h0.z)); 90 | float tex011 = tex3D(texin, float3(h1.x, h1.y, h0.z)); 91 | tex010 = lerp(tex011, tex010, g0.y); 92 | 93 | float tex100 = tex3D(texin, float3(h0.x, h0.y, h1.z)); 94 | float tex101 = tex3D(texin, float3(h0.x, h1.y, h1.z)); 95 | tex100 = lerp(tex101, tex100, g0.y); 96 | float tex110 = tex3D(texin, float3(h1.x, h0.y, h1.z)); 97 | float tex111 = tex3D(texin, float3(h1.x, h1.y, h1.z)); 98 | tex110 = lerp(tex111, tex110, g0.y); 99 | 100 | tex100 = lerp(tex110, tex100, g0.x); 101 | tex000 = lerp(tex010, tex000, g0.x); 102 | 103 | return lerp(tex100, tex000, g0.z); 104 | } 105 | 106 | vertex_fragment vertex_main( app_vertex IN ) 107 | { 108 | vertex_fragment OUT; 109 | 110 | float4x4 ModelView = glstate.matrix.modelview[0]; 111 | float4x4 ModelViewProj = glstate.matrix.mvp; 112 | 113 | OUT.Position = mul( ModelViewProj, IN.Position ); 114 | OUT.Pos = mul( ModelViewProj, IN.Position ); 115 | OUT.TexCoord = IN.TexCoord; 116 | OUT.Color = IN.Color; 117 | return OUT; 118 | } 119 | 120 | float4 sample_color( sampler3D volume_tex_col, float3 vec ) 121 | { 122 | float mul1=0.5; 123 | float mul2=1.0/8.0; 124 | float4 color_sample = float4(0,0,0,0); 125 | 126 | for(int j = 0; j < 3; j++) 127 | { 128 | color_sample += tex3D(volume_tex_col,vec*mul2)*mul1; 129 | mul1*=0.5; 130 | mul2*=2; 131 | } 132 | return color_sample ; 133 | } 134 | 135 | float sample_alpha2( sampler3D volume_tex, 136 | float3 vec, 137 | float alpha) 138 | { 139 | vec.y*=4.0; 140 | if (alpha>0.66) 141 | return bspline_3d_fp( volume_tex, vec.xyz ); 142 | else 143 | return tex3D(volume_tex,vec.xyz).x; 144 | 145 | } 146 | 147 | float sample_alpha( sampler3D volume_tex, 148 | float3 vec, 149 | float alpha) 150 | { 151 | vec.y*=4.0; 152 | return tex3D(volume_tex,vec.xyz).x; 153 | } 154 | 155 | fragment_out fragment_main( vertex_fragment IN, 156 | float2 position : WPOS, 157 | uniform sampler2D lowres_tex, 158 | uniform sampler3D volume_tex, 159 | uniform sampler3D volume_tex_col, 160 | uniform float4 viewpoint, 161 | uniform float2 screen, 162 | uniform float3 box_pos[2] 163 | ) 164 | { 165 | fragment_out OUT; 166 | 167 | float2 rgba_posi = float2( 168 | (int(int(position.x)/1)*1), 169 | (int(int(position.y)/1)*1) 170 | ); 171 | 172 | float2 rgba_pos0 = float2 ( (rgba_posi.x+0)/screen.x , (rgba_posi.y+0)/screen.y ); 173 | float2 rgba_pos1 = float2 ( (rgba_posi.x+4)/screen.x , (rgba_posi.y-4)/screen.y ); 174 | float2 rgba_pos2 = float2 ( (rgba_posi.x+4)/screen.x , (rgba_posi.y+4)/screen.y ); 175 | float2 rgba_pos3 = float2 ( (rgba_posi.x-4)/screen.x , (rgba_posi.y-4)/screen.y ); 176 | float2 rgba_pos4 = float2 ( (rgba_posi.x-4)/screen.x , (rgba_posi.y+4)/screen.y ); 177 | float dist0 = tex2D(lowres_tex, rgba_pos0 ).x ; 178 | float dist1 = tex2D(lowres_tex, rgba_pos1 ).x ; 179 | float dist2 = tex2D(lowres_tex, rgba_pos2 ).x ; 180 | float dist3 = tex2D(lowres_tex, rgba_pos3 ).x ; 181 | float dist4 = tex2D(lowres_tex, rgba_pos4 ).x ; 182 | if(dist10)&& vec.y<0.5&& vec.y>-0.5) ; 364 | while(alpha0)&& lightpos.y>0) ; 365 | 366 | 367 | float shado = 0.0; 368 | if (lightpos.y<=0) shado = 1.0; 369 | 370 | OUT.Depth = dist3/4.5f;//dist3/4.5f; 371 | OUT.Color.x = shado ; 372 | 373 | return OUT; 374 | } 375 | 376 | fragment_out fragment_ssdm 377 | ( 378 | vertex_fragment IN, 379 | float2 position : WPOS, 380 | uniform float4 matrix1, 381 | uniform float4 matrix2, 382 | uniform float4 matrix3, 383 | uniform sampler2D zbuf_tex, 384 | uniform sampler2D rgba_tex, 385 | uniform float2 screen 386 | ) 387 | { 388 | fragment_out OUT; 389 | 390 | float2 rgba_pos = float2( position.x/screen.x , position.y/screen.y ); 391 | float4 rgba = tex2D(rgba_tex, rgba_pos ) ; 392 | float depth = tex2D(zbuf_tex, rgba_pos ).x/25 +0.0001; 393 | float displace = rgba.w; 394 | rgba.w=1.0; 395 | 396 | rgba.xyz = rgba.xyz*2.0-1.0; 397 | 398 | float4 ss_normal = float4 ( 399 | dot ( rgba.xyz, matrix1.xyz ), 400 | dot ( rgba.xyz, matrix2.xyz ), 401 | dot ( rgba.xyz, matrix3.xyz ), 402 | 1.0); 403 | 404 | float xs = rgba_pos.x * 2.0 - 1.0; 405 | float ys = rgba_pos.y * 2.0 - 1.0; 406 | float dx = ss_normal.x * displace * 0.01; 407 | float dy = ss_normal.y * displace * 0.01; 408 | float dz = ss_normal.z * displace * 0.0001; 409 | float2 dxs = (xs*depth + dx)/(depth-dz)-xs ; 410 | float2 dys = (ys*depth + dy)/(depth-dz)-ys ; 411 | 412 | //OUT.Color.x = ss_normal.x * displace*0.5+0.5;//dxs*0.1+0.5; 413 | //OUT.Color.y = ss_normal.y * displace*0.5+0.5;//dxs*0.1+0.5; 414 | OUT.Color.x = dxs*0.1+0.5; 415 | OUT.Color.y = dys*0.1+0.5; 416 | OUT.Color.z = depth*22.0;//(depth+dz)*4.0; 417 | OUT.Color.w = ss_normal.z;//displace; 418 | 419 | //OUT.Color = ss_normal*displace*0.5-0.5; 420 | 421 | return OUT; 422 | } 423 | 424 | fragment_out fragment_ssdm_mip 425 | ( 426 | vertex_fragment IN, 427 | float2 position : WPOS, 428 | uniform sampler2D rgba_tex, 429 | uniform float2 screen 430 | ) 431 | { 432 | fragment_out OUT; 433 | 434 | float2 rgba_pos1= float2( (position.x+0.5)/screen.x , (position.y+0.5)/screen.y ); 435 | float2 rgba_pos2= float2( (position.x+0.5)/screen.x , (position.y-0.5)/screen.y ); 436 | float2 rgba_pos3= float2( (position.x-0.5)/screen.x , (position.y+0.5)/screen.y ); 437 | float2 rgba_pos4= float2( (position.x-0.5)/screen.x , (position.y-0.5)/screen.y ); 438 | 439 | float4 rgba = 440 | ( 441 | tex2D(rgba_tex, rgba_pos1 ) + 442 | tex2D(rgba_tex, rgba_pos2 ) + 443 | tex2D(rgba_tex, rgba_pos3 ) + 444 | tex2D(rgba_tex, rgba_pos4 ) 445 | ) * 0.25; 446 | 447 | OUT.Color = rgba; 448 | 449 | return OUT; 450 | } 451 | 452 | 453 | fragment_out fragment_ssdm_final 454 | ( 455 | vertex_fragment IN, 456 | float2 position : WPOS, 457 | uniform sampler2D ssdm_m0, 458 | uniform sampler2D ssdm_m1, 459 | uniform sampler2D ssdm_m2, 460 | uniform sampler2D ssdm_m3, 461 | uniform sampler2D rgba_tex, 462 | uniform float2 screen 463 | ) 464 | { 465 | fragment_out OUT; 466 | 467 | float ax,ay; 468 | 469 | ax = 20.0 /screen.x ; 470 | ay = 20.0 /screen.y ; 471 | 472 | float2 tex_pos = float2( position.x/screen.x , position.y/screen.y ); 473 | float2 tex_nearest = tex_pos ; 474 | 475 | float xs = tex_pos.x * 2.0 - 1.0; 476 | float ys = tex_pos.y * 2.0 - 1.0; 477 | 478 | float4 delta = (tex2D( ssdm_m0, tex_pos ) * 2.0 - 1.0)*20.0/1024.0; 479 | float dist = delta.x*delta.x+delta.y*delta.y; 480 | float depth = delta.z; 481 | 482 | for (float x=-ax;x 511 | 512 | vertex_fragment vertex_ssdm_displace( 513 | app_vertex IN 514 | ,uniform sampler2D ssdm_tex 515 | ,uniform sampler2D rgba_tex 516 | ) 517 | { 518 | vertex_fragment OUT; 519 | 520 | float4x4 ModelView = glstate.matrix.modelview[0]; 521 | float4x4 ModelViewProj = glstate.matrix.mvp; 522 | 523 | float4 displace = 524 | tex2D(ssdm_tex, IN.Position.xy)+ 525 | tex2D(rgba_tex,float2(0,0))*0.0001; 526 | 527 | // if(0) 528 | //if (displace.w<0.5) 529 | { 530 | float rad=0.003; 531 | float4 d0 = displace; 532 | float4 d1 = tex2D(ssdm_tex, IN.Position.xy+float2(rad,rad)); 533 | float4 d2 = tex2D(ssdm_tex, IN.Position.xy+float2(rad,-rad)); 534 | float4 d3 = tex2D(ssdm_tex, IN.Position.xy+float2(-rad,rad)); 535 | float4 d4 = tex2D(ssdm_tex, IN.Position.xy+float2(-rad,-rad)); 536 | float4 d5 = tex2D(ssdm_tex, IN.Position.xy+float2(0.0000,rad)); 537 | float4 d6 = tex2D(ssdm_tex, IN.Position.xy+float2(0.0000,-rad)); 538 | float4 d7 = tex2D(ssdm_tex, IN.Position.xy+float2( rad,0.000)); 539 | float4 d8 = tex2D(ssdm_tex, IN.Position.xy+float2(-rad,0.000)); 540 | 541 | float mz = (d0.z*3+d1.z+d2.z+d3.z+d4.z+d5.z+d6.z+d7.z+d8.z)/(8.0+3.0); 542 | 543 | rad=0.009/(300*d0.z+0.01); 544 | d0 = displace; 545 | d1 = tex2D(ssdm_tex, IN.Position.xy+float2(rad,rad)); 546 | d2 = tex2D(ssdm_tex, IN.Position.xy+float2(rad,-rad)); 547 | d3 = tex2D(ssdm_tex, IN.Position.xy+float2(-rad,rad)); 548 | d4 = tex2D(ssdm_tex, IN.Position.xy+float2(-rad,-rad)); 549 | d5 = tex2D(ssdm_tex, IN.Position.xy+float2(0.0000,rad)); 550 | d6 = tex2D(ssdm_tex, IN.Position.xy+float2(0.0000,-rad)); 551 | d7 = tex2D(ssdm_tex, IN.Position.xy+float2( rad,0.000)); 552 | d8 = tex2D(ssdm_tex, IN.Position.xy+float2(-rad,0.000)); 553 | 554 | displace = (displace+d1+d2+d3+d4+d5+d6+d7+d8)/(8.0+1.0); 555 | } 556 | 557 | OUT.Position = mul( ModelViewProj, IN.Position ) ; 558 | float4 displace2d = displace; 559 | displace2d.z=0.0; 560 | displace2d.w=0.0; 561 | OUT.Position += (displace2d*2.0-1.0)*0.04; 562 | OUT.Position.z = displace.z; 563 | OUT.Position.xy = OUT.Position.xy * 1.2 ; 564 | OUT.TexCoord = IN.Position;//IN.TexCoord; 565 | //OUT.Color = displace;//IN.TexCoord; 566 | 567 | return OUT; 568 | } 569 | 570 | fragment_out fragment_ssdm_displace 571 | ( 572 | vertex_fragment IN 573 | ,float2 position : WPOS 574 | ,uniform sampler2D ssdm_tex 575 | ,uniform sampler2D rgba_tex 576 | ,uniform float2 screen 577 | ) 578 | { 579 | fragment_out OUT; 580 | 581 | //OUT.Color = IN.Color; return OUT; 582 | 583 | float4 displace = tex2D(ssdm_tex, IN.TexCoord.xy); 584 | float4 rgba = tex2D(rgba_tex, IN.TexCoord.xy)+displace*0.0001; 585 | 586 | float a = smoothstep ( 0.7 , 0.9 , rgba.y); 587 | 588 | float4 colorup =lerp ( 589 | float4(0.5,0.5,0.5,0.0)*(rgba.w*1.0+0.0)*1.2, 590 | float4(0.4,1.0,0.4,0.0)*(rgba.w*0.8+0.1)*1.0, 591 | a); 592 | //max(0.0 , min(1.0, rgba.y )) 593 | //); 594 | colorup =max(0,(rgba.y*1.0)) *colorup ; 595 | 596 | OUT.Color = colorup; 597 | 598 | /* 599 | 600 | float2 pos = float2( position.x/screen.x , position.y/screen.y ); 601 | float4 bgcol = float4(pos.y,pos.y*0.8+0.2,0.5+pos.y*0.5,0); 602 | 603 | OUT.Color = lerp( 604 | colorup , 605 | //float4(0.5,0.6,0.7,0), 606 | bgcol, 607 | max(0.0,min(1.0, 1.5-300.0/(200.0+displace.z*displace.z*1000)))); 608 | 609 | if ( displace.z > 0.85 ) OUT.Color = bgcol; 610 | 611 | //rgba*(displace.w*0.5+0.5);//IN.Color; 612 | 613 | */ 614 | return OUT; 615 | } 616 | 617 | fragment_out fragment_ssao 618 | ( 619 | vertex_fragment IN 620 | ,float2 position : WPOS 621 | ,uniform sampler2D depth_tex 622 | ,uniform sampler2D color_tex 623 | ,uniform sampler2D shadow_tex 624 | ,uniform float2 screen 625 | ) 626 | { 627 | fragment_out OUT; 628 | 629 | float2 tex0 = IN.Pos.xy*0.5+0.5; 630 | float z0 = tex2D(depth_tex, tex0); 631 | float shadow= tex2D(shadow_tex, ((tex0-0.5)/1.2)+0.5).x; 632 | float bright= 0.0; 633 | float4 rgba = tex2D(color_tex, tex0); 634 | 635 | float rndx = 2345.2345; 636 | float rndy = 5335.7126; 637 | float rndz = 7831.3452; 638 | 639 | float seedx = frac(frac(tex0.x*16.87635)+frac(tex0.x*4.45363)+tex0.x); 640 | float seedy = frac(frac(tex0.y*16.87635)+frac(tex0.y*4.45363)+tex0.y); 641 | 642 | float twopi=3.141592653589793*2.0; 643 | float addx = ( 644 | frac(seedy*156.32862416+seedx*156.086243456)+ 645 | frac(seedy*65.64896876264647)+ 646 | frac(seedx*35.3468523532347)+ 647 | frac(seedy*5.8342857334647)+ 648 | frac(seedx*5.256463876844267) 649 | )*twopi; 650 | float radius = 2*(1.0-z0*z0);//100.0/(100.0+z0*z0*1000); 651 | 652 | float4 normal=tex2D(color_tex, tex0 )*2.0-1.0; 653 | 654 | for (float a=0;a<1.0;a+=1.0/10.0) 655 | { 656 | float factor=sqrt(a); 657 | rndx = sin(a*30+addx)*factor*radius*0.12512; 658 | rndy = cos(a*30+addx)*factor*radius*0.12512; 659 | 660 | float2 delta_xy = tex0 + float2( rndx,rndy ) ; 661 | 662 | if (delta_xy.x > 0) 663 | if (delta_xy.y > 0) 664 | if (delta_xy.x < 1) 665 | if (delta_xy.y < 1) 666 | { 667 | float z=z0-tex2D(depth_tex, delta_xy); 668 | bright+=sign(z)*min(abs(z),0.01)*(1.0-factor); 669 | } 670 | 671 | //bright+=smoothstep(-0.01*z0+0.0005,0.01*z0+0.0005,0.01*z*(1.0-factor)); 672 | } 673 | 674 | bright = max(min(bright*30.0+0.0,1.0),0.0); 675 | 676 | float4 bgcol = float4(tex0.y,tex0.y*0.8+0.2,0.5+tex0.y*0.5,0); 677 | //OUT.Color = tex2D(color_tex, tex0); 678 | 679 | z0=z0*2.0-1.0; 680 | 681 | OUT.Color = lerp( 682 | max(float4(0,0,0,0),rgba*(1.0-bright)),//+(1.0-z0*z0)*0.1)) , 683 | // float4(0.5,0.6,0.7,0), 684 | bgcol, 685 | max(0, 686 | min(1, 687 | pow(z0*1.25 ,0.3)*1.3-0.3 688 | )) 689 | 690 | );// 691 | //max(0.0,min(1.0, 1.0-100.0/(100+z0-0.5)*2*1000)))); 692 | 693 | if ( z0 > 0.85 ) OUT.Color = bgcol; 694 | 695 | //if(IN.Pos.x<0) 696 | // OUT.Color = 1.0-bright;//tex2D(color_tex, tex0);//1.0-bright; 697 | 698 | return OUT; 699 | } 700 | 701 | //float4(0.5,0.6,0.7,0), 702 | //OUT.Color = z0;//tex2D(color_tex, tex0)*bright; 703 | 704 | //if ( z > z0+delta_z*0.01 ) bright+=1.0/20.0; 705 | 706 | //if(abs(z)<0.01+0.0015) 707 | 708 | //rndx = frac( rndx*a+a*34622.34563263+232.23451 )* 2.0-1.0; 709 | //rndy = frac( rndy*a+a*14562.14678323+523.12451 )* 2.0-1.0; 710 | //rndz = frac( rndz*a+a*13562.83325621+223.82451 )* 2.0-1.0; 711 | 712 | 713 | //float nx = float(int(normal_pos.x*127+128))/256.0; 714 | //float ny = float(int(normal_pos.y*127+128))/256.0; 715 | //float nz = float(int(normal_pos.z*127+128))/256.0; 716 | //float nxyz = nx*256.0+ny+nz*(1.0/256.0); 717 | //float nx2 = frac(nxyz/256.0); 718 | //float ny2 = frac(nxyz); 719 | //float nz2 = frac(nxyz*256); 720 | 721 | -------------------------------------------------------------------------------- /src/Bmp.cpp: -------------------------------------------------------------------------------- 1 | //#################################################################// 2 | #include "Bmp.h" 3 | //#################################################################// 4 | Bmp::Bmp() 5 | { 6 | width=height=depth=0; 7 | data=NULL; 8 | } 9 | //#################################################################// 10 | Bmp::Bmp(const char*filename) 11 | { 12 | width=height=depth=0; 13 | data=NULL; 14 | load(filename); 15 | } 16 | //#################################################################// 17 | Bmp::Bmp(int x,int y,int b,unsigned char*buffer) 18 | { 19 | width=height=depth=0; 20 | data=NULL; 21 | set(x,y,b,buffer); 22 | } 23 | //#################################################################// 24 | Bmp::~Bmp() 25 | { 26 | if (data) free(data); 27 | } 28 | //#################################################################// 29 | bool Bmp::save(const char*filename) 30 | { 31 | unsigned char bmp[58]= 32 | {0x42,0x4D,0x36,0x30,0,0,0,0,0,0,0x36,0,0,0,0x28,0,0,0, 33 | 0x40,0,0,0, // X-Size 34 | 0x40,0,0,0, // Y-Size 35 | 1,0,0x18,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; 36 | 37 | bmp[18] =width; 38 | bmp[19] =width>>8; 39 | bmp[22] =height; 40 | bmp[23] =height>>8; 41 | bmp[28] =bpp; 42 | 43 | FILE* fn; 44 | if ((fn = fopen (filename,"wb")) != NULL) 45 | { 46 | fwrite(bmp ,1,54 ,fn); 47 | fwrite(data,1,width*height*(bpp/8),fn); 48 | fclose(fn); 49 | return true; 50 | } 51 | return false; 52 | } 53 | //#################################################################// 54 | bool Bmp::addalpha(unsigned char r,unsigned char g,unsigned char b) 55 | { 56 | if(bpp==32)return true; 57 | 58 | unsigned char *data32=(unsigned char*)malloc(width*height*4); 59 | int x,y; 60 | for(x=0;x=0;y--) 180 | { 181 | int ofs = (y*width+ix)*(bpp/8); 182 | bool b = (data[ofs+0]>128) ? true : false; 183 | bool g = (data[ofs+1]>128) ? true : false; 184 | bool r = (data[ofs+2]>128) ? true : false; 185 | 186 | if (find_r) if (!r) 187 | { 188 | find_r = false; 189 | y_r = y; 190 | } 191 | if (find_g) if (!g) 192 | { 193 | find_g = false; 194 | y_g = y; 195 | } 196 | if (find_b) if (!b) 197 | { 198 | find_b = false; 199 | y_b = y; 200 | } 201 | } 202 | return vec3f ( 203 | float(y_r)/float(height), 204 | float(y_g)/float(height), 205 | float(y_b)/float(height) 206 | ); 207 | } 208 | //#################################################################// 209 | vec3f Bmp::getPixel(float x,float y) 210 | { 211 | if(x<0)x=0; 212 | if(y<0)y=0; 213 | if(x>1)x=1; 214 | if(y>1)y=1; 215 | int ofs = (int(y*float(height-1))*width+int(x*float(width-1)) )*(bpp/8); 216 | float b = float(data[ofs+0])/255.0f; 217 | float g = float(data[ofs+1])/255.0f; 218 | float r = float(data[ofs+2])/255.0f; 219 | return vec3f(r,g,b); 220 | } 221 | //#################################################################// 222 | inline int Bmp::sampleByte(int x,int y) 223 | { 224 | return (data[( ((height-1-y)%height)*width+x%width )*(bpp/8)]); 225 | } 226 | //#################################################################// 227 | inline int Bmp::sampleMap(int x,int y) 228 | { 229 | return (255-data[( (y%height)*width+x%width )*(bpp/8)]); 230 | } 231 | //#################################################################// 232 | bool Bmp::normalMap(void) 233 | { 234 | unsigned char* tmpData=(unsigned char*)malloc(width*height*3); 235 | 236 | for(int y=0;y>8; 375 | bmp[22] =height; 376 | bmp[23] =height>>8; 377 | bmp[28] =bpp; 378 | 379 | return true; 380 | } 381 | //#################################################################// 382 | bool Bmp::set3d(int x,int y,int z,int b,unsigned char*buffer) 383 | { 384 | width=x; 385 | height=y; 386 | depth=z; 387 | bpp=b; 388 | if(data) free(data); 389 | data=buffer; 390 | if(data==NULL) 391 | { 392 | data=(unsigned char*) malloc(width*height*depth*(bpp/8)); 393 | memset(data,0,width*height*depth*(bpp/8)); 394 | } 395 | 396 | bmp[18] =width; 397 | bmp[19] =width>>8; 398 | bmp[22] =height; 399 | bmp[23] =height>>8; 400 | bmp[28] =bpp; 401 | 402 | return true; 403 | } 404 | //#################################################################// 405 | bool Bmp::load(const char *filename,bool checktransparency,int check_r,int check_g, int check_b) 406 | { 407 | FILE* handle; 408 | 409 | if(filename==NULL) 410 | {printf("File not found %s !\n",filename);while(1);;} 411 | if((char)filename[0]==0) 412 | {printf("File not found %s !\n",filename);while(1);;} 413 | 414 | if ((handle = fopen(filename, "rb")) == NULL) 415 | {printf("File not found %s !\n",filename);while(1);;} 416 | 417 | if(!fread(bmp, 11, 1, handle)) 418 | { 419 | printf("Error reading file %s!\n",filename); 420 | return false; 421 | } 422 | if(!fread(&bmp[11], (int)((unsigned char)bmp[10])-11, 1, handle)) 423 | { 424 | printf("Error reading file %s!\n",filename); 425 | return false; 426 | } 427 | 428 | width =(int)((unsigned char)bmp[18])+((int)((unsigned char)(bmp[19]))<<8); 429 | height =(int)((unsigned char)bmp[22])+((int)((unsigned char)(bmp[23]))<<8); 430 | bpp =bmp[28]; 431 | 432 | if(width*height>2048*2048) 433 | { 434 | printf("Error reading file %s - too big!\n",filename); 435 | fclose(handle); 436 | return false; 437 | } 438 | 439 | if(data)free(data); 440 | // if(data_tmp)free(data_tmp); 441 | // if(lens_lookup)free(lens_lookup); 442 | 443 | // data_tmp=(unsigned char*)malloc(width*height*(bpp/8)); 444 | // memset(data_tmp,0,width*height*(bpp/8)); 445 | 446 | switch (bpp) 447 | { 448 | case 24: 449 | short i,r,g,b,alpha; 450 | alpha=false; 451 | data=(unsigned char*)malloc(width*height*4); 452 | fread(data,width*height*3,1,handle); 453 | for(i=width*height-1;i>=0;i--) 454 | { 455 | b=data[i*3+0]; 456 | g=data[i*3+1]; 457 | r=data[i*3+2]; 458 | if((r==check_r)&&(g==check_g)&&(b==check_b)) 459 | alpha=true; 460 | } 461 | 462 | if(!checktransparency) break; 463 | 464 | if (alpha) 465 | { 466 | bpp=32; 467 | for(i=width*height-1;i>=0;i--) 468 | { 469 | b=data[i*4+0]=data[i*3+0]; 470 | g=data[i*4+1]=data[i*3+1]; 471 | r=data[i*4+2]=data[i*3+2]; 472 | data[i*4+3]=255; 473 | if((r==check_r)&&(g==check_g)&&(b==check_b)) 474 | { 475 | data[i*4+0]=0; 476 | data[i*4+1]=0; 477 | data[i*4+2]=0; 478 | data[i*4+3]=0; 479 | } 480 | } 481 | } 482 | break; 483 | 484 | case 32: 485 | data=(unsigned char*)malloc(width*height*4); 486 | fread(data,width*height*4,1,handle); 487 | break; 488 | 489 | default: 490 | printf("unknown Format - %dx%dx%d Bit",width,height,bpp); 491 | fclose(handle); 492 | return false; 493 | } 494 | 495 | fclose(handle); 496 | printf("read successfully %s ; %dx%dx%d Bit \n",filename,width,height,bpp); 497 | 498 | return true; 499 | } 500 | 501 | //#################################################################// 502 | -------------------------------------------------------------------------------- /src/Bmp.h: -------------------------------------------------------------------------------- 1 | #ifndef _Bmp_class 2 | #define _Bmp_class 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "VecMath.h" 9 | 10 | class Bmp 11 | { 12 | public: 13 | 14 | Bmp(); 15 | Bmp(int x,int y,int bpp,unsigned char*data); 16 | Bmp(const char*filename); 17 | ~Bmp(); 18 | 19 | bool load(const char*filename, 20 | bool checktransparency=false, 21 | int check_r=0, 22 | int check_g=0, 23 | int check_b=0); 24 | 25 | bool save(const char*filename); 26 | 27 | vec3f getSxSyT(float x); 28 | int sampleByte(int x,int y); 29 | bool set(int x,int y,int bpp,unsigned char*data); 30 | bool set3d(int x,int y,int z,int bpp,unsigned char*buffer); 31 | void crop(int x,int y); 32 | bool scale(int x,int y); 33 | bool blur(int count); 34 | bool hblur(int count); 35 | bool vblur(int count); 36 | bool addalpha(unsigned char r,unsigned char g,unsigned char b); 37 | bool normalize(void); 38 | bool normalMap(void); 39 | vec3f getPixel(float x,float y); 40 | vec3f get_f_fdx_fdy(float x,float y); 41 | 42 | private: 43 | 44 | int sampleMap(int i,int j); 45 | 46 | public: 47 | 48 | unsigned char*data; 49 | int width; 50 | int height; 51 | int depth; 52 | int bpp; 53 | 54 | private: 55 | 56 | unsigned char bmp[54]; 57 | }; 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /src/Core.cpp: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////////////////////////// 2 | #include "core.h" 3 | //////////////////////////////////////////////////////////////////////////////// 4 | // Global variables 5 | //////////////////////////////////////////////////////////////////////////////// 6 | Keyboard keyboard; 7 | Mouse mouse; 8 | Screen screen; 9 | //////////////////////////////////////////////////////////////////////////////// 10 | 11 | -------------------------------------------------------------------------------- /src/Core.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | //////////////////////////////////////////////////////////////////////////////// 3 | #define SCREEN_SIZE_X 1024 4 | #define SCREEN_SIZE_Y 768 5 | //#define SCREEN_SIZE_Y 624 6 | #define RENDER_SIZE 2048 7 | #define VIEW_DIST_MAX 300000 8 | 9 | #define THREAD_COUNT_X 8 //16 10 | #define THREAD_COUNT_Y 32 11 | 12 | #define PERSISTENT_LG_X 5 13 | #define PERSISTENT_LG_Y 1 14 | 15 | #define PERSISTENT_X (1< 2 | #include 3 | #include 4 | #include 5 | 6 | #define M_PI 3.141592653589793238462643 7 | 8 | int timeGetTime(){return 0;}; 9 | 10 | #define IN_CUDA_ENV 11 | //#ifdef _WIN32 12 | //# define WINDOWS_LEAN_AND_MEAN 13 | //# include 14 | //#endif 15 | #include 16 | #include 17 | #include "cutil_math.h" 18 | //////////////////////////////////////////////////////////////////////////////// 19 | #define C_CHECK_GL_ERROR() ChkGLError(__FILE__, __LINE__) 20 | //////////////////////////////////////////////////////////////////////////////// 21 | extern "C" bool pboRegister(int pbo); 22 | extern "C" void pboUnregister(int pbo); 23 | extern "C" void* cuda_main( int pbo_out, 24 | int size_x, 25 | int size_y, 26 | int size_z, 27 | int level, 28 | float box_x,float box_y,float box_z, 29 | float pos_x,float pos_y,float pos_z ); 30 | //////////////////////////////////////////////////////////////////////////////// 31 | #define THREAD_COUNT_X 16 32 | #define THREAD_COUNT_Y 16 33 | #define THREAD_COUNT_Z 1 34 | #define uchar unsigned char 35 | //////////////////////////////////////////////////////////////////////////////// 36 | void gpu_memcpy(void* dst, void* src, int size) 37 | { 38 | CUDA_SAFE_CALL( cudaMemcpy( dst, src, size, cudaMemcpyHostToDevice) ); 39 | CUT_CHECK_ERROR("cudaMemcpy cudaMemcpyHostToDevice failed"); 40 | } 41 | //////////////////////////////////////////////////////////////////////////////// 42 | void cpu_memcpy(void* dst, void* src, int size) 43 | { 44 | CUDA_SAFE_CALL( cudaMemcpy( dst, src, size, cudaMemcpyDeviceToHost) ); 45 | CUT_CHECK_ERROR("cudaMemcpy cudaMemcpyDeviceToHost failed"); 46 | } 47 | //////////////////////////////////////////////////////////////////////////////// 48 | void* gpu_malloc(int size) 49 | { 50 | void* ptr=0; 51 | CUDA_SAFE_CALL( cudaMalloc( (void**) &ptr, size ) ); 52 | CUT_CHECK_ERROR("cudaMalloc failed"); 53 | if(ptr==0){printf("\ncudaMalloc %d MB: out of memory error\n",(size>>20));while(1);;} 54 | return ptr; 55 | } 56 | //////////////////////////////////////////////////////////////////////////////// 57 | texture texRnd; // 3D texture 58 | texture texBrush; // 3D texture 59 | cudaArray *d_rnd_volumeArray = 0; 60 | cudaArray *d_brush_volumeArray = 0; 61 | //////////////////////////////////////////////////////////////////////////////// 62 | bool init_rnd_texture(int size) 63 | { 64 | uchar *h_volume = new uchar [size*size*size]; 65 | 66 | for(int x = 0; x < size; x++) 67 | { 68 | printf("slice:%d \r" ,x); 69 | for(int y = 0; y < size; y++) 70 | {for(int z = 0; z < size; z++) 71 | { 72 | h_volume[(x)+ (y * size ) + (z * size * size )] = rand()&255; 73 | }}} 74 | 75 | cudaExtent volumeSize = make_cudaExtent(size, size, size); 76 | 77 | // create 3D array 78 | cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(); 79 | cudaMalloc3DArray(&d_rnd_volumeArray, &channelDesc, volumeSize) ; 80 | 81 | // copy data to 3D array 82 | cudaMemcpy3DParms copyParams = {0}; 83 | copyParams.srcPtr = make_cudaPitchedPtr((void*)h_volume, volumeSize.width*sizeof(uchar), volumeSize.width, volumeSize.height); 84 | copyParams.dstArray = d_rnd_volumeArray; 85 | copyParams.extent = volumeSize; 86 | copyParams.kind = cudaMemcpyHostToDevice; 87 | cudaMemcpy3D(©Params) ; 88 | 89 | // set texture parameters 90 | texRnd.normalized = true; // access with normalized texture coordinates 91 | texRnd.filterMode = cudaFilterModeLinear; // linear interpolation 92 | texRnd.addressMode[0] = cudaAddressModeWrap; // wrap texture coordinates 93 | texRnd.addressMode[1] = cudaAddressModeWrap; 94 | texRnd.addressMode[2] = cudaAddressModeWrap; 95 | 96 | // bind array to 3D texture 97 | cudaBindTextureToArray(texRnd, d_rnd_volumeArray, channelDesc); 98 | 99 | delete []h_volume; 100 | 101 | return true; 102 | } 103 | //////////////////////////////////////////////////////////////////////////////// 104 | __device__ int getTerrainVal( 105 | int size_x, int size_y, int size_z, 106 | int x, int y , int z, float scale, 107 | float box_x,float box_y,float box_z, 108 | float p_x,float p_y,float p_z ) 109 | { 110 | //p_x = p_y = p_z = 0; 111 | p_x /= scale; 112 | p_y /= scale; 113 | p_z /= scale; 114 | float add_x = int(p_x+1000); 115 | float add_y = int(p_y+1000); 116 | float add_z = int(p_z+1000); 117 | float frac_x= p_x + 1000 - add_x; 118 | float frac_y= p_y + 1000 - add_y; 119 | float frac_z= p_z + 1000 - add_z; 120 | add_x -= 1000; 121 | add_y -= 1000; 122 | add_z -= 1000; 123 | 124 | float x01 = float(x) / size_x ; 125 | float y01 = float(y) / size_y ; 126 | float z01 = float(z) / (size_z-1) ; 127 | 128 | if (frac_x!=0) if (x01 -box_x - 0.5 ) 142 | if ( xf_in < -box_x + 0.5 ) 143 | if ( yf_in > -box_y - 0.5 ) 144 | if ( yf_in < -box_y + 0.5 ) 145 | if ( zf_in > -box_z - 0.5 ) 146 | if ( zf_in < -box_z + 0.5 ) return -1; 147 | */ 148 | 149 | float v=0.0; 150 | 151 | for (float a=0.3,b=0.6;b>0.01;a*=2,b*=0.5) 152 | { 153 | v += tex3D(texRnd,0.5+ xf_in*a+a*999, yf_in*a, zf_in*a) * b; 154 | } 155 | v += -yf_in*8; 156 | // v=0; 157 | 158 | 159 | if(0) 160 | for (float objx = -0.1 ; objx <= 0.1 ; objx+=0.02 ) 161 | for (float objz = -0.1 ; objz <= 0.1 ; objz+=0.02 ) 162 | { 163 | float sinx=sin(objx*327.5); 164 | float cosx=cos(objx*437.5); 165 | float sinz=sin(objz*455.9); 166 | float cosz=cos(objz*655.9); 167 | 168 | float obj_size = 16.0 / 1.0;//tex3D(texRnd, objx * 3.4 , objz * 2.6, 0.35 )*1.0+1.0; 169 | 170 | float xfa = xf_in + objx; 171 | float yfa = yf_in ;//+ 0.25; 172 | float zfa = zf_in + objz; 173 | 174 | if ( xfa*xfa+yfa*yfa+zfa*zfa > 0.5 ) continue; 175 | 176 | xfa *= obj_size; 177 | yfa *= obj_size; 178 | zfa *= obj_size; 179 | 180 | float xfb = xfa * cosx - zfa * sinx; 181 | float yfb = yfa; 182 | float zfb = zfa * cosx + xfa * sinx; 183 | 184 | float xf = xfb * sinz - yfb * cosz ; 185 | float yf = yfb * sinz + xfb * cosz ; 186 | float zf = zfb ; 187 | 188 | // v = max( v, 1.0-sqrt(xf*xf+yf*yf+zf*zf)*2.0); 189 | v = max( v, tex3D(texBrush, 2.0*xf+0.5, yf+0.5, zf+0.5) ); 190 | // v += tex3D(texBrush, xf+0.5, yf+0.5, zf+0.5) ; 191 | } 192 | 193 | return float( min ( max ( v * 255.0f , 0.0f ), 255.0f )); 194 | } 195 | //////////////////////////////////////////////////////////////////////////////// 196 | __global__ void 197 | cudaTerrainKernel( unsigned int* data, 198 | int size_x , 199 | int size_y , 200 | int size_z , 201 | float anim , float scale, 202 | float box_x,float box_y,float box_z, 203 | float p_x,float p_y,float p_z ) 204 | { 205 | extern __shared__ int sdata[]; 206 | 207 | int x = blockIdx.x * blockDim.x + threadIdx.x; 208 | int y = blockIdx.y * blockDim.y + threadIdx.y; 209 | 210 | int ofs = x+y*(size_x/4); 211 | int add = size_x*size_y/4; 212 | 213 | for (int z=0;z0 && z 0.2/(obj_size*0.3)) continue; 318 | 319 | float nx = abs(xf) / len; 320 | float ny = abs(yf) / len; 321 | float nz = abs(zf) / len; 322 | 323 | float az = abs(atan2( xf,yf )); 324 | float ay = abs(atan2( xf,zf )); 325 | float ax = abs(atan2( yf,zf )); 326 | 327 | float axz = 0.2; 328 | float axy = 0.2; 329 | float ayz = 0.2; 330 | 331 | for (float a=0.003,b=0.75;a<1.4;a*=2.0,b*=0.5) 332 | { 333 | axz += tex3D(texRnd, objx*3+ax*a,objz*3+az*a ,1.0-objx*3.23 )*b; 334 | axy += tex3D(texRnd, objx*3+ax*a,objz*3+ay*a ,objz*1.234 )*b; 335 | ayz += tex3D(texRnd, objx*3+ay*a,objz*3+az*a ,objx+objz*5.22 )*b; 336 | } 337 | 338 | axz*=tex3D(texRnd, objx*14.4 , objz*15.0 , 0.4 )*2+0.5; 339 | axy*=tex3D(texRnd, objx*44.4 , objz*74.0 , 0.5 )*2+0.5; 340 | ayz*=tex3D(texRnd, objx*34.4 , objz*11.0 , 0.7 )*2+0.5; 341 | 342 | 343 | float displace = 344 | max( ayz*(nx*1.0-0.0) , 0.0 ) + 345 | max( axz*(ny*1.0-0.0) , 0.0 ) + 346 | max( axy*(nz*1.0-0.0) , 0.0 ) ; 347 | 348 | // v = max( v , 1.0-(1.0-displace*0.8) * obj_size * len ); 349 | float len2 = sqrt ( xf*xf*ayz*ayz + yf*yf*axz*axz + zf*zf*axy*axy ); 350 | v = max( v , 1.0-obj_size * len2 ); 351 | //v = min( v , max(1.0-obj_size * len2,0.0) ); 352 | } 353 | 354 | int vi = float( min ( max ( v * 255.0f , 0.0f ), 255.0f )); 355 | 356 | return vi; 357 | } 358 | //////////////////////////////////////////////////////////////////////////////// 359 | __global__ void 360 | cudaGenBrushKernel( int* data, int size , float anim ) 361 | { 362 | extern __shared__ int sdata[]; 363 | 364 | int x = blockIdx.x * blockDim.x + threadIdx.x; 365 | int y = blockIdx.y * blockDim.y + threadIdx.y; 366 | 367 | int ofs = x+y*(size/4); 368 | int add = size*size/4; 369 | 370 | for (int z=0;z>> ( h_volume , size , 0 ); 394 | 395 | cudaExtent volumeSize = make_cudaExtent(size, size, size); 396 | 397 | // create 3D array 398 | cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(); 399 | cudaMalloc3DArray(&d_brush_volumeArray, &channelDesc, volumeSize) ; 400 | 401 | // copy data to 3D array 402 | cudaMemcpy3DParms copyParams = {0}; 403 | copyParams.srcPtr = make_cudaPitchedPtr((void*)h_volume, volumeSize.width*sizeof(uchar), volumeSize.width, volumeSize.height); 404 | copyParams.dstArray = d_brush_volumeArray; 405 | copyParams.extent = volumeSize; 406 | copyParams.kind = cudaMemcpyDeviceToDevice; 407 | cudaMemcpy3D(©Params) ; 408 | 409 | // set texture parameters 410 | texBrush.normalized = true; // access with normalized texture coordinates 411 | texBrush.filterMode = cudaFilterModeLinear; // linear interpolation 412 | texBrush.addressMode[0] = cudaAddressModeClamp; // wrap texture coordinates 413 | texBrush.addressMode[1] = cudaAddressModeClamp; 414 | texBrush.addressMode[2] = cudaAddressModeClamp; 415 | 416 | // bind array to 3D texture 417 | cudaBindTextureToArray(texBrush, d_brush_volumeArray, channelDesc); 418 | 419 | return true; 420 | } 421 | //////////////////////////////////////////////////////////////////////////////// 422 | extern "C" void* cuda_main( int pbo_out, 423 | int size_x, 424 | int size_y, 425 | int size_z, 426 | int level, 427 | float box_x,float box_y,float box_z, 428 | float pos_x,float pos_y,float pos_z ) 429 | { 430 | static bool init1 = init_rnd_texture( 128 ); 431 | static bool init2 = init_brush_texture( 128 ); 432 | if(pbo_out==0) return NULL; 433 | 434 | int t0 = timeGetTime(); 435 | 436 | unsigned int* out_data; 437 | CUDA_SAFE_CALL(cudaGLMapBufferObject( (void**)&out_data, pbo_out)); 438 | if(out_data==0) return NULL; 439 | 440 | dim3 threads(THREAD_COUNT_X,THREAD_COUNT_Y,1 ); 441 | dim3 grid (size_x/(4*threads.x) , size_y/threads.y ,1 ); 442 | 443 | float anim = float((timeGetTime()>>4)&1023)*2*M_PI / 1024; 444 | 445 | cudaTerrainKernel<<< grid, threads, 0>>> ( 446 | out_data, 447 | size_x , 448 | size_y, 449 | size_z , 450 | anim, float(int(1< 2 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | 26 | 29 | 32 | 35 | 38 | 41 | 54 | 57 | 60 | 63 | 76 | 79 | 82 | 85 | 88 | 91 | 94 | 97 | 100 | 101 | 109 | 112 | 115 | 118 | 121 | 124 | 134 | 137 | 140 | 143 | 155 | 158 | 161 | 164 | 167 | 170 | 173 | 176 | 179 | 180 | 181 | 182 | 183 | 184 | 189 | 192 | 193 | 196 | 197 | 200 | 201 | 204 | 205 | 208 | 211 | 216 | 217 | 220 | 225 | 226 | 227 | 230 | 231 | 234 | 235 | 238 | 239 | 242 | 243 | 246 | 247 | 248 | 251 | 254 | 255 | 258 | 259 | 262 | 263 | 266 | 267 | 268 | 271 | 272 | 275 | 276 | 279 | 280 | 281 | 282 | 283 | 284 | -------------------------------------------------------------------------------- /src/SSDM.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | 2010.04.13.SSDM 15 | {0B814284-C76C-4274-895F-0D987181B4A4} 16 | raycasting tut 17 | Win32Proj 18 | 19 | 20 | 21 | Application 22 | v110 23 | MultiByte 24 | 25 | 26 | Application 27 | v110 28 | MultiByte 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | <_ProjectFileVersion>11.0.50727.1 44 | 45 | 46 | Debug\ 47 | Debug\ 48 | false 49 | 50 | 51 | Release\ 52 | Release\ 53 | false 54 | 55 | 56 | 57 | Disabled 58 | ..\inc;%(AdditionalIncludeDirectories) 59 | WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) 60 | true 61 | EnableFastChecks 62 | MultiThreadedDebug 63 | 64 | Level3 65 | EditAndContinue 66 | 67 | 68 | cudart.lib;cutil32.lib;libGlee.lib;%(AdditionalDependencies) 69 | ..\bin\GPU_raycasting.exe 70 | ..\lib;%(AdditionalLibraryDirectories) 71 | false 72 | LIBCMT.lib;libcpmt.lib;%(IgnoreSpecificDefaultLibraries) 73 | true 74 | $(OutDir)Level_representation.pdb 75 | Console 76 | MachineX86 77 | 78 | 79 | 80 | 81 | ..\inc;%(AdditionalIncludeDirectories) 82 | WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) 83 | MultiThreaded 84 | 85 | Level3 86 | ProgramDatabase 87 | 88 | 89 | cudart.lib;cutil32.lib;libGlee.lib;%(AdditionalDependencies) 90 | ..\bin\GPU_raycasting.exe 91 | ..\lib;%(AdditionalLibraryDirectories) 92 | true 93 | Console 94 | true 95 | true 96 | MachineX86 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | "$(CUDA_BIN_PATH)\nvcc.exe" --use_fast_math --ptxas-options=-v --maxrregcount=64 -ccbin "$(VCInstallDir)bin" -c -DWIN32 -D_CONSOLE -D_MBCS -Xcompiler /EHsc,/W0,/nologo,/Wp64,/O2,/Zi,/MT -I"$(CUDA_INC_PATH)" -I./ -I../inc -o $(Configuration)\cuda_main.obj cuda_main.cu 114 | 115 | $(Configuration)\Cuda_Main.obj;%(Outputs) 116 | "$(CUDA_BIN_PATH)\nvcc.exe" --use_fast_math --ptxas-options=-v --maxrregcount=64 -ccbin "$(VCInstallDir)bin" -c -DWIN32 -D_CONSOLE -D_MBCS -Xcompiler /EHsc,/W0,/nologo,/Wp64,/O2,/Zi,/MT -I"$(CUDA_INC_PATH)" -I./ -I../inc -o $(Configuration)\cuda_main.obj cuda_main.cu 117 | 118 | $(Configuration)\Cuda_Main.obj;%(Outputs) 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | -------------------------------------------------------------------------------- /src/SSDM.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {61db1fb8-3205-4eeb-a0f0-5ed87ca17611} 10 | 11 | 12 | {be8bb90a-5ca5-48e3-836b-24ef9e1b312b} 13 | 14 | 15 | 16 | 17 | Source Files 18 | 19 | 20 | Source Files 21 | 22 | 23 | Source Files 24 | 25 | 26 | Source Files 27 | 28 | 29 | 30 | 31 | Source Files 32 | 33 | 34 | Source Files 35 | 36 | 37 | Source Files 38 | 39 | 40 | Source Files 41 | 42 | 43 | 44 | 45 | Source Files 46 | 47 | 48 | 49 | 50 | libs 51 | 52 | 53 | libs 54 | 55 | 56 | libs 57 | 58 | 59 | libs 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | Source Files 71 | 72 | 73 | -------------------------------------------------------------------------------- /src/SSDM.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | ..\bin\GPU_raycasting.exe 5 | WindowsLocalDebugger 6 | 7 | -------------------------------------------------------------------------------- /src/VecMath.cpp: -------------------------------------------------------------------------------- 1 | #include "VecMath.h" 2 | 3 | int vec3f::random_number = 3457734; 4 | 5 | void vec3f::random_init() 6 | { 7 | random_number = 3457734; 8 | } 9 | 10 | float vec3f::random_float(){ 11 | int t = random_number; 12 | t = (t * 2345633 + t*1245 + t*356 + t*35 + t/34 + t/325 - 8647445); 13 | random_number = t; 14 | 15 | return ( float (abs(t)%10000)/10000); 16 | } 17 | 18 | vec3f vec3f::random(){ 19 | static vec3f rnd; 20 | rnd.x=random_float()*2-1; 21 | rnd.y=random_float()*2-1; 22 | rnd.z=random_float()*2-1; 23 | rnd.normalize(); 24 | return rnd; 25 | } 26 | 27 | vec3f vec3f::normalize( vec3f a ) 28 | { 29 | float square = a.x*a.x + a.y*a.y + a.z*a.z; 30 | if (square <= 0.00001f ) 31 | { 32 | a.x=1;a.y=0;a.z=0; 33 | return a; 34 | } 35 | float len = 1.0f / (float)sqrt(square); 36 | a.x*=len;a.y*=len;a.z*=len; 37 | return a; 38 | } 39 | 40 | 41 | /* 42 | bool lines_intersect ( vec3f line1[2] ,vec3f line2[2] , vec3f* intersection = NULL) 43 | { 44 | // BBox 45 | 46 | if ( line1[0].x < line2[0].x ) if ( line1[0].x < line2[1].x ) if ( line1[1].x < line2[0].x ) if ( line1[1].x < line2[1].x ) return false; 47 | if ( line1[0].x > line2[0].x ) if ( line1[0].x > line2[1].x ) if ( line1[1].x > line2[0].x ) if ( line1[1].x > line2[1].x ) return false; 48 | if ( line1[0].y > line2[0].y ) if ( line1[0].y > line2[1].y ) if ( line1[1].y > line2[0].y ) if ( line1[1].y > line2[1].y ) return false; 49 | if ( line1[0].y < line2[0].y ) if ( line1[0].y < line2[1].y ) if ( line1[1].y < line2[0].y ) if ( line1[1].y < line2[1].y ) return false; 50 | 51 | // intersect 52 | 53 | 54 | //a x + b = c y + d 55 | //a x - c y = d - b 56 | 57 | //- a2 a1 x + a2 c1 y =-a2 d1 + a2 b1 58 | // a1 a2 x - a1 c2 y = a1 d2 - a1 b2 59 | 60 | // y = ( a1 d2 - a1 b2 - a2 d1 + a2 b1 ) / ( a2 c1 - a1 c2 ) 61 | 62 | 63 | vec3f delta1 = line1[1] - line1[0]; 64 | vec3f delta2 = line2[1] - line2[0]; 65 | vec3f point1 = line1[0]; 66 | vec3f point2 = line2[0]; 67 | 68 | float div1 = delta1.y*delta2.x - delta1.x*delta2.y; 69 | float div2 = delta2.y*delta1.x - delta2.x*delta1.y; 70 | 71 | float a = 2; 72 | 73 | if ( div1 != 0) 74 | { 75 | a = ( delta1.x*point2.y - 76 | delta1.x*point1.y - 77 | delta1.y*point2.x + 78 | delta1.y*point1.x ) / div1; 79 | }else 80 | if ( div2 != 0) 81 | { 82 | a = ( delta2.x*point1.y - 83 | delta2.x*point2.y - 84 | delta2.y*point1.x + 85 | delta2.y*point2.x ) / div2; 86 | } 87 | if ( a <= 1 ) 88 | if ( a >= 0 ) 89 | { 90 | if (intersection) 91 | { 92 | if ( div1 != 0) 93 | *intersection = delta2 * a + point2; 94 | else 95 | *intersection = delta1 * a + point1; 96 | } 97 | return true; 98 | } 99 | 100 | return false; 101 | }; 102 | */ 103 | -------------------------------------------------------------------------------- /src/VecMath.h: -------------------------------------------------------------------------------- 1 | #ifndef _VecMath_class 2 | #define _VecMath_class 3 | 4 | //#include 5 | #include 6 | 7 | #ifndef vec3i 8 | struct vec3i { int x,y,z;}; 9 | #endif 10 | 11 | //#define __USE_SSE__ 12 | 13 | #ifndef IN_CUDA_ENV 14 | #include "mathlib/Vector.h" 15 | #include "mathlib/Matrix.h" 16 | #endif 17 | 18 | struct vec3f 19 | { 20 | float x, y, z; 21 | 22 | inline vec3f( void ) {} 23 | 24 | //inline vec3f operator =( vector3 a ) 25 | // { vec3f b ; b.x = a.x; b.y = a.y; b.z = a.z; return b;} 26 | 27 | #ifndef IN_CUDA_ENV 28 | inline vec3f( vector3 a ) 29 | { x = a.x; y = a.y; z = a.z; } 30 | #endif 31 | 32 | inline vec3f( const float X, const float Y, const float Z ) 33 | { x = X; y = Y; z = Z; } 34 | 35 | inline vec3f operator + ( const vec3f& a ) const 36 | { return vec3f( x + a.x, y + a.y, z + a.z ); } 37 | 38 | inline vec3f operator += ( const vec3f& a ) const 39 | { return vec3f( x + a.x, y + a.y, z + a.z ); } 40 | 41 | inline vec3f operator * ( const float a ) const 42 | { return vec3f( x * a, y * a, z * a ); } 43 | 44 | inline vec3f operator * ( const vec3f a ) const 45 | { return vec3f( x * a.x, y * a.y, z * a.z ); } 46 | 47 | #ifndef IN_CUDA_ENV 48 | inline vector3 v3 () const 49 | { return vector3( x , y, z ); } 50 | #endif 51 | 52 | #ifndef IN_CUDA_ENV 53 | inline vec3f operator = ( const vector3 a ) 54 | { x=a.x;y=a.y;z=a.z;return *this; } 55 | #endif 56 | 57 | inline vec3f operator = ( const vec3f a ) 58 | { x=a.x;y=a.y;z=a.z;return *this; } 59 | 60 | inline vec3f operator / ( const vec3f a ) const 61 | { return vec3f( x / a.x, y / a.y, z / a.z ); } 62 | 63 | inline vec3f operator - ( const vec3f& a ) const 64 | { return vec3f( x - a.x, y - a.y, z - a.z ); } 65 | 66 | inline vec3f operator / ( const float a ) const 67 | { return vec3f( x / a, y / a, z / a ); } 68 | 69 | inline float dot( const vec3f& a ) const 70 | { return a.x*x + a.y*y + a.z*z; } 71 | 72 | inline vec3f cross( const vec3f& a , const vec3f& b ) 73 | { 74 | x = a.y * b.z - a.z * b.y; 75 | y = a.z * b.x - a.x * b.z; 76 | z = a.x * b.y - a.y * b.x; 77 | return *this; 78 | } 79 | 80 | inline float angle( const vec3f& v ) 81 | { 82 | vec3f a = v , b = *this; 83 | float dot = v.x*x + v.y*y + v.z*z; 84 | float len = a.length() * b.length(); 85 | if(len==0)len=0.00001f; 86 | float input = dot / len; 87 | if (input<-1) input=-1; 88 | if (input>1) input=1; 89 | return (float) acos ( input ); 90 | } 91 | 92 | inline float angle2( const vec3f& v , const vec3f& w ) 93 | { 94 | vec3f a = v , b= *this; 95 | float dot = a.x*b.x + a.y*b.y + a.z*b.z; 96 | float len = a.length() * b.length(); 97 | if(len==0)len=1; 98 | 99 | vec3f plane; plane.cross( b,w ); 100 | 101 | if ( plane.x * a.x + plane.y * a.y + plane.z * a.z > 0 ) 102 | return (float) -acos ( dot / len ); 103 | 104 | return (float) acos ( dot / len ); 105 | } 106 | 107 | inline vec3f rot_x( float a ) 108 | { 109 | float yy = cos ( a ) * y + sin ( a ) * z; 110 | float zz = cos ( a ) * z - sin ( a ) * y; 111 | y = yy; z = zz; 112 | return *this; 113 | } 114 | inline vec3f rot_y( float a ) 115 | { 116 | float xx = cos ( -a ) * x + sin ( -a ) * z; 117 | float zz = cos ( -a ) * z - sin ( -a ) * x; 118 | x = xx; z = zz; 119 | return *this; 120 | } 121 | inline void clamp( float min, float max ) 122 | { 123 | if (xmax) x=max; 127 | if (y>max) y=max; 128 | if (z>max) z=max; 129 | } 130 | inline vec3f rot_z( float a ) 131 | { 132 | float yy = cos ( a ) * y + sin ( a ) * x; 133 | float xx = cos ( a ) * x - sin ( a ) * y; 134 | y = yy; x = xx; 135 | return *this; 136 | } 137 | inline vec3f invert() 138 | { 139 | x=-x;y=-y;z=-z;return *this; 140 | } 141 | inline vec3f frac() 142 | { 143 | return vec3f( 144 | x-float(int(x)), 145 | y-float(int(y)), 146 | z-float(int(z)) 147 | ); 148 | } 149 | 150 | inline vec3f integer() 151 | { 152 | return vec3f( 153 | float(int(x)), 154 | float(int(y)), 155 | float(int(z)) 156 | ); 157 | } 158 | 159 | inline float length() const 160 | { 161 | return (float)sqrt(x*x + y*y + z*z); 162 | } 163 | 164 | inline vec3f normalize( float desired_length = 1 ) 165 | { 166 | float square = x*x + y*y + z*z; 167 | if (square <= 0.00001f ) 168 | { 169 | x=1;y=0;z=0; 170 | return *this; 171 | } 172 | float len = desired_length / (float)sqrt(square); 173 | x*=len;y*=len;z*=len; 174 | return *this; 175 | } 176 | static vec3f normalize( vec3f a ); 177 | 178 | static void random_init(); 179 | static float random_float(); 180 | static vec3f random(); 181 | 182 | static int random_number; 183 | }; 184 | 185 | /* 186 | class Matrix 187 | { 188 | public: 189 | 190 | float m[16]; 191 | 192 | void identity() 193 | { 194 | static float identity[16]={ 195 | 1,0,0,0, 196 | 0,1,0,0, 197 | 0,0,1,0, 198 | 0,0,0,1 199 | }; 200 | memcpy( m,identity,16 ); 201 | } 202 | 203 | Matrix( vec3f a,vec3f b,vec3f c,vec3f d = vec3f(0,0,0)) { 204 | 205 | identity() ; 206 | 207 | m[ 0] = a.x;m[ 1] = b.x;m[ 2] = c.x;m[ 3] = d.x; 208 | m[ 4] = a.y;m[ 5] = b.y;m[ 6] = c.y;m[ 7] = d.y; 209 | m[ 8] = a.z;m[ 9] = b.z;m[10] = c.z;m[11] = d.z; 210 | m[15]=1; 211 | } 212 | 213 | Matrix( void ) { 214 | 215 | identity() ; 216 | } 217 | 218 | inline vec3f operator * ( const vec3f& a ) const 219 | { 220 | vec3f out; 221 | out.x = a.x*m[ 0] + a.y*m[ 4] + a.z*m[ 8] + m[12]; 222 | out.y = a.x*m[ 1] + a.y*m[ 5] + a.z*m[ 9] + m[13]; 223 | out.z = a.x*m[ 2] + a.y*m[ 6] + a.z*m[10] + m[14]; 224 | // out.x = a.x*m[ 0] + a.y*m[ 1] + a.z*m[ 2] + m[ 3]; 225 | // out.y = a.x*m[ 4] + a.y*m[ 5] + a.z*m[ 6] + m[ 7]; 226 | // out.z = a.x*m[ 8] + a.y*m[ 9] + a.z*m[10] + m[11]; 227 | // out.x = a.x*m[ 0] + a.x*m[ 1] + a.x*m[ 2] + m[ 3]; 228 | // out.y = a.y*m[ 4] + a.y*m[ 5] + a.y*m[ 6] + m[ 7]; 229 | // out.z = a.z*m[ 8] + a.z*m[ 9] + a.z*m[10] + m[11]; 230 | return out; 231 | } 232 | 233 | Matrix operator * ( const Matrix& a ) const 234 | { 235 | int i,j,k; 236 | Matrix result; 237 | 238 | for (i=0;i<4;i++) 239 | for (j=0;j<4;j++) 240 | { 241 | result.m[i+j*4]=0; 242 | 243 | for (k=0;k<4;k++) 244 | result.m[i+j*4] += a.m[i+k*4] * m[j*4+k] ; 245 | } 246 | return result; 247 | } 248 | }; 249 | */ 250 | #endif -------------------------------------------------------------------------------- /src/Vector3.h: -------------------------------------------------------------------------------- 1 | #ifndef VECTOR3_H 2 | #define VECTOR3_H 3 | 4 | 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | class Vector3 { 15 | public: 16 | 17 | Vector3() { e[0] = 0; e[1] = 0; e[2] = 0;} 18 | Vector3(float e0, float e1, float e2) {e[0]=e0; e[1]=e1; e[2]=e2;} 19 | float x() const { return e[0]; } 20 | float y() const { return e[1]; } 21 | float z() const { return e[2]; } 22 | void setX(float a) { e[0] = a; } 23 | void setY(float a) { e[1] = a; } 24 | void setZ(float a) { e[2] = a; } 25 | 26 | inline Vector3(const Vector3 &v) { 27 | e[0] = v.e[0]; e[1] = v.e[1]; e[2] = v.e[2]; 28 | } 29 | 30 | const Vector3& operator+() const { return *this; } 31 | Vector3 operator-() const { return Vector3(-e[0], -e[1], -e[2]); } 32 | float& operator[](int i) { return e[i]; } 33 | float operator[](int i) const { return e[i];} 34 | 35 | Vector3& operator+=(const Vector3 &v2); 36 | Vector3& operator-=(const Vector3 &v2); 37 | Vector3& operator*=(const float t); 38 | Vector3& operator/=(const float t); 39 | 40 | 41 | 42 | float length() const { return sqrt(e[0]*e[0] + e[1]*e[1] + e[2]*e[2]); } 43 | float squaredLength() const { return e[0]*e[0] + e[1]*e[1] + e[2]*e[2]; } 44 | 45 | void makeUnitVector(); 46 | 47 | 48 | float minComponent() const { return e[indexOfMinComponent()]; } 49 | float maxComponent() const { return e[indexOfMaxComponent()]; } 50 | float maxAbsComponent() const { return e[indexOfMaxAbsComponent()]; } 51 | float minAbsComponent() const { return e[indexOfMinAbsComponent()]; } 52 | int indexOfMinComponent() const { 53 | return (e[0]< e[1] && e[0]< e[2]) ? 0 : (e[1] < e[2] ? 1 : 2); 54 | } 55 | 56 | int indexOfMinAbsComponent() const { 57 | if (fabs(e[0]) < fabs(e[1]) && fabs(e[0]) < fabs(e[2])) 58 | return 0; 59 | else if (fabs(e[1]) < fabs(e[2])) 60 | return 1; 61 | else 62 | return 2; 63 | } 64 | 65 | int indexOfMaxComponent() const { 66 | return (e[0]> e[1] && e[0]> e[2]) ? 0 : (e[1] > e[2] ? 1 : 2); 67 | } 68 | 69 | int indexOfMaxAbsComponent() const { 70 | if (fabs(e[0]) > fabs(e[1]) && fabs(e[0]) > fabs(e[2])) 71 | return 0; 72 | else if (fabs(e[1]) > fabs(e[2])) 73 | return 1; 74 | else 75 | return 2; 76 | } 77 | 78 | float e[3]; 79 | }; 80 | 81 | 82 | inline bool operator==(const Vector3 &t1, const Vector3 &t2) { 83 | return ((t1[0]==t2[0])&&(t1[1]==t2[1])&&(t1[2]==t2[2])); 84 | } 85 | 86 | inline bool operator!=(const Vector3 &t1, const Vector3 &t2) { 87 | return ((t1[0]!=t2[0])||(t1[1]!=t2[1])||(t1[2]!=t2[2])); 88 | } 89 | 90 | inline istream &operator>>(istream &is, Vector3 &t) { 91 | is >> t[0] >> t[1] >> t[2]; 92 | return is; 93 | } 94 | 95 | inline ostream &operator<<(ostream &os, const Vector3 &t) { 96 | os << t[0] << " " << t[1] << " " << t[2]; 97 | return os; 98 | } 99 | 100 | inline Vector3 unitVector(const Vector3& v) { 101 | float k = 1.0f / sqrt(v.e[0]*v.e[0] + v.e[1]*v.e[1] + v.e[2]*v.e[2]); 102 | return Vector3(v.e[0]*k, v.e[1]*k, v.e[2]*k); 103 | } 104 | 105 | inline void Vector3::makeUnitVector() { 106 | float k = 1.0f / sqrt(e[0]*e[0] + e[1]*e[1] + e[2]*e[2]); 107 | e[0] *= k; e[1] *= k; e[2] *= k; 108 | } 109 | 110 | inline Vector3 operator+(const Vector3 &v1, const Vector3 &v2) { 111 | return Vector3( v1.e[0] + v2.e[0], v1.e[1] + v2.e[1], v1.e[2] + v2.e[2]); 112 | } 113 | 114 | inline Vector3 operator-(const Vector3 &v1, const Vector3 &v2) { 115 | return Vector3( v1.e[0] - v2.e[0], v1.e[1] - v2.e[1], v1.e[2] - v2.e[2]); 116 | } 117 | 118 | inline Vector3 operator*(float t, const Vector3 &v) { 119 | return Vector3(t*v.e[0], t*v.e[1], t*v.e[2]); 120 | } 121 | 122 | inline Vector3 operator*(const Vector3 &v, float t) { 123 | return Vector3(t*v.e[0], t*v.e[1], t*v.e[2]); 124 | } 125 | 126 | inline Vector3 operator/(const Vector3 &v, float t) { 127 | return Vector3(v.e[0]/t, v.e[1]/t, v.e[2]/t); 128 | } 129 | 130 | 131 | inline float dot(const Vector3 &v1, const Vector3 &v2) { 132 | return v1.e[0] *v2.e[0] + v1.e[1] *v2.e[1] + v1.e[2] *v2.e[2]; 133 | } 134 | 135 | inline Vector3 cross(const Vector3 &v1, const Vector3 &v2) { 136 | return Vector3( (v1.e[1]*v2.e[2] - v1.e[2]*v2.e[1]), 137 | (v1.e[2]*v2.e[0] - v1.e[0]*v2.e[2]), 138 | (v1.e[0]*v2.e[1] - v1.e[1]*v2.e[0])); 139 | } 140 | 141 | 142 | inline Vector3& Vector3::operator+=(const Vector3 &v){ 143 | e[0] += v.e[0]; 144 | e[1] += v.e[1]; 145 | e[2] += v.e[2]; 146 | return *this; 147 | } 148 | 149 | inline Vector3& Vector3::operator-=(const Vector3& v) { 150 | e[0] -= v.e[0]; 151 | e[1] -= v.e[1]; 152 | e[2] -= v.e[2]; 153 | return *this; 154 | } 155 | 156 | inline Vector3& Vector3::operator*=(const float t) { 157 | e[0] *= t; 158 | e[1] *= t; 159 | e[2] *= t; 160 | return *this; 161 | } 162 | 163 | inline Vector3& Vector3::operator/=(const float t) { 164 | e[0] /= t; 165 | e[1] /= t; 166 | e[2] /= t; 167 | return *this; 168 | } 169 | 170 | inline 171 | Vector3 reflect(const Vector3& in, const Vector3& normal) 172 | { 173 | // assumes unit length normal 174 | return in - normal * (2 * dot(in, normal)); 175 | } 176 | 177 | 178 | #endif 179 | -------------------------------------------------------------------------------- /src/fbo.h: -------------------------------------------------------------------------------- 1 | class FBO { 2 | 3 | public: 4 | 5 | enum Type { RGBA8=1 ,ALPHA32F=2 , RGBA32F=3 ,ALPHA16F=4 , RGBA16F=5 }; 6 | 7 | int color_tex; 8 | int color_bpp; 9 | int depth_tex; 10 | int depth_bpp; 11 | Type type; 12 | 13 | int width; 14 | int height; 15 | 16 | int tmp_viewport[4]; 17 | 18 | FBO (int texWidth,int texHeight,Type type_in=RGBA8)//,Type type = Type(COLOR | DEPTH),int color_bpp=32,int depth_bpp=24) 19 | { 20 | color_tex = -1; 21 | depth_tex = -1; 22 | fbo = -1; 23 | dbo = -1; 24 | init (texWidth, texHeight, type_in); 25 | } 26 | 27 | void clear () 28 | { 29 | if(color_tex!=-1) 30 | { 31 | // destroy objects 32 | glDeleteRenderbuffersEXT(1, &dbo); 33 | glDeleteTextures(1, (GLuint*)&color_tex); 34 | glDeleteTextures(1, (GLuint*)&depth_tex); 35 | glDeleteFramebuffersEXT(1, &fbo); 36 | } 37 | } 38 | 39 | void enable() 40 | { 41 | //glBindTexture(GL_TEXTURE_2D, 0); 42 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); 43 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, dbo); 44 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, 45 | GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, color_tex, 0); 46 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, 47 | GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, depth_tex, 0); 48 | 49 | glGetIntegerv(GL_VIEWPORT, tmp_viewport); 50 | glViewport(0, 0, width, height); // Reset The Current Viewport And Perspective Transformation 51 | //glMatrixMode(GL_PROJECTION); 52 | //glPushMatrix(); 53 | //glLoadIdentity(); 54 | //gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f); 55 | //glMatrixMode(GL_MODELVIEW); 56 | 57 | } 58 | 59 | void disable() 60 | { 61 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 62 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0); 63 | glViewport( 64 | tmp_viewport[0], 65 | tmp_viewport[1], 66 | tmp_viewport[2], 67 | tmp_viewport[3]); 68 | //glMatrixMode(GL_PROJECTION); 69 | //glPopMatrix(); 70 | //glMatrixMode(GL_MODELVIEW); 71 | } 72 | 73 | void init (int texWidth,int texHeight,Type type_in)// = Type(COLOR | DEPTH),int color_bpp=32,int depth_bpp=24) 74 | { 75 | // clear (); 76 | this->width = texWidth; 77 | this->height= texHeight; 78 | this->type = type_in; 79 | 80 | glGenFramebuffersEXT(1, &fbo); 81 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); 82 | get_error(); 83 | 84 | // init texture 85 | glGenTextures(1, (GLuint*)&color_tex); 86 | glBindTexture(GL_TEXTURE_2D, color_tex); 87 | 88 | // rgba8 89 | if(type==RGBA8) 90 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 91 | texWidth, texHeight, 0, 92 | GL_RGBA, GL_UNSIGNED_BYTE, NULL);//GL_RGBA 93 | 94 | // r float 95 | if(type==ALPHA32F) 96 | glTexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY32F_ARB,//GL_ALPHA32F_ARB,//GL_RGBA8, 97 | texWidth, texHeight, 0, 98 | GL_LUMINANCE, GL_FLOAT, NULL);//GL_RGBA 99 | 100 | if(type==RGBA32F) 101 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB,//GL_ALPHA32F_ARB,//GL_RGBA8, 102 | texWidth, texHeight, 0, 103 | GL_RGBA, GL_FLOAT, NULL);//GL_RGBA 104 | 105 | if(type==ALPHA16F) 106 | glTexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY16F_ARB,//GL_ALPHA32F_ARB,//GL_RGBA8, 107 | texWidth, texHeight, 0, 108 | GL_LUMINANCE, GL_FLOAT, NULL);//GL_RGBA 109 | 110 | if(type==RGBA16F) 111 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB,//GL_ALPHA32F_ARB,//GL_RGBA8, 112 | texWidth, texHeight, 0, 113 | GL_RGBA, GL_FLOAT, NULL);//GL_RGBA 114 | 115 | //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, 116 | // GL_RGBA, GL_UNSIGNED_BYTE, NULL); 117 | 118 | //GL_TEXTURE_2D,GL_RGBA, bmp.width, bmp.height, 119 | // /*GL_RGBA*/GL_BGRA_EXT, GL_UNSIGNED_BYTE, bmp.data ); 120 | 121 | get_error(); 122 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);//GL_NEAREST);//GL_LINEAR);// 123 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);//GL_NEAREST);//GL_LINEAR);// 124 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 125 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 126 | 127 | glFramebufferTexture2DEXT( 128 | GL_FRAMEBUFFER_EXT, 129 | GL_COLOR_ATTACHMENT0_EXT, 130 | GL_TEXTURE_2D, color_tex, 0); 131 | get_error(); 132 | 133 | glGenRenderbuffersEXT(1, &dbo); 134 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, dbo); 135 | 136 | glGenTextures(1, (GLuint*)&depth_tex); 137 | glBindTexture(GL_TEXTURE_2D, depth_tex); 138 | //glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, 139 | // texWidth, texHeight, 0, 140 | // GL_DEPTH_COMPONENT, GL_FLOAT, NULL); 141 | get_error(); 142 | //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE); 143 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); 144 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); 145 | glTexParameteri (GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE); 146 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 147 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 148 | 149 | /* 150 | //Use generated mipmaps if supported 151 | if(GLEE_SGIS_generate_mipmap) 152 | { 153 | glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, true); 154 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 155 | glHint(GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST); 156 | } 157 | 158 | //Use maximum anisotropy if supported 159 | if(GLEE_EXT_texture_filter_anisotropic) 160 | { 161 | GLint maxAnisotropy=1; 162 | glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAnisotropy); 163 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, maxAnisotropy); 164 | } 165 | */ 166 | 167 | glTexImage2D (GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, 168 | texWidth, texHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT/*GL_UNSIGNED_INT*/, NULL); 169 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, 170 | GL_TEXTURE_2D, depth_tex, 0); 171 | 172 | get_error(); 173 | glBindTexture(GL_TEXTURE_2D, 0);// don't leave this texture bound or fbo (zero) will use it as src, want to use it just as dest GL_DEPTH_ATTACHMENT_EXT 174 | 175 | get_error(); 176 | 177 | check_framebuffer_status(); 178 | 179 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 180 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0); 181 | } 182 | 183 | private: 184 | 185 | GLuint fbo; // frame buffer object ref 186 | GLuint dbo; // depth buffer object ref 187 | 188 | void get_error() 189 | { 190 | GLenum err = glGetError(); 191 | if (err != GL_NO_ERROR) 192 | { 193 | printf("GL FBO Error: %s\n",gluErrorString(err)); 194 | printf("Programm Stopped!\n"); 195 | while(1);; 196 | } 197 | } 198 | 199 | void check_framebuffer_status() 200 | { 201 | GLenum status; 202 | status = (GLenum) glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); 203 | switch(status) { 204 | case GL_FRAMEBUFFER_COMPLETE_EXT: 205 | return; 206 | break; 207 | case GL_FRAMEBUFFER_UNSUPPORTED_EXT: 208 | printf("Unsupported framebuffer format\n"); 209 | break; 210 | case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT: 211 | printf("Framebuffer incomplete, missing attachment\n"); 212 | break; 213 | // case GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT_EXT: 214 | // printf("Framebuffer incomplete, duplicate attachment\n"); 215 | // break; 216 | case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT: 217 | printf("Framebuffer incomplete, attached images must have same dimensions\n"); 218 | break; 219 | case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT: 220 | printf("Framebuffer incomplete, attached images must have same format\n"); 221 | break; 222 | case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT: 223 | printf("Framebuffer incomplete, missing draw buffer\n"); 224 | break; 225 | case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT: 226 | printf("Framebuffer incomplete, missing read buffer\n"); 227 | break; 228 | case 0: 229 | printf("Not ok but trying...\n"); 230 | return; 231 | break; 232 | default:; 233 | printf("Framebuffer error code %d\n",status); 234 | break; 235 | }; 236 | printf("Programm Stopped!\n"); 237 | while(1)Sleep(100);; 238 | } 239 | }; 240 | -------------------------------------------------------------------------------- /src/fbo_2.h: -------------------------------------------------------------------------------- 1 | class FBO { 2 | 3 | public: 4 | 5 | enum Type { RGBA8=1 ,ALPHA32F=2 , RGBA32F=3 ,ALPHA16F=4 , RGBA16F=5 }; 6 | 7 | int color_tex; 8 | int color_bpp; 9 | int depth_tex; 10 | int depth_bpp; 11 | Type type; 12 | 13 | int width; 14 | int height; 15 | 16 | int tmp_viewport[4]; 17 | 18 | FBO (int texWidth,int texHeight,Type type_in=RGBA8)//,Type type = Type(COLOR | DEPTH),int color_bpp=32,int depth_bpp=24) 19 | { 20 | color_tex = -1; 21 | depth_tex = -1; 22 | fbo = -1; 23 | dbo = -1; 24 | init (texWidth, texHeight, type_in); 25 | } 26 | 27 | void clear () 28 | { 29 | if(color_tex!=-1) 30 | { 31 | // destroy objects 32 | //glDeleteRenderbuffersEXT(1, &dbo); 33 | glDeleteFramebuffersEXT(1, &fbo); 34 | glDeleteTextures(1, (GLuint*)&color_tex); 35 | glDeleteTextures(1, (GLuint*)&depth_tex); 36 | } 37 | } 38 | 39 | void enable() 40 | { 41 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); 42 | //glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); 43 | //glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, dbo); 44 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, 45 | GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, color_tex, 0); 46 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, 47 | GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, depth_tex, 0); 48 | 49 | glGetIntegerv(GL_VIEWPORT, tmp_viewport); 50 | glViewport(0, 0, width, height); // Reset The Current Viewport And Perspective Transformation 51 | } 52 | 53 | void disable() 54 | { 55 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 56 | //glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0); 57 | glViewport( 58 | tmp_viewport[0], 59 | tmp_viewport[1], 60 | tmp_viewport[2], 61 | tmp_viewport[3]); 62 | } 63 | 64 | void init (int texWidth,int texHeight,Type type_in)// = Type(COLOR | DEPTH),int color_bpp=32,int depth_bpp=24) 65 | { 66 | // clear (); 67 | this->width = texWidth; 68 | this->height= texHeight; 69 | this->type = type_in; 70 | 71 | // init texture 72 | glGenTextures(1, (GLuint*)&color_tex); 73 | glBindTexture(GL_TEXTURE_2D, color_tex); 74 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);//GL_NEAREST);//GL_LINEAR);// 75 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);//GL_NEAREST);//GL_LINEAR);// 76 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 77 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 78 | // rgba8 79 | if(type==RGBA8) 80 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 81 | texWidth, texHeight, 0, 82 | GL_RGBA, GL_UNSIGNED_BYTE, NULL);//GL_RGBA 83 | // r float 84 | if(type==ALPHA32F) 85 | glTexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY32F_ARB,//GL_ALPHA32F_ARB,//GL_RGBA8, 86 | texWidth, texHeight, 0, 87 | GL_LUMINANCE, GL_FLOAT, NULL);//GL_RGBA 88 | if(type==RGBA32F) 89 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB,//GL_ALPHA32F_ARB,//GL_RGBA8, 90 | texWidth, texHeight, 0, 91 | GL_RGBA, GL_FLOAT, NULL);//GL_RGBA 92 | if(type==ALPHA16F) 93 | glTexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY16F_ARB,//GL_ALPHA32F_ARB,//GL_RGBA8, 94 | texWidth, texHeight, 0, 95 | GL_LUMINANCE, GL_FLOAT, NULL);//GL_RGBA 96 | if(type==RGBA16F) 97 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB,//GL_ALPHA32F_ARB,//GL_RGBA8, 98 | texWidth, texHeight, 0, 99 | GL_RGBA, GL_FLOAT, NULL);//GL_RGBA 100 | glBindTexture(GL_TEXTURE_2D, 0); 101 | get_error(); 102 | 103 | glGenTextures(1, (GLuint*)&depth_tex); 104 | glBindTexture(GL_TEXTURE_2D, depth_tex); 105 | get_error(); 106 | /* 107 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); 108 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);*/ 109 | glTexParameteri (GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE); 110 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 111 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 112 | glTexImage2D (GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, 113 | texWidth, texHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); 114 | glBindTexture(GL_TEXTURE_2D, 0); 115 | 116 | // create a framebuffer object 117 | glGenFramebuffersEXT(1, &fbo); 118 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); 119 | 120 | // attach two textures to FBO color0 and depth attachement points 121 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, depth_tex, 0); 122 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, color_tex, 0); 123 | 124 | get_error(); 125 | 126 | check_framebuffer_status(); 127 | 128 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 129 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0); 130 | } 131 | 132 | private: 133 | 134 | GLuint fbo; // frame buffer object ref 135 | GLuint dbo; // depth buffer object ref 136 | 137 | void get_error() 138 | { 139 | GLenum err = glGetError(); 140 | if (err != GL_NO_ERROR) 141 | { 142 | printf("GL FBO Error: %s\n",gluErrorString(err)); 143 | printf("Programm Stopped!\n"); 144 | while(1);; 145 | } 146 | } 147 | 148 | void check_framebuffer_status() 149 | { 150 | GLenum status; 151 | status = (GLenum) glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); 152 | switch(status) { 153 | case GL_FRAMEBUFFER_COMPLETE_EXT: 154 | return; 155 | break; 156 | case GL_FRAMEBUFFER_UNSUPPORTED_EXT: 157 | printf("Unsupported framebuffer format\n"); 158 | break; 159 | case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT: 160 | printf("Framebuffer incomplete, missing attachment\n"); 161 | break; 162 | // case GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT_EXT: 163 | // printf("Framebuffer incomplete, duplicate attachment\n"); 164 | // break; 165 | case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT: 166 | printf("Framebuffer incomplete, attached images must have same dimensions\n"); 167 | break; 168 | case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT: 169 | printf("Framebuffer incomplete, attached images must have same format\n"); 170 | break; 171 | case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT: 172 | printf("Framebuffer incomplete, missing draw buffer\n"); 173 | break; 174 | case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT: 175 | printf("Framebuffer incomplete, missing read buffer\n"); 176 | break; 177 | case 0: 178 | printf("Not ok but trying...\n"); 179 | return; 180 | break; 181 | default:; 182 | printf("Framebuffer error code %d\n",status); 183 | break; 184 | }; 185 | printf("Programm Stopped!\n"); 186 | while(1)Sleep(100);; 187 | } 188 | }; 189 | /* 190 | //Use generated mipmaps if supported 191 | if(GLEE_SGIS_generate_mipmap) 192 | { 193 | glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, true); 194 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 195 | glHint(GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST); 196 | } 197 | 198 | //Use maximum anisotropy if supported 199 | if(GLEE_EXT_texture_filter_anisotropic) 200 | { 201 | GLint maxAnisotropy=1; 202 | glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAnisotropy); 203 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, maxAnisotropy); 204 | } 205 | */ 206 | /* 207 | // create a texture object for depth 208 | glGenTextures(1, &depthTex); 209 | glBindTexture(GL_TEXTURE_2D, depthTex); 210 | glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); 211 | glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, ImageW, ImageH, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); 212 | glBindTexture(GL_TEXTURE_2D, 0); 213 | 214 | 215 | // create a texture object for color 216 | glGenTextures(1, &colorTex); 217 | glBindTexture(GL_TEXTURE_2D, colorTex); 218 | glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); 219 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, ImageW, ImageH, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); 220 | glBindTexture(GL_TEXTURE_2D, 0); 221 | 222 | 223 | // create a framebuffer object 224 | glGenFramebuffersEXT(1, &fbo); 225 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); 226 | 227 | // attach two textures to FBO color0 and depth attachement points 228 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, depthTex, 0); 229 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorTex, 0); 230 | 231 | // check FBO status 232 | GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); 233 | if(status != GL_FRAMEBUFFER_COMPLETE_EXT) 234 | cerr << "FBO incomplete!! Code:" << status << endl; 235 | 236 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 237 | 238 | */ 239 | 240 | /* 241 | glGenRenderbuffersEXT(1, &dbo); 242 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, dbo); 243 | 244 | 245 | glGenTextures(1, (GLuint*)&depth_tex); 246 | glBindTexture(GL_TEXTURE_2D, depth_tex); 247 | //glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, 248 | // texWidth, texHeight, 0, 249 | // GL_DEPTH_COMPONENT, GL_FLOAT, NULL); 250 | get_error(); 251 | //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE); 252 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); 253 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); 254 | glTexParameteri (GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE); 255 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 256 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 257 | 258 | 259 | glTexImage2D (GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, 260 | texWidth, texHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); 261 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, 262 | GL_TEXTURE_2D, depth_tex, 0); 263 | 264 | get_error(); 265 | glBindTexture(GL_TEXTURE_2D, 0);// don't leave this texture bound or fbo (zero) will use it as src, want to use it just as dest GL_DEPTH_ATTACHMENT_EXT 266 | 267 | */ 268 | //glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE); 269 | //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE); 270 | -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "glee.h" 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | //#include "Vector3.h" 20 | #include 21 | #include "Core.h" 22 | #include "VecMath.h" 23 | #include "fbo.h" 24 | #include "Bmp.h" 25 | 26 | #define MAX_KEYS 256 27 | //#define VOLUME_TEX_SIZE 512 28 | #define VOLUME_TEX_SIZE_X 1024 29 | #define VOLUME_TEX_SIZE_Y 256 30 | #define VOLUME_TEX_SIZE_Z 1024 31 | #define VOLUME_TEX_SIZE_COL 64 32 | 33 | extern "C" bool pboRegister(int pbo); 34 | extern "C" void* cuda_main( int pbo_out, 35 | int size_x, 36 | int size_y, 37 | int size_z, 38 | int level, 39 | float box_x,float box_y,float box_z, 40 | float pos_x,float pos_y,float pos_z ); 41 | 42 | void MouseMotionStatic (int x,int y) 43 | { 44 | mouse.mouseX = float(x)/float(SCREEN_SIZE_X); 45 | mouse.mouseY = float(y)/float(SCREEN_SIZE_Y); 46 | } 47 | 48 | using namespace std; 49 | 50 | // Globals ------------------------------------------------------------------ 51 | 52 | bool gKeys[MAX_KEYS]; 53 | bool toggle_visuals = true; 54 | CGcontext context; 55 | CGprofile vertexProfile, fragmentProfile; 56 | CGparameter param1,param2,param3,param4,param5; 57 | CGprogram vertex_main, 58 | vertex_ssdm_displace, 59 | fragment_ssao, 60 | fragment_ssdm, 61 | fragment_ssdm_displace, 62 | fragment_ssdm_mip, 63 | fragment_ssdm_final, 64 | fragment_main, 65 | fragment_main_lowres; // the raycasting shader programs 66 | GLuint volume_texture; // the volume texture 67 | GLuint volume_texture_col; // the volume texture 68 | float stepsize = 1.0/50.0; 69 | 70 | vec3f box_ini(1000,1000,1000); 71 | vec3f box_pos[2]={box_ini,box_ini}; 72 | float box_dist[2]={1000,1000}; 73 | 74 | /// Implementation ---------------------------------------- 75 | 76 | void cgErrorCallback() 77 | { 78 | CGerror lastError = cgGetError(); 79 | if(lastError) 80 | { 81 | cout << cgGetErrorString(lastError) << endl; 82 | if(context != NULL) 83 | cout << cgGetLastListing(context) << endl; 84 | while(1);; 85 | exit(0); 86 | } 87 | } 88 | 89 | // Sets a uniform texture parameter 90 | void set_tex_param(char* par, GLuint tex,const CGprogram &program,CGparameter param) 91 | { 92 | param = cgGetNamedParameter(program, par); 93 | cgGLSetTextureParameter(param, tex); 94 | cgGLEnableTextureParameter(param); 95 | } 96 | 97 | 98 | // load_vertex_program: loading a vertex program 99 | void load_vertex_program(CGprogram &v_program,char *shader_path, char *program_name) 100 | { 101 | assert(cgIsContext(context)); 102 | // v_program = cgCreateProgram(context, CG_SOURCE,cgshader, 103 | v_program = cgCreateProgramFromFile(context, CG_SOURCE,shader_path, 104 | vertexProfile,program_name, NULL); 105 | if (!cgIsProgramCompiled(v_program)) 106 | cgCompileProgram(v_program); 107 | 108 | cgGLEnableProfile(vertexProfile); 109 | cgGLLoadProgram(v_program); 110 | cgGLDisableProfile(vertexProfile); 111 | } 112 | 113 | // load_fragment_program: loading a fragment program 114 | void load_fragment_program(CGprogram &f_program,char *shader_path, char *program_name) 115 | { 116 | char *param[3]={"-unroll","none","-fastmath"} ; 117 | 118 | assert(cgIsContext(context)); 119 | // f_program = cgCreateProgram(context, CG_SOURCE,cgshader, 120 | // v_program = cgCreateProgramFromFile(context, CG_SOURCE,shader_path, 121 | f_program = cgCreateProgramFromFile(context, CG_SOURCE, shader_path, 122 | fragmentProfile,program_name, NULL); 123 | if (!cgIsProgramCompiled(f_program)) 124 | cgCompileProgram(f_program); 125 | 126 | cgGLEnableProfile(fragmentProfile); 127 | cgGLLoadProgram(f_program); 128 | cgGLDisableProfile(fragmentProfile); 129 | } 130 | 131 | void vertex(float x, float y, float z) 132 | { 133 | glColor3f(x,y,z); 134 | glMultiTexCoord3fARB(GL_TEXTURE1_ARB, x, y, z); 135 | glVertex3f(x,y,z); 136 | } 137 | 138 | // this method is used to draw the front and backside of the volume 139 | void drawQuads(float x, float y, float z) 140 | { 141 | 142 | glBegin(GL_QUADS); 143 | /* Back side */ 144 | glNormal3f(0.0, 0.0, -1.0); 145 | vertex(0.0, 0.0, 0.0); 146 | vertex(0.0, y, 0.0); 147 | vertex(x, y, 0.0); 148 | vertex(x, 0.0, 0.0); 149 | 150 | /* Front side */ 151 | glNormal3f(0.0, 0.0, 1.0); 152 | vertex(0.0, 0.0, z); 153 | vertex(x, 0.0, z); 154 | vertex(x, y, z); 155 | vertex(0.0, y, z); 156 | 157 | /* Top side */ 158 | glNormal3f(0.0, 1.0, 0.0); 159 | vertex(0.0, y, 0.0); 160 | vertex(0.0, y, z); 161 | vertex(x, y, z); 162 | vertex(x, y, 0.0); 163 | 164 | /* Bottom side */ 165 | glNormal3f(0.0, -1.0, 0.0); 166 | vertex(0.0, 0.0, 0.0); 167 | vertex(x, 0.0, 0.0); 168 | vertex(x, 0.0, z); 169 | vertex(0.0, 0.0, z); 170 | 171 | /* Left side */ 172 | glNormal3f(-1.0, 0.0, 0.0); 173 | vertex(0.0, 0.0, 0.0); 174 | vertex(0.0, 0.0, z); 175 | vertex(0.0, y, z); 176 | vertex(0.0, y, 0.0); 177 | 178 | /* Right side */ 179 | glNormal3f(1.0, 0.0, 0.0); 180 | vertex(x, 0.0, 0.0); 181 | vertex(x, y, 0.0); 182 | vertex(x, y, z); 183 | vertex(x, 0.0, z); 184 | glEnd(); 185 | 186 | } 187 | 188 | // create a test volume texture, here you could load your own volume 189 | void create_volumetexture() 190 | { 191 | int size = 192 | VOLUME_TEX_SIZE_X * 193 | VOLUME_TEX_SIZE_Y * 194 | VOLUME_TEX_SIZE_Z ; 195 | GLubyte *data = new GLubyte[size]; 196 | 197 | cout << "creating terrain volume texture " << (int)(size/1000000) << "MB" << endl; 198 | glPixelStorei(GL_UNPACK_ALIGNMENT,1); 199 | glGenTextures(1, &volume_texture); 200 | glBindTexture(GL_TEXTURE_3D, volume_texture); 201 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 202 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 203 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 204 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT); 205 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP); 206 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT); 207 | glTexImage3D(GL_TEXTURE_3D, 0,GL_LUMINANCE, 208 | VOLUME_TEX_SIZE_X, 209 | VOLUME_TEX_SIZE_Y, 210 | VOLUME_TEX_SIZE_Z, 211 | 0, GL_LUMINANCE, GL_UNSIGNED_BYTE,data); 212 | 213 | delete []data; 214 | cout << "terrain volume texture creation finished" << endl; 215 | 216 | int size2 = VOLUME_TEX_SIZE_COL*VOLUME_TEX_SIZE_COL*VOLUME_TEX_SIZE_COL* 4; 217 | GLubyte *data2 = new GLubyte[size2]; 218 | 219 | for (int x = 0; x < VOLUME_TEX_SIZE_COL; x++) 220 | {for(int y = 0; y < VOLUME_TEX_SIZE_COL; y++) 221 | {for(int z = 0; z < VOLUME_TEX_SIZE_COL; z++) 222 | { 223 | int 224 | rn=rand()&255;data2[(x*4)+0+ (y * VOLUME_TEX_SIZE_COL * 4) + (z * VOLUME_TEX_SIZE_COL * VOLUME_TEX_SIZE_COL * 4)] = rn; 225 | rn=rand()&255;data2[(x*4)+1+ (y * VOLUME_TEX_SIZE_COL * 4) + (z * VOLUME_TEX_SIZE_COL * VOLUME_TEX_SIZE_COL * 4)] = rn; 226 | rn=rand()&255;data2[(x*4)+2+ (y * VOLUME_TEX_SIZE_COL * 4) + (z * VOLUME_TEX_SIZE_COL * VOLUME_TEX_SIZE_COL * 4)] = rn; 227 | rn=rand()&255;data2[(x*4)+3+ (y * VOLUME_TEX_SIZE_COL * 4) + (z * VOLUME_TEX_SIZE_COL * VOLUME_TEX_SIZE_COL * 4)] = rn; 228 | }}} 229 | 230 | glPixelStorei(GL_UNPACK_ALIGNMENT,1); 231 | glGenTextures(1, &volume_texture_col); 232 | glBindTexture(GL_TEXTURE_3D, volume_texture_col); 233 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 234 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 235 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 236 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT); 237 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT); 238 | glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT); 239 | glTexImage3D(GL_TEXTURE_3D, 0,GL_LUMINANCE, 240 | VOLUME_TEX_SIZE_COL, 241 | VOLUME_TEX_SIZE_COL, 242 | VOLUME_TEX_SIZE_COL, 243 | 0, GL_LUMINANCE, GL_UNSIGNED_BYTE,data2); 244 | /* glTexImage3D(GL_TEXTURE_3D, 0,GL_RGBA, 245 | VOLUME_TEX_SIZE_COL, 246 | VOLUME_TEX_SIZE_COL, 247 | VOLUME_TEX_SIZE_COL,0, GL_RGBA, GL_UNSIGNED_BYTE,data2);*/ 248 | 249 | delete []data2; 250 | 251 | cout << "color volume texture created" << endl; 252 | } 253 | 254 | // ok let's start things up 255 | 256 | void init() 257 | { 258 | /* 259 | cout << "Glew init " << endl; 260 | GLenum err = glewInit(); 261 | 262 | // initialize all the OpenGL extensions 263 | glewGetExtension("glMultiTexCoord2fvARB"); 264 | 265 | if (glewGetExtension("GL_ARB_fragment_shader") != GL_TRUE || 266 | glewGetExtension("GL_ARB_vertex_shader") != GL_TRUE || 267 | glewGetExtension("GL_ARB_shader_objects") != GL_TRUE || 268 | glewGetExtension("GL_ARB_shading_language_100") != GL_TRUE) 269 | { 270 | cout << "Driver does not support OpenGL Shading Language" << endl; 271 | exit(1); 272 | }*/ 273 | 274 | glEnable(GL_CULL_FACE); 275 | glClearColor(0.0, 0.0, 0.0, 0); 276 | create_volumetexture(); 277 | 278 | // CG init 279 | cgSetErrorCallback(cgErrorCallback); 280 | context = cgCreateContext(); 281 | if (cgGLIsProfileSupported(CG_PROFILE_VP40)) 282 | { 283 | vertexProfile = CG_PROFILE_VP40; 284 | cout << "CG_PROFILE_VP40 supported." << endl; 285 | } 286 | else 287 | { 288 | if (cgGLIsProfileSupported(CG_PROFILE_ARBVP1)) 289 | vertexProfile = CG_PROFILE_ARBVP1; 290 | else 291 | { 292 | cout << "Neither arbvp1 or vp40 vertex profiles supported on this system." << endl; 293 | exit(1); 294 | } 295 | } 296 | 297 | if (cgGLIsProfileSupported(CG_PROFILE_FP40)) 298 | { 299 | fragmentProfile = CG_PROFILE_FP40; 300 | cout << "CG_PROFILE_FP40 supported." << endl; 301 | } 302 | else 303 | { 304 | if (cgGLIsProfileSupported(CG_PROFILE_ARBFP1)) 305 | fragmentProfile = CG_PROFILE_ARBFP1; 306 | else 307 | { 308 | cout << "Neither arbfp1 or fp40 fragment profiles supported on this system." << endl; 309 | exit(1); 310 | } 311 | } 312 | 313 | // load the vertex and fragment raycasting programs 314 | load_vertex_program(vertex_main,"raycasting_shader.cg","vertex_main"); 315 | cgErrorCallback(); 316 | load_vertex_program(vertex_ssdm_displace,"raycasting_shader.cg","vertex_ssdm_displace"); 317 | cgErrorCallback(); 318 | load_fragment_program(fragment_main,"raycasting_shader.cg","fragment_main"); 319 | cgErrorCallback(); 320 | load_fragment_program(fragment_main_lowres,"raycasting_shader.cg","fragment_main_lowres"); 321 | cgErrorCallback(); 322 | load_fragment_program(fragment_ssdm,"raycasting_shader.cg","fragment_ssdm"); 323 | cgErrorCallback(); 324 | load_fragment_program(fragment_ssdm_mip,"raycasting_shader.cg","fragment_ssdm_mip"); 325 | cgErrorCallback(); 326 | load_fragment_program(fragment_ssdm_final,"raycasting_shader.cg","fragment_ssdm_final"); 327 | cgErrorCallback(); 328 | load_fragment_program(fragment_ssdm_displace,"raycasting_shader.cg","fragment_ssdm_displace"); 329 | cgErrorCallback(); 330 | load_fragment_program(fragment_ssao,"raycasting_shader.cg","fragment_ssao"); 331 | cgErrorCallback(); 332 | } 333 | 334 | // for contiunes keypresses 335 | void ProcessKeys() 336 | { 337 | static int t1 = timeGetTime(); 338 | static int t2 = timeGetTime(); 339 | 340 | t2=t1; 341 | t1=timeGetTime(); 342 | float dt = t1-t2; 343 | 344 | float step = 0.0000031 * dt; 345 | 346 | 347 | screen.rotx = screen.rotx*0.9+0.1*((mouse.mouseY)*15); 348 | screen.roty = screen.roty*0.9+0.1*((mouse.mouseX)*15+M_PI/2); 349 | screen.rotx = 1*((mouse.mouseY)*15); 350 | screen.roty = 1*((mouse.mouseX)*15+M_PI/2); 351 | 352 | matrix44 m; 353 | m.ident(); 354 | //m.rotate_z(screen.rotz ); 355 | m.rotate_x(screen.rotx ); 356 | m.rotate_y(screen.roty ); 357 | 358 | vec3f pos( screen.posx,screen.posy,screen.posz ); 359 | vec3f forward = m * vec3f(0,0,-step).v3(); forward.x*=-1;forward.y*=-1; 360 | vec3f side = m * vec3f(-step,0,0).v3(); side.x*=-1;side.y*=-1; 361 | vec3f updown = m * vec3f(0,-step,0).v3(); updown.x*=-1;updown.y*=-1; 362 | 363 | // Process keys 364 | for (int i = 0; i < 256; i++) 365 | { 366 | if (!gKeys[i]) { continue; } 367 | switch (i) 368 | { 369 | case ' ': 370 | break; 371 | case 'w': pos = pos + forward; break; 372 | case 's': pos = pos - forward; break; 373 | case 'a': pos = pos - side; break; 374 | case 'd': pos = pos + side; break; 375 | case 'q': pos = pos - updown; break; 376 | case 'e': pos = pos + updown; break; 377 | case 'r': 378 | stepsize += 1.0/2048.0; 379 | if(stepsize > 0.25) stepsize = 0.25; 380 | break; 381 | case 'f': 382 | stepsize -= 1.0/2048.0; 383 | if(stepsize <= 1.0/200.0) stepsize = 1.0/200.0; 384 | break; 385 | } 386 | } 387 | screen.posx=pos.x; 388 | screen.posy=pos.y; 389 | screen.posz=pos.z; 390 | } 391 | 392 | void key(unsigned char k, int x, int y) 393 | { 394 | gKeys[k] = true; 395 | } 396 | 397 | void KeyboardUpCallback(unsigned char key, int x, int y) 398 | { 399 | gKeys[key] = false; 400 | 401 | switch (key) 402 | { 403 | case 27 : 404 | { 405 | exit(0); break; 406 | } 407 | case ' ': 408 | toggle_visuals = !toggle_visuals; 409 | break; 410 | } 411 | } 412 | 413 | // glut idle function 414 | void idle_func() 415 | { 416 | ProcessKeys(); 417 | glutPostRedisplay(); 418 | } 419 | 420 | void reshape_ortho(int w, int h) 421 | { 422 | if (h == 0) h = 1; 423 | glViewport(0, 0,w,h); 424 | glMatrixMode(GL_PROJECTION); 425 | glLoadIdentity(); 426 | gluOrtho2D(0, 1, 0, 1); 427 | glMatrixMode(GL_MODELVIEW); 428 | } 429 | 430 | 431 | void resize(int w, int h) 432 | { 433 | if (h == 0) h = 1; 434 | glViewport(0, 0, w, h); 435 | glMatrixMode(GL_PROJECTION); 436 | glLoadIdentity(); 437 | gluPerspective(90.0, (GLfloat)w/(GLfloat)h, 0.01, 400.0); 438 | glMatrixMode(GL_MODELVIEW); 439 | } 440 | 441 | void draw_fullscreen_quad() 442 | { 443 | glDisable(GL_DEPTH_TEST); 444 | glBegin(GL_QUADS); 445 | 446 | glTexCoord2f(0,0); 447 | glVertex2f(0,0); 448 | glTexCoord2f(1,0); 449 | glVertex2f(1,0); 450 | glTexCoord2f(1, 1); 451 | glVertex2f(1, 1); 452 | glTexCoord2f(0, 1); 453 | glVertex2f(0, 1); 454 | 455 | glEnd(); 456 | glEnable(GL_DEPTH_TEST); 457 | } 458 | 459 | void draw_fullscreen_quad_ssao(int color_tex,int depth_tex,int shadow_tex) 460 | { 461 | glDisable(GL_DEPTH_TEST); 462 | glDepthFunc(GL_LEQUAL); 463 | 464 | CGprogram fragment_prg = fragment_ssao; 465 | CGprogram vertex_prg = vertex_main; 466 | 467 | cgGLEnableProfile(vertexProfile); 468 | cgGLEnableProfile(fragmentProfile); 469 | cgGLBindProgram(vertex_prg); 470 | cgGLBindProgram(fragment_prg); 471 | set_tex_param("depth_tex",depth_tex,fragment_prg,param1); 472 | set_tex_param("color_tex",color_tex,fragment_prg,param2); 473 | set_tex_param("shadow_tex",shadow_tex,fragment_prg,param3); 474 | 475 | 476 | cgGLSetParameter2f (cgGetNamedParameter( fragment_prg, "screen"), 477 | float(screen.window_width),float(screen.window_height)); 478 | 479 | glBegin(GL_QUADS); 480 | 481 | glTexCoord2f(0,0); 482 | glVertex2f(0,0); 483 | glTexCoord2f(1,0); 484 | glVertex2f(1,0); 485 | glTexCoord2f(1, 1); 486 | glVertex2f(1, 1); 487 | glTexCoord2f(0, 1); 488 | glVertex2f(0, 1); 489 | 490 | glEnd(); 491 | glEnable(GL_DEPTH_TEST); 492 | cgGLDisableProfile(vertexProfile); 493 | cgGLDisableProfile(fragmentProfile); 494 | } 495 | 496 | void draw_fullscreen_quad_2mio(int rgba_tex,int ssdm_tex) 497 | { 498 | // create one display list 499 | static int gl_list = -1; 500 | 501 | glDisable(GL_DEPTH_TEST); 502 | 503 | if(gl_list == -1) 504 | { 505 | gl_list=glGenLists(1); 506 | glNewList(gl_list, GL_COMPILE); 507 | 508 | int gridx=screen.window_width; 509 | int gridy=screen.window_height; 510 | 511 | for (int b=0;b float(1<bmp.width) return; if(x1<0) return; 874 | if(x2>bmp.width) return; if(x2<0) return; 875 | if(y1>bmp.height) return; if(y1<0) return; 876 | if(y2>bmp.height) return; if(y2<0) return; 877 | 878 | int dx=x2-x1; 879 | int dy=y2-y1; 880 | int steps= ( abs(dx) > abs(dy) ) ? abs(dx) : abs(dy); 881 | 882 | if(steps==0){ 883 | int x=x1; 884 | int y=y1; 885 | int o=3*(x+y*bmp.width); 886 | 887 | if(bmp_z.data[o+0]>z) 888 | { 889 | bmp.data[o+0]=b; 890 | bmp.data[o+1]=g; 891 | bmp.data[o+2]=r; 892 | bmp_z.data[o+0]=z; 893 | } 894 | return; 895 | } 896 | 897 | for (int a=0;az) 904 | { 905 | bmp.data[o+0]=b; 906 | bmp.data[o+1]=g; 907 | bmp.data[o+2]=r; 908 | bmp_z.data[o+0]=z; 909 | } 910 | } 911 | } 912 | 913 | int main(int argc, char* argv[]) 914 | { 915 | if(0) 916 | { 917 | Bmp bmp( "test.bmp" ); 918 | Bmp bmp_out( bmp.width,bmp.height,24,0 ); 919 | Bmp bmp_z ( bmp.width,bmp.height,24,0 ); 920 | memset(bmp_z.data,255,bmp.width*bmp.height*3); 921 | //memcpy(bmp_out.data,bmp.data,bmp.width*bmp.height*3); 922 | 923 | for ( float x=0;x<1.0;x+=1.0f/float(bmp.width)) 924 | for ( float y=0;y<1.0;y+=1.0f/float(bmp.height)) 925 | { 926 | int xi =int(float(x*float(bmp.width))); 927 | int yi =int(float(y*float(bmp.height))); 928 | int o =3*(xi+yi*bmp.width); 929 | int b = bmp.data[o+0]; 930 | int g = bmp.data[o+1]; 931 | int r = bmp.data[o+2]; 932 | 933 | int dx = (r-128)/6; 934 | int dy = (g-128)/6; 935 | int depth = b; 936 | /* 937 | int yii=bmp.height-1-yi; 938 | if(yii>620 && yii<630) 939 | if(xi>968) 940 | printf("xi:%d\tyi%d\tdx:%d\tdy:%d\tr:%d\tg:%d\n",xi,yi,dx,dy,r,g); 941 | */ 942 | 943 | 944 | draw_line(xi,yi,xi+dx,yi+dy,r,g,b,depth,bmp_out,bmp_z); 945 | } 946 | if(0) 947 | for ( float x=0;x<1.0;x+=1.0f/float(bmp.width)) 948 | for ( float y=0;y<1.0;y+=1.0f/float(bmp.height)) 949 | { 950 | int xi =int(float(x*float(bmp.width))); 951 | int yi =int(float(y*float(bmp.height))); 952 | int o =3*(xi+yi*bmp.width); 953 | int b = bmp.data[o+0]; 954 | int g = bmp.data[o+1]; 955 | int r = bmp.data[o+2]; 956 | 957 | int dx = (r-128)/6; 958 | int dy = (g-128)/6; 959 | int depth = b; 960 | /* 961 | int yii=bmp.height-1-yi; 962 | if(yii>620 && yii<630) 963 | if(xi>968) 964 | printf("xi:%d\tyi%d\tdx:%d\tdy:%d\tr:%d\tg:%d\n",xi,yi,dx,dy,r,g); 965 | */ 966 | 967 | if(xi+dx0) 970 | if(yi+dy>0) 971 | { 972 | o+=3*(dx+dy*bmp.width); 973 | bmp_out.data[o+0]=b; 974 | bmp_out.data[o+1]=g; 975 | bmp_out.data[o+2]=r; 976 | } 977 | 978 | } 979 | if(0) 980 | for ( float a=0;a<6.2;a+=6.2/380.0f) 981 | { 982 | int xi=bmp.width/2; 983 | int yi=bmp.height/2; 984 | int dxi=float(bmp.width/2*sin(a-1)*a/16.2); 985 | int dyi=float(bmp.height/2*cos(a-1)*a/16.2); 986 | draw_line(xi,yi,xi+dxi,yi+dyi,255,255,0,0,bmp,bmp_z); 987 | } 988 | bmp_out.save("test_out.bmp"); 989 | ShellExecute( 990 | 0, //__in_opt HWND hwnd, 991 | "open", //__in_opt LPCTSTR lpOperation, 992 | "C:/Program Files/IrfanView/i_view32.exe", //__in LPCTSTR lpFile, 993 | "test_out.bmp", //__in_opt LPCTSTR lpParameters, 994 | ".",//__in_opt LPCTSTR lpDirectory, 995 | SW_SHOW//__in INT nShowCmd 996 | ); 997 | //while(1)Sleep(100); 998 | return 0; 999 | } 1000 | 1001 | //+float3(0.5,0.125,0.5) 1002 | screen.posx=0; 1003 | screen.posy=0; 1004 | screen.posz=0; 1005 | screen.rotx =screen.roty = screen.rotz = 0; 1006 | screen.window_width = SCREEN_SIZE_X; 1007 | screen.window_height = SCREEN_SIZE_Y; 1008 | 1009 | glutInit(&argc,argv); 1010 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); 1011 | glutCreateWindow("Perlin Noise Raycasting"); 1012 | glutReshapeWindow(SCREEN_SIZE_X,SCREEN_SIZE_Y); 1013 | glutKeyboardFunc(key); 1014 | glutKeyboardUpFunc(KeyboardUpCallback); 1015 | glutMotionFunc(&MouseMotionStatic); 1016 | glutPassiveMotionFunc(&MouseMotionStatic); 1017 | 1018 | wglSwapIntervalEXT(0); 1019 | 1020 | glutDisplayFunc(display); 1021 | glutIdleFunc(idle_func); 1022 | glutReshapeFunc(resize); 1023 | resize(SCREEN_SIZE_X,SCREEN_SIZE_Y); 1024 | init(); 1025 | glutMainLoop(); 1026 | return 0; 1027 | } 1028 | 1029 | -------------------------------------------------------------------------------- /src/raycasting_shader.cg: -------------------------------------------------------------------------------- 1 | struct app_vertex 2 | { 3 | float4 Position : POSITION; 4 | float4 TexCoord : TEXCOORD1; 5 | float4 Color : COLOR0; 6 | }; 7 | 8 | struct vertex_fragment 9 | { 10 | float4 Position : POSITION; // For the rasterizer 11 | float4 TexCoord : TEXCOORD0; 12 | float4 Color : TEXCOORD1; 13 | float4 Pos : TEXCOORD2; 14 | }; 15 | 16 | struct fragment_out 17 | { 18 | float4 Color : COLOR0; 19 | float Depth : DEPTH; 20 | }; 21 | 22 | float bspline_3d_fp( sampler3D texin, float3 vecin) 23 | { 24 | //return tex3D(texin, vecin).x; 25 | // transform the coordinate from [0,extent] to [-0.5, extent-0.5] 26 | float3 tsize = float3(1024.0,256.0,1024.0); 27 | float3 one_tsize = float3(1.0/tsize.x,1.0/tsize.y,1.0/tsize.z); 28 | float3 coord_grid = vecin*tsize - float3(0.5,0.5,0.5); 29 | float3 fraction = frac(coord_grid); 30 | float3 one_frac = 1.0 - fraction; 31 | float3 one_frac2 = one_frac * one_frac; 32 | float3 fraction2 = fraction * fraction; 33 | float3 w0 = 1.0/6.0 * one_frac2 * one_frac; 34 | float3 w1 = 2.0/3.0 - 0.5 * fraction2 * (2.0-fraction); 35 | float3 w2 = 2.0/3.0 - 0.5 * one_frac2 * (2.0-one_frac); 36 | float3 w3 = 1.0/6.0 * fraction2 * fraction; 37 | float3 g0 = w0 + w1; 38 | float3 g1 = w2 + w3; 39 | float3 index = coord_grid-fraction; 40 | float3 h0 = ((w1 / g0) - 0.5 + index)*one_tsize; 41 | float3 h1 = ((w3 / g1) + 1.5 + index)*one_tsize; 42 | 43 | // fetch the four linear interpolations 44 | float tex000 = tex3D(texin, float3(h0.x, h0.y, h0.z)); 45 | float tex001 = tex3D(texin, float3(h0.x, h1.y, h0.z)); 46 | tex000 = lerp(tex001, tex000, g0.y); 47 | float tex010 = tex3D(texin, float3(h1.x, h0.y, h0.z)); 48 | float tex011 = tex3D(texin, float3(h1.x, h1.y, h0.z)); 49 | tex010 = lerp(tex011, tex010, g0.y); 50 | 51 | float tex100 = tex3D(texin, float3(h0.x, h0.y, h1.z)); 52 | float tex101 = tex3D(texin, float3(h0.x, h1.y, h1.z)); 53 | tex100 = lerp(tex101, tex100, g0.y); 54 | float tex110 = tex3D(texin, float3(h1.x, h0.y, h1.z)); 55 | float tex111 = tex3D(texin, float3(h1.x, h1.y, h1.z)); 56 | tex110 = lerp(tex111, tex110, g0.y); 57 | 58 | tex100 = lerp(tex110, tex100, g0.x); 59 | tex000 = lerp(tex010, tex000, g0.x); 60 | 61 | return lerp(tex100, tex000, g0.z); 62 | } 63 | 64 | float bspline_3d_fp_col( sampler3D texin, float3 vecin) 65 | { 66 | //return tex3D(texin, vecin).x; 67 | // transform the coordinate from [0,extent] to [-0.5, extent-0.5] 68 | float3 tsize = float3(128.0,128.0,128.0); 69 | float3 one_tsize = float3(1.0/tsize.x,1.0/tsize.y,1.0/tsize.z); 70 | float3 coord_grid = vecin*tsize - float3(0.5,0.5,0.5); 71 | float3 fraction = frac(coord_grid); 72 | float3 one_frac = 1.0 - fraction; 73 | float3 one_frac2 = one_frac * one_frac; 74 | float3 fraction2 = fraction * fraction; 75 | float3 w0 = 1.0/6.0 * one_frac2 * one_frac; 76 | float3 w1 = 2.0/3.0 - 0.5 * fraction2 * (2.0-fraction); 77 | float3 w2 = 2.0/3.0 - 0.5 * one_frac2 * (2.0-one_frac); 78 | float3 w3 = 1.0/6.0 * fraction2 * fraction; 79 | float3 g0 = w0 + w1; 80 | float3 g1 = w2 + w3; 81 | float3 index = coord_grid-fraction; 82 | float3 h0 = ((w1 / g0) - 0.5 + index)*one_tsize; 83 | float3 h1 = ((w3 / g1) + 1.5 + index)*one_tsize; 84 | 85 | // fetch the four linear interpolations 86 | float tex000 = tex3D(texin, float3(h0.x, h0.y, h0.z)); 87 | float tex001 = tex3D(texin, float3(h0.x, h1.y, h0.z)); 88 | tex000 = lerp(tex001, tex000, g0.y); 89 | float tex010 = tex3D(texin, float3(h1.x, h0.y, h0.z)); 90 | float tex011 = tex3D(texin, float3(h1.x, h1.y, h0.z)); 91 | tex010 = lerp(tex011, tex010, g0.y); 92 | 93 | float tex100 = tex3D(texin, float3(h0.x, h0.y, h1.z)); 94 | float tex101 = tex3D(texin, float3(h0.x, h1.y, h1.z)); 95 | tex100 = lerp(tex101, tex100, g0.y); 96 | float tex110 = tex3D(texin, float3(h1.x, h0.y, h1.z)); 97 | float tex111 = tex3D(texin, float3(h1.x, h1.y, h1.z)); 98 | tex110 = lerp(tex111, tex110, g0.y); 99 | 100 | tex100 = lerp(tex110, tex100, g0.x); 101 | tex000 = lerp(tex010, tex000, g0.x); 102 | 103 | return lerp(tex100, tex000, g0.z); 104 | } 105 | 106 | vertex_fragment vertex_main( app_vertex IN ) 107 | { 108 | vertex_fragment OUT; 109 | 110 | float4x4 ModelView = glstate.matrix.modelview[0]; 111 | float4x4 ModelViewProj = glstate.matrix.mvp; 112 | 113 | OUT.Position = mul( ModelViewProj, IN.Position ); 114 | OUT.Pos = mul( ModelViewProj, IN.Position ); 115 | OUT.TexCoord = IN.TexCoord; 116 | OUT.Color = IN.Color; 117 | return OUT; 118 | } 119 | 120 | float4 sample_color( sampler3D volume_tex_col, float3 vec ) 121 | { 122 | float mul1=0.5; 123 | float mul2=1.0/8.0; 124 | float4 color_sample = float4(0,0,0,0); 125 | 126 | for(int j = 0; j < 3; j++) 127 | { 128 | color_sample += tex3D(volume_tex_col,vec*mul2)*mul1; 129 | mul1*=0.5; 130 | mul2*=2; 131 | } 132 | return color_sample ; 133 | } 134 | 135 | float sample_alpha2( sampler3D volume_tex, 136 | float3 vec, 137 | float alpha) 138 | { 139 | vec.y*=4.0; 140 | if (alpha>0.66) 141 | return bspline_3d_fp( volume_tex, vec.xyz ); 142 | else 143 | return tex3D(volume_tex,vec.xyz).x; 144 | 145 | } 146 | 147 | float sample_alpha( sampler3D volume_tex, 148 | float3 vec, 149 | float alpha) 150 | { 151 | vec.y*=4.0; 152 | return tex3D(volume_tex,vec.xyz).x; 153 | } 154 | 155 | fragment_out fragment_main( vertex_fragment IN, 156 | float2 position : WPOS, 157 | uniform sampler2D lowres_tex, 158 | uniform sampler3D volume_tex, 159 | uniform sampler3D volume_tex_col, 160 | uniform float4 viewpoint, 161 | uniform float2 screen, 162 | uniform float3 box_pos[2] 163 | ) 164 | { 165 | fragment_out OUT; 166 | 167 | float2 rgba_posi = float2( 168 | (int(int(position.x)/1)*1), 169 | (int(int(position.y)/1)*1) 170 | ); 171 | 172 | float2 rgba_pos0 = float2 ( (rgba_posi.x+0)/screen.x , (rgba_posi.y+0)/screen.y ); 173 | float2 rgba_pos1 = float2 ( (rgba_posi.x+4)/screen.x , (rgba_posi.y-4)/screen.y ); 174 | float2 rgba_pos2 = float2 ( (rgba_posi.x+4)/screen.x , (rgba_posi.y+4)/screen.y ); 175 | float2 rgba_pos3 = float2 ( (rgba_posi.x-4)/screen.x , (rgba_posi.y-4)/screen.y ); 176 | float2 rgba_pos4 = float2 ( (rgba_posi.x-4)/screen.x , (rgba_posi.y+4)/screen.y ); 177 | float dist0 = tex2D(lowres_tex, rgba_pos0 ).x ; 178 | float dist1 = tex2D(lowres_tex, rgba_pos1 ).x ; 179 | float dist2 = tex2D(lowres_tex, rgba_pos2 ).x ; 180 | float dist3 = tex2D(lowres_tex, rgba_pos3 ).x ; 181 | float dist4 = tex2D(lowres_tex, rgba_pos4 ).x ; 182 | if(dist10)&& vec.y<0.5&& vec.y>-0.5) ; 367 | while(alpha0)&& lightpos.y>0) ; 368 | 369 | 370 | float shado = 0.0; 371 | if (lightpos.y<=0) shado = 1.0; 372 | 373 | OUT.Depth = dist3/4.5f;//dist3/4.5f; 374 | OUT.Color.x = shado ; 375 | 376 | return OUT; 377 | } 378 | 379 | fragment_out fragment_ssdm 380 | ( 381 | vertex_fragment IN, 382 | float2 position : WPOS, 383 | uniform float4 matrix1, 384 | uniform float4 matrix2, 385 | uniform float4 matrix3, 386 | uniform sampler2D zbuf_tex, 387 | uniform sampler2D rgba_tex, 388 | uniform float2 screen 389 | ) 390 | { 391 | fragment_out OUT; 392 | 393 | float2 rgba_pos = float2( position.x/screen.x , position.y/screen.y ); 394 | float4 rgba = tex2D(rgba_tex, rgba_pos ) ; 395 | float depth = tex2D(zbuf_tex, rgba_pos ).x/25 +0.0001; 396 | float displace = rgba.w; 397 | rgba.w=1.0; 398 | 399 | rgba.xyz = rgba.xyz*2.0-1.0; 400 | 401 | float4 ss_normal = float4 ( 402 | dot ( rgba.xyz, matrix1.xyz ), 403 | dot ( rgba.xyz, matrix2.xyz ), 404 | dot ( rgba.xyz, matrix3.xyz ), 405 | 1.0); 406 | 407 | float xs = rgba_pos.x * 2.0 - 1.0; 408 | float ys = rgba_pos.y * 2.0 - 1.0; 409 | float dx = ss_normal.x * displace * 0.01; 410 | float dy = ss_normal.y * displace * 0.01; 411 | float dz = ss_normal.z * displace * 0.0001; 412 | float2 dxs = (xs*depth + dx)/(depth-dz)-xs ; 413 | float2 dys = (ys*depth + dy)/(depth-dz)-ys ; 414 | 415 | //OUT.Color.x = ss_normal.x * displace*0.5+0.5;//dxs*0.1+0.5; 416 | //OUT.Color.y = ss_normal.y * displace*0.5+0.5;//dxs*0.1+0.5; 417 | OUT.Color.x = dxs*0.1+0.5; 418 | OUT.Color.y = dys*0.1+0.5; 419 | OUT.Color.z = depth*22.0;//(depth+dz)*4.0; 420 | OUT.Color.w = ss_normal.z;//displace; 421 | 422 | //OUT.Color = ss_normal*displace*0.5-0.5; 423 | 424 | return OUT; 425 | } 426 | 427 | fragment_out fragment_ssdm_mip 428 | ( 429 | vertex_fragment IN, 430 | float2 position : WPOS, 431 | uniform sampler2D rgba_tex, 432 | uniform float2 screen 433 | ) 434 | { 435 | fragment_out OUT; 436 | 437 | float2 rgba_pos1= float2( (position.x+0.5)/screen.x , (position.y+0.5)/screen.y ); 438 | float2 rgba_pos2= float2( (position.x+0.5)/screen.x , (position.y-0.5)/screen.y ); 439 | float2 rgba_pos3= float2( (position.x-0.5)/screen.x , (position.y+0.5)/screen.y ); 440 | float2 rgba_pos4= float2( (position.x-0.5)/screen.x , (position.y-0.5)/screen.y ); 441 | 442 | float4 rgba = 443 | ( 444 | tex2D(rgba_tex, rgba_pos1 ) + 445 | tex2D(rgba_tex, rgba_pos2 ) + 446 | tex2D(rgba_tex, rgba_pos3 ) + 447 | tex2D(rgba_tex, rgba_pos4 ) 448 | ) * 0.25; 449 | 450 | OUT.Color = rgba; 451 | 452 | return OUT; 453 | } 454 | 455 | 456 | fragment_out fragment_ssdm_final 457 | ( 458 | vertex_fragment IN, 459 | float2 position : WPOS, 460 | uniform sampler2D ssdm_m0, 461 | uniform sampler2D ssdm_m1, 462 | uniform sampler2D ssdm_m2, 463 | uniform sampler2D ssdm_m3, 464 | uniform sampler2D rgba_tex, 465 | uniform float2 screen 466 | ) 467 | { 468 | fragment_out OUT; 469 | 470 | float ax,ay; 471 | 472 | ax = 20.0 /screen.x ; 473 | ay = 20.0 /screen.y ; 474 | 475 | float2 tex_pos = float2( position.x/screen.x , position.y/screen.y ); 476 | float2 tex_nearest = tex_pos ; 477 | 478 | float xs = tex_pos.x * 2.0 - 1.0; 479 | float ys = tex_pos.y * 2.0 - 1.0; 480 | 481 | float4 delta = (tex2D( ssdm_m0, tex_pos ) * 2.0 - 1.0)*20.0/1024.0; 482 | float dist = delta.x*delta.x+delta.y*delta.y; 483 | float depth = delta.z; 484 | 485 | for (float x=-ax;x 514 | 515 | vertex_fragment vertex_ssdm_displace( 516 | app_vertex IN 517 | ,uniform sampler2D ssdm_tex 518 | ,uniform sampler2D rgba_tex 519 | ) 520 | { 521 | vertex_fragment OUT; 522 | 523 | float4x4 ModelView = glstate.matrix.modelview[0]; 524 | float4x4 ModelViewProj = glstate.matrix.mvp; 525 | 526 | float4 displace = 527 | tex2D(ssdm_tex, IN.Position.xy)+ 528 | tex2D(rgba_tex,float2(0,0))*0.0001; 529 | 530 | // if(0) 531 | //if (displace.w<0.5) 532 | { 533 | float rad=0.003; 534 | float4 d0 = displace; 535 | float4 d1 = tex2D(ssdm_tex, IN.Position.xy+float2(rad,rad)); 536 | float4 d2 = tex2D(ssdm_tex, IN.Position.xy+float2(rad,-rad)); 537 | float4 d3 = tex2D(ssdm_tex, IN.Position.xy+float2(-rad,rad)); 538 | float4 d4 = tex2D(ssdm_tex, IN.Position.xy+float2(-rad,-rad)); 539 | float4 d5 = tex2D(ssdm_tex, IN.Position.xy+float2(0.0000,rad)); 540 | float4 d6 = tex2D(ssdm_tex, IN.Position.xy+float2(0.0000,-rad)); 541 | float4 d7 = tex2D(ssdm_tex, IN.Position.xy+float2( rad,0.000)); 542 | float4 d8 = tex2D(ssdm_tex, IN.Position.xy+float2(-rad,0.000)); 543 | 544 | float mz = (d0.z*3+d1.z+d2.z+d3.z+d4.z+d5.z+d6.z+d7.z+d8.z)/(8.0+3.0); 545 | 546 | rad=0.009/(300*d0.z+0.01); 547 | d0 = displace; 548 | d1 = tex2D(ssdm_tex, IN.Position.xy+float2(rad,rad)); 549 | d2 = tex2D(ssdm_tex, IN.Position.xy+float2(rad,-rad)); 550 | d3 = tex2D(ssdm_tex, IN.Position.xy+float2(-rad,rad)); 551 | d4 = tex2D(ssdm_tex, IN.Position.xy+float2(-rad,-rad)); 552 | d5 = tex2D(ssdm_tex, IN.Position.xy+float2(0.0000,rad)); 553 | d6 = tex2D(ssdm_tex, IN.Position.xy+float2(0.0000,-rad)); 554 | d7 = tex2D(ssdm_tex, IN.Position.xy+float2( rad,0.000)); 555 | d8 = tex2D(ssdm_tex, IN.Position.xy+float2(-rad,0.000)); 556 | 557 | displace = (displace+d1+d2+d3+d4+d5+d6+d7+d8)/(8.0+1.0); 558 | } 559 | 560 | OUT.Position = mul( ModelViewProj, IN.Position ) ; 561 | float4 displace2d = displace; 562 | displace2d.z=0.0; 563 | displace2d.w=0.0; 564 | OUT.Position += (displace2d*2.0-1.0)*0.04; 565 | OUT.Position.z = displace.z; 566 | OUT.Position.xy = OUT.Position.xy * 1.2 ; 567 | OUT.TexCoord = IN.Position;//IN.TexCoord; 568 | //OUT.Color = displace;//IN.TexCoord; 569 | 570 | return OUT; 571 | } 572 | 573 | fragment_out fragment_ssdm_displace 574 | ( 575 | vertex_fragment IN 576 | ,float2 position : WPOS 577 | ,uniform sampler2D ssdm_tex 578 | ,uniform sampler2D rgba_tex 579 | ,uniform float2 screen 580 | ) 581 | { 582 | fragment_out OUT; 583 | 584 | //OUT.Color = IN.Color; return OUT; 585 | 586 | float4 displace = tex2D(ssdm_tex, IN.TexCoord.xy); 587 | float4 rgba = tex2D(rgba_tex, IN.TexCoord.xy)+displace*0.0001; 588 | 589 | float a = smoothstep ( 0.7 , 0.9 , rgba.y); 590 | 591 | float4 colorup =lerp ( 592 | float4(0.5,0.5,0.5,0.0)*(rgba.w*1.0+rgba.y*1.0-0.7)*1.2, 593 | float4(0.4,1.0,0.4,0.0)*(rgba.w*0.9+0.1)*1.0, 594 | a); 595 | //max(0.0 , min(1.0, rgba.y )) 596 | //); 597 | colorup =(abs(rgba.y-0.5)+0.8) *colorup ; 598 | 599 | OUT.Color = colorup; 600 | 601 | /* 602 | 603 | float2 pos = float2( position.x/screen.x , position.y/screen.y ); 604 | float4 bgcol = float4(pos.y,pos.y*0.8+0.2,0.5+pos.y*0.5,0); 605 | 606 | OUT.Color = lerp( 607 | colorup , 608 | //float4(0.5,0.6,0.7,0), 609 | bgcol, 610 | max(0.0,min(1.0, 1.5-300.0/(200.0+displace.z*displace.z*1000)))); 611 | 612 | if ( displace.z > 0.85 ) OUT.Color = bgcol; 613 | 614 | //rgba*(displace.w*0.5+0.5);//IN.Color; 615 | 616 | */ 617 | return OUT; 618 | } 619 | 620 | fragment_out fragment_ssao 621 | ( 622 | vertex_fragment IN 623 | ,float2 position : WPOS 624 | ,uniform sampler2D depth_tex 625 | ,uniform sampler2D color_tex 626 | ,uniform sampler2D shadow_tex 627 | ,uniform float2 screen 628 | ) 629 | { 630 | fragment_out OUT; 631 | 632 | float2 tex0 = IN.Pos.xy*0.5+0.5; 633 | float z0 = tex2D(depth_tex, tex0); 634 | float shadow= tex2D(shadow_tex, ((tex0-0.5)/1.2)+0.5).x; 635 | float bright= 0.0; 636 | 637 | float rndx = 2345.2345; 638 | float rndy = 1235.7126; 639 | float rndz = 7831.3452; 640 | 641 | float addx = frac(tex0.y*115.356264647+tex0.x*675.34647)*3.1415*2.0; 642 | float radius = 1.0;//100.0/(100.0+z0*z0*1000); 643 | 644 | for (float a=0;a<1.0;a+=1.0/20.0) 645 | { 646 | //rndx = frac( rndx*a+a*34622.34563263+232.23451 )* 2.0-1.0; 647 | //rndy = frac( rndy*a+a*14562.14678323+523.12451 )* 2.0-1.0; 648 | //rndz = frac( rndz*a+a*13562.83325621+223.82451 )* 2.0-1.0; 649 | 650 | float factor=a; 651 | rndx = sin(a*30+addx)*factor*radius*0.12; 652 | rndy = cos(a*30+addx)*factor*radius*0.12; 653 | 654 | float2 delta_xy=float2( rndx,rndy ) ; 655 | //float delta_z =rndz ; 656 | 657 | float z=z0-tex2D(depth_tex, tex0 + delta_xy); 658 | 659 | //if ( z > z0+delta_z*0.01 ) bright+=1.0/20.0; 660 | 661 | if(abs(z)<0.01*z0+0.002) bright+=z*1.0*(1.0-factor); 662 | } 663 | 664 | bright = max(min(1.0-bright*5.0,1.0),0.0); 665 | 666 | float4 bgcol = float4(tex0.y,tex0.y*0.8+0.2,0.5+tex0.y*0.5,0); 667 | //OUT.Color = tex2D(color_tex, tex0); 668 | 669 | z0=z0*2.0-1.0; 670 | 671 | OUT.Color = lerp( 672 | max(float4(0,0,0,0),(tex2D(color_tex, tex0)*1.0+0.0)-(1.0-bright)) , 673 | //float4(0.5,0.6,0.7,0), 674 | bgcol, 675 | max(0.0,min(1.0, 1.0-150.0/(100+z0*1000)))); 676 | 677 | 678 | if ( z0 > 0.85 ) OUT.Color = bgcol; 679 | 680 | //OUT.Color=max(float4(0,0,0,0),(tex2D(color_tex, tex0)*1.0+0.0)-(1.0-bright)); 681 | 682 | //if(IN.Pos.x<0) OUT.Color = bright;;//tex2D(color_tex, tex0);//bright; 683 | 684 | //OUT.Color = z0;//tex2D(color_tex, tex0)*bright; 685 | 686 | return OUT; 687 | } 688 | 689 | -------------------------------------------------------------------------------- /src/resource.h: -------------------------------------------------------------------------------- 1 | //{{NO_DEPENDENCIES}} 2 | // Microsoft Visual C++ generated include file. 3 | // Used by Jelly ball.rc 4 | // 5 | #define IDI_ICON1 101 6 | 7 | // Next default values for new objects 8 | // 9 | #ifdef APSTUDIO_INVOKED 10 | #ifndef APSTUDIO_READONLY_SYMBOLS 11 | #define _APS_NEXT_RESOURCE_VALUE 102 12 | #define _APS_NEXT_COMMAND_VALUE 40001 13 | #define _APS_NEXT_CONTROL_VALUE 1001 14 | #define _APS_NEXT_SYMED_VALUE 101 15 | #endif 16 | #endif 17 | --------------------------------------------------------------------------------