OpenGl Es Sprite render

Moderator
Posts: 3,577
Joined: 2003.06
Post: #31
I don't know how to know if there's a depth buffer attached while running. Can't you just look at your frame buffer creation code and see if it's attaching a depth buffer? Heck, search your project for glGenFramebuffersOES. It should be right there somewhere.
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #32
glGetIntegerv(GL_DEPTH_BITS, &bits);
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #33
Ah, of course! I was thinking directly of querying for the depth buffer attachment, but if bits shows up zip, then that'd do the trick.
Quote this message in a reply
Member
Posts: 39
Joined: 2008.10
Post: #34
Hello again,

I thought it would be best if i just showed you the render buffer setup function so its easier to work out if ive got whats needed.

Code:
- (BOOL)createFramebuffer
{
    glGenFramebuffersOES(1, &viewFramebuffer);
    glGenRenderbuffersOES(1, &viewRenderbuffer);
    
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(id<EAGLDrawable>)self.layer];
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
    
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
    
    if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
        NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
        return NO;
    }
    
    return YES;
}

Thanks for any help
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #35
Try this:

Code:
- (BOOL)createFramebuffer
{
    glGenFramebuffersOES(1, &viewFramebuffer);
    glGenRenderbuffersOES(1, &viewRenderbuffer);
    
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(id<EAGLDrawable>)self.layer];
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
    
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
    
    // attach depth buffer
    glGenRenderbuffersOES(1, &depthRenderbuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
    glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, boundsWidth, boundsHeight);
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
        
    if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
        NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
        return NO;
    }
    
    return YES;
}
Quote this message in a reply
Member
Posts: 469
Joined: 2002.10
Post: #36
The approach I'm taking with my OpenGL ES projects is to draw GL_TRIANGLES pairs (6 verts per quad). I use a single 512x512 texture and layout all my sprites so I can just dump all the translated coords to GL in 1 draw without having to swap the texture. So far I get pretty good performance even with hundreds to thousands of sprites.

---Kelvin--
15.4" MacBook Pro revA
1.83GHz/2GB/250GB
Quote this message in a reply
Member
Posts: 39
Joined: 2008.10
Post: #37
Hey,

I tried the code you gave and it is definitely a step in the right direction, there is one problem that needs solving first; the alpha of the png calculates for the road (you can see the road through it) but is solid to each of the other cars so it displays the road instead of the car next to it... any ideas on this one?

Thanks
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #38
Depth testing and blending are incompatible. Take a second to think about why.

If you need blending, you have to sort your drawing yourself.
Quote this message in a reply
Member
Posts: 94
Joined: 2008.08
Post: #39
I seem to be having some problems trying to do the same thing. Basically im trying to show various nebulars when I limit the code below to show just one it will work, but when i tell it to show more than one it will just create them all as white squares:

Code:
//Setup code
    for (int ii = 0; ii < numberofNebulars; ii++){
        CGContextRef spriteContext;
        
        NSMutableString *fn  = [NSString stringWithFormat: @"web%i.jpg", ii];
        CGImageRef spriteImage = [UIImage imageNamed:fn].CGImage;
        
        if(spriteImage) {
            size_t    width, height;// Creates a Core Graphics image from an image file
            width = CGImageGetWidth(spriteImage);
            height = CGImageGetHeight(spriteImage);
            GLubyte *spriteData = (GLubyte *) malloc(width * height * 4);

            
            spriteContext = CGBitmapContextCreate(spriteData, width, height, 8, width * 4, CGImageGetColorSpace(spriteImage), kCGImageAlphaPremultipliedLast);
            CGContextDrawImage(spriteContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), spriteImage);
            CGContextRelease(spriteContext);
            
            GLuint    st;
            glGenTextures(1, &st);
            allNebulars[ii].glTexName=st;
            glBindTexture(GL_TEXTURE_2D, st);
            
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);
            free(spriteData);

        }
    }

//Draw code:
            glPushMatrix();
                glBindTexture(GL_TEXTURE_2D, allNebulars[ii].glTexName);
                glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);        
            glPopMatrix();

Any ideas what im doing wrong?
Quote this message in a reply
Apprentice
Posts: 13
Joined: 2008.10
Post: #40
Wonza,

There are 2 things i'm not seeing. First, I'm not seeing where you are setting the texture coordinates, you know, a call like:

Code:
// Sets up an array of values for the texture coordinates.
const GLshort spriteTexcoords[] = {
0, 0,
1, 0,
0, 1,
1, 1,
};
    glTexCoordPointer(2, GL_SHORT, 0, spriteTexcoords);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

Secondly, either you left out the for loop that is drawing each of your objects, or you are drawing them all at once from 1 vertex pointer. If you just left out the for loop then it looks like it should work. If you havn't and you are drawing them all in one vertex pointer, then you texCoords will need to be much much much larger. Basically repeat the 4 coordinates for every object in your array.
Quote this message in a reply
Member
Posts: 94
Joined: 2008.08
Post: #41
Hi there,
yeah sorry, i just left out the drawing loop to try and shorten it Smile the full code is here:

Code:
const GLfloat spriteVertices[] = {
-70.0f, -70.0f,
70.0f, -70.0f,
-70.0f,  70.0f,
70.0f,  70.0f,
};

// Sets up an array of values for the texture coordinates.
const GLshort spriteTexcoords[] = {
0, 0,
1, 0,
0, 1,
1, 1,
};


    glDisable(GL_BLEND);
    glDisableClientState(GL_COLOR_ARRAY);
    
    
    glVertexPointer(2, GL_FLOAT, 0, spriteVertices);
    glEnableClientState(GL_VERTEX_ARRAY);
    glTexCoordPointer(2, GL_SHORT, 0, spriteTexcoords);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glEnable(GL_TEXTURE_2D);
    

    for (int ss = 0; ss < numberofNebulars; ss++){
        if (allNebulars[ss].active) {
    
            glPushMatrix();
                glBindTexture(GL_TEXTURE_2D, allNebulars[ss].glTexName);
                glTranslatef(viewPosition.x+allNebulars[ss].Position.x, viewPosition.y+allNebulars[ss].Position.y, viewPosition.z+allNebulars[ss].Position.z);    
                glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);        
            glPopMatrix();
        }
    }

So, im not sure why its not working for more than one. any ideas? thanks Smile
Quote this message in a reply
Apprentice
Posts: 13
Joined: 2008.10
Post: #42
Wonza,

Ok, having read up on a similar problem Here it turns out that you have to set up the GL_BLEND information like this:
Code:
    // Set the texture parameters to use a minifying filter and a linear filer (weighted average)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    // Enable use of the texture
    glEnable(GL_TEXTURE_2D);
    // Set a blending function to use
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
    // Enable blending
    glEnable(GL_BLEND);

so just Add the 2 missing functions (glBlendFunc and glEnable) and you should be looking at some nice textures... The blend function is telling opengl to use 1 texture at a time, not to try and multi-texture which is what i believe is getting you those white textures.

Christian
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #43
Blending doesn't have anything to do with multitexturing. You can use both at the same time.

Wozna, in your code snippet above, you haven't provided mipmaps. Mipmapping is enabled by default, so if you don't provide them, the texture is incomplete and you'll get white (or whatever the primary color is.)

You should disable mipmapping if you don't want to use it.
Quote this message in a reply
Member
Posts: 94
Joined: 2008.08
Post: #44
Thanks, I think you were both talking about the same thing really Smile I had to set the blending functions you listed before I setup the textures and that worked! thanks! Smile
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Modfied OpenGL Sprite Rendering tonyb 3 3,009 Jul 26, 2009 07:30 AM
Last Post: tonyb
  For 2D (sprite) games, do I use OpenGL or something else? lindsay 24 12,342 May 9, 2009 12:06 PM
Last Post: Weltevrede
  OpenGL Animated Sprite Question Aboqa 6 6,067 May 6, 2009 11:17 AM
Last Post: AnotherJake
  OpenGL ES sprite alpha Holmes 27 15,236 Mar 16, 2009 01:06 PM
Last Post: Holmes
  OpenGL render loop - NSTimer vs rendering thread smallstepforman 27 24,054 Feb 2, 2009 10:22 AM
Last Post: ThemsAllTook