PDA

View Full Version : glTranslatef



vikramsingh
06-22-2011, 05:53 AM
in the following code

// Triangle.cpp

// Our first OpenGL program that will just draw a triangle on the screen.



#include <GLTools.h> // OpenGL toolkit

#include <GLMatrixStack.h>

#include <GLFrame.h>

#include <GLFrustum.h>

#include <GLBatch.h>

#include <GLGeometryTransform.h>



#include <math.h>

#ifdef __APPLE__

#include <glut/glut.h>

#else

#define FREEGLUT_STATIC

#include <GL/glut.h>

#endif



GLBatch triangleBatch;

GLShaderManager shaderManager;

GLMatrixStack modelViewMatrix;

GLMatrixStack projectionMatrix;

GLFrame cameraFrame;

GLFrame objectFrame;

GLFrustum viewFrustum;



GLGeometryTransform transformPipeline;

M3DMatrix44f shadowMatrix;





GLfloat vGreen[] = { 0.0f, 1.0f, 0.0f, 1.0f };

GLfloat vBlack[] = { 0.0f, 0.0f, 0.0f, 1.0f };

///////////////////////////////////////////////////////////////////////////////

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

viewFrustum.SetPerspective(35.0f, float(w) / float(h), 1.0f, 500.0f);

projectionMatrix.LoadMatrix(viewFrustum.GetProject ionMatrix());

modelViewMatrix.LoadIdentity();

}





///////////////////////////////////////////////////////////////////////////////

// This function does any needed initialization on the rendering context.

// This is the first opportunity to do any OpenGL related tasks.

void SetupRC()

{

// Blue background

glClearColor(0.0f, 0.0f, 1.0f, 1.0f );

shaderManager.InitializeStockShaders();

glEnable(GL_DEPTH_TEST);

transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);



cameraFrame.MoveForward(-5.0f);

// Load up a triangle

GLfloat vertices[] = {0.5f,0.5f,0.5f, -0.5f,0.5f,0.5f, -0.5f,-0.5f,0.5f, 0.5f,-0.5f,0.5f, // v0-v0.5f-v2-v3

0.5f,0.5f,0.5f, 0.5f,-0.5f,0.5f, 0.5f,-0.5f,-0.5f, 0.5f,0.5f,-0.5f, // v0-v3-v4-v5

0.5f,0.5f,0.5f, 0.5f,0.5f,-0.5f, -0.5f,0.5f,-0.5f, -0.5f,0.5f,0.5f, // v0-v5-v6-v0.5f

-0.5f,0.5f,0.5f, -0.5f,0.5f,-0.5f, -0.5f,-0.5f,-0.5f, -0.5f,-0.5f,0.5f, // v0.5f-v6-v7-v2

-0.5f,-0.5f,-0.5f, 0.5f,-0.5f,-0.5f, 0.5f,-0.5f,0.5f, -0.5f,-0.5f,0.5f, // v7-v4-v3-v2

0.5f,-0.5f,-0.5f, -0.5f,-0.5f,-0.5f, -0.5f,0.5f,-0.5f, 0.5f,0.5f,-0.5f}; // v4-v7-v6-v5



triangleBatch.Begin(GL_QUADS, 24);

triangleBatch.CopyVertexData3f(vertices);

triangleBatch.End();

}







///////////////////////////////////////////////////////////////////////////////

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


glTranslatef(200.0f,200.0f,200.0f);


modelViewMatrix.PushMatrix();

M3DMatrix44f mCamera;

cameraFrame.GetCameraMatrix(mCamera);

modelViewMatrix.MultMatrix(mCamera);



M3DMatrix44f mObjectFrame;

objectFrame.GetMatrix(mObjectFrame);

modelViewMatrix.MultMatrix(mObjectFrame);



shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vBlack);



glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

glLineWidth(2.5f);
triangleBatch.Draw();

modelViewMatrix.PopMatrix();


// Perform the buffer swap to display back buffer

glutSwapBuffers();

}

void KeyPressFunc(unsigned char key, int x, int y)

{

switch (key)

{

case 27:

exit(0);

}

}



void SpecialKeys(int key, int x, int y)

{

if(key == GLUT_KEY_UP)

objectFrame.RotateWorld(m3dDegToRad(-5.0f), 1.0f, 0.0f, 0.0f);



if(key == GLUT_KEY_DOWN)

objectFrame.RotateWorld(m3dDegToRad(5.0f), 1.0f, 0.0f, 0.0f);



if(key == GLUT_KEY_LEFT)

objectFrame.RotateWorld(m3dDegToRad(-5.0f), 0.0f, 1.0f, 0.0f);



if(key == GLUT_KEY_RIGHT)

objectFrame.RotateWorld(m3dDegToRad(5.0f), 0.0f, 1.0f, 0.0f);



glutPostRedisplay();

}

///////////////////////////////////////////////////////////////////////////////

// Main entry point for GLUT based programs

int main(int argc, char* argv[])

{

gltSetWorkingDirectory(argv[0]);



glutInit(&amp;argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);

glutInitWindowSize(800, 600);

glutCreateWindow("Triangle");

glutReshapeFunc(ChangeSize);

glutDisplayFunc(RenderScene);

glutSpecialFunc(SpecialKeys);

glutKeyboardFunc(KeyPressFunc);

GLenum err = glewInit();

if (GLEW_OK != err) {

fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));

return 1;

}



SetupRC();



glutMainLoop();

return 0;

}

the glTranslatef() function is not translating the cude sideways. why ?
just check the renderScene funtion