Modfied OpenGL Sprite Rendering

Nibbie
Posts: 3
Joined: 2009.07
Post: #1
Hi

I've been playing with the sample code from Aaron Fothergill's chapter in the iPhone games book to draw sprites on the screen. It has sample code for some 2d drawing functions in Texture2d.m

I want to add some none sprite drawing functions to the code so i added a function:
Code:
void DrawHFadeRectangle(GLubyte p_r1, GLubyte p_g1, GLubyte p_b1,
                        GLubyte p_r2, GLubyte p_g2, GLubyte p_b2,
                        float p_x1,float p_y1,float p_z,
                        float p_x2,float p_y2)
{
    glPushMatrix();
    
    GLfloat squareVertices[] = {
        p_x1,p_y1,p_z,
        p_x1,p_y2,p_z,
        p_x2,p_y2,p_z,
        p_x2,p_y1,p_z
    };
    
    GLubyte squareColors[] = {
        p_r1, p_g1, p_b1, 255,
        p_r1, p_g1, p_b1, 255,
        p_r2, p_g2, p_b2, 255,
        p_r2, p_g2, p_b2, 255,
    };
    
    glVertexPointer(3, GL_FLOAT, 0, squareVertices);
    glEnableClientState(GL_VERTEX_ARRAY);
    glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
    glEnableClientState(GL_COLOR_ARRAY);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    
    glPopMatrix();
}

Which i'd hope take the coordinates given a draw a rectangle with a horizontal fade.

It doesn't, i call the function once to draw a red faded rectangle in the bottom left but it seems to draw lots of non-visible rectangles everywhere. I say nonvisible as elsewhere on the screen other solid colour test spites have become graduated.

Any ideas whats gone wrong? or do you need more information?

cheers

tony
Quote this message in a reply
Member
Posts: 269
Joined: 2005.04
Post: #2
tonyb Wrote:
Code:
void DrawHFadeRectangle(GLubyte p_r1, GLubyte p_g1, GLubyte p_b1,
                        GLubyte p_r2, GLubyte p_g2, GLubyte p_b2,
                        float p_x1,float p_y1,float p_z,
                        float p_x2,float p_y2)
{
    glPushMatrix();
    
    GLfloat squareVertices[] = {
        p_x1,p_y1,p_z,
        p_x1,p_y2,p_z,
        p_x2,p_y2,p_z,
        p_x2,p_y1,p_z
    };
    
    GLubyte squareColors[] = {
        p_r1, p_g1, p_b1, 255,
        p_r1, p_g1, p_b1, 255,
        p_r2, p_g2, p_b2, 255,
        p_r2, p_g2, p_b2, 255,
    };
    
    glVertexPointer(3, GL_FLOAT, 0, squareVertices);
    glEnableClientState(GL_VERTEX_ARRAY);
    glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
    glEnableClientState(GL_COLOR_ARRAY);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    
    glPopMatrix();
}

Which i'd hope take the coordinates given a draw a rectangle with a horizontal fade.

It doesn't, i call the function once to draw a red faded rectangle in the bottom left but it seems to draw lots of non-visible rectangles everywhere. I say nonvisible as elsewhere on the screen other solid colour test spites have become graduated.

Are your vertices out of order? They look to be, assuming CCW winding with triangle strips. Try swapping vert 3 and 4 like so:

Code:
GLfloat squareVertices[] = {
    p_x1,p_y1,p_z,
    p_x1,p_y2,p_z,
    p_x2,p_y1,p_z,
    p_x2,p_y2,p_z
};
Quote this message in a reply
Nibbie
Posts: 3
Joined: 2009.07
Post: #3
Many thanks, that did the trick for drawing the rectangle, but it still appears only black. Even if for experimentation purposes i set all the values to be 255. Am I missing a setting somewhere? i'm comparing against the OpenGL template project code and for my untrained opengl i'm not seeing anything different in the view code...

thanks again in advance

tony
Quote this message in a reply
Nibbie
Posts: 3
Joined: 2009.07
Post: #4
managed to figure it out now,

I needed a glDisable(GL_TEXTURE_2D); and the corresponding enable after the rectangle code

tony
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  For 2D (sprite) games, do I use OpenGL or something else? lindsay 24 12,235 May 9, 2009 12:06 PM
Last Post: Weltevrede
  OpenGL Animated Sprite Question Aboqa 6 6,030 May 6, 2009 11:17 AM
Last Post: AnotherJake
  OpenGL ES sprite alpha Holmes 27 15,118 Mar 16, 2009 01:06 PM
Last Post: Holmes
  OpenGL render loop - NSTimer vs rendering thread smallstepforman 27 23,880 Feb 2, 2009 10:22 AM
Last Post: ThemsAllTook
  OpenGl Es Sprite render jjslay 43 25,972 Nov 1, 2008 04:24 AM
Last Post: wonza