Thursday, 15 May 2014

opengl - custom zbuffer in a 2d game / reading pixels from a frame buffer in a fragment shader (glsl) -



opengl - custom zbuffer in a 2d game / reading pixels from a frame buffer in a fragment shader (glsl) -

i'm writing 2d (actually 2.5d) isometric game in opengl. circumvent sorting tiles can quite complicated in cases i'm trying simulate kind of depth buffer. every tile have 2 images, 1 color info , 1 depth information.

image1: http://dl.dropbox.com/u/91457585/depth_buffer_test.png

as first approach created these pictures. thought draw them on top of each other , utilize shader depth test. in case doesn't create much sense know, first approach see if it's technically possible.

so problem!

below see code of shader. supposed compare depth info of framebuffer 1 of object. not seem work , read somewhere not possible way did.

listing:

#version 130 uniform sampler2d colormap; uniform sampler2d zmap; uniform float level; out vec4 gs_fragcolor[ 2 ]; void main(void) { float z = texture2d( zmap, gl_texcoord[0].st ).r; if ( z > gs_fragcolor[ 1 ].r ){ gs_fragcolor[ 1 ] = vec4( z, 0, 0, 1 ); gs_fragcolor[ 0 ] = texture2d( colormap, gl_texcoord[0].st ).rgba; } }

so there improve method simulate depth buffer? maybe writing real depth buffer?

there (complicated , perchance slow) ways simulate depth buffer, don't see why should need that. recommend using regular opengl depth test: each fragment of tile, corresponding depth value in texture , write gl_fragdepth. if have depth test enabled in host code (glenable( gl_depth_test)), opengl work you.

opengl glsl depth-buffer 2d-games

No comments:

Post a Comment