opengl 3d texture issue

Posted by user1478217 on Stack Overflow See other posts from Stack Overflow or by user1478217
Published on 2012-06-24T15:06:48Z Indexed on 2012/06/24 15:15 UTC
Read the original article Hit count: 314

Filed under:
|

Hi i'm trying to use a 3d texture in opengl to implement volume rendering. Each voxel has an rgba colour value and is currently rendered as a screen facing quad.(for testing purposes). I just can't seem to get the sampler to give me a colour value in the shader. The quads always end up black. When I change the shader to generate a colour (based on xyz coords) then it works fine. I'm loading the texture with the following code:

glGenTextures(1, &tex3D);
glBindTexture(GL_TEXTURE_3D, tex3D);
unsigned int colours[8];

colours[0] = Colour::AsBytes<unsigned int>(Colour::Blue);
colours[1] = Colour::AsBytes<unsigned int>(Colour::Red);
colours[2] = Colour::AsBytes<unsigned int>(Colour::Green);
colours[3] = Colour::AsBytes<unsigned int>(Colour::Magenta);
colours[4] = Colour::AsBytes<unsigned int>(Colour::Cyan);
colours[5] = Colour::AsBytes<unsigned int>(Colour::Yellow);
colours[6] = Colour::AsBytes<unsigned int>(Colour::White);
colours[7] = Colour::AsBytes<unsigned int>(Colour::Black);

glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, 2, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, colours);

The colours array contains the correct data, i.e. the first four bytes have values 0, 0, 255, 255 for blue. Before rendering I bind the texture to the 2nd texture unit like so:

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_3D, tex3D);

And render with the following code:

shaders["DVR"]->Use();
shaders["DVR"]->Uniforms["volTex"].SetValue(1); 
shaders["DVR"]->Uniforms["World"].SetValue(Mat4(vl_one));
shaders["DVR"]->Uniforms["viewProj"].SetValue(cam->GetViewTransform() * cam->GetProjectionMatrix());

QuadDrawer::DrawQuads(8);

I have used these classes for setting shader params before and they work fine. The quaddrawer draws eight instanced quads. The vertex shader code looks like this:

#version 330

layout(location = 0) in vec2 position;
layout(location = 1) in vec2 texCoord;

uniform sampler3D volTex;
ivec3 size = ivec3(2, 2, 2);
uniform mat4 World; 
uniform mat4 viewProj;

smooth out vec4 colour;

void main()
{
    vec3 texCoord3D;

    int num = gl_InstanceID;

    texCoord3D.x = num % size.x;
    texCoord3D.y = (num / size.x) % size.y;
    texCoord3D.z = (num / (size.x * size.y));

    texCoord3D /= size;
    texCoord3D *= 2.0;
    texCoord3D -= 1.0;

    colour = texture(volTex, texCoord3D);
    //colour = vec4(texCoord3D, 1.0);   

    gl_Position = viewProj * World * vec4(texCoord3D, 1.0) + (vec4(position.x, position.y, 0.0, 0.0) * 0.05);

}

uncommenting the line where I set the colour value equal to the texcoord works fine, and makes the quads coloured. The fragment shader is simply:

#version 330

smooth in vec4 colour;
out vec4 outColour;

void main()
{
    outColour = colour;
}

So my question is, what am I doing wrong, why is the sampler not getting any colour values from the 3d texture?

[EDIT] Figured it out but can't self answer (new user):

As soon as I posted this I figured it out, I'll put the answer up to help anyone else (it's not specifically a 3d texture issue, and i've also fallen afoul of it before, D'oh!). I didn't generate mipmaps for the texture, and the default magnification/minification filters weren't set to either GL_LINEAR, or GL_NEAREST. Boom! no textures. Same thing happens with 2d textures.

© Stack Overflow or respective owner

Related posts about c++

Related posts about opengl