OpenGl Es Sprite render

Member
Posts: 39
Joined: 2008.10
Post: #1
Hey,

Im stuck on a couple of aspects of textures/sprites in opengl.

1) I need to know how to set rotation of individual textures

2) I need to stop textures from disappearing before they leave the screen completely ( it happens when the x,y coords are no longer visible)

I am rendering with this method:

glVertexPointer(2, GL_FLOAT, 0, vertexBuffer);
glDrawArrays(GL_POINTS, 0, ObjCountPerm);

The vertexBuffer[0] and vertexBuffer[1] stores the x y coords

it is like the painting example that comes with the sdk

Let me know if you need any more information, thanks for any help and direction.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
Drawing textured GL_QUADS (or pairs of GL_TRIANGLES? I can't remember if GL_QUADS exists in OpenGL ES or not) instead of GL_POINTS will alleviate your disappearing-at-edge-of-screen problem, and allow you to easily rotate your objects.
Quote this message in a reply
Member
Posts: 339
Joined: 2004.07
Post: #3
ThemsAllTook Wrote:I can't remember if GL_QUADS exists in OpenGL ES or not)

It doesn't.

For sprites, I just use a textured quad, drawn as a triangle strip. Make vertexBuffer store 8 values, (4 x/y pairs) and use:

Code:
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

Justin Ficarrotta
http://www.justinfic.com
"It is better to be The Man than to work for The Man." - Alexander Seropian
Quote this message in a reply
Member
Posts: 39
Joined: 2008.10
Post: #4
Thanks, this sounds perfect.. a couple of questions though

say my current x y coords are 100, 150

what would my new 8 coords have to be for this new method?
could i write a function to convert my current positions to the new ones?

also how would i then rotate an object?

thanks again i really appreciate the help
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #5
You will need to know about transformations, and how to use them in OpenGL.

http://glprogramming.com/red/chapter03.html

You might be able to find a simpler tutorial elsewhere, but that is a good general introduction at least.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 339
Joined: 2004.07
Post: #6
jjslay Wrote:Thanks, this sounds perfect.. a couple of questions though

say my current x y coords are 100, 150

what would my new 8 coords have to be for this new method?

You would need to know the size of your sprite, which is (size.x, size.y). Your 8 values are:

Code:
x-size.x, y-size.y
x+size.x, y-size.y
x-size.x, y+size.y
x+size.x, y+size.y

Quote:also how would i then rotate an object?

Skorche's link has a lot of good info, but in a nutshell:

Code:
glPushMatrix();   // Save the previous transformation, so we can screw with it
glTranslatef(x, y, 0); // Move to the point of the sprite
glRotatef(angle, 0, 0, 1); // Rotate by angle degrees
// Draw here, for verts, only use the size, instead of pos+size, since we already moved to the position by translating
glPopMatrix(); // Return to the saved transformation

The Redbook or http://nehe.gamedev.net will have more in depth explanations of everything.

Justin Ficarrotta
http://www.justinfic.com
"It is better to be The Man than to work for The Man." - Alexander Seropian
Quote this message in a reply
Member
Posts: 39
Joined: 2008.10
Post: #7
Thanks Justin, ill give it a go tomorrow... that was just what i needed to know Smile
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #8
jjslay Wrote:Thanks, this sounds perfect.. a couple of questions though

say my current x y coords are 100, 150

what would my new 8 coords have to be for this new method?

There are different approaches. In addition to JustinFic's suggestion, another way would be to do something like:

Code:
GLfloat    genericSpriteVerts[] = {
                    -0.5f, -0.5f,
                     0.5f, -0.5f,
                    -0.5f,  0.5f,
                     0.5f,  0.5f };
GLfloat      genericSpriteCoords[] = {
                    0.0f,  0.0f,
                    1.0f,  0.0f,
                    0.0f,  1.0f,
                    1.0f,  1.0f };

glVertexPointer(2, GL_FLOAT, 0, genericSpriteVerts);
glTexCoordPointer(2, GL_FLOAT, 0, genericSpriteCoords);

glPushMatrix();
glTranslatef(100.0f, 150.0f, 0.0f);
glRotatef(mySpriteRotation, 0.0f, 0.0f, 1.0f);
glScalef(mySpriteScale, mySpriteScale, 1.0f);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glPopMatrix();
Quote this message in a reply
Member
Posts: 39
Joined: 2008.10
Post: #9
Hey,

Ive got for the moment one of my sprites rendering in the middle of the screen using the sprite example which looks a lot like your code.

How do i move the spirte around (x,y coords)

the coords being used to render are these

Code:
GLfloat    genericSpriteVerts[] = {
                    -0.5f, -0.5f,
                     0.5f, -0.5f,
                    -0.5f,  0.5f,
                     0.5f,  0.5f };
GLfloat      genericSpriteCoords[] = {
                    0.0f,  0.0f,
                    1.0f,  0.0f,
                    0.0f,  1.0f,
                    1.0f,  1.0f };

if i wanted to move the sprite downwards or to another position how would i?

i am good with game mechanics and i had a pretty decent game going with the old 2d sprite method but as i said i was using two x,y coords that i just defined and manipulated as i wished. I want to use this new type of rendering and i need some help getting these methods applied to the new render technique...

before i was using numbers like 300 and 100 not 1.0f etc

Please let me know how i can still use simple coords and have them converted just before rendering etc
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #10
To move 100 across and 150 up, the trick is:

Code:
glPushMatrix();
glTranslatef(100.0f, 150.0f, 0.0f);   <-- ***** Right here ****
glRotatef(mySpriteRotation, 0.0f, 0.0f, 1.0f);
glScalef(mySpriteScale, mySpriteScale, 1.0f);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glPopMatrix();

Instead of modifying genericSpriteVerts yourself, you tell OpenGL to transform them with the glTranslatef:

glTranslatef(x, y, z);

That's why the vertex coordinates are cleverly setup to be -0.5, 0.5, etc. The distance between -0.5 and 0.5 is 1.0, and centered around zero. That way all you do is translate it to where you want it, and scale it to the size you want, without having to do any vertex calcs yourself.
Quote this message in a reply
Member
Posts: 39
Joined: 2008.10
Post: #11
ah that makes sense, thanks.

Does this still work when you have 100 of the same object etc?

If so do you render each object in a loop asigning different xpos ypos rotation and scale ?
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #12
jjslay Wrote:Does this still work when you have 100 of the same object etc?

If so do you render each object in a loop asigning different xpos ypos rotation and scale ?

Yes, that is the way to do it.

The trick there is the glPushMatrix() before doing the transform for each object, and then glPopMatrix() after you've drawn the object. The push saves the current matrix so you can mess it up, and then the pop goes back to the matrix before you messed it up for each object.

So:

Code:
for (i = 0; i < NUM_OBJECTS; i++)
{
     push
     transform
     draw
     pop
}
Quote this message in a reply
Member
Posts: 39
Joined: 2008.10
Post: #13
thats awesome, perfect for what i need it for. Thanks again Smile

one thing i just noticed was that transform doesnt relate to a pixel position but more of a -1 to 1 of the render window?
is there any way to use pixel values or will i have to convert pixel positions to the scale?
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #14
You can do it either way. It all depends on how you have your projection set up. If you want to do it on a pixel basis, use:

glOrthof(0, viewBoundsWidth, 0, viewBoundsHeight, -1, 1);
Quote this message in a reply
Member
Posts: 39
Joined: 2008.10
Post: #15
Thanks, i managed to get it to translate the pixel positions perfectly. The rotation of objects seems to be relative to the middle of the screen, is there a way i can rotate them from the center of themselves, i tried moving them to the middle rotating then moving to the position and it didnt seem to work.
Quote this message in a reply
Post Reply 

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