Hello!
I am trying to implement a ray casting algorithm in glsl. I want automatically rotate the volume, as I don’t want to implement any mouse/keyboard interaction yet.
So in order to do this, I have 2 textures: frontfaceTex, backfaceTex attached to GL_COLOR_ATTACHMENT0_EXT and GL_COLOR_ATTACHMENT1_EXT of my framebuffer. In these 2 textures I am rendering the front face and the back face of a cube.
void drawCubesToTextures()
{
static int i=0;
cout<<i++<<endl;
static float rotate = 0;
rotate += 1.0;
//////////////////////////
//frontface of the cube///
//////////////////////////
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer);
glPushAttrib(GL_VIEWPORT_BIT | GL_COLOR_BUFFER_BIT);
glViewport(0,0,256, 256);
// Set the render target
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
glPushMatrix();
glRotatef(rotate,1,1,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glClearColor(0.0,0.0,0.0,0.0);
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
drawQuads(1.0,1.0, 1.0);
glDisable(GL_CULL_FACE);
glPopMatrix();
glPopAttrib();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
///////////////////////////
//backface of the cube/////
///////////////////////////
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer);
glPushAttrib(GL_VIEWPORT_BIT | GL_COLOR_BUFFER_BIT);
glViewport(0,0,256, 256);
// Set the render target
glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT);
glPushMatrix();
glRotatef(rotate,1,1,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glClearColor(0.0,0.0,0.0,0.0);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
drawQuads(1.0,1.0, 1.0);
glDisable(GL_CULL_FACE);
glPopMatrix();
glPopAttrib();
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
Now, can someone please tell me what am I doing wrong in the display function, as my texture will appear only for the first time the “display” function is called? (ONLY when variable i form the above function = 0 the texture is displayed)
void displayShader()
{
drawCubesToTextures();
GLint location1,location2, location3D, locationTF;
glPushAttrib(GL_ALL_ATTRIB_BITS);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
setShaders();
location1=glGetUniformLocationARB(p,"frontTexture");
glActiveTextureARB(GL_TEXTURE0_ARB);
glBindTexture(GL_TEXTURE_2D, frontfaceTex);
glUniform1iARB(location1, 0);
location2=glGetUniformLocationARB(p,"backTexture");
glActiveTextureARB(GL_TEXTURE1_ARB);
glBindTexture(GL_TEXTURE_2D, backfaceTex);
glUniform1iARB(location2, 1);
glBegin( GL_QUADS );
glTexCoord2d(0.0,0.0); glVertex2f(-1.0,-1.0);
glTexCoord2d(1.0,0.0); glVertex2f(1.0,-1.0);
glTexCoord2d(1.0,1.0); glVertex2f(1.0,1.0);
glTexCoord2d(0.0,1.0); glVertex2f(-1.0,1.0);
glEnd();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glPopAttrib();
releaseShaders();
glutSwapBuffers();
glFlush();
}
my shaders (dunno if the problem is here, as for the first rendering pass I get the result I need…)
varying vec4 texCoord;
void main()
{
gl_Position = ftransform();
texCoord = gl_TextureMatrix[0] * gl_MultiTexCoord0;
}
uniform sampler2D frontTexture;
uniform sampler2D backTexture;
varying vec4 texCoord;
void main()
{
gl_FragColor = texture2D(frontTexture,texCoord.xy);
}
p.s.
When using the normal opengl way to display a texture, I get the desired result no matter how many times the “display” function is called. That’s my reason why I am posting in the Shader part of this forum
ps2
I am minimizing/maximizing the display window to get the “display” function called again… (the result is in increasing the value of i variable, rotating the cube by i degrees, and the part that it does not work, displaying the textures using glsl)
Tnx a lot for taking the time to read my long story