OpenGL - how to draw background image?

Posts: 1
Joined: 2009.03
Post: #1
Hi, I just want to draw a square with a texture in it. I created an opengl project and added the Texture2D object from the TouchFighter example. I am able to get color gradient squares to display without a problem, following the code that the basic opengl project gives you. However, I can't get a texture to display on a square for the life of me. It is driving me crazy. Can anyone help? What is wrong with this code? If anyone has a simple example of how to load a texture and display it on a simple square that would be fantastic! I can't find this kind of example anywhere. My source code is:

Here is the code in EAGLView init that loads the texture:

// Load background texture
mBackgroundTexture = [[Texture2D alloc] initWithImagePath:@"backgroundWood01.png" sizeToFit:YES];
NSLog(@"mBackgroundTexture.description = %@", [mBackgroundTexture description]);

The NSLog above displays this in the debugger console when the app runs:

mBackgroundTexture.description = <Texture2D = 0057B080 | Name = 1 | Dimensions = 256x128 | Coordinates = (1.17, 1.72)>

Here is the entire EAGLView drawView function:
- (void)drawView {
    // Replace the implementation of this method to do your own custom drawing
    [EAGLContext setCurrentContext:context];
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glViewport(0, 0, backingWidth, backingHeight);
    glOrthof(-1.0f, 1.0f, -1.5f, 1.5f, -1.0f, 1.0f);
    glClearColor(0.5f, 0.0f, 0.0f, 1.0f);
    // Set to modify data in the model space.
    // Draw background texture manually
    const GLfloat squareVertices3[] = {
        -0.75f, -0.75f,
        0.75f,  -0.75f,
        -0.75f,  0.75f,
        0.75f,   0.75f,
    const GLfloat squareTexCoord3[] = {
        0.0f, 0.0f,
        1.0f,  0.0f,
        0.0f,  1.0f,
        1.0f, 1.0f,
    glVertexPointer(2, GL_FLOAT, 0, squareVertices3);
    glTexCoordPointer(2, GL_FLOAT, 0, squareTexCoord3);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    // Render everything.
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
Quote this message in a reply
Posts: 3,587
Joined: 2003.06
Post: #2
glEnableClientState(GL_TEXTURE_2D); should probably be glEnable(GL_TEXTURE_2D);
Quote this message in a reply
Post Reply