Public Group

OpenGL animating sprites

Recommended Posts

I finally got soil to load a png file. my next step is to animate a sprite using a sprite sheet I am unsure of  how to start. here is my code.

GLuint textureBrick[8];

{
/* load an image file directly as a new OpenGL texture */
(
texname,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y
);
return texture;
}

void init()
{
}

void renderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureBrick[0]);
glTexCoord2i(0, 0);
glVertex2i(10, 10);
glTexCoord2i(1, 0);
glVertex2i(10, -10);
glTexCoord2i(1, 1);
glVertex2i(-10, -10);
glTexCoord2i(0, 1);
glVertex2i(-10, 10);
glEnd();
glPopMatrix();
glutSwapBuffers();
glDisable(GL_TEXTURE_2D);
}

Share on other sites

Hey Phil,

How I've done it before is to divide the number of sprites in a column, and row by 1.0

y_stride = 1.0f / number_of_sprites_in_column
x_stride = 1.0f / number_of_sprites_in_row

And define a "step" rate for my animation, that is, how often should I increment the current animation U/V values by the aforementioned strides.

With these u/v values, you can then plug them into your immediate mode code there with the spritesheet bound.

Share on other sites
Posted (edited)

well I have done this before, but it has been awhile since I have done this. I think that I can use the glTexcoord2i function, thanks for all the  help.

Edited by phil67rpg

Share on other sites

use the float version instead. The image coordinate space are all values between 0.0 and 1.0

Share on other sites
Posted (edited)

I have changed my code, but I still do not know how to animate sprites also I have got to draw two  sprites on the screen.

void init()
{
}

void renderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureBrick[0]);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(100.0f, 10.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(100.0f, -10.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(80.0f, -10.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(80.0f, 10.0f);
glEnd();

glBindTexture(GL_TEXTURE_2D, textureBrick[1]);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(-100.0f, 10.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(-100.0f, -10.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(-80.0f, -10.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(-80.0f, 10.0f);
glEnd();
glPopMatrix();
glutSwapBuffers();
glDisable(GL_TEXTURE_2D);
}

Edited by phil67rpg

Share on other sites

Hey Phil,

ignoring your past record here so you don't feel like we are keeping tabs on you , however, you need to give us more then

Quote

I ﻿have changed my code, but I still do not know how to animate sprites also I have got to draw two  sprites o﻿n the screen.

and then reposting your wall of code. That tells me very little on what exactly you are struggling with. Was it how I phrased it? Is there something I said that came off as ambiguous, or confusing? Let me know!

What I STRONGLY advise is to avoid copy/pasting code; hoping it works, and resting all your hopes on a forum to essentially crowd-build your application for you. You will not get far in any flavor of software development with that approach.

Share on other sites
Posted (edited)

well I figured out  how to animate a sprite but I don't know what game I should work on. also what is a good link for finding sprites?

Edited by phil67rpg

Share on other sites
5 hours ago, phil67rpg said:

I don't know what game I should work on.

Choose a very simple game. Remake Breakout or Space Invaders.

Share on other sites

does anyone remember 1942 I am going to make a game like space invaders only using planes and a vertical side scroller.

Share on other sites
This topic is now closed to further replies.

• Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 22
• 17
• 13
• 14