user969416
user969416

Reputation:

OpenGL gluperspective()

I think I might have greatly misunderstood the usage of gluperspective() and require help.

I currently have this code which produces a blank black screen (from what I thought I understood, it should produce two squares, one closer and one further in distance). It does not do this and I need to understand why not.

#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include <GL/glu.h>
#include "functionfile.h"

int main(int argc, char **argv)
{
init_perspective(640, 480);

glTranslatef(0,0,10);
glColor4f(1.0,1.0,1.0,1.0);

glBegin(GL_QUADS);
glVertex3f(0,  0,0);
glVertex3f(10, 0,0);
glVertex3f(10,10,0);
glVertex3f(0, 10,0);
glEnd();

glLoadIdentity();
glTranslatef(0,0,50);

glBegin(GL_QUADS);
glVertex3f(0,   0, 0);
glVertex3f(10,  0, 0);
glVertex3f(10, 10, 0);
glVertex3f(0,  10, 0);
glEnd();

glLoadIdentity();

SDL_GL_SwapBuffers();

SDL_Delay(2000);

SDL_Quit();

return 0;
}

// from header file "functionfile.h" 

void init_perspective(int width, int height)

{

SDL_Init(SDL_INIT_EVERYTHING);
SDL_SetVideoMode(width, height, 32, SDL_OPENGL);
SDL_WM_SetCaption( "OpenGL Test", NULL );

glClearColor( 0, 0, 0, 0 );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective(45, width/height, 1, 100);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glClear( GL_COLOR_BUFFER_BIT );

}

Upvotes: 0

Views: 3507

Answers (2)

datenwolf
datenwolf

Reputation: 162164

Well, your code contains several issues. First and foremost you lack to set the viewport (see glViewport documentation). Next you should know that OpenGL is a state machine and it is good practice to set all required state prior to each rendering iterations. That includes setting viewport and projection. So glViewport and setting the projection matrix semantically belongs to the drawing commands, not the window management ones.

In your case from the point of view of program execution there is yet no difference, but as soon as you introduce a animation and event loop, this becomes important. Also always remember, that you can change viewport projection parameters at any time, if the rendering process demands it, say for drawing a minimap overlay in the corner of the screen.

Upvotes: 3

Mike Boers
Mike Boers

Reputation: 6735

Your camera is looking down the negative Z axis, so your glTranslatef calls are moving the geometry behind the camera, instead of in front.

Upvotes: 4

Related Questions