Raycasting backface texture lookup

Hi, All

I am try to integrate raycasting into my apps. basically I am using “http://www.daimi.au.dk/~trier/?page_id=98” and the source code for reference.
The code I am drawing quads are:( I am not drawing a unit cube, )

[b]
glBegin(GL_QUADS);
            /* Bottom side */
            glColor3f(0.0, 0.0, 0.0);     glVertex3f(corner[0][0], corner[0][1], corner[0][2]);
            glColor3f(0.0, 1.0, 0.0);     glVertex3f(corner[6][0], corner[6][1], corner[6][2]);
            glColor3f(1.0, 1.0, 0.0);     glVertex3f(corner[4][0], corner[4][1], corner[4][2]);
            glColor3f(1.0, 0.0, 0.0);     glVertex3f(corner[2][0], corner[2][1], corner[2][2]);

//......................
            /* Top side */
            glColor3f(0.0, 0.0, 1.0);     glVertex3f(corner[1][0], corner[1][1], corner[1][2]);
            glColor3f(1.0, 0.0, 1.0);     glVertex3f(corner[3][0], corner[3][1], corner[3][2]);
            glColor3f(1.0, 1.0, 1.0);     glVertex3f(corner[5][0], corner[5][1], corner[5][2]);
            glColor3f(0.0, 1.0, 1.0);     glVertex3f(corner[7][0], corner[7][1], corner[7][2]);

    
glEnd();
 [/b]
[/b][/QUOTE]

The code I am rendering the backface(using frame buffer object, myBackfaceBuffer is initialized as a 1024*720 block)

[b]
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, myFBO);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, myBackfaceBuffer, 0);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);

drawQuads();

glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

[/b]
[/b][/QUOTE]

// casting code

[b]
glUseProgramObjectARB( myVolShaderProgram);

    // step size when walking through the data set
    int step_size = glGetUniformLocationARB(myVolShaderProgram, "stepSize");
    glUniform1fARB(step_size, stepSize);

    // backface texture
    int back_face = glGetUniformLocationARB(myVolShaderProgram, "backFace");
    glActiveTexture(GL_TEXTURE1_ARB);
    glBindTexture( GL_TEXTURE_2D, myBackfaceBuffer);
    glUniform1iARB(back_face, 1);

    // data volume
    int data_volume = glGetUniformLocationARB(myVolShaderProgram, "dataVolume");
    glActiveTexture(GL_TEXTURE0_ARB);
    glBindTexture(GL_TEXTURE_3D, myVolumeTexture);
    glUniform1iARB(data_volume, 0);

    // draw front face
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);
    drawQuads();
    glDisable(GL_CULL_FACE);

    glUseProgramObjectARB(0);
 [/b]
[/b][/QUOTE]
The shader I am drawing:

[b]
// vertex shader
varying vec4 pos;
varying vec4 color;
void main()
{
color = gl_Color;
pos = ftransform();

  gl_Position = pos;

}
[/b]
[/b][/QUOTE]

[b]
varying vec4 pos;
varying vec4 color;

uniform float stepSize;
uniform sampler2D backFace;
uniform sampler3D dataVolume;

// Raycasting fragment program implementation
void main()
{
  vec2 texc  =  (pos.xy / pos.w +1.0)/2.0;
  vec4 start =  color;
  vec4 back_position  = texture2D(backFace, texCoord);

// .......... tracing part
  
  gl_FragColor = back_position;//start;//col_acc;
}
 [/b]
[/b][/QUOTE]

My problem is: when I draw the front face (gl_FragColor = start;)
I can get the colored cube. But when I try to draw the backface (gl_FragColor = back_position;), I can't get the colored cube but something black or white.....  I think my back face texture coordinate look up is not correct, But I am really not sure why even the tutorial gave some explanation about it...

Can anybody point out why?

tks

Hello,
in vertex shader: pos = ftransform(); now means, that the vertex position is in clipping space

If you divide pos.xyz with pos.w you get normalized device coordinates between [-1, 1].

vec2 texc = (pos.xy / pos.w +1.0)/2.0;

transforms this normalized device coordinates [NDC] into texture coordinates [0,1] .
So for example if pos.xy/w = -1 then the result of texc is
texc = (-1 + 1)/2.0 = 0 and for pos.xy/w = 1 it would be
texc = (1 + 1) / 2.0 = 1

Check your view frustum, maybe your znear or zfar planes cutting off your cube.

wolfed

Thank you very much for the reply.

Finally I got it. It is because of the lighting.
When I render the cube, seems the light is on, so for the front face, the rendering result is looks OK, but for the back face, I always got some gray cut box.

before render to frame buffer object, set “glDisable(GL_LIGHTING);” gives me the right output.

when I calculated the ray direction. I tried two ways, one is


vec2 texc = (pos.xy / pos.w +1.0)/2.0;
vec4 start = color;
vec4 back_position = texture2D(backFace, texCoord);
vec4 rayDir = back_position - start;

[/b]
.
the other one is


rayDir = start - eyeInTexture;

[/b]

I found out that the second approach gives me much better resolution comparing to the first one.( the rendering image is smoother than the first one ).

I think the reason of that is: when I make texture2D(backFace, texCoord) call, the color I got is retrieved from the backface buffer which is not interpolated( the quality depends on the buffer size).

wondering why nobody ever mentioned this problem? Is there any way to make this up ? Because I want to implement the flying through, I need a high resolution ray direction when the eye is in the volume.

tks

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.