Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I need to draw inside a buffer and then upload it as texture to OpenGL. So basicaly unsigned char* buffer and then call to glTexImage2D().

I will be using Marmalade mobile SDK and OpenGL ES 1.0.

share|improve this question

4 Answers 4

You're looking for a frame buffer object (FBO). If you're buffer generating doesn't need anything too complex, you can just do as you said..

share|improve this answer
    
+1 This is the way to go since the texture can be generated programmatically with a fragment shader. –  labotsirc Sep 30 '12 at 14:29
1  
@labotsirc Though, the OP cannot use any fragment shaders, since he uses ES 1.0. –  Christian Rau Oct 1 '12 at 12:50
    
@Christian Rau you are right thanks for pointing it out. –  labotsirc Oct 5 '12 at 17:53

In addition to DevilWithin's answer using FBOs, you can also use glCopyTex(Sub)Image2D if FBOs are not supported (which may be the case on an ES 1.0 device, don't know). This invloves a copy, but only a single GPU-GPU copy, since it copies the current framebuffer directly into the texture. But by any means don't do a glReadPixels followed by a glTexImage2D.

share|improve this answer

This function can create 4 different OpenGL textures (red, green, blue, or chessboard) programmatically, of size VIEW_TEX_SIZE x VIEW_TEX_SIZE. It returns the texture ID.

GLuint colourTexture(int colour)
{
    GLubyte image[VIEW_TEX_SIZE][VIEW_TEX_SIZE][3]; // RGB storage

    for (int i = 0; i < VIEW_TEX_SIZE; i++)
    {
        for (int j = 0; j < VIEW_TEX_SIZE; j++)
        {
            switch(colour)
            {
                case 0:
                    image[i][j][0]  = (GLubyte)255;
                    image[i][j][1]  = (GLubyte)0;
                    image[i][j][2]  = (GLubyte)0;
                    break;
                case 1:
                    image[i][j][0]  = (GLubyte)0;
                    image[i][j][1]  = (GLubyte)255;
                    image[i][j][2]  = (GLubyte)0;
                    break;
                case 2:
                    image[i][j][0]  = (GLubyte)0;
                    image[i][j][1]  = (GLubyte)0;
                    image[i][j][2]  = (GLubyte)255;
                    break;
                case 3:
                    int c = ((((i & 0x8) == 0) ^ ((j & 0x8)) == 0))*255;
                    image[i][j][0]  = (GLubyte)c;
                    image[i][j][1]  = (GLubyte)c;
                    image[i][j][2]  = (GLubyte)c;
                    image[i][j][3]  = (GLubyte)255;
                    break;
            }
        }
    }

    GLuint texName;
    glGenTextures(1, &texName);
    glBindTexture(GL_TEXTURE_2D, texName);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, VIEW_TEX_SIZE, VIEW_TEX_SIZE, 0, GL_RGB, GL_UNSIGNED_BYTE, image);

    return texName;
}
share|improve this answer

Marmalade has exactly that example

c:\Marmalade\6.0.6\examples\IwGL\IwGLExt\ 

It implements CFBO class that is used to render in buffer then it uses FBO's texture for further rendering.

Also it checks if FBO extension is available if there is not FBO extension then you get red square texture 1x1 pixel size.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.