PDA

View Full Version : Code for camera & viewport problem below



11-22-2003, 05:50 PM
#include "window.h"
#include <gl/glut.h>
#include "asteroid.h"
#include <iostream.h>
#include "ranNumGen.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

window::window()
{

}

window::~window()
{

}

window::drawWindow(int argc, char** argv)
{
glutInit (&argc, argv) ;
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB) ;
glutInitWindowSize (1024, 760) ;
glutInitWindowPosition (0, 0) ;
glutCreateWindow ("Asteroids") ;
}

window::init()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
cameraPos.x = 0.0;
cameraPos.y = 0.0;
cameraPos.z = 3.0;
}

ranNumGen randomObject(-2000, 2000);
float a = (randomObject.generateNumber() / 1000);
float b = (randomObject.generateNumber() / 1000);
float c = (randomObject.generateNumber() / 1000);
float d = (randomObject.generateNumber() / 1000);
float e = (randomObject.generateNumber() / 1000);
float f = (randomObject.generateNumber() / 1000);
float g = (randomObject.generateNumber() / 1000);
float h = (randomObject.generateNumber() / 1000);
float i = (randomObject.generateNumber() / 1000);

window::resize(int w, int h)
{
//control the 2d viewport
glViewport(0, 0, (GLsizei) w, (GLsizei) h);

//control the 3d clipping volume
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
const double aspect = (double)w/(double)h;
glFrustum(-1.0*aspect, 1.0*aspect, -1.0, 1.0, 8.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}


window::draw()
{
glLoadIdentity();
gluLookAt(cameraPos.x, cameraPos.y, cameraPos.z, 0.0,0.0,0.0,0.0,1.0,0.0);
int P_asteroid;
asteroid asteroid1;
P_asteroid = asteroid1.Gen3DObjectList();

glPushMatrix();
glScalef(0.4, 0.4, 0.4);
glTranslatef(-2, -2, -3);
glRotatef(angY,0.0,1.0,0.0);
glRotatef(angX,1.0,0.0,0.0);
glCallList(P_asteroid);

glPopMatrix();

glPushMatrix();
glScalef(0.4, 0.4, 0.4);
glTranslatef( 2, 2, 3);
glRotatef(angY,0.0,1.0,0.0);
glRotatef(angX,1.0,0.0,0.0);
glCallList(P_asteroid);
glPopMatrix();

glPushMatrix();
glScalef(0.4, 0.4, 0.4);
glTranslatef( -1, -1, -1);
glRotatef(angY,0.0,1.0,0.0);
glRotatef(angX,1.0,0.0,0.0);
glCallList(P_asteroid);
glPopMatrix();
glFlush();

glPushMatrix();
glScalef(0.4, 0.4, 0.4);
glTranslatef( 1, 1, 1);
glRotatef(angY,0.0,1.0,0.0);
glRotatef(angX,1.0,0.0,0.0);
glCallList(P_asteroid);
glPopMatrix();
glFlush();
}


window::inputKey(int key, int o, int p)
{
switch(key)
{
case GLUT_KEY_RIGHT:
angY += 5.0;
break;
case GLUT_KEY_LEFT:
angY -= 5.0;
break;
case GLUT_KEY_UP:
angX += 5.0;
break;
case GLUT_KEY_DOWN:
angX -= 5.0;
break;
case GLUT_KEY_PAGE_UP:
cameraPos.z -= 1.0f;
break;
case GLUT_KEY_PAGE_DOWN:
cameraPos.z += 1.0f;
break;
default:
break;
}
glutPostRedisplay(); //force refresh / re-render scene
}

Deiussum
11-24-2003, 05:28 AM
I see at least a couple of obvious problems.

You don't give any clue as to what it is you want us to fix for you. Giving us a bunch of code without saying what it is you expect it to do, and what it is doing instead, doesn't do much. Depending on what your problem is, you probably need to show your main function as well, so that we can see specifically when these class functions are called... Code tags are always nice to make code more readable

11-30-2003, 03:31 AM
Sorry this post was part of two posts that was immediatly next to one another, the other post contained the problem. Ive solved the problem now. I set the camera position in the wrong place, ive now moved it to the constructo and it works fine