animating sprites

Started by
35 comments, last by Tom Sloper 4 years, 7 months ago

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]; 

GLuint loadTex(const char* texname)                                    
{
	/* load an image file directly as a new OpenGL texture */
	GLuint texture = SOIL_load_OGL_texture
	(
		texname,
		SOIL_LOAD_AUTO,
		SOIL_CREATE_NEW_ID,
		SOIL_FLAG_INVERT_Y
	);
	return texture;
}

void init()
{
	textureBrick[0] = loadTex("C:\\Users\\Owner\\Desktop\\img.png");
}

void renderScene(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPushMatrix();
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, textureBrick[0]);
	glBegin(GL_QUADS);
	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);
}

 

Advertisement

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.

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.

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

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()
{
	textureBrick[0] = loadTex("C:\\Users\\Owner\\Desktop\\img.png");
	textureBrick[1] = loadTex("C:\\Users\\Owner\\Desktop\\img.png");
}

void renderScene(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPushMatrix();
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, textureBrick[0]);
	glBegin(GL_QUADS);
	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]);
	glBegin(GL_QUADS);
	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);
}

 

A year ago, you claimed to have solved this. Maybe start looking there.

 

Hello to all my stalkers.

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 on 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. 

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?

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.

-- Tom Sloper -- sloperama.com

This topic is closed to new replies.

Advertisement