views:

41

answers:

1

Hello there. I'm trying to get a bunch of stuff done in OpenGLES, but here's where I'm starting - drawing stuff to a texture, then displaying that texture.

I create some buffers:

glGenFramebuffersOES(1, &frameBuffer);
glGenRenderbuffersOES(1, &colorRenderbuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, frameBuffer);

And then I set myself up to draw some lines to a framebuffer.

glViewport(0, 0, backingWidth, backingHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glVertexPointer(2, GL_FLOAT, 0, vertices);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, colours);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_LINES, 0, 2000);

I then try to copy this into a freshly made texture.

glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 128, 128, 0, GL_RGBA, GL_UNSIGNED_BYTE, nil);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 128, 128, 128, 128);

And then display the texture.

GLfloat vertices[] = {-1,1,1,1,-1,-1,1,-1};
GLfloat texcoords[] = { 0,1,
    1,1,
0,0,
1,0,};

glColor4f(0.5, 0.5, 0.5, 0.1);
glVertexPointer(2, GL_FLOAT, 0, vertices);
glEnableClientState(GL_VERTEX_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
GL_RENDERBUFFER_OES, colorRenderbuffer);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
[context presentRenderbuffer:GL_FRAMEBUFFER_OES];

What I get is this - my lines displayed at the 'back' of the screen (the way I draw the Triangle Fan means that I can see behind it) which means my lines are being drawn to the renderbuffer, even though I never bind it. On top of that, I get my texture drawn as some weird colours, rather than my lines as I expected.

So I've got two problems here:

Why are my lines still appearing at the back of the screen if I'm not writing them to a renderbuffer?

Am I writing to the texture correctly? If so, why isn't it displaying?

+1  A: 

You need to use a Framebuffer Object (FBO) to draw directly into a texture, the following tutorials should help you:

http://www.flashbang.se/archives/48

Note that those tutorials are for OpenGL, for OpenGL ES is the same, but the EXT is changed to OES in most methods and enumerations.

Also note that binding a framebuffer (glBindFramebufferEXT/OES) makes all rendering go to that framebuffer (and it's attached textures/renderbuffers), so you need to unbind the framebuffer first before drawing again to the screen, which is made by:

glBindFramebuffer(GL_FRAMEBUFFER, 0);
Matias Valdenegro
I've taken a look at that tutorial. It makes a lot of sense, although the code he supplies is not yet working. I'll persevere, thanks for now!
mtc06