render to 3D texture

Started by
0 comments, last by blubberbert 12 years, 7 months ago
im working on a volumetric renderer and want to implement updating mechanisms to the volume (wich is represented as 3D texture)

i wanted to know wether and how rendering to 3D textures is possibly (there is glFramebufferTexture3D but i dont quite get the rest)

so normally(when rendering to 2D textures) i would bind a framebuffer with the target texture and in my fragment shader do something like:

gl_FragData[0] = stuff.xyzw;

but since the fragment shader is run after the projection, coordinates should be 2d.
basically what im looking for is a way to write to a specific position in the 3D texture, lets say [0.43,0.5,0.7] without uploading the data from RAM
but instead writing it from a shader
------------------------------
Join the revolution and get a free donut!
Advertisement
ok so after many sleepless nights (and no replies here unsure.gif)
i finally found out how to do it


void buildFramebuffer()
{
renderTarget = new Texture3D(GL_RGB, GL_RGB8, GL_UNSIGNED_BYTE, GL_LINEAR, GL_CLAMP_TO_EDGE, 100,100,100,NULL);
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D, renderTarget->getId(), 0, 0);
GLenum buffers[] = { GL_COLOR_ATTACHMENT0 };
glDrawBuffers(1, buffers);
glReadBuffer(GL_NONE);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
printf("Framebuffer status: %s\n", status == GL_FRAMEBUFFER_COMPLETE?"OK":"ERROR");
}

void fillTarget()
{
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glPushAttrib(GL_VIEWPORT_BIT);
glViewport(0,0,100,100);
for(int i = 0; i < 100; i++)
{
glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, renderTarget->getId(), 0, i);
glClearColor(1.0f,(1.0f/100.0f)*((float)i),0.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-5.0f);
glColor3f(0.0f, 1.0f, 1.0f);
glBegin(GL_TRIANGLES);
glVertex3f(-1.0f,0.0f,0.0f);
glVertex3f( 0.0f,1.0f,0.0f);
glVertex3f( 1.0f,0.0f,0.0f);
glEnd();
}
glPopAttrib();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

void draw()
{
fillTarget();
glClearColor(0.0f,0.0f,1.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_3D);
renderTarget->bind();
glColor3f(1.0f,1.0f,1.0f);
glBegin(GL_QUADS);
glTexCoord3f(0.0f,0.0f,1.0f); glVertex3f(-1.0f, 1.0f,0.0f);
glTexCoord3f(0.0f,1.0f,1.0f); glVertex3f(-1.0f,-1.0f,0.0f);
glTexCoord3f(1.0f,1.0f,1.0f); glVertex3f( 1.0f,-1.0f,0.0f);
glTexCoord3f(1.0f,0.0f,1.0f); glVertex3f( 1.0f, 1.0f,0.0f);
glEnd();
Texture3D::unbind();
SDL_GL_SwapBuffers();
}

just posting this here for the people facing similar problems

this code should render a blue background with a yellow quad in the center with a turquoise triangle in it
(the quad is yellow because the texcoords Z is 1 if it were 0 the quad would be red)
however if someone finds anything wrong with my code feel free to point any mistakes out (apart from every 2nd function call being deprecated)
------------------------------
Join the revolution and get a free donut!

This topic is closed to new replies.

Advertisement