Translate and Rotate

Notes:

I modify the last exercise and translate/rotate it using matrices.

Source:

// Translate.cpp
// Move a Block based on arrow key movements using translation.

#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.5f;
GLfloat vVerts[] = { -blockSize, -blockSize, 0.0f, 
	 	      blockSize, -blockSize, 0.0f,
		      blockSize,  blockSize, 0.0f,
		     -blockSize,  blockSize, 0.0f};

// our Translate mutators X, Y, R
GLfloat xPos = 0.0f;
GLfloat yPos = 0.0f;
GLfloat zRot = 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()
{
	// Grey background
	glClearColor(0.2f, 0.2f, 0.2f, 1.0f );
    
	shaderManager.InitializeStockShaders();

	// Load up a triangle fan
	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;
    }

    if(key == GLUT_KEY_PAGE_UP)
    {
        zRot += 1.0f;
        if (zRot > 360) zRot = 0;
    }

    if(key == GLUT_KEY_PAGE_DOWN)
    {
        zRot -= 1.0f;
        if (zRot < -360) zRot = 0;
    }

	// Collision detection
	if(xPos < -0.5f) xPos = -0.5f;
	if(xPos >  0.5f) xPos =  0.5f;
	if(yPos < -0.5f) yPos = -0.5f;
	if(yPos >  0.5f) yPos =  0.5f;

    // force refresh
    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;

    // Just translate
    m3dTranslationMatrix44(mTranslationMatrix, xPos, yPos, 0.0f);
    //printf("xPos: %f\n", xPos);
    //printf("yPos: %f\n", yPos);
    //printf("zRot: %f\n", zRot);

    m3dRotationMatrix44(mRotationMatrix, m3dDegToRad(zRot), 0.0f, 0.0f, 1.0f);

    m3dMatrixMultiply44(mFinalTransform, mTranslationMatrix, mRotationMatrix);

    shaderManager.UseStockShader(GLT_SHADER_FLAT, mFinalTransform, vRed);
    squareBatch.Draw();

	// Flush drawing commands
	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(480, 480);
	glutCreateWindow("Move Block with Arrow Keys using translation");
	
	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;
}

Answer:


Back to index