/* Both normals and colors are assigned to the vertices */ /* Model is centered at origin so (unnormalized) normals are the same as the vertex values */ #include #include GLfloat vertices[][2] = {{1.0, -1.0}, {-1.0, 1.0}}; GLfloat normals[][2] = {{-1.0, -1.0},{1.0, -1.0}}; GLfloat colors[][3] = {{0.0, 0.0, 1.0},{1.0, 0.0, 0.0}}; void polygon(int a, int b) { /* draw a polygon via list of vertices */ /* Draw lines first...*/ glBegin(GL_LINES); glColor3fv(colors[a]); //glNormal2fv(normals[a]); glVertex2fv(vertices[a]); glColor3fv(colors[b]); //glNormal2fv(normals[b]); glVertex2fv(vertices[b]); glEnd(); /* ...than draw vertices*/ glBegin(GL_POINTS); glColor3fv(colors[a]); //glNormal2fv(normals[a]); glVertex2fv(vertices[a]); glColor3fv(colors[b]); //glNormal2fv(normals[b]); glVertex2fv(vertices[b]); glEnd(); } void vertex_mapping(void) { /* map vertices to faces */ polygon(0,1); } void display(void) { /* display callback, clear frame buffer and z buffer, swap buffers */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glPointSize(5); vertex_mapping(); glFlush(); glutSwapBuffers(); } /* Idle callback */ /* If you want to make your model do some constant movement, place any motion stuff here! */ void idle(void) { /* display(); */ glutPostRedisplay(); } void mouse(int btn, int state, int x, int y) { /* mouse callback */ if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) { /* NULL */ } if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) { /* NULL */ } if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) { /* NULL */ } } void myReshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= h) glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w, 2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0); else glOrtho(-2.0 * (GLfloat) w / (GLfloat) h, 2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0); glMatrixMode(GL_MODELVIEW); } void main(int argc, char **argv) { glutInit(&argc, argv); /* need both double buffering and z buffer */ glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow("colorcube"); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutIdleFunc(idle); glutMouseFunc(mouse); glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */ glutMainLoop(); }