PDA

View Full Version : Dorbie. Help? (3D Texture mapping) almost working



KraftDinner
10-06-2004, 12:14 PM
Dorbie... I think its almost working but there seems to be something I'm not understanding about the rotating...
I'm rotating the texture (about the origin) and then switching back to model view and passing planes through the texture...
It I rotate model view everything looks normal but I don't get back faces of objects... However if I rotate the texture then I can't even begin to describe the results.. they are truely warped...

I'm at my wits end with this dorbie... I've been through the thread you have suggested.. and this is still not doing what I expect...

When this works I'll add the clipping code..

Keep in mind dorbie that my texture is not a cube in dimensions... in fact its like 512x512x32. So it seems a bit stretched when viewed.. because the code looks to me like it assumes that the texture was cubed. (In your example)


void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);

ThreeDTexture * text = readRawSlice("morerawct/ct.raw", iWidth, iHeight, iDepth);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_3D, texName);

glTexImage3D(GL_TEXTURE_3D, 0, GL_INTENSITY16, iWidth, iHeight, iDepth, 0, GL_LUMINANCE, GL_UNSIGNED_SHORT, (GLvoid *) text->image);

glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP);

glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glEnable(GL_TEXTURE_GEN_R);

glEnable(GL_TEXTURE_3D);

glEnable(GL_BLEND);
glBlendEquation(GL_MAX);
}

void display(void)
{
static float planeS[] = {1.0f, 0.0f, 0.0f, 1.0f};
static float planeT[] = {0.0f, 1.0f, 0.0f, 1.0f};
static float planeR[] = {0.0f, 0.0f, 1.0f, 1.0f};
int slice;
float z;
static float r=0.0;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glTranslatef(-0.5f, -0.5f, -0.5f);
glRotatef(r, 1.0f, 1.0f, 1.0f);

glMatrixMode(GL_MODELVIEW);

glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);

glTexGenfv(GL_S, GL_EYE_PLANE, planeS);
glTexGenfv(GL_T, GL_EYE_PLANE, planeT);
glTexGenfv(GL_R, GL_EYE_PLANE, planeR);

glBegin(GL_QUADS);
for (slice=0; slice < iDepth; ++slice)
{
z = ((1.0/((float) (iDepth-1))) * (float) slice) - 0.5f;

glVertex3f(-0.5f, -0.5f, z);
glVertex3f(+0.5f, -0.5f, z);
glVertex3f(+0.5f, +0.5f, z);
glVertex3f(-0.5f, +0.5f, z);
}
glEnd();

glFlush();

r = r + 0.5;
}

void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(60.0, (GLfloat) w / (GLfloat) h, 1.0, 30.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glTranslatef(0.0, 0.0, -2.5);
}

dorbie
10-06-2004, 01:14 PM
Hmm... your texgen in eye_linear means your plane equations and the subsequent coordinate generation is relative to the eye and additionally rotations are around the eye's position (the origin in eye space).

The translation will move it by half a volume.

You have a couple of options, try the following:

Clamp the texture and have an an alpha = 0 border, or just zero color since you're using a max bland equation. Eliminate all rotations and then move the volume to the desired location. This will involve some translations, this should force you to position the volume in front of the viewer with the correct texgen. You should then be able to rotate around any point but remember that this will require a translation back to the origin (the eye for texgen eye). To rotate about a point translate -point, rotate then translate +point. The -.5 -.5 -.5 is to move the center of the cube to the origin you should leave that in addition to everything else. Your viewing translation is -2.5 in z that means you have to translate your texgen out with that also, and undo that translation then redo it for the rotation, however just slipping the rotation in there before you translate the cube out on the texture stack but after the -.5 translate will also do the trick.

The (IMHO simpler) alternative is to specify the texgen in object space but before you specify the plane equations load the translation and rotation for the volume onto the modelview matrix (it's already there it's the -2.5 z for the viewing translate). This will transform the plane equations to the appropriate space and fix your problem. You may find this approach easier. This would be better with cleaner code that doesn't jumble the various calls & state settings so much, but only to aid your understanding. In object space the rotation orogin will be around the local origin and the -0.5 texture matrix translate recenters the cube locally, so a simple rotate after this will suffice.

You need to start working on your understanding of what these matrices do in this rendering algorithm w.r.t. object, world and eye space and *when* & where these spaces exist, I think that's what's missing here. You'll have trouble getting the clip planes to transform correctly(via modelview translations) if you don't grok this stuff.

KraftDinner
10-06-2004, 01:26 PM
Ok thanks dorbie... Its going to take me a while to absorbIe all that... I think I understand code better than english these days...

However dorbie.. I started all of this off from the red book example. In the red book texture3d example there were no texGen calls, and I like that because if you will remember i need to deform my texture and I had control over how the texture was mapped to my polygons. I gave up on this for two reasons... 1) I couldn't get the back faces of the polygons to render 2) because you pointed me to the other code...

If I continue with the automatic texture co-ordinate generation code I'm going to be ______ because then I can't deform my texture.

Remember here I am using glBlend with the GL_MAX equation.
To do a maxium intensity projection and I suspect that I'm not getting the back face of my polygon because of the drawing order.

One more thing I don't understand is that if my polygon is outside the bounding box of the texture I just want it to be transparent... I think I messed that up by using GL_CLAMP but I can't figure out what else I could use...

Sorry to take up so much of this groups time..