问题
Guys, I'm trying to finish up my homework but I'm having some problems here on these models on openGL... any Idea why is my draw not happening? One thing that strange is that if I change to gluPerspective it works..
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
static int shoulder = 0;
static int elbow = 0;
void init(void) {
glClearColor(1.0, 1.0, 1.0, 0.0);
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
/* BASE */
glRotatef((GLfloat) shoulder, 0.0, 0.0, 1.0);
glTranslatef(1.0, 0.0, 0.0);
glPushMatrix();
//glScalef(2.0, 0.4, 1.0);
glBegin(GL_QUADS);
glColor3f(0, 0, 0);
glVertex2f(0.0, 0.0);
glVertex2f(0.0, 10.0);
glVertex2f(10.0, 10.0);
glVertex2f(10.0, 0.0);
glEnd();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void reshape(int w, int h) {
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho((GLfloat)-w/2, (GLfloat)w/2, (GLfloat)-h/2, (GLfloat)h/2, -1.0, 1.0); // modo de projecao ortogonal
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
}
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 's':
shoulder = (shoulder + 5) % 360;
glutPostRedisplay();
break;
case 'S':
shoulder = (shoulder - 5) % 360;
glutPostRedisplay();
break;
case 'e':
elbow = (elbow + 5) % 360;
glutPostRedisplay();
break;
case 'E':
elbow = (elbow - 5) % 360;
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
回答1:
The polygon you draw is outside of the volume set up by glOrtho(). You will only see points with Z coordinate between -1 and 1.
This part of your code will cause drawing outside of that range.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
Either replace the glOrtho()'s last 2 parameters to something like -10, +10 or remove that glTranslatef()
来源:https://stackoverflow.com/questions/2799231/opengl-problems-with-gl-modelview-gl-projection