OpenGL - how to draw background image?
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:
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:
Code:
- (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);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(-1.0f, 1.0f, -1.5f, 1.5f, -1.0f, 1.0f);
glClearColor(0.5f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Set to modify data in the model space.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// 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,
};
glEnableClientState(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture(GL_TEXTURE_2D, mBackgroundTexture.name);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glVertexPointer(2, GL_FLOAT, 0, squareVertices3);
glTexCoordPointer(2, GL_FLOAT, 0, squareTexCoord3);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
// Render everything.
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
glEnableClientState(GL_TEXTURE_2D); should probably be glEnable(GL_TEXTURE_2D);
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| cocos2d draw polys over background | aerospaceman | 4 | 6,281 |
Dec 29, 2010 11:56 AM Last Post: Skorche |
|
| stupid question about IB - background image | aerospaceman | 2 | 3,019 |
Apr 29, 2010 07:17 PM Last Post: aerospaceman |
|
| [Problem] Loading image with OpenGL orthogonal view pixel-to-pixel | Lexis | 2 | 3,255 |
Aug 21, 2009 01:35 AM Last Post: Lexis |
|
| OpenGL ES: Doing a background distortion | soulstorm | 2 | 3,023 |
May 14, 2009 04:00 PM Last Post: soulstorm |
|
| Background Pic, Rotate, and Swap Image? | kahanejosh | 1 | 2,184 |
Apr 28, 2009 11:10 AM Last Post: Malarkey |
|

