PDA

View Full Version : Perspective projection problem



arseue
10-09-2004, 06:34 AM
hi, I has a problem about showing an obejct with perspective projection. The problem is that the wired object have some miss lines when displaying and the solid object also has unusual display.

Here is my code:


#include <GL/glut.h>

// Initialize the OpenGL propramming environment.
void init(void);

// Handle display events.
void display(void);

// Handle window events.
void reshape(int w, int h);

// Handle mouse events.
void mouse(int button, int state, int x, int y);

// Handle mouse pointer moving events.
void motion(int x, int y);

// Handle keyorard events.
void keyboard(unsigned char key, int x, int y);

int winWidth = 800; // The width of the window
int winHeight = 600; // The heitht of the window
int xRotation = 0; // The angle of rotation about x-axis
int yRotation = 0; // The angle of rotation about y-axis

int main(int argc, char** argv)
{
// Initialize and creat a window.
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_ACCUM);
glutInitWindowSize(winWidth,winHeight);
glutInitWindowPosition(100,100);
glutCreateWindow("OpenGL Project");

init();

// Handle window and input events.
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();

return 0;
}

// Initialize the OpenGL propramming environment.
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearAccum(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);

glShadeModel(GL_SMOOTH);

glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);

// Create the display list.
glNewList(1, GL_COMPILE);
glutWireTeapot(0.5);
glEndList();

glNewList(2, GL_COMPILE);
glutSolidTeapot(0.5);
glEndList();

// initialize GL_LIGHT0.
GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 }; // Ambient value of GL_LIGHT0
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; // Diffuse value of GL_LIGHT0
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 }; // Specular vaule of GL_LIGHT0
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; // Poition of GL_LIGHT0

glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glEnable(GL_LIGHT0);
}

// Handle display events.
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_ACCUM_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, winWidth/2, winWidth / winHeight * winWidth/2);
glTranslatef(0.0, 0.0, -2.5); // Move camera away.
glRotated(xRotation, 1.0, 0, 0);
glRotated(yRotation, 0, 1.0, 0);

glCallList(1); // Display the models.
//glutWireSphere(0.5,100,100);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(winWidth/2, 0, winWidth/2, winWidth / winHeight * winWidth/2);
glTranslatef(0.0, 0.0, -2.5); // Move camera away.
glRotated(xRotation, 1.0, 0, 0);
glRotated(yRotation, 0, 1.0, 0);

glCallList(2); // Display the models.

glFlush();
glutSwapBuffers();
}

// Handle window events.
void reshape(int w, int h)
{
winWidth = w;
winHeight = h;
glViewport(0, 0, (GLsizei) w, (GLsizei) h);

// Perspective projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, winWidth / winHeight, 0, 10);
}

// Handle keyorard events
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'a': case 'A':
yRotation = (yRotation - 10) % 360;
glutPostRedisplay();
break;

case 'd': case 'D':
yRotation = (yRotation + 10) % 360;
glutPostRedisplay();
break;

case 's': case 'S':
xRotation = (xRotation - 10) % 360;
glutPostRedisplay();
break;

case 'w': case 'W':
xRotation = (xRotation + 10) % 360;
glutPostRedisplay();
break;

case 27:
exit(0);
break;

default:
break;
}
}Thanks for help.

chowe6685
10-09-2004, 10:30 AM
for starters

gluPerspective(45, winWidth / winHeight, 0, 10);

don't set the near plane (3rd parameter) to 0, try something like .01