c++ - Return floats from fragmentshader -
for application need create depth-image of mesh. want image, each pixel tells distance between camera center , intersection point. choose task opengl, of computation can done on gpu instead of cpu.
here code vertex-shader. computes real-world coordinate of intersection point , stores coordinates in varying vec4
, have access in fragment-shader.
varying vec4 verpos; void main(void) { gl_position = ftransform(); verpos = gl_modelviewmatrix*gl_vertex; }
and here code fragment-shader. in fragment-shader use these coordinates compute eulidean distance origin (0,0,0) using pythagoras. access computed value on cpu, plan store distance color using gl_fragcolor
, , extract color values using glreadpixels(0, 0, width, height, gl_red, gl_float, &distances[0]);
varying vec4 verpos; void main() { float x = verpos.x; float y = verpos.y; float z = verpos.z; float depth = sqrt(x*x + y*y + z*z) / 5000.0; gl_fragcolor = vec4(depth, depth, depth, 1.0); }
the problem is, receive results in precision of 0.003921569
(=1/255
). resulting values contain instance 0.364705890, 0.364705890, 0.364705890, 0.364705890, 0.368627459, 0.368627459, 0.368627459
. of values in row same, , there jump of 1/255. somewhere inbetween gl_fragcolor
, glreadpixels
opengl converts floats 8 bits , afterwards converts them float.
how can extract distance-values float without loosing precision?
attach r32f or r16f texture fbo , render it. extract pixels glgetteximage2d()
glreadpixels()
.
Comments
Post a Comment