1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
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);
|