OpenGL 着色器 平移缩放

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

#include <GLTools.h>	// OpenGL toolkit
#include <GLShaderManager.h>
#include <math3d.h>

#ifdef __APPLE__
#include <glut/glut.h>
#else
#define FREEGLUT_STATIC
#include <GL/glut.h>
#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 };

GLfloat xPos = 0.0f;
GLfloat yPos = 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(0.0f, 0.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;


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

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

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

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

	// Collision detection
	if (xPos < (-1.0f + blockSize)) xPos = -1.0f + blockSize;

	if (xPos > (1.0f - blockSize)) xPos = 1.0f - blockSize;

	if (yPos < (-1.0f + blockSize))  yPos = -1.0f + blockSize;

	if (yPos > (1.0f - blockSize)) yPos = 1.0f - blockSize;

	glutPostRedisplay();
}





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

	M3DMatrix44f mFinalTransform, mTranslationMatrix, mRotationMatrix;

	// 平移操作
	m3dTranslationMatrix44(mTranslationMatrix, xPos, yPos, 0.0f);

	// 每次绘制旋转5度
	static float yRot = 0.0f;
	yRot += 5.0f;
	m3dRotationMatrix44(mRotationMatrix, m3dDegToRad(yRot), 0.0f, 0.0f, 1.0f);

	//连接平移和旋转的结果到最终矩阵 //mFinalTransform为先平移再旋转 
	m3dMatrixMultiply44(mFinalTransform, mTranslationMatrix, mRotationMatrix);

	//使用矩阵将坐标存储到平面着色器中
	shaderManager.UseStockShader(GLT_SHADER_FLAT, mFinalTransform, vRed);
	squareBatch.Draw();

	// Perform the buffer swap
	glutSwapBuffers();
}



///////////////////////////////////////////////////////////////////////////////
// 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(600, 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;
}

猜你喜欢

转载自blog.csdn.net/a1317338022/article/details/78737581