Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble Displaying Textures OpenGL ES 1.1
    primarykey
    data
    text
    <p>I'm working on a simple little game for the iPhone, and I'd like to use textures, however I can't quite seem to get it working...</p> <p>After some research I found <a href="http://mobile.dzone.com/news/loading-images-opengl-iphone" rel="nofollow">this page</a> and <a href="http://iphonedevelopment.blogspot.com/2009/05/opengl-es-from-ground-up-part-6_25.html" rel="nofollow">this site</a>. Both are great references, and taught me a little bit about textures, however, after loading a texture using either function I can't get the texture displayed, here's what my code looks like:</p> <p><strong>Very Simple Texture Display Function</strong> (not working)</p> <pre><code>void drawTexture(GLuint texture, float x, float y, float w, float h) { glBindTexture(GL_TEXTURE_2D,texture); GLfloat box[] = {x,y+h, x+w,y+h, x,y, x+w,y}; GLfloat tex[] = {0,0, 1,0, 1,1, 0,1}; glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glVertexPointer(2, GL_FLOAT, 0,box); glTexCoordPointer(2, GL_FLOAT, 0, tex); glDrawArrays(GL_TRIANGLE_STRIP,0,4); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); } </code></pre> <p>Normally, I'd not create an array every single frame only to display an image, but this is just an example. When I run this function, I get nothing. Blank- no image, nothing (unless of course I'd previously enabled a color array and hadn't disabled it afterwards)</p> <p><strong>Second Simple Display Function</strong> (this one uses a quick little class)</p> <pre><code>void draw_rect(RectObject* robj){ glVertexPointer(2, GL_FLOAT, 0, [robj vertices]); glEnableClientState(GL_VERTEX_ARRAY); glColorPointer(4, GL_UNSIGNED_BYTE, 0, [robj colors]); glEnableClientState(GL_COLOR_ARRAY); if ([robj texture] != -1){ glEnable(GL_TEXTURE_COORD_ARRAY); glEnable(GL_TEXTURE_2D); glClientActiveTexture([robj texture]); glTexCoordPointer(2, GL_FLOAT, 0, defaultTexCoord); glBindTexture(GL_TEXTURE_2D, [robj texture]); } glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDisable(GL_TEXTURE_2D); glDisable(GL_TEXTURE_COORD_ARRAY); } </code></pre> <p>This function on the other hand, does change the display, instead of outputting the texture however it outputs a black square...</p> <p><strong>Setup Background</strong></p> <p>In my init function I'm calling </p> <pre><code>glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glBlendFunc(GL_ONE, GL_SRC_COLOR); </code></pre> <p><strong>Two LONG Texture Loading Functions</strong></p> <pre><code>struct Texture2D LoadImage(NSString* path) { struct Texture2D tex; tex.texture = -1; // Id for texture GLuint texture; // Generate textures glGenTextures(1, &amp;texture); // Bind it glBindTexture(GL_TEXTURE_2D, texture); // Set a few parameters to handle drawing the image // at lower and higher sizes than original glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); //NSString *path = [[NSString alloc] initWithUTF8String:imagefile.c_str()]; path = [[NSBundle mainBundle] pathForResource:path ofType:@""]; NSData *texData = [[NSData alloc] initWithContentsOfFile:path]; UIImage *image = [[UIImage alloc] initWithData:texData]; if (image == nil) return tex; // Get Image size GLuint width = CGImageGetWidth(image.CGImage); GLuint height = CGImageGetHeight(image.CGImage); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); // Allocate memory for image void *imageData = malloc( height * width * 4 ); CGContextRef imgcontext = CGBitmapContextCreate( imageData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big ); CGColorSpaceRelease( colorSpace ); CGContextClearRect( imgcontext, CGRectMake( 0, 0, width, height ) ); CGContextTranslateCTM( imgcontext, 0, height - height ); CGContextDrawImage( imgcontext, CGRectMake( 0, 0, width, height ), image.CGImage ); // Generate texture in opengl glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData); // Release context CGContextRelease(imgcontext); // Free Stuff free(imageData); [image release]; [texData release]; // Create and return texture tex.texture=texture; tex.width=width; tex.height=height; return tex; } GLuint makeTexture(NSString* path){ GLuint texture[1]={-1}; glGenTextures(1, texture); glBindTexture(GL_TEXTURE_2D, texture[0]); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); path = [[NSBundle mainBundle] pathForResource:path ofType:@"png"]; NSData *texData = [[NSData alloc] initWithContentsOfFile:path]; UIImage *image = [[UIImage alloc] initWithData:texData]; if (image == nil) NSLog(@"Do real error checking here"); GLuint width = CGImageGetWidth(image.CGImage); GLuint height = CGImageGetHeight(image.CGImage); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); void *imageData = malloc( height * width * 4 ); CGContextRef context = CGBitmapContextCreate( imageData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big ); CGColorSpaceRelease( colorSpace ); CGContextClearRect( context, CGRectMake( 0, 0, width, height ) ); CGContextTranslateCTM( context, 0, height - height ); CGContextDrawImage( context, CGRectMake( 0, 0, width, height ), image.CGImage ); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData); CGContextRelease(context); free(imageData); [image release]; [texData release]; return texture[0]; } </code></pre> <p>If you could point me in the right direction it would be much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload