سؤال

i searched for the answer but i still didn't find a understandable answer, so i'm asking how to calculate the value at depth buffer.

In https://en.wikipedia.org/wiki/Z-buffering here is the formula as image: enter image description here

and here it is written by me:
z' = (2^d -1 ) * ((far + near)/(2 * (far - near) + (1 / z) * (-far * near) / (far - near) + 1/2) when d is the depth of the z-buffer (24 bits at my case) and z is the z value of the vertex.

i wrote a simple code and tried to run it and read the depth buffer value and calculate it, but i receive deifferent answers.

my code example:

/* init */
#define CUBE_SIDE_SIZE 0.25
glViewport(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); 
glOrtho(-1, 1, -1, 1, -1, 1);
/* draw */
glEnable(GL_DEPTH_TEST);
glBegin(GL_QUADS);
glColor3f(1, 0, 0);
glVertex3f(-CUBE_SIDE_SIZE, -CUBE_SIDE_SIZE, -CUBE_SIDE_SIZE);
glVertex3f(-CUBE_SIDE_SIZE, CUBE_SIDE_SIZE, -CUBE_SIDE_SIZE);
glVertex3f(CUBE_SIDE_SIZE, CUBE_SIDE_SIZE, -CUBE_SIDE_SIZE);
glVertex3f(CUBE_SIDE_SIZE, -CUBE_SIDE_SIZE, -CUBE_SIDE_SIZE);
glEnd();

I read from the depth buffer 0.375 value. How can i get this value by calculation?

Thanks.

هل كانت مفيدة؟

المحلول

nearValue is -1, farValue is +1. Your cube is at -0.25. So the distance from the near plane is 0.75. The z values are scaled to the depth range, which defaults to [0,1]. Since farValue-nearValue = 2, that means the depth value is 0.75/2 = 0.375.

Also, you posted a formula for perspective projection, but you use an orthographic projection.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top