Opengl ES 2.0,FBO和片段着色器

时间:2011-12-09 09:50:23

标签: opengl-es render textures fractals fbo

这是我的渲染循环:

  1. 绑定自定义FBO
  2. 将纹理绑定(以前与FBO关联为COLOR_ATTACHMENT0)
  3. 使用自定义片段着色器渲染,该着色器根据分形算法选择片段颜色。 (如果算法未使用片段,则为其指定黑色)
  4. 重新绑定窗口提供的framebuffer和renderbuffer。 (在ios 5上,这是[view bindDrawable]方法。
  5. 将屏幕清除为白色。
  6. 将fbo纹理渲染到一个比窗口本身小得多的帧中。
  7. 预期结果: 分形应出现在较小的框架中。框架应该有黑色背景。屏幕的其余部分应为白色。

    目前的结果: 整个屏幕被分形所占据,好像我渲染到提供fbo的窗口,以及我自定义的fbo /纹理。

    我真的不知道我做错了什么,所以我会感激任何帮助。

    编辑:

    Fragment Shader:

    void main()
    {
       highp vec2 pix = gl_FragCoord.xy;
       lowp vec4 color = vec4(0.0,0.0,0.0,0.0);
    
       //determine if fragment is part of the fractal using the algorithm
       //if yes, change the value of the color vec4
    
       gl_FragColor = color;
    }
    

    FBO初始化:

        //First create and bind the frame buffer
    glGenFramebuffers(1, &frameBuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
    
    //Create a texture
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    
    //Attach the texture to the framebuffer
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
    
    GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);    
    if (status != GL_FRAMEBUFFER_COMPLETE)  {
        printf("\n Incomplete Frame Buffer!");
    }
    

    渲染循环:

    {
    glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, texture);
    glViewport(0, 0, width, height);
    
    //Call the fractal render, basically a plane of 4 vertices happening through GL_TRIANGLE_STRIP, but that calls the fragment shader above.
    
    [self.view bindDrawable];
    glClearColor(1.0,1.0,1.0,1.0);
    
    matrix4x4 mvp = multiplyMatrices(projectionMatrix, modelView);
    
    glUseProgram(shaderId);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glVertexAttribPointer(positionLocation, 4, GL_FLOAT, GL_FALSE, 0, NULL);   
    glBindBuffer(GL_ARRAY_BUFFER, texelBuffer);
    glVertexAttribPointer(texelLocation, 2, GL_FLOAT, GL_FALSE, 0, NULL);
    
    glUniformMatrix4fv(mvpLocation, 1, 0, mvp.val);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, textureId);
    glUniform1i(textureLocation, 0);
    
    glDrawArrays(GL_TRIANGLE_STRIP, 0, tVertices);
    }
    

    我希望这会有所帮助。如果您想了解我正在做什么的更多信息,请告诉我。

    我也发现了一些非常奇怪的事情。

    如果在绑定FBO后,我尝试

    glClear(GL_COLOR_BUFFER_BIT);
    glClearColor(1.0,0.0,0.0,0.5); 
    

    ...它是窗口提供的帧缓冲区,实际上发生了清除。

    谢谢你们的帮助。

0 个答案:

没有答案