Berke Cagkan Toptas
Berke Cagkan Toptas

Reputation: 1034

GLSL Reading From Sampler3d texture

I have 3D volume texture which I initialize it with below line :

glTexImage3D(GL_TEXTURE_3D, 0, GL_RED, numX, numY, numZ, 0, GL_RED, GL_UNSIGNED_BYTE, voldata);

In fragment shader, I want to read values of this texture but I cant read with texture3d() function.

below line give a compile error in fragment shader : (No matching Overloaded for texture3d)

float value = texture3d(VolumeTexture,vec3(0.2f,0.2f,0.2f);

How can I get the data from sampler3d?

OpenGLPart :

unsigned int texture;   
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_3D, texture);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
glTexImage3D(GL_TEXTURE_3D, 0, GL_RED, numX, numY, numZ, 0, GL_RED, GL_UNSIGNED_BYTE, voldata);
free(voldata ); //free the texture
                ...
    display()
    {
                  ....
       glActiveTexture(GL_TEXTURE0);    
       int texture_location = glGetUniformLocation(shader.id(), "VolumeTexture");  
       glUniform1i(texture_location, 0);  
       glBindTexture(GL_TEXTURE_3D,volumetext);
                  ....
    }                

My Fragment Shader :

    uniform sampler3D VolumeTexture;
    void main()
    {
        float value = 0;
                ...
        value = texture3d(VolumeTexture,vec3(0.2f,0.2f,0.2f);
                ...
    gl_FragColor = IntersectRay(ray);
    }

Note : My OpenGL version is 3.3

Upvotes: 3

Views: 13767

Answers (3)

Grimmy
Grimmy

Reputation: 4137

Moving comment into a proper answer..

Always (with no exceptions) do proper error checking when loading, compiling and linking your shaders. In the long run you will save a lot of time. This applies even if you verify your shader in an external program.

  • After you call glCompileShader(..), get the compile status by calling glGetShaderiv(..) with GL_COMPILE_STATUS as parameter.
  • If the return value is 0, something went wrong
  • Get the info log by querying for the length using glGetShaderiv(..) parameter GL_INFO_LOG_LENGTH.
  • Then fetch the log with glGetShaderInfoLog

This needs to be done each shader type (vertex, fragment, geo.. ).

In additon you should check link status after glLinkProgram() using glGetProgramiv() and GL_LINK_STATUS as parameter.

You would immediately seen an error like 0(19) : error C1008: undefined variable "texture3d" not having to spend hours to track down the issue.

Upvotes: 6

Vasaka
Vasaka

Reputation: 2022

Try texture without 3D suffix, they deprecated functions with dimension specified in the OpenGL 4 and just overload texture function for different samplers. Also your line lacks one brace.

Upvotes: 2

genpfault
genpfault

Reputation: 52084

I cant read with texture3d() function.

Try texture3D(). Note the capital D.

Upvotes: 5

Related Questions