IPhone: OpenGL ES Texturing

I just began writing an iPhone game and I'm having difficulties displaying multiple textures.
Only the last texture I generate is appearing and all the textures I generated previously appear white.
Here is major rendering functions so far, is there anything that stands out as incorrect?
//Generates textures for a game object
- (void)GenerateTextures:(Hairball::Objects)Obj
CGImageRef spriteImage;
CGContextRef spriteContext;
GLubyte *spriteData;
size_t width, height;
GLuint *tempTex;
// Creates a Core Graphics image from an image file
switch (Obj)
case Hairball::PLAYER:
spriteImage = [UIImage imageNamed:@"Sprite.png"].CGImage;
tempTex = &(TextureArray[0]);
break;
case Hairball::BACKGROUND:
spriteImage = [UIImage imageNamed:@"BG1.png"].CGImage;
tempTex = &(TextureArray[1]);
break;
case Hairball::HUD:
spriteImage = [UIImage imageNamed:@"Icon.png"].CGImage;
tempTex = &(TextureArray[2]);
break;
default:
break;
// Get the width and height of the image
width = CGImageGetWidth(spriteImage);
height = CGImageGetHeight(spriteImage);
if(spriteImage)
// Allocated memory needed for the bitmap context
spriteData = (GLubyte *) malloc(width * height * 4);
// Uses the bitmatp creation function provided by the Core Graphics framework.
spriteContext = CGBitmapContextCreate(spriteData, width, height, 8, width * 4, CGImageGetColorSpace(spriteImage), kCGImageAlphaPremultipliedLast);
// After you create the context, you can draw the sprite image to the context.
CGContextDrawImage(spriteContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), spriteImage);
// You don't need the context at this point, so you need to release it to avoid memory leaks.
CGContextRelease(spriteContext);
// Use OpenGL ES to generate a name for the texture.
glGenTextures(1, tempTex);
// Bind the texture name.
glBindTexture(GLTEXTURE2D, *tempTex);
// Speidfy a 2D texture image, provideing the a pointer to the image data in memory
glTexImage2D(GLTEXTURE2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GLUNSIGNEDBYTE, spriteData);
// Release the image data
free(spriteData);
//Inits OpenGl for drawing
- (void)setupView
//Creates object Manager to handle
ObjectMgr = new ObjectManager();
//Create Objects
GameObject* Object1 = new GameObject(Hairball::BACKGROUND, 0.0f, 0.0f, 0.0f, 0.0f, 3.2f, 3.2f);
ObjectMgr->AddToList(Object1);
GameObject* Object2 = new GameObject(Hairball::PLAYER, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f);
ObjectMgr->AddToList(Object2);
//Generate all textures
[self GenerateTextures:Hairball::PLAYER];
[self GenerateTextures:Hairball::BACKGROUND];
//For each obj assign a texture
std::list<GameObject*>::iterator Object = (ObjectMgr->mObjectList.begin());
std::list<GameObject*>::iterator End = (ObjectMgr->mObjectList.end());
//For each game object
while(Object != End)
//Apply texture to each object
switch ((*Object)->mObjectType)
case Hairball::PLAYER: (*Object)->mSpriteTexture = TextureArray[0]; break;
case Hairball::BACKGROUND: (*Object)->mSpriteTexture = TextureArray[1]; break;
case Hairball::HUD: (*Object)->mSpriteTexture = TextureArray[2]; break;
default:
break;
++Object;
// Clears the view with grey
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
// Set the texture parameters to use a minifying filter and a linear filer (weighted average)
glTexParameteri(GLTEXTURE2D, GLTEXTURE_MINFILTER, GL_LINEAR);
// Enable use of the texture
glEnable(GLTEXTURE2D);
// Set a blending function to use
glBlendFunc(GL_ONE, GLONE_MINUS_SRCALPHA);
// Enable blending
glEnable(GL_BLEND);
- (void)drawView
[EAGLContext setCurrentContext:context];
glBindFramebufferOES(GLFRAMEBUFFEROES, viewFramebuffer);
glViewport(0, 0, backingWidth, backingHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(-1.0f, 1.0f, -1.5f, 1.5f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
//Clear the backbuffer
glClear(GLCOLOR_BUFFERBIT);
std::list<GameObject*>::iterator Object = (ObjectMgr->mObjectList.begin());
std::list<GameObject*>::iterator End = (ObjectMgr->mObjectList.end());
//For each game object
while(Object != End)
// Bind the texture name.
glBindTexture(GLTEXTURE2D, (*Object)->mSpriteTexture);
//apply transformations
glPushMatrix();
glTranslatef((*Object)->mPosition.x, (*Object)->mPosition.y, (*Object)->mPosition.z);
glRotatef((*Object)->mRotation, 0.0f, 0.0f, 1.0f);
(*Object)->mRotation += 0.5f; //Rotate
//Get current sprites verticies
glVertexPointer(2, GL_FLOAT, 0, (*Object)->mSpriteVertices);
glEnableClientState(GLVERTEXARRAY);
//Get current sprites tex coords
glTexCoordPointer(2, GL_SHORT, 0, (*Object)->mSpriteTexcoords);
glEnableClientState(GLTEXTURE_COORDARRAY);
//Render the vertex array
glDrawArrays(GLTRIANGLESTRIP, 0, 4);
//pop the transformation matrix
glPopMatrix();
++Object;
glBindRenderbufferOES(GLRENDERBUFFEROES, viewRenderbuffer);
[context presentRenderbuffer:GLRENDERBUFFEROES];
}

The value generated by glGenTextures() in (*Object)->mSpriteTexture for the BACKGROUND object is 1 and for the sprite object it is 2. Which appear to be correct values?
If I exchange:
self GenerateTextures:Hairball::PLAYER;
self GenerateTextures:Hairball::BACKGROUND;
To
self GenerateTextures:Hairball::BACKGROUND;
self GenerateTextures:Hairball::PLAYER;
then my scene goes from this:
http://img207.imageshack.us/img207/8229/picture1nf6.png
to this:
http://img253.imageshack.us/img253/5282/picture2gr8.png
So both textures draw, just not at the same time?
Oh, here it is formatted better:
//Generates textures for a game object
- (void)GenerateTextures:(Hairball::Objects)Obj
CGImageRef spriteImage;
CGContextRef spriteContext;
GLubyte *spriteData;
size_t width, height;
int texIndex = 0;
// Creates a Core Graphics image from an image file
switch (Obj)
case Hairball::PLAYER:
spriteImage = [UIImage imageNamed:@"Sprite.png"].CGImage;
texIndex = 0;
break;
case Hairball::BACKGROUND:
spriteImage = [UIImage imageNamed:@"BG1.png"].CGImage;
texIndex = 1;
break;
case Hairball::HUD:
spriteImage = [UIImage imageNamed:@"Icon.png"].CGImage;
texIndex = 2;
break;
default:
break;
// Get the width and height of the image
width = CGImageGetWidth(spriteImage);
height = CGImageGetHeight(spriteImage);
if(spriteImage)
// Allocated memory needed for the bitmap context
spriteData = (GLubyte *) malloc(width * height * 4);
// Uses the bitmatp creation function provided by the Core Graphics framework.
spriteContext = CGBitmapContextCreate(spriteData, width, height, 8, width * 4, CGImageGetColorSpace(spriteImage), kCGImageAlphaPremultipliedLast);
// After you create the context, you can draw the sprite image to the context.
CGContextDrawImage(spriteContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), spriteImage);
// You don't need the context at this point, so you need to release it to avoid memory leaks.
CGContextRelease(spriteContext);
// Bind the texture name.
glBindTexture(GLTEXTURE2D, TextureArray[texIndex]);
// Speidfy a 2D texture image, provideing the a pointer to the image data in memory
glTexImage2D(GLTEXTURE2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GLUNSIGNEDBYTE, spriteData);
// Release the image data
free(spriteData);
//Inits OpenGl for drawing
- (void)setupView
//Creates object Manager to handle
ObjectMgr = new ObjectManager();
//Create Objects
GameObject* Object1 = new GameObject(Hairball::BACKGROUND, 0.0f, 0.0f, 0.0f, 0.0f, 3.2f, 3.2f);
ObjectMgr->AddToList(Object1);
GameObject* Object2 = new GameObject(Hairball::PLAYER, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f);
ObjectMgr->AddToList(Object2);
//Generate all textures
glGenTextures(NUMTEXTURES, TextureArray); // Use OpenGL ES to generate a name for the texture.
[self GenerateTextures:Hairball::BACKGROUND];
[self GenerateTextures:Hairball::PLAYER];
//For each obj assign a texture
std::list<GameObject*>::iterator Object = (ObjectMgr->mObjectList.begin());
std::list<GameObject*>::iterator End = (ObjectMgr->mObjectList.end());
//For each game object
while(Object != End)
//Apply texture to each object
switch ((*Object)->mObjectType)
case Hairball::PLAYER: (*Object)->mSpriteTexture = TextureArray[0]; break;
case Hairball::BACKGROUND: (*Object)->mSpriteTexture = TextureArray[1]; break;
case Hairball::HUD: (*Object)->mSpriteTexture = TextureArray[2]; break;
default:
break;
++Object;
// Clears the view with grey
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
// Set the texture parameters to use a minifying filter and a linear filer (weighted average)
glTexParameteri(GLTEXTURE2D, GLTEXTURE_MINFILTER, GL_LINEAR);
// Enable use of the texture
glEnable(GLTEXTURE2D);
// Set a blending function to use
glBlendFunc(GL_ONE, GLONE_MINUS_SRCALPHA);
// Enable blending
glEnable(GL_BLEND);
- (void)drawView
[EAGLContext setCurrentContext:context];
glBindFramebufferOES(GLFRAMEBUFFEROES, viewFramebuffer);
glViewport(0, 0, backingWidth, backingHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(-1.0f, 1.0f, -1.5f, 1.5f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
//Clear the backbuffer
glClear(GLCOLOR_BUFFERBIT);
std::list<GameObject*>::iterator Object = (ObjectMgr->mObjectList.begin());
std::list<GameObject*>::iterator End = (ObjectMgr->mObjectList.end());
//For each game object
while(Object != End)
// Bind the texture name.
glBindTexture(GLTEXTURE2D, (*Object)->mSpriteTexture);
//apply transformations
glPushMatrix();
glTranslatef((*Object)->mPosition.x, (*Object)->mPosition.y, (*Object)->mPosition.z);
glRotatef((*Object)->mRotation, 0.0f, 0.0f, 1.0f);
//(*Object)->mRotation += 0.5f; //Rotate
//Get current sprites verticies
glVertexPointer(2, GL_FLOAT, 0, (*Object)->mSpriteVertices);
glEnableClientState(GLVERTEXARRAY);
//Get current sprites tex coords
glTexCoordPointer(2, GL_SHORT, 0, (*Object)->mSpriteTexcoords);
glEnableClientState(GLTEXTURE_COORDARRAY);
//Render the vertex array
glDrawArrays(GLTRIANGLESTRIP, 0, 4);
//pop the transformation matrix
glPopMatrix();
++Object;
glBindRenderbufferOES(GLRENDERBUFFEROES, viewRenderbuffer);
[context presentRenderbuffer:GLRENDERBUFFEROES];

Similar Messages

  • [iPhone] OpenGL ES Antialiasing?

    Hi,
    I'm writing an OpenGL ES application and I'm wondering if any form of antialiasing is supported on the iPhone hardware. I'm mainly drawing a textured polygons, so it would be great if there were something like (GLPOYGONSMOOTH), which doesn't exist in ES. I've also tried to use multisampling, but it appears that isn't supported by the hardware either. Does anyone know of any way to do some form of antialiasing in iPhone OpenGL ES apps?
    Thanks,
    -Greg

    Your best bet is probably to go to the source and check out the documentation of the PowerVR MBX chipsets directly if you want to know what's in hardware. You can also check out the Khronos OGL ES spec for 1.1 but alot of the added functionality comes from extensions and you don't have control over which extensions are being used.
    http://www.imgtec.com/PowerVR/mbx.asp
    http://www.khronos.org/opengles/
    Good luck,
    =Tod

  • OpenGL ES iPhone - Smooth (Antialiases) Textures

    Hi
    Does anyone has a idea how to make textures displayed antialiased in opengl es?
    id like to code something like a "cover flow" in the ipod application.
    thanks

    Yes, I have try with the latest version. I will retest this evening.
    A detail : with workaround, I have installed the iPhone SDK on a PPC G4 Mac, maybe an incompatibility problem ?
    Yet, the GLSprite example work fine.
    But when I modify the texture "Sprite.png" of the GLSprite example, with a red rectangle in the texture, I have a strange problem : The originals colors of the texture are good, but the rectangle is not red, but clear blue color.
    I don't understand ? Why this texture work partially, and the modification is corrupted ??

  • Compressed textures in iPhone openGL ES

    After doing some research it seems the iphone supports pvrtc compressed textures in its openGL implementation. Has anyone gotten these working? I found some tools online to convert png's to pvrtc textures but I'm not sure how to take those files and read them into openGL as a texture.
    Please let me know as this would save a ton of memory in my application. Right now I'm loading in PNG's at storing them as GL_RGBA internally which is very wasteful and slow to load.

    I don't have it running yet because I didn't try very hard.
    I only used the interactive tool, where you just click on 'encode' and then have a whole bunch of options. The ones that matter are pvr 4bpp or pvr 2bpp, depending on the quality you want. The interactive tools allows you to compare between the original and compressed version. 2bpp is usually to ugly...
    Be sure to compare the right file sizes:
    if you have a .PNG file that compresses very well (e.g. a file with a lot of constant color regions), then the .PNG file will be smaller than the texture compressed one, but when used as a real texture, it will be stored in the iPhone uncompressed. That's that value against which you have to compare.
    So just check the size of the original texture.
    Say 256x256x32bpp => 256KB uncompressed
    If you are converting it to a 656 texture when loading it into the iPhone, it will only take up 128KB in memory.
    When using 4bbp texture compression => 32KB
    Your .pvr file should be slightly larger because of the header.
    If you are using mipmapping, then size will grow by 30%.
    I don't have a WWDC access and I doubt it's legal to post those examples freely.
    Tom
    Message was edited by: wallclimber21
    Message was edited by: wallclimber21

  • [iPhone + OpenGL ES]  - glDrawElements memory leak!?

    Hello -
    I found in the Leaks tool that when glDrawElements is called, a huge chunk of memory gets allocated. This memory is the exact size of a texture that was already loaded earlier. (1024x1024xRGBA = 4mb)
    There is no other resource in the app that it could be, except another allocation of that texture. So instead of seeing 4mb in Leaks, I see 8mb - the second allocation being done by gfxAllocateTextureLevel.
    If I comment out glDrawElements, this problem does not occur.
    This is all on the simulator. I can't tell if it happens on the phone because Leaks crashes when I use it on the device.. Any ideas?

    typewriter wrote:
    This is all on the simulator. I can't tell if it happens on the phone because Leaks crashes when I use it on the device.. Any ideas?
    You probably have nothing to worry about. The simulator runs VERY differently than the real hardware.
    For instance, as I was porting our company's game engine to the iPhone Simulator, I had to give the global heap an extra 5 megabytes, for memory allocations coming from Apple's threads. On the real iPhone, it needs less than 15k.
    I've had one of our games running in a "demo loop" for hours on the real iPhone, with no crashes or other weird problems. So I doubt the iPhone's OpenGL is leaking memory.
    Hope this helps.

  • [ iPhone ] OpenGL ES glLineWidth()

    Hey guys,
    Quick question: I'm using OpenGL on the iPhone and have code to set the width of rendered lines using glLineWidth(). This works great in the simulator but doesn't seem to work on an actual device.
    I've searched the specification for OpenGL ES and it definitely should be functioning correctly. Anyone know i need to do anything special to get this feature to work on a device? Or if it's just not supported ( hopefully not )?
    Thanks!

    Is GLLINESMOOTH enabled? The iPhone does not support anti-aliased lines. If you query the SMOOTHLINE_WIDTHRANGE, it reports (1,1), which is kind of a hint that it doesn't work (ie, line widths are clamped to 1). Smoothed lines (and points) have long been a very inconsistent thing in OpenGL implementations, so the iPhone is not alone in having problems with it.
    See http://homepage.mac.com/arekkusu/bugs/invariance/index.html for an example of desktop GL renderers, and also some sample code on how to use Framebuffer Blending + Texturing to create fake smooth lines. Note, by 'fake', I mean 'much better quality than most built-in smoothed-line implementations', and perfectly portable as well (unlike lines themselves).
    Your other (easier) option is to use non-smoothed lines.

  • [iPhone] OpenGL IPhone Question

    Greetings, I am new to programming on the iPhone and Objective-C so I hope you will excuse me if I ask a stupid question. I am experienced at Java (having worked with it for 14 years professionally) and I have some background in C and C++ that I have half forgotten but not completely.
    I am trying to learn OpenGL programming on the iPhone and I have encountered a problem. In my test application I want to draw a icosahedron and I am having trouble with several sides completely missing. I was hoping someone could lead me in the right direction. Pasted below are the relevant sections of code based off the template for the OpenGL template.
    It draws a good portion of the icosahedron but there appear to be sides missing and that causes odd things. I have been fiddling with this for hours and im out of ideas. I assume it must be something simple.
    #import <UIKit/UIKit.h>
    #import <OpenGLES/EAGL.h>
    #import <OpenGLES/ES1/gl.h>
    #import <OpenGLES/ES1/glext.h>
    #import "GameBead.h"
    This class wraps the CAEAGLLayer from CoreAnimation into a convenient UIView subclass.
    The view content is basically an EAGL surface you render your OpenGL scene into.
    Note that setting the view non-opaque will only work if the EAGL surface has an alpha channel.
    @interface EAGLGameView : UIView {
    @private
    GLint backingWidth;
    GLint backingHeight;
    EAGLContext *context;
    GLuint viewRenderbuffer, viewFramebuffer;
    GLuint depthRenderbuffer;
    GameBead* bead;
    - (void)drawWithRotationX:(GLfloat)rotX Y:(GLfloat)rotY;
    @end
    #import <QuartzCore/QuartzCore.h>
    #import <OpenGLES/EAGLDrawable.h>
    #import "EAGLGameView.h"
    #define USEDEPTHBUFFER 0
    @interface EAGLGameView ()
    @property (nonatomic, retain) EAGLContext *context;
    - (BOOL) createFramebuffer;
    - (void) destroyFramebuffer;
    @end
    @implementation EAGLGameView
    @synthesize context;
    // You must implement this
    + (Class)layerClass {
    return [CAEAGLLayer class];
    //The GL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:
    - (id)initWithCoder:(NSCoder*)coder {
    bead = [[GameBead alloc] init];
    if ((self = [super initWithCoder:coder])) {
    // Get the layer
    CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
    eaglLayer.opaque = YES;
    eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
    context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
    if (!context || ![EAGLContext setCurrentContext:context]) {
    [self release];
    return nil;
    return self;
    - (void)drawWithRotationX:(GLfloat)rotX Y:(GLfloat)rotY {
    [EAGLContext setCurrentContext:context];
    glBindFramebufferOES(GLFRAMEBUFFEROES, viewFramebuffer);
    glViewport(0, 0, backingWidth, backingHeight);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrthof(-2.0f, 2.0f, -3.0f, 3.0f, -2.0f, 2.0f);
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear(GLCOLOR_BUFFERBIT| GLDEPTH_BUFFERBIT | GLSTENCIL_BUFFERBIT);
    glPushMatrix();
    GLfloat rotZ = 0.0f;
    [bead drawWithRotationX: rotX Y: rotY Z:rotZ];
    glPopMatrix();
    glBindRenderbufferOES(GLRENDERBUFFEROES, viewRenderbuffer);
    [context presentRenderbuffer:GLRENDERBUFFEROES];
    - (void)layoutSubviews {
    [EAGLContext setCurrentContext:context];
    [self destroyFramebuffer];
    [self createFramebuffer];
    [self drawWithRotationX: 0.0f Y:0.0f];
    - (BOOL)createFramebuffer {
    glGenFramebuffersOES(1, &viewFramebuffer);
    glGenRenderbuffersOES(1, &viewRenderbuffer);
    glBindFramebufferOES(GLFRAMEBUFFEROES, viewFramebuffer);
    glBindRenderbufferOES(GLRENDERBUFFEROES, viewRenderbuffer);
    [context renderbufferStorage:GLRENDERBUFFEROES fromDrawable:(CAEAGLLayer*)self.layer];
    glFramebufferRenderbufferOES(GLFRAMEBUFFEROES, GLCOLOR_ATTACHMENT0OES, GLRENDERBUFFEROES, viewRenderbuffer);
    glGetRenderbufferParameterivOES(GLRENDERBUFFEROES, GLRENDERBUFFER_WIDTHOES, &backingWidth);
    glGetRenderbufferParameterivOES(GLRENDERBUFFEROES, GLRENDERBUFFER_HEIGHTOES, &backingHeight);
    if (USEDEPTHBUFFER) {
    glGenRenderbuffersOES(1, &depthRenderbuffer);
    glBindRenderbufferOES(GLRENDERBUFFEROES, depthRenderbuffer);
    glRenderbufferStorageOES(GLRENDERBUFFEROES, GLDEPTH_COMPONENT16OES, backingWidth, backingHeight);
    glFramebufferRenderbufferOES(GLFRAMEBUFFEROES, GLDEPTH_ATTACHMENTOES, GLRENDERBUFFEROES, depthRenderbuffer);
    if(glCheckFramebufferStatusOES(GLFRAMEBUFFEROES) != GLFRAMEBUFFER_COMPLETEOES) {
    NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GLFRAMEBUFFEROES));
    return NO;
    // -- set up the lighting.
    GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
    GLfloat mat_shininess[] = { 50.0 };
    GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
    GLfloat matambdiff[] = { 0.6, 1.0, 0.6, 1.0 };
    glClearColor (0.0, 0.0, 0.0, 0.0);
    glShadeModel (GL_SMOOTH);
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
    glMaterialfv(GL_FRONT, GLAMBIENT_ANDDIFFUSE, matambdiff);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GLDEPTHTEST);
    // -- done
    return YES;
    - (void)destroyFramebuffer {
    glDeleteFramebuffersOES(1, &viewFramebuffer);
    viewFramebuffer = 0;
    glDeleteRenderbuffersOES(1, &viewRenderbuffer);
    viewRenderbuffer = 0;
    if(depthRenderbuffer) {
    glDeleteRenderbuffersOES(1, &depthRenderbuffer);
    depthRenderbuffer = 0;
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint now = [touch locationInView:self];
    CGPoint old = [touch previousLocationInView:self];
    CGFloat rotX = now.x - old.x * 1.0f;
    CGFloat rotY = now.y - old.y * 1.0f;
    [self drawWithRotationX:rotX Y:rotY];
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    - (void)dealloc {
    if ([EAGLContext currentContext] == context) {
    [EAGLContext setCurrentContext:nil];
    [context release];
    [bead release];
    [super dealloc];
    @end
    #import <OpenGLES/EAGL.h>
    #import <OpenGLES/ES1/gl.h>
    #import <OpenGLES/ES1/glext.h>
    @interface GameBead : NSObject {
    GLfloat vertexData[20 * 3 * 3];
    -(id)init;
    -(void)drawWithRotationX:(GLfloat)xrot Y:(GLfloat)yrot Z:(GLfloat)zrot;
    @end
    #import "GameBead.h"
    @implementation GameBead
    - (id)init {
    // ---- The data that makes up a unit Icosahedron. ----
    // References:
    // http://en.wikipedia.org/wiki/Golden_ratio
    // http://en.wikipedia.org/wiki/Golden_rectangle
    const uint sideCount = 20;
    const GLfloat x = 0.525731112119133606f;
    const GLfloat z = 0.850650808352039932f;
    const GLfloat ico_vertices[12][3] = {
    {-x, 0.0, z}, {x, 0.0, z}, {-x, 0.0, -z}, {x, 0.0, -z},
    {0.0, z, x}, {0.0, z, -x}, {0.0, -z, x}, {0.0, -z, -x},
    {z, x, 0.0}, {-z, x, 0.0}, {z, -x, 0.0}, {-z, -x, 0.0}
    const GLuint ico_triangles[20][3] = {
    {0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},
    {8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},
    {7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6},
    {6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11}
    uint idx, vidx;
    [super init];
    for (idx = 0; idx < sideCount; idx++) {
    for (vidx = 0; vidx < 3; vidx++) {
    vertexData[(idx * 9) + (vidx * 3) + 0] = icovertices[icotriangles[idx][vidx]][0];
    vertexData[(idx * 9) + (vidx * 3) + 1] = icovertices[icotriangles[idx][vidx]][1];
    vertexData[(idx * 9) + (vidx * 3) + 2] = icovertices[icotriangles[idx][vidx]][2];
    return self;
    - (void)drawWithRotationX:(GLfloat)rotX Y:(GLfloat)rotY Z:(GLfloat)rotZ {
    glMatrixMode(GL_MODELVIEW);
    glEnableClientState(GLVERTEXARRAY);
    glVertexPointer(3, GL_FLOAT, 0, vertexData);
    glEnableClientState(GLNORMALARRAY);
    glNormalPointer(GL_FLOAT, 0, vertexData);
    glDrawArrays(GL_TRIANGLES, 0, 180);
    glRotatef(rotX, 0.0f, 1.0f, 0.0f); // x rotation is about y axis
    glRotatef(rotY, 1.0f, 0.0f, 0.0f); // y rotation is about x axis
    glRotatef(rotZ, 0.0f, 0.0f, 1.0f); // z rotation is about z axis
    - (void) normalizeVertex:(GLfloat*)vertex {
    GLfloat d = sqrt((vertex[0]vertex[0])(vertex[0]*vertex[0])(vertex[0]vertex[0]));
    // guarantee that the vector isnt zero length
    if (d == 0.0) {
    NSException* ex = [NSException exceptionWithName:@"Bad Arguments" reason:@"Arguments resulted in a zero length vector." userInfo:nil];
    @throw ex;
    vertex[0] /= d; vertex[1] /= d; vertex[2] /= d;
    - (void)dealloc {
    free(vertexData);
    [super dealloc];
    @end

    hausy wrote:
    have you skype ? we can learn from each other
    Im sorry, I cant give out personal information like that on an open forum. Especially when you arent willing to state your reasons. That would just be stupid.
    -- Robert

  • IPhone OpenGL swapBuffers problem! Help!

    Hi guys,
    I am writing a 2D game using OpenGL, based heavily on Apple's CrashLanding sample.
    My main game loop calls three functions:
    1. update, which moves all my objects
    2. draw, which draws the objects to the current OpenGL context using Apple's Texture2D
    3. finally, the swapBuffers function provided by apple.
    I've set the rendering timer to 60 FPS. Right now all I'm drawing is a background texture (320x480 - fills the screen) and 10 or so clouds, about 100x50 px each, with blending enabled.
    **Here's the problem: most of the time it runs fine, but seemingly at random, about every tenth frame or so, the swapBuffers function takes 10x as long as usual (about 0.002 sec. instead of 0.0002 sec) - Does anyone know what could be causing this? If I understand correctly, it seems like any slowdown should come in the draw function, and the speed of swapbuffers should be completely independent of what's on the buffers being swapped, right?
    Anyone know what could be causing this or seen anything similar?

    Yea I noticed it because there are a few really choppy frames every 2-3 seconds but it's smooth as silk the rest of the time, even though (seemingly) nothing changes.
    I had one thought though: In the process of moving my objects around, I allocate and then release a lot of Vector2D's (a class I wrote). Is there any way that the de-allocated vectors pile up and the stutter is when it decides to clean them out?
    When I run the Object Allocations instrument, the "Net" number of Vector2D's stays steady, so I don't think I'm leaking anything. However, the big red bar to the side of that is huge, though mostly light pink with a tiny sliver of dark red. What does that mean? Sorry for all the newbie questions
    Thanks a ton!

  • IPhone OpenGL Bug?

    Hi,
    I'm trying to mask out a certain part of the screen by using GLDEPTHTEST ( writing a certain value to an area, then rendering with the glDepthFunc at GL_GEQUAL ). This works fine in regular OpenGL, however the depth test doesn't seem to do anything on the iPhone. I've even tried glDepthFunc at GL_NEVER, yet it still renders everything. I've made certain that I'm calling glEnable( GLDEPTHTEST ) and that at the beginning of each frame I clear the depth buffer.
    Any ideas as to why the depth test doesn't seem to work on the iPhone?

    Whoops! Sorry for the post, I just realized that I'm using the OpenGL GLSprite sample app and didn't set "USEDEPTHBUFFER" to "1". Problem fixed

  • IPhone - OpenGL ES - Polygon Offset

    I need to draw lines over polygons and I am using polygon offset as described here.
    http://www.opengl.org/resources/faq/technical/polygonoffset.htm
    It works fine for me on the desktop but it does not seem to work on OpenGL ES on iPhone. Is there a workaround? Does polygon offset even work on iPhone or are the parameters interpreted differently?

    Your best bet is probably to go to the source and check out the documentation of the PowerVR MBX chipsets directly if you want to know what's in hardware. You can also check out the Khronos OGL ES spec for 1.1 but alot of the added functionality comes from extensions and you don't have control over which extensions are being used.
    http://www.imgtec.com/PowerVR/mbx.asp
    http://www.khronos.org/opengles/
    Good luck,
    =Tod

  • IPhone OpenGL ES anti-aliasing?

    Is it possible to enable anti-aliasing for OpenGL ES on the iPhone? I've tried a few different things:
    glBlendFunc(GLSRCALPHA, GLONE_MINUS_SRCALPHA);
    glEnable(GL_BLEND);
    glEnable(GL_SMOOTH);
    glEnable(GL_MULTISAMPLE);
    and a few others, but none of them work. I'm not exactly an OpenGL heavy, so I wouldn't know if these were even the right kinds of things to enable for anti-aliasing content (it seems like GL_SMOOTH or GL_MULTISAMPLE would work). It seems like the general consensus for OpenGL ES is that anti-aliasing is platform-dependent, but I haven't seen anything which says whether it's possible on the iPhone. I would assume that it would be possible on the iPhone, considering that things drawn with the Quartz CG methods are anti-aliased by default. It looks to me, though, like the only way to do it would be to render into a buffer with two times the width and height, and then scale it down to fit the display. That seems a little bit processor heavy, though, especially on a mobile device. Any input would be greatly appreciated .
    Sonny Jim

    I've accomplished FSAA(full screen antialiasing) on the iPhone by rendering the same scene multiple times and jittering the perspective matrix.
    The technique has been used since the 70s. Details on the technique can be found at http://www.opengl.org/resources/code/samples/advanced/advanced97/notes/node63.ht ml
    The iPhone doesn't have an accumulation buffer, but you can still use the same idea by faking an accumulation buffer by copying the contents of the color buffer and blending the passes together yourself. If you depth-sort your scene you can just blend everything together and forget about an accumulation buffer.
    There are variations on this technique where you re-render the scene several times in wireframe mode and use the results to antialias just the edges of polygons.
    I'm doing a puzzle game that never has a front-facing polygon behind another front-facing polygon, so I don't have to mess around with faking an accumulation buffer or worry about depth sorting. Remember that this will make your fps drop by whatever factor you antialias.

  • OpenGL App Texture Colours look rubbish and blotchy

    I've copied the CrashLanding app and made some adjustments and all is ok, except that if I load a texture (tried png, jpg, bmp) saved from photoshop that uses the "clouds" filter, when it loads on screen in the simulator and on my iPhone, the colours look all blotchy almost like the image has been downgraded to 256 colours or something.
    I haven't modified the Texture2D class at all.
    Could anyone shed some light on why the image is coming out like this? I want to be able to use all the colours that show in photoshop. Even when I import them into XCode as resources they look fine. It is only when they are loaded into the app that they display badly.
    PS, this is not the same problem that some were having with PPC hardware, I'm using an intel macmini.

    I've had similer problems when using Apple's Texture2D class. For starters, during linking, the resource compiler will OPTIMISE your PNG's (by converting them to Apple's proprietary CgBI format). One of the sideeffects is that it ignores interim alpha values (eg. 0.5f). It also creates artifacts with some PNG files (haven't bothered investigating why)
    I've ditched Apples Texture2D and ported libpng to the iPhone (together with glpng). You need to prevent Apples's resource compiler from optimising PNG's (add a build setting called "IPHONEOPTIMIZEOPTIONS" and set it to "-skip-PNGs" ignore the quotes), or rename your .png files.
    Now my PNG's look great, partial alpha values (0.5f) are displayed properly, and there is no texture corruption. Frame rates are basically the same.

  • IPhone OpenGL ES Shadow

    What is the best technique for shadow on iPhone? Since there's no stencil buffer and the shadow map resolution is quite limited what is the best way to do it?
    Tks in advance,
    Cheers,

    Your best bet is probably to go to the source and check out the documentation of the PowerVR MBX chipsets directly if you want to know what's in hardware. You can also check out the Khronos OGL ES spec for 1.1 but alot of the added functionality comes from extensions and you don't have control over which extensions are being used.
    http://www.imgtec.com/PowerVR/mbx.asp
    http://www.khronos.org/opengles/
    Good luck,
    =Tod

  • OpenGL ES textures

    Hi, I need to render a texture that is not n^2 * n^2. This was accomplished in standard openGL by using GLTEXTURE_RECTANGLEEXT. Snippet:
    glBindTexture(GLTEXTURE_RECTANGLEEXT, _texture);
    glTexParameterf(GLTEXTURE_RECTANGLEEXT, GLTEXTUREPRIORITY, 1.0);
    glTexParameteri(GLTEXTURE_RECTANGLEEXT, GLTEXTURE_WRAPS, GLCLAMP_TOEDGE);
    glTexParameteri(GLTEXTURE_RECTANGLEEXT, GLTEXTURE_WRAPT, GLCLAMP_TOEDGE);
    glTexParameteri(GLTEXTURE_RECTANGLEEXT, GLTEXTURE_MAGFILTER, GL_LINEAR);
    glTexParameteri(GLTEXTURE_RECTANGLEEXT, GLTEXTURE_MINFILTER, GL_LINEAR);
    However I have no idea how to do this in OpenGL ES. For one, it only supports triangles, and every tutorial I see loads power of 2 images.
    There must be a way. Else all these games wouldn't be around!

    There is a detailed 'OpenGL ES' discussion going on here, by Jeff LaMarche, if you haven't seen it yet (sorry, the link starts at what appears to be the latest segment, pt. 8.)

  • Will the iPhone OpenGL take advantage of vector animation?

    I've noticed on Windows Mobile devices any type of movement, zooming or animation lags big time. I was wondering if OpenGL on the iPhone will help with this?

    Hi,
    Sorry, you can only get the full benefit of indexes if you know the starting portion of the indexed value.
    That is, if there's an index on column_x, then you might do an index look-up to resolve
    where COLUMN_X like 'ORACLE%'     -- end is unknownbut not
    where COLUMN_X like '%ORACLE'     -- beginning is unknownIf the entire query can be done by just looking at the index, then the index may help some. For example, in
    SELECT  COUNT (column_x)
    FROM    table_a
    where   COLUMN_X like '%ORACLE%';the optimizer may choose to do a full scan of the index rather than a full scan of the table.
    Say you have 100,000 rows in the table, which occupies 1,000 blocks. Say the index occupies 50 blocks. A full scan of the index would be about 20 times faster than scanning the entire table, regardless of how many rows actually matched. There might only be one like '%ORACLE%, or there might be none, but you'd still have to read the entire index to find out.

Maybe you are looking for

  • Java C# Encryption compatibility

    Would anyone happen to know or could point me to a place where I can find out the compatibility in the crypto libraries between C#/.NET and the various Java providers including JCE, BouncyCastle and Cryptix. I would need to exchange things like sessi

  • I can download iTunes 7.0.2 but it won't open!!! Help!!

    I am able to successfully download iTunes 7.0.2 but after is is done and I try to open it nothing happens. Nothing at all, no error or anything. I tried uninstalling an downloading many times. Then, just to test, I downloaded an older version of iTun

  • Deploying ejb with webservice annotation problem

    I have the following code: HelloInf.java: package com.bekijkhet; import java.rmi.Remote; import java.rmi.RemoteException; import javax.jws.WebService; @WebService public interface HelloInf extends java.rmi.Remote { java.lang.String sayHello() throws

  • Installing Camera Raw update for PSE 9.0.3 fails

    I have been trying to update either manually or with the update function the camera raw plugin for Photoshop Elements 9 to the newest version. It keeps saying that the update has failed. I know I don't have the newest version because I can't open my

  • Fields for Available capacity in SAP

    We are trying to generate Z report on Availabile capacity Under normal transaction : by going in CR03 we can have Start time / Finish Time based on which we get operating time in hours, we need to capture the Avaialble capacity we are not getting thi