Sunday 15 January 2012

c++ - Calculating z-buffer from glm::project output -


I want to calculate the z-buffer of an object's position with the output of glm :: project. The code below has the calculation of z-buffer.

What I have tried

  int windowWidth = 800; Int Windowsite = 600; Float positions [] = {-42.5806f, 27.8838f, 49.9729f} // example point glm :: mat4 model; Glm :: mat4 view; Glm :: mat4 proj; See = glm :: lookAt (glm :: vec3 (0.0f, 0.0f, 2.0f), glm :: vec3 (0.0f, 0.0f, 0.0f), glm :: vec3 (0.0f, 1.0f, 0.0f ))); Proj = glm :: perspective (45.0 F, aspect, 0.1 F, 10.0F); // Receive screen coordinates from an object point glm :: vec3 screen quotes = glm :: project (glm :: vec3 (positions [0], position [1], positions [2]), see * model, proj, glm :: Vec4 (0, 0, WindowWind, windowHight)); Calculation of z-buffer zFar = 10; Int zNear = 0.1; Boat zBufferValue = (zFar + zNear) / (zFar-zNear) + (1 / screenCoords.z) * ((-2 * zFar * zNear) / (zFar-zNear)); Value of zBufferValue 1 does not matter how I rotate or use my model. According to the wiki page, price must be between -1 (near aircraft) and 1 (distant aircraft).  

Am I doing wrong in my calculations?

Your final line of code is redundant, calculating depth during the launch result (and divided in later perspective) Is performed. What is basically glm :: project is this:

  // P: Projection matrix // MV: modelview matrix // v: to change the screen space to vec4 Top results for = P * MV * VC4 (V, 1.0F); Results / = results. W; // perspective split // Change the X / Y / Z from the normal device device to the screen codes ... result.z = (result.z + 1.0f) * 0.5F; // ... back to vec3 (results);   

This normalized device location converts X / Y coordinate from [(- 1, -1), (1, 1)] screen space [(0, 0), (viewport_width, viewport_heat)] But since you are only related to the depth buffer, I have stepped off this step.

So ignoring your previous 3 lines, your code should have a deeper buffer value equal to what you get in screen cores. .

Of course, depending on the actual bits stored on the graphics card, depending on the depth of your buffer and how to install OpenGL to use it, especially if you are using custom above The value of will differ from those stored in depth buffers.

No comments:

Post a Comment