0

I have a scalar field of values which I have mapped to a 3D texture( image_texture ). And then given a plane gPlaneParams , I have to render the texture of the scalar-field along it.

What I'm doing: I send 4 points which span the window dimensions using two triangles to the shaders. I bind the texture using a sampler in the fragment shader. Below is the fragment shader code.

#version 330 core
uniform sampler3D text_sampler;
uniform vec4 gPlaneParams;

in vec4 inPos;

void main()
{
    vec4 Pos = inPos;
    // position input is a square[-1,1]^2
    // and needs to be mapped the plane ax+by+cz=d, where a,b,c,d are the plane parameters;
    //where x,y,z belongs to [0,1]^3
    if (gPlaneParams.z!=0){
        Pos.z = (gPlaneParams.w - gPlaneParams.x*Pos.x - gPlaneParams.y*Pos.y)/gPlaneParams.z;
    }
    else{
        if (gPlaneParams.x!=0){
            Pos.z=Pos.x;
            Pos.x = (gPlaneParams.w - gPlaneParams.y*Pos.y - gPlaneParams.z*Pos.z)/gPlaneParams.x;
        }
        else if (gPlaneParams.y!=0){
            Pos.z=Pos.y;
            Pos.y = (gPlaneParams.w - gPlaneParams.x*Pos.x - gPlaneParams.z*Pos.z)/gPlaneParams.y;
        }
    }

    gl_FragColor=vec4(1.0,0,0,0)*texture3D(text_sampler,(Pos.xyz+1)/2);

}

In my C++ code, I bind the texture as follows:

glGenTextures(1,textureID);
    glBindTexture(GL_TEXTURE_3D,textureID[0]);
    glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB,object_size[0],object_size[1],object_size[2], 0, GL_RGB, GL_UNSIGNED_INT, image_texture1);
    glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_3D,GL_TEXTURE_WRAP_R,GL_CLAMP_TO_EDGE);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_3D,textureID[0]);
    bool err=glIsTexture(textureID[0]);
    cout<<"Texture bound?"<<err<<endl;

Unfortunately, this does not render any output. Can someone help me figure out what I'm doing wrong?


I have done everything else correctly

  • The 4 Vertices and 2 triangles are properly bound (I can render them by giving them constant colours)
  • The image texture is contiguous in memory image_texture = (unsigned int*) malloc(object_size[0] *object_size[1] *object_size[2]*3* sizeof(unsigned int));
  • All my inputs to the shader are successfully bound.:
gSamplerLocation = glGetUniformLocation(ShaderProgram, "text_sampler");
gPLaneLoc = glGetUniformLocation(ShaderProgram, "gPlaneParams");
glUniform1i(gSamplerLocation, 0);    
glUniform4f(gPLaneLoc,plane_params[0],plane_params[1],plane_params[2],plane_params[3]);
  • What is with all of the `#extension` spam? That looks like copy-and-paste from a bunch of other shaders without understanding why they used them. There's no reason to *both* `enable` and `require` an extension. `GL_ARB_explicit_attrib_location` is a core part of 3.30, so you didn't need to ask for it at all. And you don't seem to use explicit attribute locations *at all*. – Nicol Bolas Oct 13 '22 at 14:20
  • You're right. They were part of desperate attempts to make my code work – Sudhanshu Bharadwaj Oct 13 '22 at 14:51
  • I would use vertex or geometry shader to compute the texture 3D coordinates of the passed triangles from screen position and plane equation. The if you pass that to fragment GL will interpolate it linearly so no need to do anything you just fetch the texel and output it as color ... non need for any if/else plane equation is normal and offset so you just compute 4x4 matrix for it exploiting cross product and use that directly ... can you share sample input texture? ideally in ASCII ... – Spektre Oct 16 '22 at 07:42

0 Answers0