- #include <stdlib.h>
- #include <stdio.h>
- #include <gl/glut.h>
- static GLfloat vertices[] = {-5.0, 0.0, 5.0,
- 5.0, 0.0, 5.0,
- 5.0, 0.0, -5.0,
- -5.0, 0.0, -5.0,
- -5.0, 10.0, 5.0,
- 5.0, 10.0, 5.0,
- 5.0, 10.0, -5.0,
- -5.0, 10.0, -5.0};
- /*
- static GLfloat vertices[] = {-0.25, 0.0, 0.25,
- 0.25, 0.0, 0.25,
- 0.25, 0.0, -0.25,
- -0.25, 0.0, -0.25,
- -0.25, 0.5, 0.25,
- 0.25, 0.5, 0.25,
- 0.25, 0.5, -0.25,
- -0.25, 0.5, -0.25};
- */
- static GLubyte front[] = {0, 1, 5, 4};
- static GLubyte right[] = {1, 2, 6, 5};
- static GLubyte back[] = {2, 6, 7, 3};
- static GLubyte left[] = {0, 4, 7, 3};
- static GLubyte bottom[] = {0, 1, 2, 3};
- static GLubyte top[] = {4, 5, 6, 7};
- static GLfloat colors[] = {0.5, 0.3, 0.3,
- 0.6, 0.5, 0.6,
- 0.8, 0.3, 0.1,
- 0.2, 0.5, 0.1,
- 0.4,0.8, 0.3,
- 0.9, 0.1, 0.32,
- 0.2, 0.1, 0.6,
- 0.9, 0.2, 0.1};
- void init(void)
- {
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glShadeModel(GL_SMOOTH);
- }
- void display(void)
- {
- glClear(GL_COLOR_BUFFER_BIT);
- glLoadIdentity();
- gluLookAt(25.0, 25.0, 25.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- // gluLookAt(2.0, 2.0, 2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- // glColor3f(1.0, 1.0, 1.0);
- glEnableClientState(GL_VERTEX_ARRAY);
- glEnableClientState(GL_COLOR_ARRAY);
- glColorPointer(3, GL_FLOAT, 0, colors);
- glVertexPointer(3, GL_FLOAT, 0, vertices);
- glDrawElements(GL_LINE_LOOP, 4, GL_UNSIGNED_BYTE,front);
- glDrawElements(GL_LINE_LOOP, 4, GL_UNSIGNED_BYTE, right);
- glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, back);
- glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, left);
- glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, bottom);
- glDrawElements(GL_LINE_LOOP, 4, GL_UNSIGNED_BYTE, top);
- glDisableClientState(GL_COLOR_ARRAY);
- glDisableClientState(GL_VERTEX_ARRAY);
- glFlush();
- }
- void reshape(int w, int h)
- {
- double aspect;
- if(0 == h)
- h = 1;
- aspect = 1.0*w/h;
- glViewport(0, 0, (GLsizei)w, (GLsizei)h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(45.0, aspect, 0.1, 50.0);
- glMatrixMode(GL_MODELVIEW);
- }
- int main(int argc, char *argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
- glutInitWindowSize(320, 320);
- glutInitWindowPosition(100, 100);
- glutCreateWindow("Cube First");
- init();
- glutReshapeFunc(reshape);
- glutDisplayFunc(display);
- glutMainLoop();
- return 0;
- }