1
votes

the following code is used to draw an ocean, i tried to convert it to c# code, but i failed because i didn't understand what 3d functions do, so i want some explanation on these functions (3d texture i mean)...

int res = 64;
int nr = res / 2;
int nv = res * 2;
int nb = res / 2;
int na = 8;
f = fopen("E:\\New folder\\ConsoleApplication1\\Debug\\data\\inscatter.raw", "rb");
data = new float[nr*nv*nb*na*4];
fread(data, 1, nr*nv*nb*na*4*sizeof(float), f);
fclose(f);
glActiveTexture(GL_TEXTURE0 + INSCATTER_UNIT);
glGenTextures(1, &inscatterTex);
glBindTexture(GL_TEXTURE_3D, inscatterTex);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA16F_ARB, na*nb, nv, nr, 0, GL_RGBA, GL_FLOAT, data);
delete[] data;
1

1 Answers

6
votes

3D textures are volumetric images, i.e. they're sort of a cuboid filled with voxels of color. So instead of a picture of width × height · color_channels you have a picture of width × height × depth · color_channels.

This is an animation showing a volumetric image (courtsey of the research working group I'm a member of): http://www.bmo.physik.uni-muenchen.de/~z19/OCTanimation.gif (unfortunately StackOverflow doesn't allow linking of animated GIFs).

I saw that sb. else was about ocean rendering in another post. Be advised that this is a hugely nontrivial subject requiring some very sophisticated algorithms. The use of a 3D texture called "inscatter" suggests that whatever code you've looked up implements one of those complex models, where the 3D texture is used as a scattering term lookup table.

Note that without a solid understanding of OpenGL and numerical methods implementing an ocean renderer is a almost impossible to perform task. I'd start with understanding the basics first.