PDA

View Full Version : Problem with gluLookAt and Viewport



alexjascott
08-12-2004, 04:28 AM
I an drawing a 2d style game, but I would like to be able to zoom up and down.

The 2d stuff works fine, but for some reason I cannot get glulookat working properly. If the camera Z value is positive, it displays normally, and if it goes negative it does not display anything.

Do I need to be doing something with the projection matrix?
</font><blockquote><font size="1" face="Verdana, Arial">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;"> private void render()
{
GL11.glViewport(VIEWPORT_X, VIEWPORT_Y, VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
// set modelview
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GLU.gluPerspective( 45.0f, VIEWPORT_WIDTH/VIEWPORT_HEIGHT, 0.1f, 1000f );
GL11.glLoadIdentity(); // reset the matrix

//clear background
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);


GLU.gluLookAt( ships[0].position.x, ships[0].position.y, 0f,
ships[0].position.x, ships[0].position.y, -ZOOM,
0f, 1f, 0f );

GL11.glTranslatef( VIEWPORT_WIDTH/2, VIEWPORT_HEIGHT/2, 0f );
GL11.glPushMatrix();
renderGalaxyTexture();
renderGalaxy();
renderPlanets();
renderBullets();
renderShips();

GL11.glPushMatrix();
{
// GL11.glTranslatef( -VIEWPORT_WIDTH/2, -VIEWPORT_HEIGHT/2, 0f );
GL11.glTranslatef( ships[0].position.x, ships[0].position.y, 0f );
renderShip();
}
GL11.glPopMatrix();
GL11.glPopMatrix();

GL11.glPushMatrix();
{
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glEnable( GL11.GL_BLEND );
GL11.glPolygonMode(GL11.GL_FRONT, GL11.GL_FILL);
GL11.glPolygonMode(GL11.GL_BACK, GL11.GL_FILL);
GL11.glColor3f(1.0f, 1.0f, 1.0f);

// glPrint( 0, VIEWPORT_HEIGHT-FONT_HEIGHT, 0, "FPS = " + timer.fps +"(" +timer.frameInterval +"ms)" );
glPrint( 0, VIEWPORT_HEIGHT-FONT_HEIGHT, 0, "" + timer.fps );
}
GL11.glPopMatrix();

GL11.glFlush();

//
// now for the non-game screen stuff
//
// set projection matrix
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluOrtho2D( 0, SCREEN_WIDTH, 0, SCREEN_HEIGHT);

// modelview
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GLU.gluPerspective( 45.0f, SCREEN_WIDTH/SCREEN_HEIGHT,0.1f,1000 );
GL11.glViewport( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT );

GL11.glLoadIdentity(); // reset the matrix

renderConsole();
renderScores();
renderMap();

GL11.glPushMatrix();
{
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glPolygonMode(GL11.GL_FRONT, GL11.GL_FILL);
GL11.glPolygonMode(GL11.GL_BACK, GL11.GL_FILL);
GL11.glEnable( GL11.GL_BLEND );
GL11.glColor3f(0f,0f,0f); // black

glPrint( VIEWPORT_X+VIEWPORT_WIDTH, SCREEN_HEIGHT-FONT_HEIGHT-GUI_TOP_Y, 0, "Engines" );

if ( ships[0].state == Ship.STATE_FLYING )
{
glPrint( VIEWPORT_X+VIEWPORT_WIDTH, SCREEN_HEIGHT-FONT_HEIGHT*2-GUI_TOP_Y, 0, (int)(100*ships[0].thrust)/ships[0].SHIP_SPEED +"%");
glPrint( VIEWPORT_X+VIEWPORT_WIDTH, SCREEN_HEIGHT-FONT_HEIGHT*3-GUI_TOP_Y, 0, "(" +(int)ships[0].thrust +")" );
}
else if ( ships[0].state == Ship.STATE_WARPPREP

digitec devil
08-12-2004, 06:31 PM
You should do gluPerspective when GL_PROJECTION is the current matrix mode, so switch to projection, load identity, do gluPerspective, switch to modelview, load identity, and clear background etc. as you normally were. By looking at your code, you might want to post this in beginners section; it doesn't seem like you completely understand what you're doing with the matrices.

alexjascott
08-13-2004, 12:29 AM
Yeah sorry that code sample wasnt the best - that was after a day of hacking things around to see if I could solve the problem!

The whole thing can be found at www.alexjascott.demon.co.uk/trek.zip (http://www.alexjascott.demon.co.uk/trek.zip) if anyone felt nice enough to take a look (and its an interesting little project atm)!