Translate and Rotate

Notes:

Implement model, view, and projection matrices.

Source:

// ModelviewProjection.cpp
// Example if combining the ModelView and Projection Matrices

#include <GLTools.h>	// OpenGL toolkit
#include <GLMatrixStack.h>
#include <GLFrame.h>
#include <GLFrustum.h>
#include <GLGeometryTransform.h>
#include <GLBatch.h>
#include <StopWatch.h>
#include <math.h>

#ifdef __APPLE__
#include <glut/glut.h>
#else
#define FREEGLUT_STATIC
#include <GL/glut.h>
#endif

// Global view frustum class
GLFrustum           viewFrustum;

// The shader manager
GLShaderManager     shaderManager;

// The torus
//GLTriangleBatch     torusBatch;

// The cylinder
GLTriangleBatch     cylinderBatch;

// Set up the viewport and the projection matrix
void ChangeSize(int w, int h)
{
	// Prevent a divide by zero
	if(h == 0)
    {
		h = 1;
    }
    
	// Set Viewport to window dimensions
    glViewport(0, 0, w, h);
    
    viewFrustum.SetPerspective(35.0f, float(w)/float(h), 1.0f, 1000.0f);
}

// Called to draw scene
void RenderScene(void)
{
    // Set up time based animation
    static CStopWatch rotTimer;
    float yRot = rotTimer.GetElapsedSeconds() * 60.0f;
    
	// Clear the window and the depth buffer
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Matrix Variables
    M3DMatrix44f mTranslate, mRotate, mSpin, mModelview, mModelViewProjection, mIdent;

    // Create an identity matrix
    m3dLoadIdentity44(mIdent);
    
    // Create a translation matrix to move the torus back and into sight
    m3dTranslationMatrix44(mTranslate, 0.0f, 0.5f, -4.5f);

    // Create the cylinder's rotation matrix so it stands vertically
    m3dRotationMatrix44(mRotate, m3dDegToRad(90), 1.0f, 0.0f, 0.0f);
    
    // Rotate the cylinder based on the current value of yRot
    m3dRotationMatrix44(mSpin, m3dDegToRad(yRot), 0.0f, 0.0f, 1.0f);

    // combine rotations
    m3dMatrixMultiply44(mRotate, mRotate, mSpin);
    
    // Multiply them together
    m3dMatrixMultiply44(mModelview, mTranslate, mRotate);
    
    // Add the modelview matrix to the projection matrix, 
    // the final matrix is the ModelViewProjection matrix.
    m3dMatrixMultiply44(mModelViewProjection, viewFrustum.GetProjectionMatrix(),mModelview);
		
    // colors
    GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f };
    GLfloat vMaroon[] = { 0.5f, 0.0f, 0.0f, 1.0f };

    // Pass this completed matrix to the shader, and render the torus
    shaderManager.UseStockShader(GLT_SHADER_FLAT, mModelViewProjection, vRed);  
    //torusBatch.Draw();
    cylinderBatch.Draw();

    // Draw outline
    glPolygonOffset(-1.0f, -1.0f);      // Shift depth values
    glEnable(GL_POLYGON_OFFSET_LINE);   // our important offset
    glEnable(GL_LINE_SMOOTH);           // fancy smooth lines
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    // Draw wireframe version of geometry
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    glLineWidth(2.5f);
    shaderManager.UseStockShader(GLT_SHADER_FLAT, mModelViewProjection, vMaroon);
    cylinderBatch.Draw();

    // Put everything back the way we found it
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    glDisable(GL_POLYGON_OFFSET_LINE);
    glLineWidth(1.0f);
    glDisable(GL_BLEND);
    glDisable(GL_LINE_SMOOTH);

    // Swap buffers, and immediately refresh
    glutSwapBuffers();
    glutPostRedisplay();
}

// This function does any needed initialization on the rendering
// context. 
void SetupRC()
	{
	// Black background
	glClearColor(0.2f, 0.2f, 0.2f, 1.0f );

    glEnable(GL_DEPTH_TEST);

    shaderManager.InitializeStockShaders();
  
    // This makes a torus
    //gltMakeTorus(torusBatch, 0.4f, 0.15f, 30, 30);
    // This generates a cylinder
    gltMakeCylinder(cylinderBatch,
                    0.5,    // base radius
                    0.5,    // top radius
                    1.0,    // length
                    16,     // slices
                    1);     // stacks

    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	}

///////////////////////////////////////////////////////////////////////////////
// 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 | GLUT_STENCIL);
	glutInitWindowSize(480, 480);
	glutCreateWindow("ModelViewProjection");
    glutReshapeFunc(ChangeSize);
    glutDisplayFunc(RenderScene);
    
	GLenum err = glewInit();
	if (GLEW_OK != err) 
    {
		fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
		return 1;
    }
	
	SetupRC();
    
	glutMainLoop();
	return 0;
    }

Answer:


Back to index