OpenGL 3D texture on cube

1.8k Views Asked by At

I have a simple 3d cube (all sides equal size) and a 3D texture in OpenGL. I want to map the 3D texture to the cube.

The result should be a cube which is colored all the way through such that every point inside the cube gets the color from a corresponding point inside texture. This is at least my interpretation of a 3D texture.

How to do this? I suppose it will not work to make a cube with GL_QUADS (6 quads) and specifying one texture coordinate for each of the six sides?

I assume I have to make a cube and while making the cubing specifying glTexCoord in the correct way.

2

There are 2 best solutions below

4
On BEST ANSWER

What you are describing sounds like voxel rendering, typically done with raytracing.

3D textures unfortunately don't do what you describe. Though a 3D texture takes U,V,W for each texel fetch, OpenGL still draws along the flat surfaces of the primitives. You will have to write some raytracing code in your fragment shader to cast into your 3D texture. Since you'd be effectively raytracing, you'd might as well just place a flat (non-rotated) single quad (better yet, two triangles making a pseudo-quad since quads are deprecated) and write fragment shader code with your raytracing stuff.

For more details look up 'direct volume rendering.'

0
On

You should draw a lot of parallel quads instead of drawing a single Cude.

For example, draw a series of quads along z-axis:

#define MAP_3DTEXT( TexIndex ) \
glTexCoord3f(0.0f, 0.0f, ((float)TexIndex+1.0f)/2.0f);  \
glVertex3f(-dViewPortSize,-dViewPortSize,TexIndex);\
glTexCoord3f(1.0f, 0.0f, ((float)TexIndex+1.0f)/2.0f);  \
glVertex3f(dViewPortSize,-dViewPortSize,TexIndex);\
glTexCoord3f(1.0f, 1.0f, ((float)TexIndex+1.0f)/2.0f);  \
glVertex3f(dViewPortSize,dViewPortSize,TexIndex);\
glTexCoord3f(0.0f, 1.0f, ((float)TexIndex+1.0f)/2.0f);  \
glVertex3f(-dViewPortSize,dViewPortSize,TexIndex);



 for ( float fIndx = -1.0f; fIndx <= 1.0f; fIndx+=0.003f )
{
    glBegin(GL_QUADS);
        MAP_3DTEXT( fIndx );
    glEnd();
}

Now you will get what you want. More details can be found here

The aforementioned method is the so-called Texture based Volume Rendering. But if you seek for better visual effects and performance, Ray-Casting will be more suitable and it will enable you go through the volume.