opengl序列帧特效

前些天逛论坛,发现了些png序列帧的图片资源。一时兴起写个程序玩一玩。

 

理论非常简单,循环改变纹理即可。

 

上代码

class SequenceFrame
{
public:
	SequenceFrame()
	{

	}

	void Init(const char* filePath, const char* fileHeadName, int sumFrame);
	void update();
	void drawRect(float left, float right, float bottom, float top,float z);


	GLuint* mTextures;
	int currentFrame; // 当前的帧
	int maxFrame;  // 最大的帧
};
#include "SequenceFrame.h"

GLuint createTexture(const char* fileName)
{
	GLuint texture;
	texture = SOIL_load_OGL_texture(fileName, 0, 0, SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_INVERT_Y);
	return texture;
}

void SequenceFrame::Init(const char* filePath, const char* fileHeadName, int sumFrame)
{
	mTextures = new GLuint[sumFrame];
	//glGenTextures(sumFrame, mTextures);

	maxFrame = sumFrame;
	currentFrame = 0;

	char imgFileName[256];
	for (int i = 0; i < maxFrame; ++i)
	{
		memset(imgFileName, 0, 256);
		sprintf(imgFileName,"%s/%s%d.png", filePath, fileHeadName, i+1);
		mTextures[i] = createTexture(imgFileName);
		printf("%s\n", imgFileName);
	}
}

void SequenceFrame::update()
{
	currentFrame += 1;
	
	if (currentFrame == maxFrame)
		currentFrame = 0;
	//printf("%d   ", currentFrame);
}

void SequenceFrame::drawRect(float left, float right, float bottom, float top,float z)
{
	glBindTexture(GL_TEXTURE_2D, mTextures[currentFrame]);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0f, 0.0f); glVertex3f(left, bottom, z);
	glTexCoord2f(1.0f, 0.0f); glVertex3f(right, bottom, z);
	glTexCoord2f(1.0f, 1.0f); glVertex3f(right, top, z);
	glTexCoord2f(0.0f, 1.0f); glVertex3f(left, top, z);
	glEnd();
}

 

Init参数如下例

test.Init("imageFrame/2", "1406-", 20);

项目资源如图

opengl序列帧特效_第1张图片

 画的时候,先draw再update就行了。效果还是挺炫酷的,挺唬人的技术

你可能感兴趣的:(opengl序列帧特效)