0

Why we have to align the textures in different directions depending on the perspective of the camera? Shouldn't it be the same?(If I enabled depth test)

First Slide Second Slide


Edit 1:

I used my own program to test it.

Here is my code for the rendering function.

        if (position.z >= 0) pz = true;
        else pz = false;

        if (lz != pz) {
            slice.clear();
            printf("Changed !!! :: %s", (pz?"Positive Z":"Negative Z") );
            if (pz) {
                for (float i = 0; i < count; ++i) {
                    slice.push_back( glm::vec2( -SIZE/2 + SIZE * i / (count-1), i ) );
                }
                lz = pz;
            } else {
                for (float i = count-1; i >= 0; --i) {
                    slice.push_back( glm::vec2( -SIZE/2 + SIZE * i / (count-1), i ) );
                }
                lz = pz;
            }
            glBindBuffer(GL_ARRAY_BUFFER, ibo);
            glBufferData(GL_ARRAY_BUFFER, slice.size()* 2 *sizeof(float), &slice[0], GL_STATIC_DRAW);
        }

        glUseProgram(programID);
        glBindVertexArray(vao);
        glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &mvp[0][0]);
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, texture);
        glEnableVertexAttribArray(0);
        glEnableVertexAttribArray(1);
        glEnableVertexAttribArray(2);
        glDrawArraysInstanced(GL_TRIANGLES, 0, 6, slice.size());
        glDisableVertexAttribArray(0);
        glDisableVertexAttribArray(1);
        glDisableVertexAttribArray(2);
        glBindVertexArray(0);

The vector "slice" contains 2 floats (z position and the id of the slice). It is unique for each instance. Without this "if (lz != pz) ... " clause, I can only see the model in +ve Z direction.


Edit 2:

This is one perspective. enter image description here

But when my camera move to near z=0, parts of the textures can't be seen. I tried to turn off the depth test and then I see the textures AT THE BACK. Why the texture at the front disappeared? enter image description here

  1. Enabled depth test
  2. Disabled back-face culling
  3. Enabled alpha testing

Edit 3:

I changed the "if (lz != pz) ... " clause, to the following code and every thing works fine now. But I still don't understand why.

        slice.clear();
        for (float i = 0; i < count; ++i) { // position is a vector representing the location of the camera
            if (-SIZE/2 + SIZE * i / (count-1) >= position.z) break;
            slice.push_back( glm::vec2( -SIZE/2 + SIZE * i / (count-1), i ) );
        }

        for (float i = count-1; i >= 0; --i) {
            if (-SIZE/2 + SIZE * i / (count-1) < position.z) break;
            slice.push_back( glm::vec2( -SIZE/2 + SIZE * i / (count-1), i ) );
        }

enter image description here Although it doesn't look appealing, it worked as expected. For each frame, I ordered the slices according to the position of the camera.


Edit 4:

My alpha testing code

glEnable (GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.1);

These two lines are in the init() function.


FunnyFunkyBuggy
  • 495
  • 1
  • 7
  • 21

1 Answers1

2

Essentially I see two questions here:

Why choose the axis closest to the camera direction?

Because otherwise the slices might not even fill the whole screen area of the volume. Imagine that you render the slices parallel to the XY plane, but the camera looks along the X axis, i.e. 'from the side'. Then you will see the gaps between the slices. Like this:

Plates

Why render back to front?

This is not always necessary. In principle, if you enable alpha-test and depth-test then you may render in any order. However, volume rendering usually assumes support of partially-transparent data, in which case for the usual alpha-blending equations to work the geometry has to be rendered back to front.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
  • Thanks. The first answer is very informative. For the second part, I still get the weird results. I will post after editing the post. I enabled depth test and alpha testing. – FunnyFunkyBuggy Jun 06 '17 at 03:49
  • see Edit 2. I added my compiled program – FunnyFunkyBuggy Jun 06 '17 at 05:14
  • @FunnyFunkyBuggy: It looks as you didn't do the first thing (i.e. you don't render along the major axis). Besides that I have a feeling that you have face-culling enabled, which means that all slices that face away from the camera aren't rendered. – Yakov Galka Jun 06 '17 at 15:29
  • Indeed, I didn't render along the major axis(I will implement this later). The problem bother me the most is that I can't see the other textures, even they are not perpendicular to me. Theoretically, there should only be a few slices I can't see. BTW, I disabled face culling. – FunnyFunkyBuggy Jun 06 '17 at 16:31
  • @FunnyFunkyBuggy: can you show how you enabled the alpha test? There is simply something wrong with your setup. – Yakov Galka Jun 06 '17 at 18:33