Note that there are some explanatory texts on larger screens.

plurals
  1. POopengl shader problems with mrt
    primarykey
    data
    text
    <p>I have two textures with same size attached to one fbo, and I want to render another texture into one of the attached textures, and I want to render a blue shape of the figure of the input texture to the other fbo attached texture.</p> <p>the (updated) vertex shader:</p> <pre><code>#version 330 in vec2 coord; out vec2 f_coord; void main() { gl_Position = vec4(coord, 0.0, 1.0); f_coord = (1.0+coord)/2.0; } </code></pre> <p>the (updated) fragment shader:</p> <pre><code>#version 330 uniform sampler2D my_texture; in vec2 f_coord; out vec4 FragData[2]; void main() { if(texture(my_texture,f_coord).a == 0.0) discard; FragData[0]=texture(my_texture,f_coord); FragData[1]=vec4(0.5, 0.5, 1.0, 1.0); } </code></pre> <p>With this fragment shader everything works fine. The texture and the blue stamp of the texture contures are rendered properly.</p> <p>Here is a screenshot: <a href="http://s14.directupload.net/file/d/3487/kvkqdbl7_png.htm" rel="nofollow">http://s14.directupload.net/file/d/3487/kvkqdbl7_png.htm</a></p> <p>But if I want to render the texture into gl_FragData[1] and the blue conture stamp into gl_FragData[0]</p> <pre><code>#version 330 uniform sampler2D my_texture; in vec2 f_coord; out vec4 FragData[2]; void main() { if(texture(my_texture,f_coord).a == 0.0) discard; FragData[0]=vec4(0.5, 0.5, 1.0, 1.0); FragData[1]=texture(my_texture,f_coord); } </code></pre> <p>than I get two totally blue textures. The strange thing is: Both target textures are totally blue, but I have set the glViewport just to a part of the texture.</p> <p>Here a screenshot: <a href="http://s1.directupload.net/file/d/3487/9loqibdc_png.htm" rel="nofollow">http://s1.directupload.net/file/d/3487/9loqibdc_png.htm</a></p> <p>The fbo attachments have the half screen size</p> <pre><code> glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, glutGet(GLUT_WINDOW_WIDTH)/2, glutGet(GLUT_WINDOW_HEIGHT), 0, GL_RGBA, GL_BYTE, 0); </code></pre> <p>So I can render both textures to the screen for checking.</p> <p>This is how I created the framebufferobject</p> <pre><code> void init_fbo() { glGenTextures(1, &amp;fbo.texture0); glBindTexture(GL_TEXTURE_2D, fbo.texture0); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, glutGet(GLUT_WINDOW_WIDTH)/2, glutGet(GLUT_WINDOW_HEIGHT), 0, GL_RGBA, GL_BYTE, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glGenTextures(1, &amp;fbo.texture1); glBindTexture(GL_TEXTURE_2D, fbo.texture1); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, glutGet(GLUT_WINDOW_WIDTH)/2, glutGet(GLUT_WINDOW_HEIGHT), 0, GL_RGBA, GL_BYTE, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glGenFramebuffers(1, &amp;fbo.fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo.fbo); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbo.texture0, 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, fbo.texture1, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0); } </code></pre> <p>I use those global struct variables</p> <pre><code> struct { GLuint fbo; GLuint texture0; GLuint texture1; } fbo; struct { GLuint program; GLint uni_texture; GLint att_coord; } shader_fbo; struct { GLuint program; GLint uni_texture; GLint att_coord; } shader_screen; </code></pre> <p>And this is my render function</p> <pre><code> void render() { GLenum buffers[2]; GLfloat vertices[] = { -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0 }; GLubyte indices[] = { 0, 1, 2, 1, 2, 3 }; /*render to mrt fbo*/ glUseProgram(shader_fbo.program); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture.texID); glUniform1i(shader_fbo.uni_texture, 0); glEnableVertexAttribArray(shader_fbo.att_coord); glVertexAttribPointer(shader_fbo.att_coord, 2, GL_FLOAT, GL_FALSE, 0, vertices); glBindFramebuffer(GL_FRAMEBUFFER, fbo.fbo); buffers[0] = GL_COLOR_ATTACHMENT0; buffers[1] = GL_COLOR_ATTACHMENT1; glDrawBuffers(2, buffers); glViewport(0, 0, texture.width, texture.height); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture.texID); glUniform1i(shader_fbo.uni_texture, 0); glClear(GL_COLOR_BUFFER_BIT); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices); glDisableVertexAttribArray(shader_fbo.att_coord); glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindTexture(GL_TEXTURE_2D, 0); /*render both textures now to screen*/ glUseProgram(shader_screen.program); glEnableVertexAttribArray(shader_screen.att_coord); glVertexAttribPointer(shader_screen.att_coord, 2, GL_FLOAT, GL_FALSE, 0, vertices); /*render the first texture to the left side of the screen*/ glViewport(0, 0, glutGet(GLUT_WINDOW_WIDTH)/2, glutGet(GLUT_WINDOW_HEIGHT)); glClear(GL_COLOR_BUFFER_BIT); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, fbo.texture0); glUniform1i(shader_screen.uni_texture, 0); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices); /*render the second texture to the right side of the screen*/ glViewport(glutGet(GLUT_WINDOW_WIDTH)/2, 0, glutGet(GLUT_WINDOW_WIDTH)/2, glutGet(GLUT_WINDOW_HEIGHT)); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, fbo.texture1); glUniform1i(shader_screen.uni_texture, 0); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices); glutSwapBuffers(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload