1

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.

genpfault
  • 51,148
  • 11
  • 85
  • 139
igr
  • 4,529
  • 2
  • 15
  • 20

2 Answers2

2

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.'

user515655
  • 989
  • 2
  • 10
  • 24
  • Thanks! Now I better understand what a 3D texture actually is. – igr Feb 25 '13 at 00:07
  • 1
    relief mapping is not quite what OP needs. What OP wants is "direct volume rendering", also known as "volume raycasting". There are several methods for doing this. – datenwolf Feb 25 '13 at 00:41
  • @datenwolf Edited my answer to reflect your suggestion. – user515655 Feb 25 '13 at 21:34
  • ** a flat (non-rotated) single quad** is not enough to trace. For two-pass methods, both a front face (stores the entry points of each ray) and a back face (stores the exit point of each ray) are needed. You should draw a (3D) **cube** twice. For single-pass method, you can draw either of them depending on the direction you used to trace/cast. But you also need to draw a cube. For the texture-based methods, the website I post has made a detailed description. In conclusion, drawing a flat single quad won't work well for DVR. – Shannon Apr 29 '19 at 01:08
0

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.

Shannon
  • 323
  • 1
  • 11