Simple texture to Cube

Member
Posts: 241
Joined: 2008.07
Post: #1
OpenGL ES for iPhone

If anyone can see where I'm screwing up, please comment.

So far I've been able to draw a cube with colored sides (same color on opposite sides). I've also got it rotating.

I am able to load the texture, I'm pretty sure it's loading just fine.

The problem comes when I attempt to draw the texture. Specifically, the problem appears to be when I change the parameters of glTexImage2D.

I'm loading a PNG file, so the parameters should be as such:

Code:
CGImageRef newImage = [UIImage imageNamed:fileName].CGImage;

width = CGImageGetWidth(newImage);
height = CGImageGetHeight(newImage);
...
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
...
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

If I change either of the format parameters to pretty much anything, it should break the texturing because PNG is a ARGB format, right?. Also, if I comment out the glTexParameter functions, it should also break it, right?.

Well, with this code just how it is, nothing draws at all. If I "break" the texturing by changing the format or commenting out the filtering functions, it draws the cube with only the colors.

Any help will be greatly appreciated. Thanks!!
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
You've heard of glGenTextures and glBindTexture, right?
Quote this message in a reply
Member
Posts: 241
Joined: 2008.07
Post: #3
Yes, that's in the ... part of the code.

Here, I'll just paste it all.

Code:
CGImageRef newImage;
GLubyte *imageData;
size_t width, height;
    
newImage = [UIImage imageNamed:fileName].CGImage;
    
width = CGImageGetWidth(newImage);
height = CGImageGetHeight(newImage);

if(newImage)
{
    imageData = (GLubyte *)malloc(width * height *4);
    memset(imageData, 0, sizeof(imageData));

        // GLuint textureObjects[1]; // member data
    glGenTextures(1, textureObjects);
    glBindTexture(GL_TEXTURE_2D, textureObjects[0]);

        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
                     GL_UNSIGNED_BYTE, imageData);
        
    free(imageData);
    imageData = 0;
    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    glEnable(GL_TEXTURE_2D);
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_BLEND);
}
The entire block of memory for imageData is always 0. That's a clue but I don't know what to do about it. Sad
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
You need to draw your CGImage into imageData using a CGBitmapContext. There's Apple sample code that shows how to do this; why not follow that?
Quote this message in a reply
Member
Posts: 241
Joined: 2008.07
Post: #5
I got it, thank you for your help OneSadCookie.
Quote this message in a reply
Apprentice
Posts: 16
Joined: 2008.10
Post: #6
bmantzey Wrote:I got it, thank you for your help OneSadCookie.

Would you mind posting your code? I'm trying to display a cube and am having trouble converting NEHE and other samples from C++ to Objective-C.

Thanks!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL Cube drawing: VBOs, instanced geometry, octrees Symbol$ 5 9,093 Dec 23, 2011 01:43 PM
Last Post: Skorche
  Simple 3D Game Engine(well not that simple) geoface 9 9,003 Apr 14, 2010 04:08 AM
Last Post: sio2interactive
  Trying to render a cube kendric 2 2,616 Oct 7, 2009 11:46 AM
Last Post: kendric
  Help : Rubics Cube Iphone blueMac 4 3,248 Jun 24, 2009 10:29 AM
Last Post: MattDiamond