iOS Rendering a object to a texture

I'm currently trying to divide the OpenGL ES 2.0 drawing process onto two halves: the first half where I render an object of interest (ie a cube or triangle) to a framebuffer that has a texture attached to it, and the second half where I apply that texture onto the face of a shape drawn in another framebuffer (ie another cube or triangle).

I cleared the framebuffer binded to the texture with a green color, and have been able to get that color to appear onto a triangle that I've drawn in another framebuffer that has the main renderbuffer attached and that I call [context presentRenderbuffer: renderbuffer] on. However, no matter what I do I'm not able to additionally draw another shape into that texture after I've cleared it to a green background, and render that onto the shape I've drawn.

For some visual reference, currently I'm drawing a square to the screen in my main framebuffer, and then applying a texture that is supposed to have a green background plus a triangle in the middle, but all that I get is this green screen.

It has everything that I currently want, except there is no triangle that is also in the middle. Essentially, this should look like a big green square with a black triangle in the middle of it, where the green and the triangle all came from the texture (the square would have originally been black).

My texture drawing method and main framebuffer drawing methods are included below (without the setup code):

- (BOOL) loadModelToTexture: (GLuint*) tex {
    GLuint fb;
    GLenum status;

    glGenFramebuffers(1, &fb);

    // Set up the FBO with one texture attachment
    glBindFramebuffer(GL_FRAMEBUFFER, fb);
    glGenTextures(1, tex);
    glBindTexture(GL_TEXTURE_2D, *tex);

    NSLog(@"Error1: %x", glGetError());

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 128, 128, 0,
                 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                           GL_TEXTURE_2D, *tex, 0);

    status = glCheckFramebufferStatus(GL_FRAMEBUFFER);

    if (status != GL_FRAMEBUFFER_COMPLETE) {
        // Handle error here
        NSLog(@"Loading model to texture failed");
        return FALSE;
    }

    glClearColor(0.0f, 1.0f, 0.0f, 1.0f);  // Set color's clear-value to red
    glClearDepthf(1.0f);            // Set depth's clear-value to farthest
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glViewport(0, 0, self.frame.size.width*self.contentsScale, self.frame.size.height*self.contentsScale);

    NSLog(@"Error2: %x", glGetError());

    // Update attribute values.
    glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, vertices);
    glEnableVertexAttribArray(ATTRIB_VERTEX);
    NSLog(@"Error3: %x", glGetError());
    glDrawArrays(GL_TRIANGLES, 0, 3);

    return TRUE;
}

- (void) draw {
    [EAGLContext setCurrentContext:context];

    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);

    glClearColor(0.0f, 0.5f, 0.5f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glViewport(0, 0, self.frame.size.width*self.contentsScale, self.frame.size.height*self.contentsScale);

    // Use shader program.
    glUseProgram(program);

    // Update attribute values.
    glEnableVertexAttribArray(ATTRIB_VERTEX);
    glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, vertices);

    //for some reason this is unneeded, but I'm not sure why
    glVertexAttribPointer(ATTRIB_TEXTURE_COORD, 2, GL_FLOAT, 0, 0, texCoords);
    glEnableVertexAttribArray(ATTRIB_TEXTURE_COORD);

    glBindTexture(GL_TEXTURE_2D, textureName0);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glDrawArrays(GL_TRIANGLES, 3, 3);

    [context presentRenderbuffer:renderbuffer];
}

What other steps do I need to take to get it to draw correctly to the texture/apply correctly to the main framebuffer drawing?


没关系,事实证明我正在绘制我的三角形纹理,但三角形自动默认为与背景纹理相同的颜色,所以这是一个完全不同的问题。

链接地址: http://www.djcxy.com/p/33912.html

上一篇: 将像素渲染为纹理

下一篇: iOS将对象渲染到纹理