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)
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:
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?
- Enabled depth test
- Disabled back-face culling
- 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 ) );
}
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.