OpenGL学习笔记 2 让正方形动起来

在笔记1里面我记录下来渲染一个最基本的小三角形需要哪些准备工作以及程序的主要框架,这一节我继续跟上蓝宝书的脚步,试着理解二维动画怎么做出来的。这个笔记主要记录了 消息相应应该用什么函数以及应该加在程序的什么位置比较合适。

下面的内容是在屏幕中央绘制一个正方形,并且可以按照我们键盘上的上下左右四个按键进行移动,源码如下:

当然你也需要链接freeglut_static.lib以及gltools.lib,并将头文件include 进去

// Move.cpp
// Move a Block based on arrow key movements

#include 	// OpenGL toolkit
#include 

#ifdef __APPLE__
#include 
#else
#define FREEGLUT_STATIC
#include 
#endif

GLBatch	squareBatch;
GLShaderManager	shaderManager;


GLfloat blockSize = 0.1f;
GLfloat vVerts[] = { -blockSize, -blockSize, 0.0f,
blockSize, -blockSize, 0.0f,
blockSize, blockSize, 0.0f,
-blockSize, blockSize, 0.0f };

///////////////////////////////////////////////////////////////////////////////
// This function does any needed initialization on the rendering context. 
// This is the first opportunity to do any OpenGL related tasks.
void SetupRC()
{
	// Black background
	glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

	shaderManager.InitializeStockShaders();

	// Load up a triangle
	squareBatch.Begin(GL_TRIANGLE_FAN, 4);
	squareBatch.CopyVertexData3f(vVerts);
	squareBatch.End();
}

// Respond to arrow keys by moving the camera frame of reference
void SpecialKeys(int key, int x, int y)
{
	GLfloat stepSize = 0.025f;

	GLfloat blockX = vVerts[0];   // Upper left X
	GLfloat blockY = vVerts[7];  // Upper left Y

	if (key == GLUT_KEY_UP)
		blockY += stepSize;

	if (key == GLUT_KEY_DOWN)
		blockY -= stepSize;

	if (key == GLUT_KEY_LEFT)
		blockX -= stepSize;

	if (key == GLUT_KEY_RIGHT)
		blockX += stepSize;

	// Collision detection
	if (blockX < -1.0f) blockX = -1.0f;
	if (blockX >(1.0f - blockSize * 2)) blockX = 1.0f - blockSize * 2;;
	if (blockY < -1.0f + blockSize * 2)  blockY = -1.0f + blockSize * 2;
	if (blockY > 1.0f) blockY = 1.0f;

	// Recalculate vertex positions
	vVerts[0] = blockX;
	vVerts[1] = blockY - blockSize * 2;

	vVerts[3] = blockX + blockSize * 2;
	vVerts[4] = blockY - blockSize * 2;

	vVerts[6] = blockX + blockSize * 2;
	vVerts[7] = blockY;

	vVerts[9] = blockX;
	vVerts[10] = blockY;

	squareBatch.CopyVertexData3f(vVerts);

	
}





///////////////////////////////////////////////////////////////////////////////
// Called to draw scene
void RenderScene(void)
{
	// Clear the window with current clearing color
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

	GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f };
	shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vRed);
	squareBatch.Draw();

	// Flush drawing commands
	glutSwapBuffers();
	glutPostRedisplay();
}



///////////////////////////////////////////////////////////////////////////////
// Window has changed size, or has just been created. In either case, we need
// to use the window dimensions to set the viewport and the projection matrix.
void ChangeSize(int w, int h)
{
	glViewport(0, 0, w, h);
}

///////////////////////////////////////////////////////////////////////////////
// Main entry point for GLUT based programs
int main(int argc, char* argv[])
{
	gltSetWorkingDirectory(argv[0]);

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
	glutInitWindowSize(800, 600);
	glutCreateWindow("Move Block with Arrow Keys");

	GLenum err = glewInit();
	if (GLEW_OK != err)
	{
		// Problem: glewInit failed, something is seriously wrong.
		fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
		return 1;
	}

	glutReshapeFunc(ChangeSize);
	glutDisplayFunc(RenderScene);
	glutSpecialFunc(SpecialKeys);

	SetupRC();

	glutMainLoop();
	return 0;
}

OpenGL学习笔记 2 让正方形动起来_第1张图片

复制进入你的电脑当中,看看它能不能动起来吧!

源码解读:

1. 和最基本的三角形渲染一样,我们需要设置视区函数,设置渲染方式

glutReshapeFunc(ChangeSize);
glutDisplayFunc(RenderScene);

2.接下来就是本程序的核心部分了,让正方形动起来。这里面主要对正方形四个点进行坐标计算变换,最后将计算过后点拷贝进入程序存储中,以便最后的setupRC

squareBatch.CopyVertexData3f(vVerts);

glutSpecialFunc(SpecialKeys);

glutSpecialFunc()函数在按键的时候被调用,它在按键的时候接受一个相应的按键编码,以及在移动鼠标时的指针位置,可以放在绘制函数setupRC之前或之后。

3. 最后就是要把我们的顶点连接起来绘制出正方形了

SetupRC();

注意RenderScence函数中最后一个函数:glutPostRedisplay告诉GLUT窗口需要重新绘制,程序会调用glutdisplayfunc函数进行窗口的更新,也就是调用RenderScence函数,这样程序就是一直持续自动刷新了。

void RenderScene(void)
{
	// Clear the window with current clearing color
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

	GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f };
	shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vRed);
	squareBatch.Draw();

	// Flush drawing commands
	glutSwapBuffers();
	glutPostRedisplay();
}


你可能感兴趣的:(OpenGL)