Jump to content
  • Advertisement
phil67rpg

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

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);
}

 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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()
{
	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);
}

 

Edited by phil67rpg

Share this post


Link to post
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 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. 

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!