OpenGL(三)特殊按键,控制三角形移动

一、代码

1.在上篇代码中新增代码

//key,特殊按键编码,x,y位置
void SpecialKeys(int key, int x, int y)
{
	GLfloat stepSize = 0.025f;  //增加的步量
	GLfloat blockX = 0; 
	GLfloat blockY = 0;

	//上
	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;
	}

	std::cout << "blockX = " << blockX << std::endl;
	std::cout << "blockY = " << blockY << std::endl;

	//改变顶点数据
	vVertex[0] = vVertex[0] + blockX;
	vVertex[1] = vVertex[1] + blockY;

	vVertex[3] = vVertex[3] + blockX;
	vVertex[4] = vVertex[4] + blockY;

	vVertex[6] = vVertex[6] + blockX;
	vVertex[7] = vVertex[7] + blockY;

	batch.CopyVertexData3f(vVertex); //复制顶点到批处理中
	glutPostRedisplay(); //更新窗口内容
}

glutSpecialFunc(SpecialKeys);         //特殊按键回调

2.完整代码

#include 
#include 
#include 

GLBatch batch; //批处理
GLShaderManager shaderManager; //着色器管理

//定义顶点数据
GLfloat vVertex[] = { -0.5f, -0.5f, 0.0f,
					  0.5f, -0.5f, 0.0f,
					  0.0f, 0.5f, 0.0f };

void ChangeSize(int width, int height)
{
	glViewport(0, 0, width, height);  //改变窗口大小
}

void SetupRc(void)
{
	glClearColor(0.0f, 0.0f, 1.0f, 1.0f);    //背景颜色

	shaderManager.InitializeStockShaders();  //初始化着色器

	batch.Begin(GL_TRIANGLES, 3);
	batch.CopyVertexData3f(vVertex);  //复制顶点数据到批处理中
	batch.End();
}

void MyDisplay(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); //清除特定的缓冲区
	GLfloat vRed[] = { 0.0f, 1.0f, 0.0f, 1.0f };  //RGBA
	shaderManager.UseStockShader(GLT_SHADER_IDENTITY ,vRed);  //颜色传递到存储着色器中
	batch.Draw();        //绘制
	glutSwapBuffers();   //交换buffer,双缓冲
}

//key,特殊按键编码,x,y位置
void SpecialKeys(int key, int x, int y)
{
	GLfloat stepSize = 0.025f;  //增加的步量
	GLfloat blockX = 0;
	GLfloat blockY = 0;

	//上
	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;
	}

	//改变顶点数据
	vVertex[0] = vVertex[0] + blockX;
	vVertex[1] = vVertex[1] + blockY;

	vVertex[3] = vVertex[3] + blockX;
	vVertex[4] = vVertex[4] + blockY;

	vVertex[6] = vVertex[6] + blockX;
	vVertex[7] = vVertex[7] + blockY;

	batch.CopyVertexData3f(vVertex); //复制顶点到批处理中
	glutPostRedisplay(); //更新窗口内容
}

int main(int argc, char * argv[])
{
	gltSetWorkingDirectory(argv[0]);   //设置工作目录
	glutInit(&argc, argv);             //初始化glut
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL); //显示类型模式
	glutInitWindowSize(800, 600);      //窗口大小
	glutCreateWindow("第一个OpenGL");  //创建窗口
	glutReshapeFunc(ChangeSize);       //改变窗口大小
	glutDisplayFunc(MyDisplay);        //显示绘制回调
	glutSpecialFunc(SpecialKeys);         //特殊按键回调
	GLenum err = glewInit();           //驱动程序初始化过程是否出错
	if (err != GLEW_OK)
	{
		return 1;
	}
	SetupRc();                         
	glutMainLoop();                    //调用之后才能显示
	return 0;
}

二、运行效果

按上下左右键移动三角形

OpenGL(三)特殊按键,控制三角形移动_第1张图片





你可能感兴趣的:(OpenGL)