c - opengl render to texture just see a black region -


i having trouble in opengl, making render texture example work. @ initialization, generate texture 'randtex' random values of green , black. if render texture directly window (mapped quad) works well. this: displaying texture directly

but if render 'randtex' texture 'tex' attached framebuffer object, rendering 'tex' on screen gives me black image on fbo's blue background , know should give me original texture on blue background. in other words, getting black region should green , black texture

vertex shader display (display_shaderp).

#version 420 in vec4 pos; in vec2 tex_coord; out vec2 vtex_coord; uniform mat4 projection; uniform mat4 modelview; void main(){ gl_position = projection * modelview * pos;     vtex_coord = tex_coord; } 

fragment shader display (display_shaderp)

#version 420 in vec2 vtex_coord; uniform sampler2d tex; out vec4 color; void main(){     color = texture2d(tex, vtex_coord);     //color = vec4(1.0f, 1.0f, 1.0f, 1.0f); } 

shader program compiles , links ok, no gl errors , framebuffer complete without errors too. rendering code:

glclearcolor(0.5, 0.5, 0.5, 1.0); glclear(gl_color_buffer_bit | gl_depth_buffer_bit); glviewport(0, 0, win_width, win_height); glmatrixmode(gl_projection); glloadidentity(); gluperspective(60.0f, (glfloat)win_width / (glfloat) win_height, 0.1f, 50.0f); glmatrixmode(gl_modelview); glloadidentity(); gltranslate3f(0.0f, 0.0f, -3.0f)  // render texture glbindframebuffer(gl_framebuffer, fbo); glviewport(0,0, win_width, win_height); glclearcolor(0.0, 0.0, 0.5, 1.0); glclear(gl_color_buffer_bit | gl_depth_buffer_bit); gluseprogram(fbo_shaderp); glfloat m_matrix[16], p_matrix[16]; glgetfloatv(gl_modelview_matrix, m_matrix);  glgetfloatv(gl_projection_matrix, p_matrix); gluniformmatrix4fv(glgetuniformlocation(fbo_shaderp, "modelview"),1,gl_false,m_matrix); gluniformmatrix4fv(glgetuniformlocation(fbo_shaderp, "projection"),1,gl_false,p_matrix); glactivetexture(gl_texture0); glbindtexture(gl_texture_2d, randtex); gluniform1i(glgetuniformlocation(fbo_shaderp, "tex"), randtex); gluint _p = glgetattriblocation(fbo_shaderp, "pos"); gluint _t = glgetattriblocation(fbo_shaderp, "tex_coord"); glvertexattribpointer(_p, 3, gl_float,  gl_false, 5 * sizeof(glfloat), 0); glvertexattribpointer(_t, 2, gl_float,  gl_false, 5 * sizeof(glfloat), (glvoid*)(sizeof(float)*3)); glenablevertexattribarray(_p); glenablevertexattribarray(_t); gldrawelements(gl_triangles, 6, gl_unsigned_byte, 0); gluseprogram(0);     glbindframebuffer(gl_framebuffer, 0);      // render window glbindframebuffer(gl_framebuffer, 0); glviewport(0,0, win_width, win_height); glclearcolor(0.5, 0.5, 0.5, 1.0); glclear(gl_color_buffer_bit | gl_depth_buffer_bit); gluseprogram(display_shaderp); gluniformmatrix4fv(glgetuniformlocation(display_shaderp,"modelview"),1,gl_false,m_matrix); gluniformmatrix4fv(glgetuniformlocation(display_shaderp,"projection"),1,gl_false,p_matrix); glactivetexture(gl_texture0);  glbindtexture(gl_texture_2d, tex);  gluniform1i(glgetuniformlocation(display_shaderp, "tex"), 0); gluint _p = glgetattriblocation(display_shaderp, "pos"); gluint _t = glgetattriblocation(display_shaderp, "tex_coord"); glvertexattribpointer ( _p, 3, gl_float, gl_false, 5 * sizeof(glfloat), 0 ); glvertexattribpointer ( _t, 2, gl_float, gl_false, 5 * sizeof(glfloat), (glvoid*) (sizeof(float) * 3) ); glenablevertexattribarray(_p); glenablevertexattribarray(_t); gldrawelements(gl_triangles, 6, gl_unsigned_byte, 0); gluseprogram(0); 

and code create textures , framebuffer

int = 0; // create random texture 'randtex' glgentextures(1, &randtex); glbindtexture(gl_texture_2d, randtex); gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_linear); gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_linear); gltexparameterf(gl_texture_2d, gl_texture_wrap_s, gl_clamp_to_edge); gltexparameterf(gl_texture_2d, gl_texture_wrap_t, gl_clamp_to_edge); // random data glubyte* data = (glubyte *) malloc(width*height*4*sizeof(glubyte)); glubyte val; (i = 0; < width * height * 4; i+=4){     if ((double)rand()/(double)rand_max > 0.8)         val = 255;     else         val = 0;     data[i]   = 0;     data[i+1] = val;     data[i+2] = 0;     data[i+3] = 255; } glteximage2d(gl_texture_2d, 0, gl_rgba, width, height, 0, gl_rgba,gl_unsigned_byte, data); // create empty texture 'tex' glgentextures(1, &tex); glbindtexture(gl_texture_2d, tex); gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_linear); gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_linear); gltexparameterf(gl_texture_2d, gl_texture_wrap_s, gl_clamp_to_edge); gltexparameterf(gl_texture_2d, gl_texture_wrap_t, gl_clamp_to_edge); glteximage2d(gl_texture_2d, 0, gl_rgba, width, height, 0, gl_rgba,gl_unsigned_byte, 0); // create framebuffer , attach 'tex' glgenframebuffers(1, &fbo); glbindframebuffer(gl_framebuffer, fbo); glframebuffertexture2d(gl_framebuffer,gl_color_attachment0,gl_texture_2d, tex, 0); glenum status; if ((status = glcheckframebufferstatus(gl_framebuffer)) != gl_framebuffer_complete)     fprintf(stderr, "glcheckframebufferstatus: error %p", status); 

shaders render texture (fbo_shaderp)

render texture vertex shader

in vec4 pos; in vec2 tex_coord; out vec2 vtex_coord; uniform mat4 projection; uniform mat4 modelview; void main(){     gl_position = projection * modelview * pos;     vtex_coord = tex_coord; } 

render texture fragment shader

#version 420 in vec2 vtex_coord; layout(location = 0) out vec4 color; uniform sampler2d tex;  void main(){ color = texture2d(tex, vtex_coord); //color = vec4(1.0f, 1.0f, 1.0f, 1.0f); } 

in last shader, if use commented line paint white , comment out texture one, white image opengl error right after rendering texture "opengl error: invalid value", confuses me more.

gluniform1i(glgetuniformlocation(fbo_shaderp, "tex"), randtex); 

you must not give id of texture, slot bind texture to. in case, should be

gluniform1i(glgetuniformlocation(fbo_shaderp, "tex"), 0); 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -