diff options
| author | Jason Maskell <[email protected]> | 2016-05-09 10:39:54 +0200 |
|---|---|---|
| committer | Jason Maskell <[email protected]> | 2016-05-09 10:39:54 +0200 |
| commit | 79b3462799c28af8ba586349bd671b1b56e72353 (patch) | |
| tree | 3b06e36c390254c0dc7f3733a0d32af213d87293 /test/gl2_mac | |
| download | waveworks_archive-79b3462799c28af8ba586349bd671b1b56e72353.tar.xz waveworks_archive-79b3462799c28af8ba586349bd671b1b56e72353.zip | |
Initial commit with PS4 and XBone stuff trimmed.
Diffstat (limited to 'test/gl2_mac')
| -rw-r--r-- | test/gl2_mac/GFSDK_WaveWorks_Attributes.fxh | 277 | ||||
| -rw-r--r-- | test/gl2_mac/GFSDK_WaveWorks_Common.fxh | 77 | ||||
| -rw-r--r-- | test/gl2_mac/GFSDK_WaveWorks_Quadtree.fxh | 108 | ||||
| -rw-r--r-- | test/gl2_mac/foam.dds | bin | 0 -> 1048703 bytes | |||
| -rw-r--r-- | test/gl2_mac/foam_intensity_perlin2_rgb.dds | bin | 0 -> 22369748 bytes | |||
| -rw-r--r-- | test/gl2_mac/main.cpp | 405 | ||||
| -rw-r--r-- | test/gl2_mac/math_code.cpp | 488 | ||||
| -rw-r--r-- | test/gl2_mac/math_code.h | 49 | ||||
| -rw-r--r-- | test/gl2_mac/ocean_surface.cpp | 874 | ||||
| -rw-r--r-- | test/gl2_mac/ocean_surface.cpp.bak | bin | 0 -> 63516 bytes | |||
| -rw-r--r-- | test/gl2_mac/ocean_surface.h | 295 | ||||
| -rw-r--r-- | test/gl2_mac/water.glsl | 173 |
12 files changed, 2746 insertions, 0 deletions
diff --git a/test/gl2_mac/GFSDK_WaveWorks_Attributes.fxh b/test/gl2_mac/GFSDK_WaveWorks_Attributes.fxh new file mode 100644 index 0000000..8cd01cd --- /dev/null +++ b/test/gl2_mac/GFSDK_WaveWorks_Attributes.fxh @@ -0,0 +1,277 @@ +/* + * This code contains NVIDIA Confidential Information and is disclosed + * under the Mutual Non-Disclosure Agreement. + * + * Notice + * ALL NVIDIA DESIGN SPECIFICATIONS AND CODE ("MATERIALS") ARE PROVIDED "AS IS" NVIDIA MAKES + * NO REPRESENTATIONS, WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO + * THE MATERIALS, AND EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTIES OF NONINFRINGEMENT, + * MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. + * + * NVIDIA Corporation assumes no responsibility for the consequences of use of such + * information or for any infringement of patents or other rights of third parties that may + * result from its use. No license is granted by implication or otherwise under any patent + * or patent rights of NVIDIA Corporation. No third party distribution is allowed unless + * expressly authorized by NVIDIA. Details are subject to change without notice. + * This code supersedes and replaces all information previously supplied. + * NVIDIA Corporation products are not authorized for use as critical + * components in life support devices or systems without express written approval of + * NVIDIA Corporation. + * + * Copyright � 2008- 2013 NVIDIA Corporation. All rights reserved. + * + * NVIDIA Corporation and its licensors retain all intellectual property and proprietary + * rights in and to this software and related documentation and any modifications thereto. + * Any use, reproduction, disclosure or distribution of this software and related + * documentation without an express license agreement from NVIDIA Corporation is + * strictly prohibited. + */ +#ifndef _GFSDK_WAVEWORKS_ATTRIBUTES_FX +#define _GFSDK_WAVEWORKS_ATTRIBUTES_FX +/* + * + * + */ +#include "GFSDK_WaveWorks_Common.fxh" +/* + * + * + */ +#if defined(GFSDK_WAVEWORKS_SM3) || defined(GFSDK_WAVEWORKS_GL) + #define GFSDK_WAVEWORKS_BEGIN_ATTR_VS_CBUFFER(Label) + #define GFSDK_WAVEWORKS_END_ATTR_VS_CBUFFER + #define GFSDK_WAVEWORKS_BEGIN_ATTR_PS_CBUFFER(Label) + #define GFSDK_WAVEWORKS_END_ATTR_PS_CBUFFER +#endif +#if defined( GFSDK_WAVEWORKS_USE_TESSELLATION ) + #define GFSDK_WAVEWORKS_BEGIN_ATTR_DISPLACEMENT_CBUFFER GFSDK_WAVEWORKS_BEGIN_ATTR_DS_CBUFFER + #define GFSDK_WAVEWORKS_DECLARE_ATTR_DISPLACEMENT_CONSTANT GFSDK_WAVEWORKS_DECLARE_ATTR_DS_CONSTANT + #define GFSDK_WAVEWORKS_END_ATTR_DISPLACEMENT_CBUFFER GFSDK_WAVEWORKS_END_ATTR_DS_CBUFFER + #define GFSDK_WAVEWORKS_DECLARE_ATTR_DISPLACEMENT_SAMPLER GFSDK_WAVEWORKS_DECLARE_ATTR_DS_SAMPLER + #define GFSDK_WAVEWORKS_DECLARE_ATTR_DISPLACEMENT_SAMPLER_TEXTUREARRAY GFSDK_WAVEWORKS_DECLARE_ATTR_DS_SAMPLER_TEXTUREARRAY +#else + #define GFSDK_WAVEWORKS_BEGIN_ATTR_DISPLACEMENT_CBUFFER GFSDK_WAVEWORKS_BEGIN_ATTR_VS_CBUFFER + #define GFSDK_WAVEWORKS_DECLARE_ATTR_DISPLACEMENT_CONSTANT GFSDK_WAVEWORKS_DECLARE_ATTR_VS_CONSTANT + #define GFSDK_WAVEWORKS_END_ATTR_DISPLACEMENT_CBUFFER GFSDK_WAVEWORKS_END_ATTR_VS_CBUFFER + #define GFSDK_WAVEWORKS_DECLARE_ATTR_DISPLACEMENT_SAMPLER GFSDK_WAVEWORKS_DECLARE_ATTR_VS_SAMPLER + #define GFSDK_WAVEWORKS_DECLARE_ATTR_DISPLACEMENT_SAMPLER_TEXTUREARRAY GFSDK_WAVEWORKS_DECLARE_ATTR_VS_SAMPLER_TEXTUREARRAY +#endif +GFSDK_WAVEWORKS_BEGIN_ATTR_DISPLACEMENT_CBUFFER(nv_waveworks_attr0) +GFSDK_WAVEWORKS_DECLARE_ATTR_DISPLACEMENT_CONSTANT(float3, nv_waveworks_attr1, 0) +#if defined( GFSDK_WAVEWORKS_GL ) + GFSDK_WAVEWORKS_DECLARE_ATTR_DISPLACEMENT_CONSTANT(float, nv_waveworks_attr2, 1) +#else + GFSDK_WAVEWORKS_DECLARE_ATTR_DISPLACEMENT_CONSTANT(float, nv_waveworks_attr3, 1) +#endif +GFSDK_WAVEWORKS_DECLARE_ATTR_DISPLACEMENT_CONSTANT(float4, nv_waveworks_attr4, 2) +GFSDK_WAVEWORKS_END_ATTR_DISPLACEMENT_CBUFFER +GFSDK_WAVEWORKS_DECLARE_ATTR_DISPLACEMENT_SAMPLER(nv_waveworks_attr5, nv_waveworks_attr6, 0) +GFSDK_WAVEWORKS_DECLARE_ATTR_DISPLACEMENT_SAMPLER(nv_waveworks_attr7, nv_waveworks_attr8, 1) +GFSDK_WAVEWORKS_DECLARE_ATTR_DISPLACEMENT_SAMPLER(nv_waveworks_attr9, nv_waveworks_attr10, 2) +GFSDK_WAVEWORKS_DECLARE_ATTR_DISPLACEMENT_SAMPLER(nv_waveworks_attr11, nv_waveworks_attr12, 3) +#if defined( GFSDK_WAVEWORKS_GL ) + GFSDK_WAVEWORKS_DECLARE_ATTR_DISPLACEMENT_SAMPLER_TEXTUREARRAY(nv_waveworks_attr13, nv_waveworks_attr14, 4) +#endif +GFSDK_WAVEWORKS_BEGIN_ATTR_PS_CBUFFER(nv_waveworks_attr15) +GFSDK_WAVEWORKS_DECLARE_ATTR_PS_CONSTANT(float, nv_waveworks_attr16, 0) +GFSDK_WAVEWORKS_DECLARE_ATTR_PS_CONSTANT(float, nv_waveworks_attr17, 1) +GFSDK_WAVEWORKS_DECLARE_ATTR_PS_CONSTANT(float, nv_waveworks_attr18, 2) +GFSDK_WAVEWORKS_DECLARE_ATTR_PS_CONSTANT(float, nv_waveworks_attr19, 3) +GFSDK_WAVEWORKS_DECLARE_ATTR_PS_CONSTANT(float, nv_waveworks_attr20, 4) +GFSDK_WAVEWORKS_DECLARE_ATTR_PS_CONSTANT(float, nv_waveworks_attr21, 5) +GFSDK_WAVEWORKS_DECLARE_ATTR_PS_CONSTANT(float, nv_waveworks_attr22, 6) +GFSDK_WAVEWORKS_DECLARE_ATTR_PS_CONSTANT(float, nv_waveworks_attr23, 7) +GFSDK_WAVEWORKS_DECLARE_ATTR_PS_CONSTANT(float, nv_waveworks_attr24, 8) +GFSDK_WAVEWORKS_DECLARE_ATTR_PS_CONSTANT(float, nv_waveworks_attr25, 9) +GFSDK_WAVEWORKS_END_ATTR_PS_CBUFFER +GFSDK_WAVEWORKS_DECLARE_ATTR_PS_SAMPLER(nv_waveworks_attr26, nv_waveworks_attr27, 0) +GFSDK_WAVEWORKS_DECLARE_ATTR_PS_SAMPLER(nv_waveworks_attr28, nv_waveworks_attr29, 1) +GFSDK_WAVEWORKS_DECLARE_ATTR_PS_SAMPLER(nv_waveworks_attr30, nv_waveworks_attr31, 2) +GFSDK_WAVEWORKS_DECLARE_ATTR_PS_SAMPLER(nv_waveworks_attr32, nv_waveworks_attr33, 3) +#if defined( GFSDK_WAVEWORKS_GL ) + GFSDK_WAVEWORKS_DECLARE_ATTR_PS_SAMPLER_TEXTUREARRAY(nv_waveworks_attr34, nv_waveworks_attr35, 4) +#endif +struct GFSDK_WAVEWORKS_INTERPOLATED_VERTEX_OUTPUT +{ + float4 nv_waveworks_attr36 SEMANTIC(TEXCOORD0); + float4 nv_waveworks_attr37 SEMANTIC(TEXCOORD1); + float4 nv_waveworks_attr38 SEMANTIC(TEXCOORD2); + float3 nv_waveworks_attr39 SEMANTIC(TEXCOORD3); +}; +struct GFSDK_WAVEWORKS_VERTEX_OUTPUT +{ + GFSDK_WAVEWORKS_INTERPOLATED_VERTEX_OUTPUT interp; + float3 pos_world; + float3 pos_world_undisplaced; + float3 world_displacement; +}; +GFSDK_WAVEWORKS_VERTEX_OUTPUT GFSDK_WaveWorks_GetDisplacedVertex(GFSDK_WAVEWORKS_VERTEX_INPUT In) +{ + float3 nv_waveworks_attr40 = GFSDK_WaveWorks_GetUndisplacedVertexWorldPosition(In); + float nv_waveworks_attr41 = length(nv_waveworks_attr1 - nv_waveworks_attr40); + float2 nv_waveworks_attr42 = nv_waveworks_attr40.xy * nv_waveworks_attr4.x; + float2 nv_waveworks_attr43 = nv_waveworks_attr40.xy * nv_waveworks_attr4.y; + float2 nv_waveworks_attr44 = nv_waveworks_attr40.xy * nv_waveworks_attr4.z; + float2 nv_waveworks_attr45 = nv_waveworks_attr40.xy * nv_waveworks_attr4.w; + float4 nv_waveworks_attr46; + float4 nv_waveworks_attr47 = 1.0/nv_waveworks_attr4.xyzw; + nv_waveworks_attr46.x = 1.0; + nv_waveworks_attr46.yzw = saturate(0.25*(nv_waveworks_attr47.yzw*24.0-nv_waveworks_attr41)/nv_waveworks_attr47.yzw); + nv_waveworks_attr46.yzw *= nv_waveworks_attr46.yzw; + #if defined(GFSDK_WAVEWORKS_GL) + float3 nv_waveworks_attr48; + if(nv_waveworks_attr2 > 0) + { + nv_waveworks_attr48 = nv_waveworks_attr46.x * SampleTex2Dlod(nv_waveworks_attr14, nv_waveworks_attr13, vec3(nv_waveworks_attr42, 0.0), 0).xyz; + nv_waveworks_attr48 += nv_waveworks_attr46.y==0? float3(0,0,0) : nv_waveworks_attr46.y * SampleTex2Dlod(nv_waveworks_attr14, nv_waveworks_attr13, vec3(nv_waveworks_attr43, 1.0), 0).xyz; + nv_waveworks_attr48 += nv_waveworks_attr46.z==0? float3(0,0,0) : nv_waveworks_attr46.z * SampleTex2Dlod(nv_waveworks_attr14, nv_waveworks_attr13, vec3(nv_waveworks_attr44, 2.0), 0).xyz; + nv_waveworks_attr48 += nv_waveworks_attr46.w==0? float3(0,0,0) : nv_waveworks_attr46.w * SampleTex2Dlod(nv_waveworks_attr14, nv_waveworks_attr13, vec3(nv_waveworks_attr45, 3.0), 0).xyz; + } + else + { + nv_waveworks_attr48 = nv_waveworks_attr46.x * SampleTex2Dlod(nv_waveworks_attr6, nv_waveworks_attr5, nv_waveworks_attr42, 0).xyz; + nv_waveworks_attr48 += nv_waveworks_attr46.y==0? float3(0,0,0) : nv_waveworks_attr46.y * SampleTex2Dlod(nv_waveworks_attr8, nv_waveworks_attr7, nv_waveworks_attr43, 0).xyz; + nv_waveworks_attr48 += nv_waveworks_attr46.z==0? float3(0,0,0) : nv_waveworks_attr46.z * SampleTex2Dlod(nv_waveworks_attr10, nv_waveworks_attr9, nv_waveworks_attr44, 0).xyz; + nv_waveworks_attr48 += nv_waveworks_attr46.w==0? float3(0,0,0) : nv_waveworks_attr46.w * SampleTex2Dlod(nv_waveworks_attr12, nv_waveworks_attr11, nv_waveworks_attr45, 0).xyz; + } + #else + float3 nv_waveworks_attr48 = nv_waveworks_attr46.x * SampleTex2Dlod(nv_waveworks_attr6, nv_waveworks_attr5, nv_waveworks_attr42, 0).xyz; + nv_waveworks_attr48 += nv_waveworks_attr46.y==0? float3(0,0,0) : nv_waveworks_attr46.y * SampleTex2Dlod(nv_waveworks_attr8, nv_waveworks_attr7, nv_waveworks_attr43, 0).xyz; + nv_waveworks_attr48 += nv_waveworks_attr46.z==0? float3(0,0,0) : nv_waveworks_attr46.z * SampleTex2Dlod(nv_waveworks_attr10, nv_waveworks_attr9, nv_waveworks_attr44, 0).xyz; + nv_waveworks_attr48 += nv_waveworks_attr46.w==0? float3(0,0,0) : nv_waveworks_attr46.w * SampleTex2Dlod(nv_waveworks_attr12, nv_waveworks_attr11, nv_waveworks_attr45, 0).xyz; + #endif + float3 nv_waveworks_attr49 = nv_waveworks_attr40 + nv_waveworks_attr48; + GFSDK_WAVEWORKS_VERTEX_OUTPUT Output; + Output.interp.nv_waveworks_attr39 = nv_waveworks_attr1 - nv_waveworks_attr49; + Output.interp.nv_waveworks_attr36.xy = nv_waveworks_attr42; + Output.interp.nv_waveworks_attr36.zw = nv_waveworks_attr43; + Output.interp.nv_waveworks_attr37.xy = nv_waveworks_attr44; + Output.interp.nv_waveworks_attr37.zw = nv_waveworks_attr45; + Output.interp.nv_waveworks_attr38 = nv_waveworks_attr46; + Output.pos_world = nv_waveworks_attr49; + Output.pos_world_undisplaced = nv_waveworks_attr40; + Output.world_displacement = nv_waveworks_attr48; + return Output; +} +GFSDK_WAVEWORKS_VERTEX_OUTPUT GFSDK_WaveWorks_GetDisplacedVertexAfterTessellation(float4 In0, float4 In1, float4 In2, float3 BarycentricCoords) +{ + float3 nv_waveworks_attr50 = In0.xyz * BarycentricCoords.x + + In1.xyz * BarycentricCoords.y + + In2.xyz * BarycentricCoords.z; + float3 nv_waveworks_attr40 = nv_waveworks_attr50; + float4 nv_waveworks_attr46; + float nv_waveworks_attr41 = length(nv_waveworks_attr1 - nv_waveworks_attr40); + float4 nv_waveworks_attr47 = 1.0/nv_waveworks_attr4.xyzw; + nv_waveworks_attr46.x = 1.0; + nv_waveworks_attr46.yzw = saturate(0.25*(nv_waveworks_attr47.yzw*24.0-nv_waveworks_attr41)/nv_waveworks_attr47.yzw); + nv_waveworks_attr46.yzw *= nv_waveworks_attr46.yzw; + float2 nv_waveworks_attr42 = nv_waveworks_attr40.xy * nv_waveworks_attr4.x; + float2 nv_waveworks_attr43 = nv_waveworks_attr40.xy * nv_waveworks_attr4.y; + float2 nv_waveworks_attr44 = nv_waveworks_attr40.xy * nv_waveworks_attr4.z; + float2 nv_waveworks_attr45 = nv_waveworks_attr40.xy * nv_waveworks_attr4.w; + #if defined(GFSDK_WAVEWORKS_GL) + float3 nv_waveworks_attr48; + if(nv_waveworks_attr2 > 0) + { + nv_waveworks_attr48 = nv_waveworks_attr46.x * SampleTex2Dlod(nv_waveworks_attr14, nv_waveworks_attr13, vec3(nv_waveworks_attr42, 0.0), 0).xyz; + nv_waveworks_attr48 += nv_waveworks_attr46.y==0? float3(0,0,0) : nv_waveworks_attr46.y * SampleTex2Dlod(nv_waveworks_attr14, nv_waveworks_attr13, vec3(nv_waveworks_attr43, 1.0), 0).xyz; + nv_waveworks_attr48 += nv_waveworks_attr46.z==0? float3(0,0,0) : nv_waveworks_attr46.z * SampleTex2Dlod(nv_waveworks_attr14, nv_waveworks_attr13, vec3(nv_waveworks_attr44, 2.0), 0).xyz; + nv_waveworks_attr48 += nv_waveworks_attr46.w==0? float3(0,0,0) : nv_waveworks_attr46.w * SampleTex2Dlod(nv_waveworks_attr14, nv_waveworks_attr13, vec3(nv_waveworks_attr45, 3.0), 0).xyz; + } + else + { + nv_waveworks_attr48 = nv_waveworks_attr46.x * SampleTex2Dlod(nv_waveworks_attr6, nv_waveworks_attr5, nv_waveworks_attr42, 0).xyz; + nv_waveworks_attr48 += nv_waveworks_attr46.y==0? float3(0,0,0) : nv_waveworks_attr46.y * SampleTex2Dlod(nv_waveworks_attr8, nv_waveworks_attr7, nv_waveworks_attr43, 0).xyz; + nv_waveworks_attr48 += nv_waveworks_attr46.z==0? float3(0,0,0) : nv_waveworks_attr46.z * SampleTex2Dlod(nv_waveworks_attr10, nv_waveworks_attr9, nv_waveworks_attr44, 0).xyz; + nv_waveworks_attr48 += nv_waveworks_attr46.w==0? float3(0,0,0) : nv_waveworks_attr46.w * SampleTex2Dlod(nv_waveworks_attr12, nv_waveworks_attr11, nv_waveworks_attr45, 0).xyz; + } + #else + float3 nv_waveworks_attr48 = nv_waveworks_attr46.x * SampleTex2Dlod(nv_waveworks_attr6, nv_waveworks_attr5, nv_waveworks_attr42, 0).xyz; + nv_waveworks_attr48 += nv_waveworks_attr46.y==0? float3(0,0,0) : nv_waveworks_attr46.y * SampleTex2Dlod(nv_waveworks_attr8, nv_waveworks_attr7, nv_waveworks_attr43, 0).xyz; + nv_waveworks_attr48 += nv_waveworks_attr46.z==0? float3(0,0,0) : nv_waveworks_attr46.z * SampleTex2Dlod(nv_waveworks_attr10, nv_waveworks_attr9, nv_waveworks_attr44, 0).xyz; + nv_waveworks_attr48 += nv_waveworks_attr46.w==0? float3(0,0,0) : nv_waveworks_attr46.w * SampleTex2Dlod(nv_waveworks_attr12, nv_waveworks_attr11, nv_waveworks_attr45, 0).xyz; + #endif + float3 nv_waveworks_attr49 = nv_waveworks_attr40 + nv_waveworks_attr48; + GFSDK_WAVEWORKS_VERTEX_OUTPUT Output; + Output.interp.nv_waveworks_attr39 = nv_waveworks_attr1 - nv_waveworks_attr49; + Output.interp.nv_waveworks_attr36.xy = nv_waveworks_attr42; + Output.interp.nv_waveworks_attr36.zw = nv_waveworks_attr43; + Output.interp.nv_waveworks_attr37.xy = nv_waveworks_attr44; + Output.interp.nv_waveworks_attr37.zw = nv_waveworks_attr45; + Output.interp.nv_waveworks_attr38 = nv_waveworks_attr46; + Output.pos_world = nv_waveworks_attr49; + Output.pos_world_undisplaced = nv_waveworks_attr40; + Output.world_displacement = nv_waveworks_attr48; + return Output; +} +struct GFSDK_WAVEWORKS_SURFACE_ATTRIBUTES +{ + float3 normal; + float3 eye_dir; + float foam_surface_folding; + float foam_turbulent_energy; + float foam_wave_hats; +}; +GFSDK_WAVEWORKS_SURFACE_ATTRIBUTES GFSDK_WaveWorks_GetSurfaceAttributes(GFSDK_WAVEWORKS_INTERPOLATED_VERTEX_OUTPUT In) +{ + float3 nv_waveworks_attr51 = normalize(In.nv_waveworks_attr39); + float4 nv_waveworks_attr52; + float4 nv_waveworks_attr53; + float4 nv_waveworks_attr54; + float4 nv_waveworks_attr55; + #if defined(GFSDK_WAVEWORKS_GL) + float3 nv_waveworks_attr48; + if(nv_waveworks_attr2 > 0) + { + nv_waveworks_attr52 = SampleTex2D(nv_waveworks_attr35, nv_waveworks_attr34, vec3(In.nv_waveworks_attr36.xy, 0.0)); + nv_waveworks_attr53 = SampleTex2D(nv_waveworks_attr35, nv_waveworks_attr34, vec3(In.nv_waveworks_attr36.zw, 1.0)); + nv_waveworks_attr54 = SampleTex2D(nv_waveworks_attr35, nv_waveworks_attr34, vec3(In.nv_waveworks_attr37.xy, 2.0)); + nv_waveworks_attr55 = SampleTex2D(nv_waveworks_attr35, nv_waveworks_attr34, vec3(In.nv_waveworks_attr37.zw, 3.0)); + } + else + { + nv_waveworks_attr52 = SampleTex2D(nv_waveworks_attr27, nv_waveworks_attr26, In.nv_waveworks_attr36.xy); + nv_waveworks_attr53 = SampleTex2D(nv_waveworks_attr29, nv_waveworks_attr28, In.nv_waveworks_attr36.zw); + nv_waveworks_attr54 = SampleTex2D(nv_waveworks_attr31, nv_waveworks_attr30, In.nv_waveworks_attr37.xy); + nv_waveworks_attr55 = SampleTex2D(nv_waveworks_attr33, nv_waveworks_attr32, In.nv_waveworks_attr37.zw); + } + #else + nv_waveworks_attr52 = SampleTex2D(nv_waveworks_attr27, nv_waveworks_attr26, In.nv_waveworks_attr36.xy); + nv_waveworks_attr53 = SampleTex2D(nv_waveworks_attr29, nv_waveworks_attr28, In.nv_waveworks_attr36.zw); + nv_waveworks_attr54 = SampleTex2D(nv_waveworks_attr31, nv_waveworks_attr30, In.nv_waveworks_attr37.xy); + nv_waveworks_attr55 = SampleTex2D(nv_waveworks_attr33, nv_waveworks_attr32, In.nv_waveworks_attr37.zw); + #endif + float2 nv_waveworks_attr56; + nv_waveworks_attr56.xy = nv_waveworks_attr52.xy*In.nv_waveworks_attr38.x + + nv_waveworks_attr53.xy*In.nv_waveworks_attr38.y*nv_waveworks_attr18 + + nv_waveworks_attr54.xy*In.nv_waveworks_attr38.z*nv_waveworks_attr21 + + nv_waveworks_attr55.xy*In.nv_waveworks_attr38.w*nv_waveworks_attr24; + float nv_waveworks_attr57 = 0.25; + float nv_waveworks_attr58 = + 100.0*nv_waveworks_attr52.w * + lerp(nv_waveworks_attr57, nv_waveworks_attr53.w, In.nv_waveworks_attr38.y)* + lerp(nv_waveworks_attr57, nv_waveworks_attr54.w, In.nv_waveworks_attr38.z)* + lerp(nv_waveworks_attr57, nv_waveworks_attr55.w, In.nv_waveworks_attr38.w); + float nv_waveworks_attr59 = + max(-100, + (1.0-nv_waveworks_attr52.z) + + (1.0-nv_waveworks_attr53.z) + + (1.0-nv_waveworks_attr54.z) + + (1.0-nv_waveworks_attr55.z)); + float3 nv_waveworks_attr60 = normalize(float3(nv_waveworks_attr56, nv_waveworks_attr16)); + float nv_waveworks_attr61 = 0.5; + float nv_waveworks_attr62 = + 10.0*(-0.55 + + (1.0-nv_waveworks_attr52.z) + + nv_waveworks_attr61*(1.0-nv_waveworks_attr53.z) + + nv_waveworks_attr61*nv_waveworks_attr61*(1.0-nv_waveworks_attr54.z) + + nv_waveworks_attr61*nv_waveworks_attr61*nv_waveworks_attr61*(1.0-nv_waveworks_attr55.z)); + GFSDK_WAVEWORKS_SURFACE_ATTRIBUTES Output; + Output.normal = nv_waveworks_attr60; + Output.eye_dir = nv_waveworks_attr51; + Output.foam_surface_folding = nv_waveworks_attr59; + Output.foam_turbulent_energy = log(1.0 + nv_waveworks_attr58); + Output.foam_wave_hats = nv_waveworks_attr62; + return Output; +} +#endif /* _GFSDK_WAVEWORKS_ATTRIBUTES_FX */ diff --git a/test/gl2_mac/GFSDK_WaveWorks_Common.fxh b/test/gl2_mac/GFSDK_WaveWorks_Common.fxh new file mode 100644 index 0000000..25de92b --- /dev/null +++ b/test/gl2_mac/GFSDK_WaveWorks_Common.fxh @@ -0,0 +1,77 @@ +/* + * This code contains NVIDIA Confidential Information and is disclosed + * under the Mutual Non-Disclosure Agreement. + * + * Notice + * ALL NVIDIA DESIGN SPECIFICATIONS AND CODE ("MATERIALS") ARE PROVIDED "AS IS" NVIDIA MAKES + * NO REPRESENTATIONS, WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO + * THE MATERIALS, AND EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTIES OF NONINFRINGEMENT, + * MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. + * + * NVIDIA Corporation assumes no responsibility for the consequences of use of such + * information or for any infringement of patents or other rights of third parties that may + * result from its use. No license is granted by implication or otherwise under any patent + * or patent rights of NVIDIA Corporation. No third party distribution is allowed unless + * expressly authorized by NVIDIA. Details are subject to change without notice. + * This code supersedes and replaces all information previously supplied. + * NVIDIA Corporation products are not authorized for use as critical + * components in life support devices or systems without express written approval of + * NVIDIA Corporation. + * + * Copyright � 2008- 2013 NVIDIA Corporation. All rights reserved. + * + * NVIDIA Corporation and its licensors retain all intellectual property and proprietary + * rights in and to this software and related documentation and any modifications thereto. + * Any use, reproduction, disclosure or distribution of this software and related + * documentation without an express license agreement from NVIDIA Corporation is + * strictly prohibited. + */ +#ifndef _GFSDK_WAVEWORKS_COMMON_FX +#define _GFSDK_WAVEWORKS_COMMON_FX +/* + * + * + */ +#if defined(GFSDK_WAVEWORKS_SM4) || defined(GFSDK_WAVEWORKS_SM5) + #define SampleTex2D(nv_waveworks_comm0,nv_waveworks_comm1,nv_waveworks_comm2) nv_waveworks_comm0.Sample(nv_waveworks_comm1,nv_waveworks_comm2) + #define SampleTex2Dlod(nv_waveworks_comm0,nv_waveworks_comm1,nv_waveworks_comm2,nv_waveworks_comm3) nv_waveworks_comm0.SampleLevel(nv_waveworks_comm1,nv_waveworks_comm2,nv_waveworks_comm3) + #define BEGIN_CBUFFER(name,slot) cbuffer name : register(b##slot) { + #define END_CBUFFER }; + #define SEMANTIC(x) : x +#elif defined(GFSDK_WAVEWORKS_SM3) + #define SampleTex2D(nv_waveworks_comm0,nv_waveworks_comm1,nv_waveworks_comm2) tex2D(nv_waveworks_comm1,nv_waveworks_comm2) + #define SampleTex2Dlod(nv_waveworks_comm0,nv_waveworks_comm1,nv_waveworks_comm2,nv_waveworks_comm3) tex2Dlod(nv_waveworks_comm1,float4(nv_waveworks_comm2,0,nv_waveworks_comm3)) + #define BEGIN_CBUFFER(name,slot) + #define END_CBUFFER + #define SV_Target COLOR + #define SV_Position POSITION + #define SEMANTIC(x) : x +#elif defined(GFSDK_WAVEWORKS_GNM) + #define SampleTex2D(nv_waveworks_comm0,nv_waveworks_comm1,nv_waveworks_comm2) nv_waveworks_comm0.Sample(nv_waveworks_comm1,nv_waveworks_comm2) + #define SampleTex2Dlod(nv_waveworks_comm0,nv_waveworks_comm1,nv_waveworks_comm2,nv_waveworks_comm3) nv_waveworks_comm0.SampleLOD(nv_waveworks_comm1,nv_waveworks_comm2,nv_waveworks_comm3) + #define BEGIN_CBUFFER(name,slot) ConstantBuffer name : register(b##slot) { + #define END_CBUFFER }; + #define SV_Target S_TARGET_OUTPUT + #define SV_Position S_POSITION + #define SEMANTIC(x) : x +#elif defined(GFSDK_WAVEWORKS_GL) + #define SampleTex2D(nv_waveworks_comm0,nv_waveworks_comm1,nv_waveworks_comm2) texture(nv_waveworks_comm1,nv_waveworks_comm2) + #define SampleTex2Dlod(nv_waveworks_comm0,nv_waveworks_comm1,nv_waveworks_comm2,nv_waveworks_comm3) textureLod(nv_waveworks_comm1,nv_waveworks_comm2,nv_waveworks_comm3) + #define BEGIN_CBUFFER(name,slot) + #define END_CBUFFER + #define SEMANTIC(x) + #define float2 vec2 + #define float3 vec3 + #define float4 vec4 + #define float4x3 mat3x4 + vec3 mul(vec4 v, mat3x4 m) { return v * m; } + #define lerp mix + #define saturate(x) clamp(x,0.0,1.0) +#else + #error Shader model not defined (expected GFSDK_WAVEWORKS_SM3, GFSDK_WAVEWORKS_SM4, GFSDK_WAVEWORKS_SM5, GFSDK_WAVEWORKS_GNM or GFSDK_WAVEWORKS_GL) +#endif +/* + * + * + */ +#endif /* _GFSDK_WAVEWORKS_COMMON_FX */ diff --git a/test/gl2_mac/GFSDK_WaveWorks_Quadtree.fxh b/test/gl2_mac/GFSDK_WaveWorks_Quadtree.fxh new file mode 100644 index 0000000..90fa6f8 --- /dev/null +++ b/test/gl2_mac/GFSDK_WaveWorks_Quadtree.fxh @@ -0,0 +1,108 @@ +/* + * This code contains NVIDIA Confidential Information and is disclosed + * under the Mutual Non-Disclosure Agreement. + * + * Notice + * ALL NVIDIA DESIGN SPECIFICATIONS AND CODE ("MATERIALS") ARE PROVIDED "AS IS" NVIDIA MAKES + * NO REPRESENTATIONS, WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO + * THE MATERIALS, AND EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTIES OF NONINFRINGEMENT, + * MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. + * + * NVIDIA Corporation assumes no responsibility for the consequences of use of such + * information or for any infringement of patents or other rights of third parties that may + * result from its use. No license is granted by implication or otherwise under any patent + * or patent rights of NVIDIA Corporation. No third party distribution is allowed unless + * expressly authorized by NVIDIA. Details are subject to change without notice. + * This code supersedes and replaces all information previously supplied. + * NVIDIA Corporation products are not authorized for use as critical + * components in life support devices or systems without express written approval of + * NVIDIA Corporation. + * + * Copyright � 2008- 2013 NVIDIA Corporation. All rights reserved. + * + * NVIDIA Corporation and its licensors retain all intellectual property and proprietary + * rights in and to this software and related documentation and any modifications thereto. + * Any use, reproduction, disclosure or distribution of this software and related + * documentation without an express license agreement from NVIDIA Corporation is + * strictly prohibited. + */ +#ifndef _GFSDK_WAVEWORKS_QUADPATCH_FX +#define _GFSDK_WAVEWORKS_QUADPATCH_FX +/* + * + * + */ +#include "GFSDK_WaveWorks_Common.fxh" +/* + * + * + */ +#if defined(GFSDK_WAVEWORKS_SM3) || defined(GFSDK_WAVEWORKS_GL) + #define GFSDK_WAVEWORKS_BEGIN_GEOM_VS_CBUFFER(Label) + #define GFSDK_WAVEWORKS_END_GEOM_VS_CBUFFER +#endif +#if defined( GFSDK_WAVEWORKS_USE_TESSELLATION ) + GFSDK_WAVEWORKS_BEGIN_GEOM_HS_CBUFFER(nv_waveworks_quad0) + GFSDK_WAVEWORKS_DECLARE_GEOM_HS_CONSTANT(float4, nv_waveworks_quad1, 0) + GFSDK_WAVEWORKS_DECLARE_GEOM_HS_CONSTANT(float4, nv_waveworks_quad2, 1) + GFSDK_WAVEWORKS_END_GEOM_HS_CBUFFER +#endif +GFSDK_WAVEWORKS_BEGIN_GEOM_VS_CBUFFER(nv_waveworks_quad3) +GFSDK_WAVEWORKS_DECLARE_GEOM_VS_CONSTANT(float4x3, nv_waveworks_quad4, 0) +GFSDK_WAVEWORKS_DECLARE_GEOM_VS_CONSTANT(float4, nv_waveworks_quad5, 3) +GFSDK_WAVEWORKS_DECLARE_GEOM_VS_CONSTANT(float4, nv_waveworks_quad6, 4) +GFSDK_WAVEWORKS_END_GEOM_VS_CBUFFER +struct GFSDK_WAVEWORKS_VERTEX_INPUT +{ + float4 nv_waveworks_quad7 SEMANTIC(POSITION); +}; +float3 GFSDK_WaveWorks_GetUndisplacedVertexWorldPosition(GFSDK_WAVEWORKS_VERTEX_INPUT In) +{ + float2 nv_waveworks_quad8 = In.nv_waveworks_quad7.xy; +#if !defined(GFSDK_WAVEWORKS_USE_TESSELLATION) + float nv_waveworks_quad9 = 0.5f; + float nv_waveworks_quad10 = nv_waveworks_quad6.w; + float2 nv_waveworks_quad11 = nv_waveworks_quad8; + float2 nv_waveworks_quad12 = nv_waveworks_quad11; + float nv_waveworks_quad13 = 0.f; + for(int nv_waveworks_quad14 = 0; nv_waveworks_quad14 != 4; ++nv_waveworks_quad14) { + float2 nv_waveworks_quad15; + float2 nv_waveworks_quad16 = modf(nv_waveworks_quad9*nv_waveworks_quad11.xy,nv_waveworks_quad15); + if(0.5f == nv_waveworks_quad16.x && 0.5f == nv_waveworks_quad16.y) nv_waveworks_quad12.xy = nv_waveworks_quad11.xy - nv_waveworks_quad10; + else if(0.5f == nv_waveworks_quad16.x) nv_waveworks_quad12.x = nv_waveworks_quad11.x + nv_waveworks_quad10; + else if(0.5f == nv_waveworks_quad16.y) nv_waveworks_quad12.y = nv_waveworks_quad11.y + nv_waveworks_quad10; + float3 nv_waveworks_quad17 = mul(float4(nv_waveworks_quad11,0.f,1.f), nv_waveworks_quad4) - nv_waveworks_quad5.xyz; + float nv_waveworks_quad18 = length(nv_waveworks_quad17); + float nv_waveworks_quad19 = log2(nv_waveworks_quad18 * nv_waveworks_quad6.x) + 1.f; + nv_waveworks_quad13 = saturate(nv_waveworks_quad19 - float(nv_waveworks_quad14)); + if(nv_waveworks_quad13 < 1.f) { + break; + } else { + nv_waveworks_quad11 = nv_waveworks_quad12; + nv_waveworks_quad9 *= 0.5f; + nv_waveworks_quad10 *= -2.f; + } + } + nv_waveworks_quad8.xy = lerp(nv_waveworks_quad11, nv_waveworks_quad12, nv_waveworks_quad13); +#endif + return mul(float4(nv_waveworks_quad8,In.nv_waveworks_quad7.zw), nv_waveworks_quad4); +} +#if defined( GFSDK_WAVEWORKS_USE_TESSELLATION ) +float GFSDK_WaveWorks_GetEdgeTessellationFactor(float4 vertex1, float4 vertex2) +{ + float4 nv_waveworks_quad20 = 0.5*(vertex1 + vertex2); + float nv_waveworks_quad21 = length (vertex1 - vertex2); + float nv_waveworks_quad22 = length(nv_waveworks_quad1.xyz - nv_waveworks_quad20.xyz); + return max(1.0,nv_waveworks_quad2.x * nv_waveworks_quad21 / nv_waveworks_quad22); +} +float GFSDK_WaveWorks_GetVertexTargetTessellatedEdgeLength(float3 vertex) +{ + float nv_waveworks_quad23 = length(nv_waveworks_quad1.xyz - vertex.xyz); + return nv_waveworks_quad23 / nv_waveworks_quad2.x; +} +#endif +/* + * + * + */ +#endif /* _GFSDK_WAVEWORKS_QUADPATCH_FX */ diff --git a/test/gl2_mac/foam.dds b/test/gl2_mac/foam.dds Binary files differnew file mode 100644 index 0000000..3a8a232 --- /dev/null +++ b/test/gl2_mac/foam.dds diff --git a/test/gl2_mac/foam_intensity_perlin2_rgb.dds b/test/gl2_mac/foam_intensity_perlin2_rgb.dds Binary files differnew file mode 100644 index 0000000..0510bb8 --- /dev/null +++ b/test/gl2_mac/foam_intensity_perlin2_rgb.dds diff --git a/test/gl2_mac/main.cpp b/test/gl2_mac/main.cpp new file mode 100644 index 0000000..be6c32b --- /dev/null +++ b/test/gl2_mac/main.cpp @@ -0,0 +1,405 @@ +// ocean_cufft_app.cpp : Defines the entry point for the console application. +// + + + +#include "math_code.h" +#include "ocean_surface.h" +#include "GFSDK_WaveWorks.h" +#include <string> + +#ifndef PI +#define PI 3.14159265358979323846f +#endif + +OceanSurface* g_pOceanSurf = NULL; +GFSDK_WaveWorks_SimulationHandle g_hOceanSimulation = NULL; +GFSDK_WaveWorks_Simulation_Params g_ocean_simulation_param; +GFSDK_WaveWorks_Simulation_Settings g_ocean_simulation_settings; +GFSDK_WaveWorks_Quadtree_Params g_ocean_param_quadtree; +GFSDK_WaveWorks_Simulation_Stats g_ocean_stats_simulation; +GFSDK_WaveWorks_Quadtree_Stats g_ocean_stats_quadtree; +GFSDK_WaveWorks_Simulation_Stats g_ocean_stats_simulation_filtered; +GFSDK_WAVEWORKS_GLFunctions g_GLFunctions; +int g_max_detail_level; + +bool g_ExitApp = false; +bool g_WindowActive = true; +bool g_PressedKeys[256]; // Array of pressed keys +int g_GLUTWindowHandle; +long long g_Microseconds; +long long g_OldMicroseconds; + +int WindowWidth = 1280; +int WindowHeight = 720; +bool LButtonPressed = false; + + +void PrintGLInfo(void) +{ + fprintf (stdout, "\nVendor: %s", glGetString (GL_VENDOR)); + fprintf (stdout, "\nRenderer: %s", glGetString (GL_RENDERER)); + fprintf (stdout, "\nVersion: %s", glGetString (GL_VERSION)); + fprintf (stdout, "\nGLSL: %s", glGetString (GL_SHADING_LANGUAGE_VERSION)); + checkError ("dumpInfo"); +} + +void KeyboardDownFunc(unsigned char key, int x, int y) +{ + g_PressedKeys[key] = true; +} + +void KeyboardUpFunc(unsigned char key, int x, int y) +{ + g_PressedKeys[key] = false; +} + +void ProcessKeys(void) +{ + if(g_pOceanSurf) + { + float direction[3]; + float strafe[3]; + float up[3] = {0.0f,0.0f,1.0f}; + + direction[0]=g_pOceanSurf->LookAtPosition[0] - g_pOceanSurf->CameraPosition[0]; + direction[1]=g_pOceanSurf->LookAtPosition[1] - g_pOceanSurf->CameraPosition[1]; + direction[2]=g_pOceanSurf->LookAtPosition[2] - g_pOceanSurf->CameraPosition[2]; + vec3Normalize(direction); + vec3CrossProductNormalized(strafe,up,direction); + + if(g_PressedKeys[119]) //'w' + { + g_pOceanSurf->CameraPosition[0]+=0.5f*direction[0]; + g_pOceanSurf->CameraPosition[1]+=0.5f*direction[1]; + g_pOceanSurf->CameraPosition[2]+=0.5f*direction[2]; + g_pOceanSurf->LookAtPosition[0]+=0.5f*direction[0]; + g_pOceanSurf->LookAtPosition[1]+=0.5f*direction[1]; + g_pOceanSurf->LookAtPosition[2]+=0.5f*direction[2]; + } + if(g_PressedKeys[115]) //'s' + { + g_pOceanSurf->CameraPosition[0]-=0.5f*direction[0]; + g_pOceanSurf->CameraPosition[1]-=0.5f*direction[1]; + g_pOceanSurf->CameraPosition[2]-=0.5f*direction[2]; + g_pOceanSurf->LookAtPosition[0]-=0.5f*direction[0]; + g_pOceanSurf->LookAtPosition[1]-=0.5f*direction[1]; + g_pOceanSurf->LookAtPosition[2]-=0.5f*direction[2]; + } + if(g_PressedKeys[97]) //'a' + { + g_pOceanSurf->CameraPosition[0]+=0.5f*strafe[0]; + g_pOceanSurf->CameraPosition[1]+=0.5f*strafe[1]; + g_pOceanSurf->CameraPosition[2]+=0.5f*strafe[2]; + g_pOceanSurf->LookAtPosition[0]+=0.5f*strafe[0]; + g_pOceanSurf->LookAtPosition[1]+=0.5f*strafe[1]; + g_pOceanSurf->LookAtPosition[2]+=0.5f*strafe[2]; + } + if(g_PressedKeys[100]) //'d' + { + g_pOceanSurf->CameraPosition[0]-=0.5f*strafe[0]; + g_pOceanSurf->CameraPosition[1]-=0.5f*strafe[1]; + g_pOceanSurf->CameraPosition[2]-=0.5f*strafe[2]; + g_pOceanSurf->LookAtPosition[0]-=0.5f*strafe[0]; + g_pOceanSurf->LookAtPosition[1]-=0.5f*strafe[1]; + g_pOceanSurf->LookAtPosition[2]-=0.5f*strafe[2]; + } + + if(g_PressedKeys[27]) // "esc" + { + throw "time to exit"; + } + } +} + +void RenderFrame(void) +{ + // Fill the simulation pipeline - this loop should execute once in all cases except the first + // iteration, when the simulation pipeline is first 'primed' + do { + GFSDK_WaveWorks_Simulation_SetTime(g_hOceanSimulation, g_pOceanSurf->total_time); + GFSDK_WaveWorks_Simulation_KickGL2(g_hOceanSimulation,NULL); + } while(gfsdk_waveworks_result_NONE == GFSDK_WaveWorks_Simulation_GetStagingCursor(g_hOceanSimulation,NULL)); + + // getting simulation timings + GFSDK_WaveWorks_Simulation_GetStats(g_hOceanSimulation,g_ocean_stats_simulation); + + + + g_pOceanSurf->Render(g_hOceanSimulation, g_ocean_simulation_settings); + + glutSwapBuffers(); + + // timing + timeval time; + gettimeofday(&time,NULL); + g_Microseconds = time.tv_sec*1000000 + time.tv_usec; + + g_pOceanSurf->delta_time = (float)(g_Microseconds - g_OldMicroseconds)/1000000.0f; + g_pOceanSurf->total_time += g_pOceanSurf->delta_time; + if(g_pOceanSurf->total_time>=36000.0f) g_pOceanSurf->total_time=0; + + g_OldMicroseconds = g_Microseconds; + + g_pOceanSurf->frame_number++; + + ProcessKeys(); +} + +void MouseFunc(int Button, int State, int MouseX, int MouseY) +{ + if((Button == GLUT_LEFT_BUTTON) && (State == GLUT_DOWN)) LButtonPressed = true; + if((Button == GLUT_LEFT_BUTTON) && (State == GLUT_UP)) LButtonPressed = false; + g_pOceanSurf->MouseX = MouseX; + g_pOceanSurf->MouseY = MouseY; +} +void MouseMotionFunc(int MouseX, int MouseY) +{ + if(g_pOceanSurf) + { + float initial_direction[4]={1.0f,0.0f,0.0f,1.0f}; + float direction[4]; + float r1[4][4],r2[4][4],rotation[4][4]; + + if(LButtonPressed) + { + g_pOceanSurf->MouseDX = (float)MouseX - (float)g_pOceanSurf->MouseX; + g_pOceanSurf->MouseDY = (float)MouseY - (float)g_pOceanSurf->MouseY; + } + else + { + g_pOceanSurf->MouseDX = 0; + g_pOceanSurf->MouseDY = 0; + } + g_pOceanSurf->MouseX = MouseX; + g_pOceanSurf->MouseY = MouseY; + + + g_pOceanSurf->Alpha+=g_pOceanSurf->MouseDX*0.002f; + g_pOceanSurf->Beta+=g_pOceanSurf->MouseDY*0.002f; + if(g_pOceanSurf->Beta>PI*0.49f)g_pOceanSurf->Beta = PI*0.49f; + if(g_pOceanSurf->Beta<-PI*0.49f)g_pOceanSurf->Beta = -PI*0.49f; + mat4CreateRotation(r1,g_pOceanSurf->Alpha,'z'); + mat4CreateRotation(r2,-g_pOceanSurf->Beta,'y'); + mat4Mat4Mul(rotation,r1,r2); + vec4Mat4Mul(direction,initial_direction,rotation); + g_pOceanSurf->LookAtPosition[0]=g_pOceanSurf->CameraPosition[0]+direction[0]; + g_pOceanSurf->LookAtPosition[1]=g_pOceanSurf->CameraPosition[1]+direction[1]; + g_pOceanSurf->LookAtPosition[2]=g_pOceanSurf->CameraPosition[2]+direction[2]; + } +} + +void Reshape(int w, int h) +{ + g_pOceanSurf->ScreenWidth = w; + g_pOceanSurf->ScreenHeight = h; +} + +int main(int argc, char* argv[]) +{ + // changing the current directory to executable directory + char currentdir[65536]; + char dir[65536]; + char file[65536]; + + getcwd(currentdir,65536); + splitToDirAndFile(argv[0],dir,file); + chdir(dir); + + + + gfsdk_waveworks_result res; + + glutInit(&argc, argv); + + glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_3_2_CORE_PROFILE); + + glutInitWindowSize((int) WindowWidth, (int) WindowHeight); + + /* create the window and store the handle to it */ + g_GLUTWindowHandle = glutCreateWindow("OpenGL Test Application"); + + + std::string cmdline; + for(int i=0 ; i<argc ; i++) + { + cmdline += argv[i]; + cmdline += " "; + } + + PrintGLInfo(); + g_ocean_simulation_settings.use_texture_arrays = true; + + // Creating OceanSurface instance + g_pOceanSurf = new OceanSurface(g_ocean_simulation_settings.use_texture_arrays); + + // Initializing GL functions to be passed to WaveWorks + g_GLFunctions.glGetError = glGetError; + g_GLFunctions.glGetIntegerv = glGetIntegerv; + g_GLFunctions.glTexParameterf = glTexParameterf; + g_GLFunctions.glTexParameteri = glTexParameteri; + g_GLFunctions.glTexImage2D = glTexImage2D; + g_GLFunctions.glTexSubImage2D = glTexSubImage2D; + g_GLFunctions.glClear = glClear; + g_GLFunctions.glClearColor = glClearColor; + g_GLFunctions.glColorMask = glColorMask; + g_GLFunctions.glDisable = glDisable; + g_GLFunctions.glViewport = glViewport; + g_GLFunctions.glBindTexture = glBindTexture; + g_GLFunctions.glDeleteTextures = glDeleteTextures; + g_GLFunctions.glGenTextures = glGenTextures; + g_GLFunctions.glDrawElements = glDrawElements; + g_GLFunctions.glActiveTexture = glActiveTexture; + g_GLFunctions.glBindBuffer = glBindBuffer; + g_GLFunctions.glDeleteBuffers = glDeleteBuffers; + g_GLFunctions.glGenBuffers = glGenBuffers; + g_GLFunctions.glBufferData = glBufferData; + g_GLFunctions.glMapBufferRange = glMapBufferRange; + g_GLFunctions.glUnmapBuffer = glUnmapBuffer; + g_GLFunctions.glAttachShader = glAttachShader; + g_GLFunctions.glCompileShader = glCompileShader; + g_GLFunctions.glCreateProgram = glCreateProgram; + g_GLFunctions.glCreateShader = glCreateShader; + g_GLFunctions.glDeleteProgram = glDeleteProgram; + g_GLFunctions.glDeleteShader = glDeleteShader; + g_GLFunctions.glDisableVertexAttribArray = glDisableVertexAttribArray; + g_GLFunctions.glEnableVertexAttribArray = glEnableVertexAttribArray; + g_GLFunctions.glGetAttribLocation = glGetAttribLocation; + g_GLFunctions.glGetProgramiv = glGetProgramiv; + g_GLFunctions.glGetProgramInfoLog = glGetProgramInfoLog; + g_GLFunctions.glGetShaderiv = glGetShaderiv; + g_GLFunctions.glGetShaderInfoLog = glGetShaderInfoLog; + g_GLFunctions.glGetUniformLocation = glGetUniformLocation; + g_GLFunctions.glLinkProgram = glLinkProgram; + g_GLFunctions.glShaderSource = glShaderSource; + g_GLFunctions.glUseProgram = glUseProgram; + g_GLFunctions.glUniform1f = glUniform1f; + g_GLFunctions.glUniform1i = glUniform1i; + g_GLFunctions.glUniform3fv = glUniform3fv; + g_GLFunctions.glUniform4fv = glUniform4fv; + g_GLFunctions.glVertexAttribPointer = glVertexAttribPointer; + g_GLFunctions.glUniformMatrix3x4fv = glUniformMatrix3x4fv; + g_GLFunctions.glBindFramebuffer = glBindFramebuffer; + g_GLFunctions.glDeleteFramebuffers = glDeleteFramebuffers; + g_GLFunctions.glGenFramebuffers = glGenFramebuffers; + g_GLFunctions.glCheckFramebufferStatus = glCheckFramebufferStatus; + g_GLFunctions.glGenerateMipmap = glGenerateMipmap; + g_GLFunctions.glFramebufferTexture2D = glFramebufferTexture2D; + g_GLFunctions.glPatchParameteri = glPatchParameteri; + g_GLFunctions.glGenQueries = glGenQueries; + g_GLFunctions.glDeleteQueries = glDeleteQueries; + g_GLFunctions.glQueryCounter = glQueryCounter; + g_GLFunctions.glGetQueryObjectui64v = glGetQueryObjectui64v; + g_GLFunctions.glGetActiveAttrib = glGetActiveAttrib; + g_GLFunctions.glFramebufferTextureLayer = glFramebufferTextureLayer; + g_GLFunctions.glBlitFramebuffer = glBlitFramebuffer; + g_GLFunctions.glTexImage3D = glTexImage3D; + g_GLFunctions.glReadBuffer = glReadBuffer; + g_GLFunctions.glDrawBuffers = glDrawBuffers; + + // initializing WaveWorks + fprintf(stdout, "\nInitializing:\n"); + res = GFSDK_WaveWorks_InitGL2(&g_GLFunctions, NULL, GFSDK_WAVEWORKS_API_GUID); + if(res == gfsdk_waveworks_result_OK) + { + fprintf(stdout, "GFSDK_WaveWorks_InitGL2: OK\n"); + } + else + { + fprintf(stdout, "GFSDK_WaveWorks_InitGL2 ERROR: %i, exiting..\n", res); + return res; + } + + // initializing QuadTree + g_ocean_param_quadtree.min_patch_length = 40.0f; + g_ocean_param_quadtree.upper_grid_coverage = 64.0f; + g_ocean_param_quadtree.mesh_dim = 128; + g_ocean_param_quadtree.sea_level = 0.f; + g_ocean_param_quadtree.auto_root_lod = 10; + g_ocean_param_quadtree.tessellation_lod = 100.0f; + g_ocean_param_quadtree.geomorphing_degree = 1.f; + g_ocean_param_quadtree.enable_CPU_timers = true; + res = g_pOceanSurf->InitQuadTree(g_ocean_param_quadtree); + if(res == gfsdk_waveworks_result_OK) + { + fprintf(stdout, "InitQuadTree: OK\n"); + } + else + { + fprintf(stdout, "InitQuadTree ERROR: %i, exiting..\n", res); + return res; + } + + // checking available detail level + int detail_level = 0; + for(; detail_level != Num_GFSDK_WaveWorks_Simulation_DetailLevels; ++detail_level) { + if(!GFSDK_WaveWorks_Simulation_DetailLevelIsSupported_GL2((GFSDK_WaveWorks_Simulation_DetailLevel)detail_level)) + break; + } + if(0 == detail_level) + return false; + g_max_detail_level = (GFSDK_WaveWorks_Simulation_DetailLevel)(detail_level - 1); + + // initializing simulation + g_ocean_simulation_param.time_scale = 0.5f; + g_ocean_simulation_param.wave_amplitude = 1.0f; + g_ocean_simulation_param.wind_dir.x = 0.8f; + g_ocean_simulation_param.wind_dir.y = 0.6f; + g_ocean_simulation_param.wind_speed = 9.0f; + g_ocean_simulation_param.wind_dependency = 0.98f; + g_ocean_simulation_param.choppy_scale = 1.f; + g_ocean_simulation_param.small_wave_fraction = 0.f; + g_ocean_simulation_param.foam_dissipation_speed = 0.6f; + g_ocean_simulation_param.foam_falloff_speed = 0.985f; + g_ocean_simulation_param.foam_generation_amount = 0.12f; + g_ocean_simulation_param.foam_generation_threshold = 0.37f; + + g_ocean_simulation_settings.fft_period = 1000.0f; + g_ocean_simulation_settings.detail_level = GFSDK_WaveWorks_Simulation_DetailLevel_Normal; + g_ocean_simulation_settings.readback_displacements = false; + g_ocean_simulation_settings.aniso_level = 4; + g_ocean_simulation_settings.CPU_simulation_threading_model = GFSDK_WaveWorks_Simulation_CPU_Threading_Model_2; + g_ocean_simulation_settings.use_Beaufort_scale = true; + g_ocean_simulation_settings.num_GPUs = 1; + + res = GFSDK_WaveWorks_Simulation_CreateGL2(g_ocean_simulation_settings, g_ocean_simulation_param, (void*) 1 /*g_hRC*/, &g_hOceanSimulation); + if(res == gfsdk_waveworks_result_OK) + { + fprintf(stdout, "GFSDK_WaveWorks_Simulation_CreateGL2: OK\n"); + } + else + { + fprintf(stdout, "GFSDK_WaveWorks_Simulation_CreateGL2 ERROR: %i, exiting..\n", res); + return res; + } + + GFSDK_WaveWorks_Simulation_UpdateProperties(g_hOceanSimulation, g_ocean_simulation_settings, g_ocean_simulation_param); + GFSDK_WaveWorks_Quadtree_SetFrustumCullMargin(g_pOceanSurf->m_hOceanQuadTree, GFSDK_WaveWorks_Simulation_GetConservativeMaxDisplacementEstimate(g_hOceanSimulation)); + + fprintf(stdout, "Entering main loop\n"); + + // entering main loop + glutIdleFunc(RenderFrame); + glutDisplayFunc(RenderFrame); + glutReshapeFunc(Reshape); + glutMouseFunc(MouseFunc); + glutMotionFunc(MouseMotionFunc); + glutKeyboardFunc(KeyboardDownFunc); + glutKeyboardUpFunc(KeyboardUpFunc); + + try + { + glutMainLoop(); + } + catch (const char* msg) + { + // do nothing, just exit GLUT main loop + } + + // Cleanup WaveWorks + delete g_pOceanSurf; + GFSDK_WaveWorks_Simulation_Destroy(g_hOceanSimulation); + GFSDK_WaveWorks_ReleaseGL2(); + +} + diff --git a/test/gl2_mac/math_code.cpp b/test/gl2_mac/math_code.cpp new file mode 100644 index 0000000..b744f48 --- /dev/null +++ b/test/gl2_mac/math_code.cpp @@ -0,0 +1,488 @@ +/* + * math_code.cpp + * + * Created on: 21.03.2011 + * Author: ttcheblokov + */ + +#include "math_code.h" + +void cutLowerHalfspace(float result[4][4], float level, float position, float viewmatrix[4][4], float projectionmatrix[4][4]) +{ + float viewprojectionmatrix[4][4],tm[4][4],n[4][4]={1.0f,0.0f,0.0f,0.0f,0.0f,1.0f,0.0f,0.0f,0.0f,0.0f,1.0f,0.0f,0.0f,0.0f,0.0f,1.0f}; + float cplane[4],clip_plane[4]; + + cplane[0]=0.0f; + cplane[1]=0.1f; + cplane[2]=0.0f; + cplane[3]=-level*0.1f;//+0.1f; + + mat4Mat4Mul(viewprojectionmatrix, projectionmatrix, viewmatrix); + mat4Inverse(tm,viewprojectionmatrix); + mat4Vec4Mul(clip_plane,tm,cplane); + + //clip_plane[0]/=cplane[1]; + //clip_plane[1]/=cplane[1]; + //clip_plane[2]/=cplane[1]; + //clip_plane[3]/=cplane[1]; + + clip_plane[3] -= 1.0f; + + if((level<position)) + { + clip_plane[0] *= -1.0f; + clip_plane[1] *= -1.0f; + clip_plane[2] *= -1.0f; + clip_plane[3] *= -1.0f; + } + + n[0][2]=clip_plane[0]; + n[1][2]=clip_plane[1]; + n[2][2]=clip_plane[2]; + n[3][2]=clip_plane[3]; + + mat4Mat4Mul(result,n,projectionmatrix); +} + +void cutUpperHalfspace(float result[4][4], float level, float position, float viewmatrix[4][4], float projectionmatrix[4][4]) +{ + float viewprojectionmatrix[4][4],tm[4][4],n[4][4]={1.0f,0.0f,0.0f,0.0f,0.0f,1.0f,0.0f,0.0f,0.0f,0.0f,1.0f,0.0f,0.0f,0.0f,0.0f,1.0f}; + float cplane[4],clip_plane[4]; + + cplane[0]=0.0f; + cplane[1]=-0.1f; + cplane[2]=0.0f; + cplane[3]=level*0.1f;//-0.1f; + + mat4Mat4Mul(viewprojectionmatrix, projectionmatrix, viewmatrix); + mat4Inverse(tm,viewprojectionmatrix); + mat4Vec4Mul(clip_plane,tm,cplane); + + //clip_plane[0]/=cplane[1]; + //clip_plane[1]/=cplane[1]; + //clip_plane[2]/=cplane[1]; + //clip_plane[3]/=cplane[1]; + + clip_plane[3] -= 1.0f; + + if((level>position)) + { + clip_plane[0] *= -1.0f; + clip_plane[1] *= -1.0f; + clip_plane[2] *= -1.0f; + clip_plane[3] *= -1.0f; + } + + n[0][2]=clip_plane[0]; + n[1][2]=clip_plane[1]; + n[2][2]=clip_plane[2]; + n[3][2]=clip_plane[3]; + + mat4Mat4Mul(result,n,projectionmatrix); +} + +void vec3CrossProductNormalized(float result[3], float a[3], float b[3]) + { + float length; + result[0]=a[1]*b[2]-a[2]*b[1]; + result[1]=a[2]*b[0]-a[0]*b[2]; + result[2]=a[0]*b[1]-a[1]*b[0]; + length=sqrt(result[0]*result[0]+result[1]*result[1]+result[2]*result[2]); + result[0]/=length; + result[1]/=length; + result[2]/=length; + } + + void vec4Sub(float a[4], float b[4], float c[4]) + { + a[0]=b[0]-c[0]; + a[1]=b[1]-c[1]; + a[2]=b[2]-c[2]; + a[3]=b[3]-c[3]; + } + void vec4Add(float a[4], float b[4], float c[4]) + { + a[0]=b[0]+c[0]; + a[1]=b[1]+c[1]; + a[2]=b[2]+c[2]; + a[3]=b[3]+c[3]; + } + float vec4DotProduct(float a[4], float b[4]) + { + return (a[0]*b[0]+a[1]*b[1]+a[2]*b[2]+a[3]*b[3]); + } + + float vec3DotProduct(float a[3], float b[3]) + { + return (a[0]*b[0]+a[1]*b[1]+a[2]*b[2]); + } + + void vec4Normalize(float a[4]) + { + float length; + length=sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]+a[3]*a[3]); + a[0]/=length; + a[1]/=length; + a[2]/=length; + a[3]/=length; + } + void vec3Normalize(float a[4]) + { + float length; + length=sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]); + a[0]/=length; + a[1]/=length; + a[2]/=length; + } + void mat4Add(float result[4][4], float a[4][4], float b[4][4]) + { + int i,j; + for (i=0;i<4;++i) + for (j=0;j<4;++j) + result[i][j]=a[i][j]+b[i][j]; + } + void mat4ConstMul(float result[4][4], float a[4][4], float b) + { + int i,j; + for (i=0;i<4;++i) + for (j=0;j<4;++j) + result[i][j]=a[i][j]*b; + } + void mat4Transpose(float result[4][4], float a[4][4]) + { + int i,j; + for (i=0;i<4;++i) + for (j=0;j<4;++j) + result[i][j]=a[j][i]; + } + void mat4Mat4Mul(float result[4][4], float a[4][4], float b[4][4]) + { + int i,j,k; + float temp; + + for (i=0;i<4;++i) + for (j=0;j<4;++j) + { + temp=0; + for(k=0;k<4;++k) + { + temp+=a[k][j]*b[i][k]; + } + result[i][j]=temp; + } + } + void vec4Mat4Mul(float result[4], float a[4], float b[4][4]) + { + int i,k; + float temp; + + for (i=0;i<4;++i) + { + temp=0; + for(k=0;k<4;++k) + { + temp+=a[k]*b[k][i]; + } + result[i]=temp; + } + } + void mat4Vec4Mul(float result[4], float a[4][4], float b[4]) + { + int i,k; + float temp; + for (i=0;i<4;++i) + { + temp=0; + for(k=0;k<4;++k) + { + temp+=a[i][k]*b[k]; + } + result[i]=temp; + } + } + void mat4CreateIdentity (float result[4][4]) + { + int i; + for (i=0;i<4;i++) + { + result[i][0] = 0; + result[i][1] = 0; + result[i][2] = 0; + result[i][3] = 0; + } + result [0][0] = 1; + result [1][1] = 1; + result [2][2] = 1; + result [3][3] = 1; + } + void mat4CreateScale (float result[4][4], float x, float y, float z) + { + int i; + for (i=0;i<4;i++) + { + result[i][0] = 0; + result[i][1] = 0; + result[i][2] = 0; + result[i][3] = 0; + } + result [0][0] = x; + result [1][1] = y; + result [2][2] = z; + result [3][3] = 1; + } + + void mat4CreateTranslation (float result[4][4], float x, float y, float z) + { + int i; + for (i=0;i<4;i++) + { + result[i][0] = 0; + result[i][1] = 0; + result[i][2] = 0; + result[i][3] = 0; + } + result [3][0] = x; + result [3][1] = y; + result [3][2] = z; + result [0][0] = 1; + result [1][1] = 1; + result [2][2] = 1; + result [3][3] = 1; + + } + + void mat4CreateProjection (float result[4][4], float w, float h, float zn, float zf) + { + for (int i=0;i<4;i++) + { + result[i][0] = 0; + result[i][1] = 0; + result[i][2] = 0; + result[i][3] = 0; + } + result [0][0] = zn/w; + result [1][1] = zn/h; + result [2][2] = -(zf+zn)/(zf-zn); + result [3][2] = -2.0f*zf*zn/(zf-zn); + result [2][3] = -1.0f; + } + + void mat4CreateView (float result[4][4], float eyepoint[3], float lookatpoint[3]) + { + float xaxis[3]; + float yaxis[3]; + float zaxis[3]; + float up[3] = {0.0f,0.0f, 1.0f}; + + zaxis[0] = - lookatpoint[0] + eyepoint[0]; + zaxis[1] = - lookatpoint[1] + eyepoint[1]; + zaxis[2] = - lookatpoint[2] + eyepoint[2]; + + vec3Normalize(zaxis); + vec3CrossProductNormalized(xaxis, up, zaxis); + vec3CrossProductNormalized(yaxis, zaxis, xaxis); + + result[0][0] = xaxis[0]; + result[0][1] = yaxis[0]; + result[0][2] = zaxis[0]; + result[0][3] = 0; + result[1][0] = xaxis[1]; + result[1][1] = yaxis[1]; + result[1][2] = zaxis[1]; + result[1][3] = 0; + result[2][0] = xaxis[2]; + result[2][1] = yaxis[2]; + result[2][2] = zaxis[2]; + result[2][3] = 0; + result[3][0] = -vec3DotProduct(xaxis,eyepoint); + result[3][1] = -vec3DotProduct(yaxis,eyepoint); + result[3][2] = -vec3DotProduct(zaxis,eyepoint); + result[3][3] = 1.0f; + } + + void mat4CreateOrthoProjection (float result[4][4], float xmin, float xmax, float ymin, float ymax, float zmin, float zmax) + { + for(int i=0;i<4;i++) + { + result[i][0]=0; + result[i][1]=0; + result[i][2]=0; + result[i][3]=0; + } + result[0][0] = 2.0f/(xmax-xmin); + result[1][1] = 2.0f/(ymax-ymin); + result[2][2] = -2.0f/(zmax-zmin); + result[3][3] = 1.0f; + result[3][0] = - (xmax + xmin) / (xmax - xmin); + result[3][1] = - (ymax + ymin) / (ymax - ymin); + result[3][2] = - (zmax + zmin) / (zmax - zmin); + + } + + void mat4CreateRotation (float result[4][4], float angle, char axis) + { + int i; + for (i=0;i<4;i++) + { + result[i][0] = 0; + result[i][1] = 0; + result[i][2] = 0; + result[i][3] = 0; + } + result [3][3] = 1; + + switch (axis) + { + case 'x': + result[0][0]=1.0f; + result[0][1]=0.0f; + result[0][2]=0.0f; + result[1][0]=0.0f; + result[1][1]=cos(angle); + result[1][2]=sin(angle); + result[2][0]=0.0f; + result[2][1]=-sin(angle); + result[2][2]=cos(angle); + break; + case 'y': + result[0][0]=cos(angle); + result[0][1]=0.0f; + result[0][2]=-sin(angle); + result[1][0]=0.0f; + result[1][1]=1.0f; + result[1][2]=0.0f; + result[2][0]=sin(angle); + result[2][1]=0.0f; + result[2][2]=cos(angle); + break; + case 'z': + result[0][0]=cos(angle); + result[0][1]=sin(angle); + result[0][2]=0.0f; + result[1][0]=-sin(angle); + result[1][1]=cos(angle); + result[1][2]=0.0f; + result[2][0]=0.0f; + result[2][1]=0.0f; + result[2][2]=1.0f; + break; + } + + } + float sgn (float a) + { + if(a>0.0f) return (1.0f); + if(a<0.0f) return (-1.0f); + return(0.0f); + } + + float det2x2(float a,float b, + float c,float d) + { + return a * d - b * c; + } + + float det3x3(float a1,float a2,float a3, + float b1,float b2,float b3, + float c1,float c2,float c3) + { + float ans; + ans = a1 * det2x2( b2, b3, c2, c3) + - b1 * det2x2( a2, a3, c2, c3) + + c1 * det2x2( a2, a3, b2, b3); + return ans; + } + + float det4x4(float m[4][4]) + { + float a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4,d1,d2,d3,d4; + + + a1 = m[0][0]; b1 = m[0][1]; + c1 = m[0][2]; d1 = m[0][3]; + + a2 = m[1][0]; b2 = m[1][1]; + c2 = m[1][2]; d2 = m[1][3]; + + a3 = m[2][0]; b3 = m[2][1]; + c3 = m[2][2]; d3 = m[2][3]; + + a4 = m[3][0]; b4 = m[3][1]; + c4 = m[3][2]; d4 = m[3][3]; + + return a1 * det3x3( b2, b3, b4, c2, c3, c4, d2, d3, d4) + - b1 * det3x3( a2, a3, a4, c2, c3, c4, d2, d3, d4) + + c1 * det3x3( a2, a3, a4, b2, b3, b4, d2, d3, d4) + - d1 * det3x3( a2, a3, a4, b2, b3, b4, c2, c3, c4); + } + + void adjoint(float adj[4][4],float m[4][4]) + { + float a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4,d1,d2,d3,d4; + + a1 = m[0][0]; b1 = m[0][1]; + c1 = m[0][2]; d1 = m[0][3]; + + a2 = m[1][0]; b2 = m[1][1]; + c2 = m[1][2]; d2 = m[1][3]; + + a3 = m[2][0]; b3 = m[2][1]; + c3 = m[2][2]; d3 = m[2][3]; + + a4 = m[3][0]; b4 = m[3][1]; + c4 = m[3][2]; d4 = m[3][3]; + + adj[0][0] = det3x3( b2, b3, b4, c2, c3, c4, d2, d3, d4); + adj[1][0] = - det3x3( a2, a3, a4, c2, c3, c4, d2, d3, d4); + adj[2][0] = det3x3( a2, a3, a4, b2, b3, b4, d2, d3, d4); + adj[3][0] = - det3x3( a2, a3, a4, b2, b3, b4, c2, c3, c4); + + adj[0][1] = - det3x3( b1, b3, b4, c1, c3, c4, d1, d3, d4); + adj[1][1] = det3x3( a1, a3, a4, c1, c3, c4, d1, d3, d4); + adj[2][1] = - det3x3( a1, a3, a4, b1, b3, b4, d1, d3, d4); + adj[3][1] = det3x3( a1, a3, a4, b1, b3, b4, c1, c3, c4); + + adj[0][2] = det3x3( b1, b2, b4, c1, c2, c4, d1, d2, d4); + adj[1][2] = - det3x3( a1, a2, a4, c1, c2, c4, d1, d2, d4); + adj[2][2] = det3x3( a1, a2, a4, b1, b2, b4, d1, d2, d4); + adj[3][2] = - det3x3( a1, a2, a4, b1, b2, b4, c1, c2, c4); + + adj[0][3] = - det3x3( b1, b2, b3, c1, c2, c3, d1, d2, d3); + adj[1][3] = det3x3( a1, a2, a3, c1, c2, c3, d1, d2, d3); + adj[2][3] = - det3x3( a1, a2, a3, b1, b2, b3, d1, d2, d3); + adj[3][3] = det3x3( a1, a2, a3, b1, b2, b3, c1, c2, c3); + } + + void mat4Inverse(float result[4][4],float source[4][4]) + { + float adj[4][4]; + float det; + int i,j; + + adjoint(adj,source); + + det = det4x4(source); + if (fabs(det) < 1e-8f) + { + return ; + } + else + { + det = 1 / det; + for (i = 0; i < 4; i++) + for (j = 0; j < 4; j++) + result[i][j] = adj[i][j] * det; + } + } + + void mat4Mat4Copy (float result[4][4], float source[4][4]) + { + int i; + for (i=0;i<4;i++) + { + result[i][0] = source[i][0]; + result[i][1] = source[i][1]; + result[i][2] = source[i][2]; + result[i][3] = source[i][3]; + } + } diff --git a/test/gl2_mac/math_code.h b/test/gl2_mac/math_code.h new file mode 100644 index 0000000..e03da47 --- /dev/null +++ b/test/gl2_mac/math_code.h @@ -0,0 +1,49 @@ +/* + * math_code.h + * + * Created on: 21.03.2011 + * Author: ttcheblokov + */ +#include <math.h> + +#ifndef MATH_CODE_H_ +#define MATH_CODE_H_ + +// vector and matrix math functions + void cutLowerHalfspace(float result[4][4], float level, float position, float viewmatrix[4][4], float projectionmatrix[4][4]); + void cutUpperHalfspace(float result[4][4], float level, float position, float viewmatrix[4][4], float projectionmatrix[4][4]); + void vec3CrossProductNormalized(float result[3], float a[3], float b[3]); + void vec4Sub(float a[4], float b[4], float c[4]); + void vec4Add(float a[4], float b[4], float c[4]); + float vec3DotProduct(float a[3], float b[3]); + float vec4DotProduct(float a[4], float b[4]); + void vec4Normalize(float a[4]); + void vec3Normalize(float a[3]); + void mat4Add(float result[4][4], float a[4][4], float b[4][4]); + void mat4ConstMul(float result[4][4], float a[4][4], float b); + void mat4Transpose(float result[4][4], float a[4][4]); + void mat4Mat4Mul(float result[4][4], float a[4][4], float b[4][4]); + void vec4Mat4Mul(float result[4], float a[4], float b[4][4]); + void mat4Vec4Mul(float result[4], float a[4][4], float b[4]); + void mat4CreateIdentity (float result[4][4]); + void mat4CreateScale (float result[4][4], float x, float y, float z); + void mat4CreateTranslation (float result[4][4], float x, float y, float z); + void mat4CreateView(float result[4][4], float eyepoint[3], float lookatpoint[3]); + void mat4CreateProjection (float result[4][4], float w, float h, float zn, float zf); + void mat4CreateOrthoProjection (float result[4][4], float xmin, float xmax, float ymin, float ymax, float zmin, float zmax); + void mat4CreateRotation (float result[4][4], float angle, char axis); + void mat4Inverse(float result[4][4],float source[4][4]); + void mat4Mat4Copy (float result[4][4], float source[4][4]); + + float sgn (float a); + float det2x2(float a,float b, float c,float d); + float det3x3(float a1,float a2,float a3, + float b1,float b2,float b3, + float c1,float c2,float c3); + float det4x4(float m[4][4]); + void adjoint(float adj[4][4],float m[4][4]); + + + + +#endif /* MATH_CODE_H_ */ diff --git a/test/gl2_mac/ocean_surface.cpp b/test/gl2_mac/ocean_surface.cpp new file mode 100644 index 0000000..4ad52c7 --- /dev/null +++ b/test/gl2_mac/ocean_surface.cpp @@ -0,0 +1,874 @@ +// Copyright (c) 2011 NVIDIA Corporation. All rights reserved. +// +// TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED +// *AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS +// OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT,IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL NVIDIA +// OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, SPECIAL, INCIDENTAL, INDIRECT, OR +// CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS +// OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY +// OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, +// EVEN IF NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. +// +// Please direct any bugs or questions to [email protected] + +#define _HAS_EXCEPTIONS 0 +#include "ocean_surface.h" +#include "math_code.h" +#include <stdio.h> +#include <vector> + +#ifndef PI +#define PI 3.14159265358979323846f +#endif + +#ifndef SAFE_DELETE_ARRAY +#define SAFE_DELETE_ARRAY(p) { if (p) { delete[] (p); (p)=NULL; } } +#endif + +enum OceanSurfaceTextureUnits +{ + FoamDiffuseTextureUnit = 0, + FoamIntensityTextureUnit, + Num_OceanSurfaceTextureUnits +}; + +bool file_exists(const char * filename) +{ + FILE *file; + file = fopen(filename, "r"); + if (file) + { + fclose(file); + return true; + } + return false; +} + +void search_file(char* found_filename, size_t numberOfElements, char* filename) +{ + char* search_paths[12] = { + "", + "test/gl2_mac/", + "../", + "../test/gl2_mac/", + "../../", + "../../test/gl2_mac/", + "../../../", + "../../../test/gl2_mac/", + "../../../../", + "../../../../test/gl2_mac/", + "../../../../../", + "../../../../../test/gl2_mac/"}; + + char trial_filename[1024]; + for(int i=0; i<12; i++) + { + snprintf(trial_filename, sizeof(trial_filename)/sizeof(trial_filename[0]), "%s%s", search_paths[i], filename); + if(file_exists(trial_filename)) + { + strncpy(found_filename, trial_filename, numberOfElements); + return; + } + } + strncpy(found_filename, filename, numberOfElements); +} + +gfsdk_waveworks_result OceanSurface::InitQuadTree(const GFSDK_WaveWorks_Quadtree_Params& params) +{ + if(NULL == m_hOceanQuadTree) + { + return GFSDK_WaveWorks_Quadtree_CreateGL2(params, (unsigned int) WaterProgram, &m_hOceanQuadTree); + } + else + { + return GFSDK_WaveWorks_Quadtree_UpdateParams(m_hOceanQuadTree, params); + } +} + +void OceanSurface::CreateTextures(void) +{ + LoadTexture("foam.dds",&FoamDiffuseTextureID); + LoadTexture("foam_intensity_perlin2_rgb.dds",&FoamIntensityTextureID); +} + +OceanSurface::OceanSurface(bool use_texture_arrays) +{ + + // creating and binding global VAO + glGenVertexArrays(1,&GlobalVAO); + glBindVertexArray(GlobalVAO); + + // loading & compiling water shader + WaterProgram = LoadProgram("water.glsl"); + + // initializing quadtree + m_hOceanQuadTree = NULL; + + // get the attribute count, so that we can search for any attrib shader inputs needed by waveworks + GLint numAttrs = 0; + glGetProgramiv(WaterProgram,GL_ACTIVE_ATTRIBUTES,&numAttrs); + + // initializing quadtree shader input mappings + UINT NumQuadtreeShaderInputs = GFSDK_WaveWorks_Quadtree_GetShaderInputCountGL2(); + m_pQuadTreeShaderInputMappings = new gfsdk_U32 [NumQuadtreeShaderInputs]; + GFSDK_WaveWorks_ShaderInput_Desc* quadtree_descs = new GFSDK_WaveWorks_ShaderInput_Desc[NumQuadtreeShaderInputs]; + for(unsigned int i = 0; i < NumQuadtreeShaderInputs; i++) + { + GFSDK_WaveWorks_Quadtree_GetShaderInputDescGL2(i, &(quadtree_descs[i])); + switch(quadtree_descs[i].Type) + { + case GFSDK_WaveWorks_ShaderInput_Desc::GL_FragmentShader_UniformLocation: + case GFSDK_WaveWorks_ShaderInput_Desc::GL_VertexShader_UniformLocation: + case GFSDK_WaveWorks_ShaderInput_Desc::GL_TessEvalShader_UniformLocation: + m_pQuadTreeShaderInputMappings[i] = glGetUniformLocation(WaterProgram, quadtree_descs[i].Name); + break; + case GFSDK_WaveWorks_ShaderInput_Desc::GL_AttribLocation: + m_pQuadTreeShaderInputMappings[i] = (UINT)GFSDK_WaveWorks_UnusedShaderInputRegisterMapping; + for(GLint attrIndex = 0; attrIndex != numAttrs; ++attrIndex) + { + char attribName[256]; + GLint size; + GLenum type; + GLsizei nameLen; + glGetActiveAttrib(WaterProgram,attrIndex,sizeof(attribName),&nameLen,&size,&type,attribName); + if(GFSDK_WaveWorks_GLAttribIsShaderInput(attribName,quadtree_descs[i])) + { + m_pQuadTreeShaderInputMappings[i] = glGetAttribLocation(WaterProgram, attribName); + } + } + + break; + } + } + + // initializing simulation shader input mappings + UINT NumSimulationShaderInputs = GFSDK_WaveWorks_Simulation_GetShaderInputCountGL2(); + m_pSimulationShaderInputMappings = new gfsdk_U32 [NumSimulationShaderInputs]; + GFSDK_WaveWorks_ShaderInput_Desc* simulation_descs = new GFSDK_WaveWorks_ShaderInput_Desc[NumSimulationShaderInputs]; + + for(unsigned int i = 0; i < NumSimulationShaderInputs; i++) + { + GFSDK_WaveWorks_Simulation_GetShaderInputDescGL2(i, &(simulation_descs[i])); + m_pSimulationShaderInputMappings[i] = glGetUniformLocation(WaterProgram, simulation_descs[i].Name); + } + + // reserve texture units for WaveWorks for use during ocean surface rendering + for(unsigned int i = 0; i != GFSDK_WaveWorks_Simulation_GetTextureUnitCountGL2(use_texture_arrays); ++i) + { + glPool.Reserved_Texture_Units[i] = (Num_OceanSurfaceTextureUnits + i) %8; + } + + // creating textures + CreateTextures(); + + // binding textures to shader + FoamDiffuseTextureBindLocation = glGetUniformLocation(WaterProgram, "g_texFoamDiffuseMap"); + FoamIntensityTextureBindLocation = glGetUniformLocation(WaterProgram, "g_texFoamIntensityMap"); + + // initializing the rest of variables + CameraPosition[0] = -20.0f; + CameraPosition[1] = 100.0f; + CameraPosition[2] = 20.0f; + LookAtPosition[0] = 0.0f; + LookAtPosition[1] = 0.0f; + LookAtPosition[2] = 0.0f; + + Alpha = 0; + Beta = 0; + + ScreenWidth = 1280; + ScreenHeight = 720; + total_time = 0; + frame_number = 0; + + // cleanup + SAFE_DELETE_ARRAY(quadtree_descs); + SAFE_DELETE_ARRAY(simulation_descs); +} + + + +OceanSurface::~OceanSurface() +{ + if(m_hOceanQuadTree) + { + GFSDK_WaveWorks_Quadtree_Destroy(m_hOceanQuadTree); + } + + SAFE_DELETE_ARRAY(m_pQuadTreeShaderInputMappings); + SAFE_DELETE_ARRAY(m_pSimulationShaderInputMappings); +} + +void checkError(const char *functionName) +{ + GLenum error; + while (( error = glGetError() ) != GL_NO_ERROR) { + fprintf (stderr, "\nGL error 0x%X detected in %s", error, functionName); + } +} + +void OceanSurface::Render(GFSDK_WaveWorks_SimulationHandle hSim, GFSDK_WaveWorks_Simulation_Settings settings) +{ + // rendering to main buffer + SetupNormalView(); + + // setting up main buffer & GL state + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glClearColor(0.38f, 0.45f, 0.56f, 0.0f); + glClearDepthf(1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glViewport(0,0,ScreenWidth,ScreenHeight); + glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); + glDepthFunc(GL_LESS); + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + + // rendering water to main buffer + // setting up program + glUseProgram(WaterProgram); + + glUniformMatrix4fv(glGetUniformLocation(WaterProgram, "u_ViewProjMatrix"), 1, GL_FALSE, (const GLfloat *) NormalViewProjMatrix); + + //fprintf (stderr, "\n\nGFSDK_WaveWorks_Quadtree_GetShaderInputCountGL2: %i",nm); + gfsdk_float4x4 ViewMatrix; + gfsdk_float4x4 ProjMatrix; + ViewMatrix._11 = NormalViewMatrix[0][0]; + ViewMatrix._12 = NormalViewMatrix[0][1]; + ViewMatrix._13 = NormalViewMatrix[0][2]; + ViewMatrix._14 = NormalViewMatrix[0][3]; + ViewMatrix._21 = NormalViewMatrix[1][0]; + ViewMatrix._22 = NormalViewMatrix[1][1]; + ViewMatrix._23 = NormalViewMatrix[1][2]; + ViewMatrix._24 = NormalViewMatrix[1][3]; + ViewMatrix._31 = NormalViewMatrix[2][0]; + ViewMatrix._32 = NormalViewMatrix[2][1]; + ViewMatrix._33 = NormalViewMatrix[2][2]; + ViewMatrix._34 = NormalViewMatrix[2][3]; + ViewMatrix._41 = NormalViewMatrix[3][0]; + ViewMatrix._42 = NormalViewMatrix[3][1]; + ViewMatrix._43 = NormalViewMatrix[3][2]; + ViewMatrix._44 = NormalViewMatrix[3][3]; + + ProjMatrix._11 = NormalProjMatrix[0][0]; + ProjMatrix._12 = NormalProjMatrix[0][1]; + ProjMatrix._13 = NormalProjMatrix[0][2]; + ProjMatrix._14 = NormalProjMatrix[0][3]; + ProjMatrix._21 = NormalProjMatrix[1][0]; + ProjMatrix._22 = NormalProjMatrix[1][1]; + ProjMatrix._23 = NormalProjMatrix[1][2]; + ProjMatrix._24 = NormalProjMatrix[1][3]; + ProjMatrix._31 = NormalProjMatrix[2][0]; + ProjMatrix._32 = NormalProjMatrix[2][1]; + ProjMatrix._33 = NormalProjMatrix[2][2]; + ProjMatrix._34 = NormalProjMatrix[2][3]; + ProjMatrix._41 = NormalProjMatrix[3][0]; + ProjMatrix._42 = NormalProjMatrix[3][1]; + ProjMatrix._43 = NormalProjMatrix[3][2]; + ProjMatrix._44 = NormalProjMatrix[3][3]; + + // binding user textures + glActiveTexture(GL_TEXTURE0 + FoamDiffuseTextureUnit); + glBindTexture(GL_TEXTURE_2D, FoamDiffuseTextureID); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, settings.aniso_level); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT); + glUniform1i(FoamDiffuseTextureBindLocation, FoamDiffuseTextureUnit); + + glActiveTexture(GL_TEXTURE0 + FoamIntensityTextureUnit); + glBindTexture(GL_TEXTURE_2D, FoamIntensityTextureID); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, settings.aniso_level); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT); + glUniform1i(FoamIntensityTextureBindLocation, FoamIntensityTextureUnit); + + GFSDK_WaveWorks_Simulation_SetRenderStateGL2(hSim, ViewMatrix, m_pSimulationShaderInputMappings, glPool); + GFSDK_WaveWorks_Quadtree_DrawGL2(m_hOceanQuadTree, ViewMatrix, ProjMatrix, m_pQuadTreeShaderInputMappings); +} + +void OceanSurface::SetupNormalView() +{ + float scene_z_near = 1.0f; + float scene_z_far = 10000.0f; + float camera_fov = 3.14f*0.5f; + mat4CreateView(NormalViewMatrix, CameraPosition, LookAtPosition); + mat4CreateProjection(NormalProjMatrix,scene_z_near,scene_z_near/tan(camera_fov*0.5f)*(float)ScreenHeight/(float)ScreenWidth,scene_z_near/tan(camera_fov*0.5f),scene_z_far); + mat4Mat4Mul(NormalViewProjMatrix,NormalProjMatrix,NormalViewMatrix); +} + + +GLuint OceanSurface::CompileShader(char *text, GLenum type) //returns shader handle or 0 if error +{ + GLint length, compiled; + + GLuint shader; + GLsizei maxlength=65535; + char infolog[65535]; + shader = glCreateShader(type); + glShaderSource(shader, 1, (const GLchar **)&text, NULL); + glCompileShader(shader); + glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); + if (!compiled) { + fprintf(stderr,"\nshader [%i] compilation error",type); + fprintf(stderr,"\n...\n%s\n...\n",text); + glGetShaderInfoLog(shader, maxlength, &length, infolog); + fprintf(stderr,"\ninfolog: %s",infolog); + glDeleteShader(shader); + return 0; + } + return shader; +} + +const char* GetIncludeFilename(char* line) +{ + char* curr = line; + + // Simple little state machine to parse a #include line... + + // Leading wspace + while(*curr && isspace(*curr)) + ++curr; + + // Check for #include... + const char* includeTag = "#include"; + if(curr != strstr(curr,includeTag)) + return NULL; + curr += strlen(includeTag); + + // Consume more wspace + while(*curr && isspace(*curr)) + ++curr; + + // Check for leading quote + if(*curr != '\"') + return NULL; + curr ++; + const char* pResult = curr; + + // Advance to closing quote + while(*curr && *curr != '\"') + ++curr; + + if(*curr != '\"') + return NULL; + + // Replace with NULL term + *curr = '\0'; + + return pResult; +} +void splitToDirAndFile(const char* filename, char* dir, char* name) +{ + char* pforward_slash = strrchr(filename, '/'); + char* pback_slash = strrchr(filename, '\\'); + char* pslash = pforward_slash > pback_slash ? pforward_slash : pback_slash; + if (pslash == NULL) + { + dir[0]= '\0'; + strcpy(name, filename); + } + else + { + strncpy(dir, filename, (pslash - filename)/sizeof(filename[0])); + strcat(dir, "/"); + strcpy(name, &pslash[1]); + } +} +int LoadProgramSource(const char* filename, std::vector<char>& loadedSrc) +{ + FILE *f = fopen(filename,"rt"); + if(f==NULL) + { + fprintf(stderr,"\ncould not open [%s]\n",filename); + return -1; + } + + char lineBuffer[1024]; + while(!feof(f)) + { + char* currLine = fgets(lineBuffer,sizeof(lineBuffer),f); + if(currLine) + { + // GLSL does not support #include + // We process #includes here for simplicity, but most real-world apps will want to do this pre-process step offline + const char* includeFileName = GetIncludeFilename(currLine); + if(includeFileName) + { + // Assume relative to curr file + char dir[65536]; + char name[65536]; + splitToDirAndFile(filename, dir, name); + char includePath[65536]; + const size_t includePathSize = sizeof(includePath)/sizeof(includePath[0]); + char* includePathCurr = includePath; + strncpy(includePathCurr, dir, includePathSize); + includePathCurr += strlen(dir); + strncpy(includePathCurr, includeFileName, includePathSize); + includePathCurr += strlen(includeFileName); + if(LoadProgramSource(includePath,loadedSrc)) + return -1; + } + else + { + loadedSrc.insert(loadedSrc.end(),currLine,currLine+strlen(currLine)); + } + } + } + fclose(f); + return 0; +} + +GLuint OceanSurface::LoadProgram(char* filename) //returns program object handle or 0 if error +{ + char tmp_text[65535]; + const char *tmp_pos_start; + const char *tmp_pos_end; + + GLuint result = 0; + GLenum program; + + GLint compiled; + + char trial_filename[1024]; + search_file(trial_filename, sizeof(trial_filename)/sizeof(trial_filename[0]), filename); + + fprintf(stderr,"\nLoading & compiling shader [%s]: ",trial_filename); + + std::vector<char> loadedSrc; + if(LoadProgramSource(trial_filename,loadedSrc)) + { + return 0; + } + loadedSrc.push_back('\0'); + const char* programSrc = &loadedSrc[0]; + GLint length = (GLint)loadedSrc.size(); + + program = glCreateProgram(); + + // looking for vertex shader + tmp_pos_start = strstr(programSrc,"<<<VSTEXT>>>"); + if(tmp_pos_start!=NULL) + { + tmp_pos_start+=12; + tmp_pos_end = strstr(tmp_pos_start,"<<<"); + if (tmp_pos_end==NULL) tmp_pos_end = programSrc + length; + strncpy(tmp_text,tmp_pos_start,tmp_pos_end-tmp_pos_start); + tmp_text[tmp_pos_end-tmp_pos_start]=0; + result = CompileShader(tmp_text,GL_VERTEX_SHADER); + if(result) + { + glAttachShader(program,result); + glDeleteShader(result); + } + } + + // looking for tessellation control shader + tmp_pos_start = strstr(programSrc,"<<<TCTEXT>>>"); + if(tmp_pos_start!=NULL) + { + tmp_pos_start+=12; + tmp_pos_end = strstr(tmp_pos_start,"<<<"); + if (tmp_pos_end==NULL) tmp_pos_end = programSrc + length; + strncpy(tmp_text,tmp_pos_start,tmp_pos_end-tmp_pos_start); + tmp_text[tmp_pos_end-tmp_pos_start]=0; + result = CompileShader(tmp_text,GL_TESS_CONTROL_SHADER); + if(result) + { + glAttachShader(program,result); + glDeleteShader(result); + } + } + + // looking for tessellation evaluation shader + tmp_pos_start = strstr(programSrc,"<<<TETEXT>>>"); + if(tmp_pos_start!=NULL) + { + tmp_pos_start+=12; + tmp_pos_end = strstr(tmp_pos_start,"<<<"); + if (tmp_pos_end==NULL) tmp_pos_end = programSrc + length; + strncpy(tmp_text,tmp_pos_start,tmp_pos_end-tmp_pos_start); + tmp_text[tmp_pos_end-tmp_pos_start]=0; + result = CompileShader(tmp_text,GL_TESS_EVALUATION_SHADER); + if(result) + { + glAttachShader(program,result); + glDeleteShader(result); + } + } + + // looking for geometry shader + tmp_pos_start = strstr(programSrc,"<<<GSTEXT>>>"); + if(tmp_pos_start!=NULL) + { + tmp_pos_start+=12; + tmp_pos_end = strstr(tmp_pos_start,"<<<"); + if (tmp_pos_end==NULL) tmp_pos_end = programSrc + length; + strncpy(tmp_text,tmp_pos_start,tmp_pos_end-tmp_pos_start); + tmp_text[tmp_pos_end-tmp_pos_start]=0; + result = CompileShader(tmp_text,GL_GEOMETRY_SHADER); + if(result) + { + glAttachShader(program,result); + glDeleteShader(result); + } + } + + // looking for fragment shader + tmp_pos_start = strstr(programSrc,"<<<FSTEXT>>>"); + if(tmp_pos_start!=NULL) + { + tmp_pos_start+=12; + tmp_pos_end = strstr(tmp_pos_start,"<<<"); + if (tmp_pos_end==NULL) tmp_pos_end = programSrc + length; + strncpy(tmp_text,tmp_pos_start,tmp_pos_end-tmp_pos_start); + tmp_text[tmp_pos_end-tmp_pos_start]=0; + result = CompileShader(tmp_text,GL_FRAGMENT_SHADER); + if(result) + { + glAttachShader(program,result); + glDeleteShader(result); + } + } + + glLinkProgram(program); + + glGetProgramiv(program, GL_LINK_STATUS, &compiled); + if (!compiled) { + const GLsizei maxlength=65535; + char infolog[maxlength]; + fprintf(stderr,"program link error\n"); + glGetProgramInfoLog(program, maxlength, &length, infolog); + fprintf(stderr,"\ninfolog: %s\n",infolog); + return 0; + } + return program; +} + +gliGenericImage* ReadDDSFile(const char *filename, int *bufsize, int *numMipmaps) +{ + gliGenericImage *genericImage; + DDSURFACEDESC2 ddsd; + char filecode[4]; + long factor,uncompressed_components,format_found; + /* try to open the file */ + FILE *fp = fopen(filename, "rb"); + if (fp == NULL) + { + fprintf(stderr,"\nUnable to load texture file [%s]",filename); + return NULL; + } + /* verify the type of file */ + fread(filecode, 1, 4, fp); + if (strncmp(filecode, "DDS ", 4) != 0) + { + fprintf(stderr,"\nThe file [%s] is not a dds file",filename); + fclose(fp); + return NULL; + } + /* get the surface desc */ + fread(&ddsd, sizeof(ddsd), 1, fp); + if((ddsd.dwFlags & DDSD_PIXELFORMAT) != DDSD_PIXELFORMAT) + { + fprintf(stderr,"\nDDS header pixelformat field contains no valid data,\nunable to load texture."); + return 0; + } + if(((ddsd.dwFlags & DDSD_WIDTH) != DDSD_WIDTH) || ((ddsd.dwFlags & DDSD_HEIGHT) != DDSD_HEIGHT)) + { + fprintf(stderr,"\nDDS header width/height fields contains no valid data,\nunable to load texture."); + return 0; + } + if((ddsd.dwFlags & DDSD_MIPMAPCOUNT) != DDSD_MIPMAPCOUNT) + { + fprintf(stderr,"\nDDS header mipmapcount field contains no valid data,\nassuming 0 mipmaps."); + //return 0; + } + if(((ddsd.ddpfPixelFormat.dwFlags & DDPF_FOURCC) != DDPF_FOURCC) && ((ddsd.ddpfPixelFormat.dwFlags & DDPF_RGB) != DDPF_RGB)) + { + fprintf(stderr,"\nDDS header pixelformat field contains no valid FOURCC and RGB data,\nunable to load texture."); + return 0; + } + if(((ddsd.ddpfPixelFormat.dwFlags & DDPF_FOURCC) == DDPF_FOURCC) && (ddsd.dwMipMapCount<=1) && ((ddsd.ddpfPixelFormat.dwFourCC==FOURCC_DXT1)||(ddsd.ddpfPixelFormat.dwFourCC==FOURCC_DXT3)||(ddsd.ddpfPixelFormat.dwFourCC==FOURCC_DXT5)) ) + { + fprintf(stderr,"\nDDS header contains DXTx FOURCC code and no mipmaps,\nprogram does not support loading DXTx textures without mipmaps,\nunable to load texture."); + return 0; + } + genericImage = (gliGenericImage*) malloc(sizeof(gliGenericImage)); + memset(genericImage,0,sizeof(gliGenericImage)); + factor=0; + uncompressed_components=0; + format_found=0; + genericImage->width = ddsd.dwWidth; + genericImage->height = ddsd.dwHeight; + if((ddsd.ddpfPixelFormat.dwFlags & DDPF_FOURCC) == DDPF_FOURCC) // if FOURCC code is valid + { + switch(ddsd.ddpfPixelFormat.dwFourCC) + { + case FOURCC_DXT1: + if(ddsd.dwAlphaBitDepth!=0) + { + genericImage->format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; + genericImage->components = 4; + } + else + { + genericImage->format = GL_COMPRESSED_RGB_S3TC_DXT1_EXT; + genericImage->components = 3; + } + factor = 2; + format_found=1; + break; + case FOURCC_DXT3: + genericImage->format = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; + genericImage->components = 4; + factor = 4; + format_found=1; + break; + case FOURCC_DXT5: + genericImage->format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; + genericImage->components = 4; + factor = 4; + format_found=1; + break; + default: + break; + } + } + if(format_found==0) + { + switch (ddsd.ddpfPixelFormat.dwRGBBitCount) + { + case 24: + genericImage->format = GL_RGB; + genericImage->components = 3; + format_found=2; + break; + case 32: + genericImage->format = GL_RGBA; + genericImage->components = 4; + format_found=2; + default: + break; + } + } + if(format_found==1) // found compressed format + { + *bufsize = ddsd.dwMipMapCount > 1 ? ddsd.dwLinearSize * factor : ddsd.dwLinearSize; + } + if(format_found==2) + { + *bufsize = ddsd.dwMipMapCount > 1 ? ddsd.dwWidth*ddsd.dwHeight*genericImage->components+(ddsd.dwWidth*ddsd.dwHeight*genericImage->components)/2 : ddsd.dwWidth*ddsd.dwHeight*genericImage->components; + } + if(format_found==0) + { + fprintf(stderr,"\nUnsupported DDS format,\nthe program only supports DXTx and RGB/RGBA formats containing 8 bits per component"); + free(genericImage); + return 0; + } + genericImage->pixels = (unsigned char*)malloc(*bufsize); + fread(genericImage->pixels, 1, *bufsize, fp); + fclose(fp); + *numMipmaps = ddsd.dwMipMapCount; + genericImage->numMipmaps=ddsd.dwMipMapCount; + return genericImage; +} + + +int OceanSurface::LoadTexture(char * filename, GLuint * texID) +{ + gliGenericImage *ggi = NULL; + int bufsize,numMipmaps,blocksize,i; + long offset; + GLint size,width,height; + unsigned char c1,c2,c3,c4; + long n; + + char trial_filename[1024]; + search_file(trial_filename, sizeof(trial_filename)/sizeof(trial_filename[0]), filename); + + fprintf(stderr,"\nLoading %s: ", trial_filename); + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + ggi = ReadDDSFile(trial_filename, &bufsize, &numMipmaps); + if(ggi==NULL) + { + fprintf(stderr,"The file [%s] hasn't been loaded successfully.",trial_filename); + return 0; + } + height = ggi->height; + width = ggi->width; + switch (ggi->format) + { + case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: + glGenTextures(1,texID); + glBindTexture(GL_TEXTURE_2D, *texID); + blocksize=8; + offset = 0; + height = ggi->height; + width = ggi->width; + for (i = 0; i < numMipmaps && (width || height); ++i) + { + if (width == 0) + width = 1; + if (height == 0) + height = 1; + size = ((width+3)/4)*((height+3)/4)*blocksize; + glCompressedTexImage2D(GL_TEXTURE_2D, i, ggi->format, width, height, 0, size, ggi->pixels+offset); + offset += size; + width >>= 1; + height >>= 1; + } + break; + case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: + glGenTextures(1,texID); + glBindTexture(GL_TEXTURE_2D, *texID); + blocksize=8; + offset = 0; + height = ggi->height; + width = ggi->width; + for (i = 0; i < numMipmaps && (width || height); ++i) + { + if (width == 0) + width = 1; + if (height == 0) + height = 1; + size = ((width+3)/4)*((height+3)/4)*blocksize; + glCompressedTexImage2D(GL_TEXTURE_2D, i, ggi->format, width, height, 0, size, ggi->pixels + offset); + offset += size; + width >>= 1; + height >>= 1; + } + break; + case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT: + glGenTextures(1,texID); + glBindTexture(GL_TEXTURE_2D, *texID); + blocksize=16; + offset = 0; + height = ggi->height; + width = ggi->width; + for (i = 0; i < numMipmaps && (width || height); ++i) + { + if (width == 0) + width = 1; + if (height == 0) + height = 1; + size = ((width+3)/4)*((height+3)/4)*blocksize; + glCompressedTexImage2D(GL_TEXTURE_2D, i, ggi->format, width, height, 0, size, ggi->pixels + offset); + offset += size; + width >>= 1; + height >>= 1; + } + break; + case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT: + glGenTextures(1,texID); + glBindTexture(GL_TEXTURE_2D, *texID); + blocksize=16; + offset = 0; + height = ggi->height; + width = ggi->width; + for (i = 0; i < numMipmaps && (width || height); ++i) + { + if (width == 0) + width = 1; + if (height == 0) + height = 1; + size = ((width+3)/4)*((height+3)/4)*blocksize; + glCompressedTexImage2D(GL_TEXTURE_2D, i, ggi->format, width, height, + 0, size, ggi->pixels + offset); + offset += size; + width >>= 1; + height >>= 1; + } + break; + case GL_RGB: + glGenTextures(1,texID); + glBindTexture(GL_TEXTURE_2D, *texID); + offset = 0; + height = ggi->height; + width = ggi->width; + if(numMipmaps<=1) + { + for(n=0;n<width*height*3;n+=3) + { + c1=*(ggi->pixels+n); // switching R and B + c3=*(ggi->pixels+n+2); + *(ggi->pixels+n)=c3; + *(ggi->pixels+n+2)=c1; + } + glTexImage2D(GL_TEXTURE_2D, 0, ggi->format, width, height,0, ggi->format, GL_UNSIGNED_BYTE, ggi->pixels); + glGenerateMipmap(GL_TEXTURE_2D); + }else + for (i = 0; i < numMipmaps && (width || height); ++i) + { + if (width == 0) + width = 1; + if (height == 0) + height = 1; + size = width*height*3; + for(n=0;n<size;n+=3) + { + c1=*(ggi->pixels+offset+n); // switching R and B + c3=*(ggi->pixels+offset+n+2); + *(ggi->pixels+offset+n)=c3; + *(ggi->pixels+offset+n+2)=c1; + } + glTexImage2D(GL_TEXTURE_2D, i, ggi->format, width, height,0, ggi->format, GL_UNSIGNED_BYTE, ggi->pixels + offset); + offset += size; + width >>= 1; + height >>= 1; + } + break; + case GL_RGBA: + glGenTextures(1,texID); + glBindTexture(GL_TEXTURE_2D, *texID); + offset = 0; + height = ggi->height; + width = ggi->width; + if(numMipmaps<=1) + { + for(n=0;n<width*height*4;n+=4) + { + c1=*(ggi->pixels+n); // switching BGRA to RGBA + c2=*(ggi->pixels+n+1); + c3=*(ggi->pixels+n+2); + c4=*(ggi->pixels+n+3); + *(ggi->pixels+n)=c3; + *(ggi->pixels+n+1)=c2; + *(ggi->pixels+n+2)=c1; + *(ggi->pixels+n+3)=c4; + } + glTexImage2D(GL_TEXTURE_2D, 0, ggi->format, width, height,0, ggi->format, GL_UNSIGNED_BYTE, ggi->pixels); + glGenerateMipmap(GL_TEXTURE_2D); + }else + for (i = 0; i < numMipmaps && (width || height); ++i) + { + if (width == 0) + width = 1; + if (height == 0) + height = 1; + size = width*height*4; + for(n=0;n<size;n+=4) + { + c1=*(ggi->pixels+offset+n); // switching BGRA to RGBA + c2=*(ggi->pixels+offset+n+1); + c3=*(ggi->pixels+offset+n+2); + c4=*(ggi->pixels+offset+n+3); + *(ggi->pixels+offset+n)=c3; + *(ggi->pixels+offset+n+1)=c2; + *(ggi->pixels+offset+n+2)=c1; + *(ggi->pixels+offset+n+3)=c4; + } + glTexImage2D(GL_TEXTURE_2D, i, ggi->format, width, height,0, ggi->format, GL_UNSIGNED_BYTE, ggi->pixels + offset); + offset += size; + width >>= 1; + height >>= 1; + } + break; + } + free(ggi->pixels); + free(ggi); + fprintf(stderr," Loaded\n"); + return 1; +} diff --git a/test/gl2_mac/ocean_surface.cpp.bak b/test/gl2_mac/ocean_surface.cpp.bak Binary files differnew file mode 100644 index 0000000..f3b479e --- /dev/null +++ b/test/gl2_mac/ocean_surface.cpp.bak diff --git a/test/gl2_mac/ocean_surface.h b/test/gl2_mac/ocean_surface.h new file mode 100644 index 0000000..1da8338 --- /dev/null +++ b/test/gl2_mac/ocean_surface.h @@ -0,0 +1,295 @@ +// Copyright (c) 2011 NVIDIA Corporation. All rights reserved. +// +// TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED +// *AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS +// OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT,IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL NVIDIA +// OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, SPECIAL, INCIDENTAL, INDIRECT, OR +// CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS +// OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY +// OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, +// EVEN IF NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. +// +// Please direct any bugs or questions to [email protected] + +#include <GFSDK_WaveWorks.h> +#include <unistd.h> +#include <stdio.h> +#include <OPENGL/gl3.h> +#include <GLUT/glut.h> +#include <sys/time.h> + +// types for GL functoins not defined in WaveWorks +typedef void (APIENTRYP PFNGLCLEARDEPTHFPROC) (GLfloat d); +typedef void (APIENTRYP PFNGLUNIFORMMATRIX4FVPROC) (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value); +typedef void (APIENTRYP PFNGLCOMPRESSEDTEXIMAGE2DPROC) (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const GLvoid *data); + +// GL constants +#define GL_FRAMEBUFFER 0x8D40 +#define GL_TEXTURE0 0x84C0 +#define GL_COLOR_ATTACHMENT0 0x8CE0 +#define GL_FRAMEBUFFER_COMPLETE 0x8CD5 +#define GL_COMPILE_STATUS 0x8B81 +#define GL_LINK_STATUS 0x8B82 +#define GL_FRAGMENT_SHADER 0x8B30 +#define GL_VERTEX_SHADER 0x8B31 +#define GL_TESS_EVALUATION_SHADER 0x8E87 +#define GL_TESS_CONTROL_SHADER 0x8E88 +#define GL_GEOMETRY_SHADER 0x8DD9 +#define GL_ARRAY_BUFFER 0x8892 +#define GL_ELEMENT_ARRAY_BUFFER 0x8893 +#define GL_STATIC_DRAW 0x88E4 +#define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1 +#define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2 +#define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3 +#define GL_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0 +#define GL_SHADING_LANGUAGE_VERSION 0x8B8C +#define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE +#define GL_ACTIVE_ATTRIBUTES 0x8B89 + +#ifdef __APPLE__ +#define DWORD uint32_t +#define WORD uint16_t +#define LONG int32_t +#define UINT uint32_t +#define HRESULT uint32_t +#define BYTE uint8_t +#endif + +// DDS file reading related structures +typedef struct +{ + GLint width; + GLint height; + GLint components; + GLenum format; + GLint numMipmaps; + GLubyte *pixels; +} gliGenericImage; + +#ifndef _DDSURFACEDESC2 + // following structs & defines are copied from ddraw.h + + #ifndef DUMMYUNIONNAMEN + #if defined(__cplusplus) || !defined(NONAMELESSUNION) + #define DUMMYUNIONNAMEN(n) + #else + #define DUMMYUNIONNAMEN(n) u##n + #endif + #endif + + #ifndef MAKEFOURCC + #define MAKEFOURCC(ch0, ch1, ch2, ch3) \ + ((DWORD)(BYTE)(ch0) | ((DWORD)(BYTE)(ch1) << 8) | \ + ((DWORD)(BYTE)(ch2) << 16) | ((DWORD)(BYTE)(ch3) << 24 )) + #endif //defined(MAKEFOURCC) + + #define DDSD_PIXELFORMAT 0x00001000l + #define DDSD_CAPS 0x00000001l + #define DDSD_HEIGHT 0x00000002l + #define DDSD_WIDTH 0x00000004l + #define DDSD_PITCH 0x00000008l + #define DDSD_MIPMAPCOUNT 0x00020000l + #define DDPF_FOURCC 0x00000004l + #define DDPF_RGB 0x00000040l + #define FOURCC_DXT1 (MAKEFOURCC('D','X','T','1')) + #define FOURCC_DXT2 (MAKEFOURCC('D','X','T','2')) + #define FOURCC_DXT3 (MAKEFOURCC('D','X','T','3')) + #define FOURCC_DXT4 (MAKEFOURCC('D','X','T','4')) + #define FOURCC_DXT5 (MAKEFOURCC('D','X','T','5')) + + typedef struct _DDCOLORKEY + { + DWORD dwColorSpaceLowValue; // low boundary of color space that is to + // be treated as Color Key, inclusive + DWORD dwColorSpaceHighValue; // high boundary of color space that is + // to be treated as Color Key, inclusive + } DDCOLORKEY; + + typedef struct _DDPIXELFORMAT + { + DWORD dwSize; // size of structure + DWORD dwFlags; // pixel format flags + DWORD dwFourCC; // (FOURCC code) + union + { + DWORD dwRGBBitCount; // how many bits per pixel + DWORD dwYUVBitCount; // how many bits per pixel + DWORD dwZBufferBitDepth; // how many total bits/pixel in z buffer (including any stencil bits) + DWORD dwAlphaBitDepth; // how many bits for alpha channels + DWORD dwLuminanceBitCount; // how many bits per pixel + DWORD dwBumpBitCount; // how many bits per "buxel", total + DWORD dwPrivateFormatBitCount;// Bits per pixel of private driver formats. Only valid in texture + // format list and if DDPF_D3DFORMAT is set + } DUMMYUNIONNAMEN(1); + union + { + DWORD dwRBitMask; // mask for red bit + DWORD dwYBitMask; // mask for Y bits + DWORD dwStencilBitDepth; // how many stencil bits (note: dwZBufferBitDepth-dwStencilBitDepth is total Z-only bits) + DWORD dwLuminanceBitMask; // mask for luminance bits + DWORD dwBumpDuBitMask; // mask for bump map U delta bits + DWORD dwOperations; // DDPF_D3DFORMAT Operations + } DUMMYUNIONNAMEN(2); + union + { + DWORD dwGBitMask; // mask for green bits + DWORD dwUBitMask; // mask for U bits + DWORD dwZBitMask; // mask for Z bits + DWORD dwBumpDvBitMask; // mask for bump map V delta bits + struct + { + WORD wFlipMSTypes; // Multisample methods supported via flip for this D3DFORMAT + WORD wBltMSTypes; // Multisample methods supported via blt for this D3DFORMAT + } MultiSampleCaps; + + } DUMMYUNIONNAMEN(3); + union + { + DWORD dwBBitMask; // mask for blue bits + DWORD dwVBitMask; // mask for V bits + DWORD dwStencilBitMask; // mask for stencil bits + DWORD dwBumpLuminanceBitMask; // mask for luminance in bump map + } DUMMYUNIONNAMEN(4); + union + { + DWORD dwRGBAlphaBitMask; // mask for alpha channel + DWORD dwYUVAlphaBitMask; // mask for alpha channel + DWORD dwLuminanceAlphaBitMask;// mask for alpha channel + DWORD dwRGBZBitMask; // mask for Z channel + DWORD dwYUVZBitMask; // mask for Z channel + } DUMMYUNIONNAMEN(5); + } DDPIXELFORMAT; + + typedef struct _DDSCAPS2 + { + DWORD dwCaps; // capabilities of surface wanted + DWORD dwCaps2; + DWORD dwCaps3; + union + { + DWORD dwCaps4; + DWORD dwVolumeDepth; + } DUMMYUNIONNAMEN(1); + } DDSCAPS2; + + typedef struct _DDSURFACEDESC2 + { + DWORD dwSize; // size of the DDSURFACEDESC structure + DWORD dwFlags; // determines what fields are valid + DWORD dwHeight; // height of surface to be created + DWORD dwWidth; // width of input surface + union + { + LONG lPitch; // distance to start of next line (return value only) + DWORD dwLinearSize; // Formless late-allocated optimized surface size + } DUMMYUNIONNAMEN(1); + union + { + DWORD dwBackBufferCount; // number of back buffers requested + DWORD dwDepth; // the depth if this is a volume texture + } DUMMYUNIONNAMEN(5); + union + { + DWORD dwMipMapCount; // number of mip-map levels requestde + // dwZBufferBitDepth removed, use ddpfPixelFormat one instead + DWORD dwRefreshRate; // refresh rate (used when display mode is described) + DWORD dwSrcVBHandle; // The source used in VB::Optimize + } DUMMYUNIONNAMEN(2); + DWORD dwAlphaBitDepth; // depth of alpha buffer requested + DWORD dwReserved; // reserved + DWORD lpSurface; // pointer to the associated surface memory + union + { + DDCOLORKEY ddckCKDestOverlay; // color key for destination overlay use + DWORD dwEmptyFaceColor; // Physical color for empty cubemap faces + } DUMMYUNIONNAMEN(3); + DDCOLORKEY ddckCKDestBlt; // color key for destination blt use + DDCOLORKEY ddckCKSrcOverlay; // color key for source overlay use + DDCOLORKEY ddckCKSrcBlt; // color key for source blt use + union + { + DDPIXELFORMAT ddpfPixelFormat; // pixel format description of the surface + DWORD dwFVF; // vertex format description of vertex buffers + } DUMMYUNIONNAMEN(4); + DDSCAPS2 ddsCaps; // direct draw surface capabilities + DWORD dwTextureStage; // stage in multitexture cascade + } DDSURFACEDESC2; +#endif + + +class OceanSurface +{ + public: + OceanSurface(bool use_texture_arrays); + ~OceanSurface(); + + // Quadtree handle and shader mappings + GFSDK_WaveWorks_QuadtreeHandle m_hOceanQuadTree; + gfsdk_U32* m_pQuadTreeShaderInputMappings; + gfsdk_U32* m_pSimulationShaderInputMappings; + + // Quadtree initialization + gfsdk_waveworks_result InitQuadTree(const GFSDK_WaveWorks_Quadtree_Params& params); + + + // Initializing OpenGL functions addresses + HRESULT InitGLFunctions(void); + + // Creating OpenGL programs + GLuint LoadProgram(char *); + GLuint CompileShader(char *text, GLenum type); + + // Texture loading related methods + int LoadTexture(char * filename, GLuint * texID); + void CreateTextures(void); + + // Rendering related methods + void SetupNormalView(void); + void Render(GFSDK_WaveWorks_SimulationHandle hSim, GFSDK_WaveWorks_Simulation_Settings settings); + + // Constants + UINT ScreenWidth; + UINT ScreenHeight; + UINT MultiSampleCount; + UINT MultiSampleQuality; + + // Programs + GLuint WaterProgram; + + // Textures + GLuint FoamIntensityTextureID; + GLuint FoamIntensityTextureBindLocation; + GLuint FoamDiffuseTextureID; + GLuint FoamDiffuseTextureBindLocation; + + // Buffers + GLuint GlobalVAO; + + // GL resources allocated for WaveWorks during ocean surface rendering + GFSDK_WaveWorks_Simulation_GL_Pool glPool; + + // Camera reated variables + float CameraPosition[3]; + float LookAtPosition[3]; + + float NormalViewMatrix[4][4]; + float NormalProjMatrix[4][4]; + float NormalViewProjMatrix[4][4]; + + // Counters + float total_time; + float delta_time; + long frame_number; + + // Input + int MouseX,MouseY; + float MouseDX,MouseDY; + float Alpha; + float Beta; + +}; + +void checkError(const char *); +void splitToDirAndFile(const char* filename, char* dir, char* name); + diff --git a/test/gl2_mac/water.glsl b/test/gl2_mac/water.glsl new file mode 100644 index 0000000..337803c --- /dev/null +++ b/test/gl2_mac/water.glsl @@ -0,0 +1,173 @@ +<<<VSTEXT>>> +// VSTEXT +#version 330 core + + +#define GFSDK_WAVEWORKS_GL + +#define GFSDK_WAVEWORKS_DECLARE_GEOM_VS_CONSTANT(Type,Label,Regoff) uniform Type Label; +#include "GFSDK_WaveWorks_Quadtree.fxh" + +#define GFSDK_WAVEWORKS_DECLARE_ATTR_VS_CONSTANT(Type,Label,Regoff) uniform Type Label; +#define GFSDK_WAVEWORKS_DECLARE_ATTR_VS_SAMPLER(Label,TextureLabel,Regoff) uniform sampler2D Label; +#define GFSDK_WAVEWORKS_DECLARE_ATTR_VS_SAMPLER_TEXTUREARRAY(Label,TextureLabel,Regoff) uniform sampler2DArray Label; +#define GFSDK_WAVEWORKS_DECLARE_ATTR_PS_CONSTANT(Type,Label,Regoff) uniform Type Label; +#define GFSDK_WAVEWORKS_DECLARE_ATTR_PS_SAMPLER(Label,TextureLabel,Regoff) uniform sampler2D Label; +#define GFSDK_WAVEWORKS_DECLARE_ATTR_PS_SAMPLER_TEXTUREARRAY(Label,TextureLabel,Regoff) uniform sampler2DArray Label; +#include "GFSDK_WaveWorks_Attributes.fxh" + +// User uniforms +uniform mat4x4 u_ViewProjMatrix; + +in vec4 nv_waveworks_quad7; +out GFSDK_WAVEWORKS_VERTEX_OUTPUT VSOutput; + +void main() +{ + GFSDK_WAVEWORKS_VERTEX_INPUT i; + i.nv_waveworks_quad7 = nv_waveworks_quad7; + VSOutput = GFSDK_WaveWorks_GetDisplacedVertex(i); + vec3 dwpos = VSOutput.pos_world; + gl_Position = u_ViewProjMatrix*vec4(dwpos,1.f); + + //gl_Position = nv_waveworks_quad7; +} + + +<<<FSTEXT>>> +// FSTEXT +#version 330 core + +#define GFSDK_WAVEWORKS_GL + +#define GFSDK_WAVEWORKS_DECLARE_GEOM_VS_CONSTANT(Type,Label,Regoff) uniform Type Label; +#include "GFSDK_WaveWorks_Quadtree.fxh" + +#define GFSDK_WAVEWORKS_DECLARE_ATTR_VS_CONSTANT(Type,Label,Regoff) uniform Type Label; +#define GFSDK_WAVEWORKS_DECLARE_ATTR_VS_SAMPLER(Label,TextureLabel,Regoff) uniform sampler2D Label; +#define GFSDK_WAVEWORKS_DECLARE_ATTR_VS_SAMPLER_TEXTUREARRAY(Label,TextureLabel,Regoff) uniform sampler2DArray Label; +#define GFSDK_WAVEWORKS_DECLARE_ATTR_PS_CONSTANT(Type,Label,Regoff) uniform Type Label; +#define GFSDK_WAVEWORKS_DECLARE_ATTR_PS_SAMPLER(Label,TextureLabel,Regoff) uniform sampler2D Label; +#define GFSDK_WAVEWORKS_DECLARE_ATTR_PS_SAMPLER_TEXTUREARRAY(Label,TextureLabel,Regoff) uniform sampler2DArray Label; +#include "GFSDK_WaveWorks_Attributes.fxh" + +// User uniforms + +uniform sampler2D g_texFoamIntensityMap; +uniform sampler2D g_texFoamDiffuseMap; + +// User local variables +vec3 g_SkyColor = vec3(0.38, 0.45, 0.56); +vec3 g_BendParam = vec3(0.1, -0.4, 0.2); + +vec3 g_SunDir = vec3(0.936016, -0.343206, 0.0780013); +vec3 g_WaterDeepColor=vec3(0.0,0.4,0.6); +vec3 g_WaterScatterColor=vec3(0.0,0.7,0.6); +vec2 g_WaterColorIntensity=vec2(0.2,0.1); + +vec3 g_FoamColor = vec3(0.90, 0.95, 1.0); +vec3 g_FoamUnderwaterColor = vec3(0.90, 0.95, 1.0); + +// in & out variables +in GFSDK_WAVEWORKS_VERTEX_OUTPUT VSOutput; + +out vec4 color; + +void main() +{ + + GFSDK_WAVEWORKS_SURFACE_ATTRIBUTES surface_attributes = GFSDK_WaveWorks_GetSurfaceAttributes(VSOutput.interp); + + float fresnel_factor; + float diffuse_factor; + float specular_factor; + float scatter_factor; + + vec3 pixel_to_light_vector=g_SunDir; + vec3 pixel_to_eye_vector=surface_attributes.eye_dir; + vec3 reflected_eye_to_pixel_vector = reflect(-surface_attributes.eye_dir, surface_attributes.normal); + + // simulating scattering/double refraction: light hits the side of wave, travels some distance in water, and leaves wave on the other side + // it's difficult to do it physically correct without photon mapping/ray tracing, so using simple but plausible emulation below + + // only the crests of water waves generate double refracted light + scatter_factor=1.0*max(0,VSOutput.world_displacement.z*0.001+0.3); + + + // the waves that lie between camera and light projection on water plane generate maximal amount of double refracted light + scatter_factor*=pow(max(0.0,dot(normalize(vec3(pixel_to_light_vector.x,0.0,pixel_to_light_vector.z)),-pixel_to_eye_vector)),2.0); + + // the slopes of waves that are oriented back to light generate maximal amount of double refracted light + scatter_factor*=pow(max(0.0,0.5-0.5*dot(pixel_to_light_vector,surface_attributes.normal)),3.0); + + + // water crests gather more light than lobes, so more light is scattered under the crests + scatter_factor+=2.0*g_WaterColorIntensity.y*max(0,VSOutput.world_displacement.z*0.001+0.3)* + // the scattered light is best seen if observing direction is normal to slope surface + max(0,dot(pixel_to_eye_vector,surface_attributes.normal)); + + + // calculating fresnel factor + float r=(1.0 - 1.13)*(1.0 - 1.13)/(1.0 + 1.13); + fresnel_factor = r + (1.0-r)*pow(saturate(1.0 - dot(surface_attributes.normal,pixel_to_eye_vector)),4.0); + + // calculating diffuse intensity of water surface itself + diffuse_factor=g_WaterColorIntensity.x+g_WaterColorIntensity.y*max(0,dot(pixel_to_light_vector,surface_attributes.normal)); + + vec3 refraction_color=diffuse_factor*g_WaterDeepColor; + + // adding color that provide foam bubbles spread in water + refraction_color += g_FoamUnderwaterColor*saturate(surface_attributes.foam_turbulent_energy*0.2); + + // adding scatter light component + refraction_color+=g_WaterScatterColor*scatter_factor; + + // reflection color + vec3 reflection_color = g_SkyColor; + + // fading reflection color to half if reflected vector points below water surface + reflection_color.rgb *= 1.0 - 0.5*max(0.0,min(1.0,-reflected_eye_to_pixel_vector.z*4.0)); + + // applying Fresnel law + vec3 water_color = mix(refraction_color,reflection_color,fresnel_factor); + + // applying surface foam provided by turbulent energy + + // low frequency foam map + float foam_intensity_map_lf = 1.0*texture(g_texFoamIntensityMap, VSOutput.pos_world_undisplaced.xy*0.04*vec2(1,1)).x - 1.0; + + // high frequency foam map + float foam_intensity_map_hf = 1.0*texture(g_texFoamIntensityMap, VSOutput.pos_world_undisplaced.xy*0.15*vec2(1,1)).x - 1.0; + + // ultra high frequency foam map + float foam_intensity_map_uhf = 1.0*texture(g_texFoamIntensityMap, VSOutput.pos_world_undisplaced.xy*0.3*vec2(1,1)).x; + + float foam_intensity; + foam_intensity = saturate(foam_intensity_map_hf + min(3.5,1.0*surface_attributes.foam_turbulent_energy-0.2)); + foam_intensity += (foam_intensity_map_lf + min(1.5,1.0*surface_attributes.foam_turbulent_energy)); + + + foam_intensity -= 0.1*saturate(-surface_attributes.foam_surface_folding); + + foam_intensity = max(0,foam_intensity); + + foam_intensity *= 1.0+0.8*saturate(surface_attributes.foam_surface_folding); + + float foam_bubbles = texture(g_texFoamDiffuseMap, VSOutput.pos_world_undisplaced.xy).r; + foam_bubbles = saturate(5.0*(foam_bubbles-0.8)); + + // applying foam hats + foam_intensity += max(0,foam_intensity_map_uhf*2.0*surface_attributes.foam_wave_hats); + + foam_intensity = pow(foam_intensity, 0.7); + foam_intensity = saturate(foam_intensity*foam_bubbles*1.0); + // + + // foam diffuse color + float foam_diffuse_factor=max(0,0.8+max(0,0.2*dot(pixel_to_light_vector,surface_attributes.normal))); + + water_color = mix(water_color,foam_diffuse_factor*vec3(1.0,1.0,1.0),foam_intensity); + + color = vec4(water_color,0); +} + |