OpenGL Licht
-
Hallo,
mit dem folgenden Programm habe ich zwei Probleme, die ich nicht verstehe:
1. Beim Starten wird ein graues Rechteck angezeigt; ohne jegliches Licht. Warum wird das Licht nicht beim Start aktiviert?
2. Drücke ich die Tasten 'd' oder 'e' sieht es so aus, als ob das Licht so leuchtet wie gewünscht. Die Intensität des Lichts hängt aber davon ab, ob zuletzt 'd' oder 'e' gedrückt wurde. Warum ist das so?extern "C" { #include <GL/glut.h> } static double camx = 20.0; static double camy = 20.0; static double camz = 100.0; void init() { glClearColor(0.0, 0.0, 0.0, 0.0); glShadeModel(GL_FLAT); GLfloat light[] = {1.0, 1.0, 1.0, 1.0 }; glLightfv(GL_LIGHT1, GL_DIFFUSE, light); glLightfv(GL_LIGHT1, GL_SPECULAR, light); glEnable(GL_LIGHTING); glEnable(GL_LIGHT1); glEnable(GL_DEPTH_TEST); glPolygonMode(GL_FRONT, GL_FILL); glPolygonMode(GL_BACK, GL_LINE); } void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(camx, camy, camz, camx, camy, 0.0, 0.0, 1.0, 0.0); glColor3f(1.0, 0.0, 0.0); for (int x = 0; x < 40; ++x) { for (int y = 0; y < 40; ++y) { glNormal3f(0.0f, 0.0f, 1.0f); glBegin(GL_QUADS); glVertex3i(x+1, y, 0); glVertex3i(x+1, y+1, 0); glVertex3i(x, y+1, 0); glVertex3i(x, y, 0); glEnd(); } } GLfloat light_position[] = { camx, camy, 20.0, 1.0 }; glLightfv(GL_LIGHT1, GL_POSITION, light_position); glutSwapBuffers(); } void reshape(int w, int h) { glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, (GLfloat) w / (GLfloat) h, 1.0, 200.0); } void keyboard(unsigned char key, int x, int y) { if (key == 'e') camz += 5; if (key == 'd') camz -= 5; glutPostRedisplay(); } int main(int argc, char ** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutInitWindowPosition(0, 0); glutCreateWindow("Example"); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMainLoop(); }
-
weil du das licht erst anmachst nachdem das polygon schon gezeichnet worden ist:
glBegin(GL_QUADS); // ... glEnd(); glLightfv(GL_LIGHT1, GL_POSITION, light_position); glutSwapBuffers();beim naechsten durchlauf ist es dann noch an...
-
hellihjb schrieb:
weil du das licht erst anmachst nachdem das polygon schon gezeichnet worden ist:
glBegin(GL_QUADS); // ... glEnd(); glLightfv(GL_LIGHT1, GL_POSITION, light_position); glutSwapBuffers();beim naechsten durchlauf ist es dann noch an...
Ohja. Danke. Ich dachte, dass die ganzen Berechnungen nach der Funktion durchgeführt werden.