2
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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..

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

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.

\$\endgroup\$
1
\$\begingroup\$

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;
}
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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