Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <iostream>
- #include <string>
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
- /* Note: polygons must be drawn from front to back
- * for proper blending.
- */
- /*
- GLboolean polySmooth = GL_TRUE;
- static void init(void) {
- //glCullFace (GL_BACK);
- //glEnable (GL_CULL_FACE);
- //glBlendFunc (GL_SRC_ALPHA_SATURATE, GL_ONE);
- GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
- GLfloat mat_shininess[] = { 50.0 };
- GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
- glClearColor (0.0, 0.0, 0.0, 0.0);
- glShadeModel (GL_SMOOTH);
- glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
- glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glEnable(GL_DEPTH_TEST);
- glClearColor (0.0, 0.0, 0.0, 0.0);
- }
- #define NFACE 6
- #define NVERT 8
- void drawCube(GLdouble x0, GLdouble x1, GLdouble y0,
- GLdouble y1, GLdouble z0, GLdouble z1)
- {
- static GLfloat v[8][3];
- static GLfloat c[8][4] = {
- {0.0, 0.0, 0.0, 1.0}, {1.0, 0.0, 0.0, .5},
- {0.0, 1.0, 0.0, 1.0}, {1.0, 1.0, 0.0, .5},
- {0.0, 0.0, 1.0, 1.0}, {1.0, 0.0, 1.0, .5},
- {0.0, 1.0, 1.0, 1.0}, {1.0, 1.0, 1.0, .5}
- };
- // indices of front, top, left, bottom, right, back faces
- static GLubyte indices[NFACE][4] = {
- {4, 5, 6, 7}, {2, 3, 7, 6}, {0, 4, 7, 3},
- {0, 1, 5, 4}, {1, 5, 6, 2}, {0, 3, 2, 1}
- };
- v[0][0] = v[3][0] = v[4][0] = v[7][0] = x0;
- v[1][0] = v[2][0] = v[5][0] = v[6][0] = x1;
- v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
- v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
- v[0][2] = v[1][2] = v[2][2] = v[3][2] = z0;
- v[4][2] = v[5][2] = v[6][2] = v[7][2] = z1;
- #ifdef GL_VERSION_1_1
- glEnableClientState (GL_VERTEX_ARRAY);
- glEnableClientState (GL_COLOR_ARRAY);
- glVertexPointer (3, GL_FLOAT, 0, v);
- glColorPointer (4, GL_FLOAT, 0, c);
- glDrawElements(GL_QUADS, NFACE*4, GL_UNSIGNED_BYTE, indices);
- glDisableClientState (GL_VERTEX_ARRAY);
- glDisableClientState (GL_COLOR_ARRAY);
- #else
- std::cout << "If this is GL Version 1.0, ";
- std::cout << "vertex arrays are not supported.\n";
- exit(1);
- #endif
- }
- void display(void)
- {
- if (polySmooth) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glEnable(GL_BLEND);
- glEnable(GL_POLYGON_SMOOTH);
- glDisable(GL_DEPTH_TEST);
- }
- else {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glDisable(GL_BLEND);
- glDisable(GL_POLYGON_SMOOTH);
- glEnable(GL_DEPTH_TEST);
- }
- glPushMatrix ();
- glTranslatef (0.0, 0.0, -8.0);
- glRotatef (30.0, 1.0, 0.0, 0.0);
- glRotatef (60.0, 0.0, 1.0, 0.0);
- drawCube(-0.5, 0.5, -0.5, 0.5, -0.5, 0.5);
- glPopMatrix ();
- glFlush ();
- }
- void reshape(int w, int h)
- {
- glViewport(0, 0, (GLsizei) w, (GLsizei) h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(30.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- void keyboard(unsigned char key, int x, int y)
- {
- switch (key) {
- case 't':
- case 'T':
- polySmooth = !polySmooth;
- glutPostRedisplay();
- break;
- case 27:
- exit(0);
- break;
- default:
- break;
- }
- }
- int main(int argc, char** argv) {
- glutInit(&argc, argv);
- glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_ALPHA | GLUT_DEPTH);
- glEnable(GL_DEPTH_TEST);
- glutInitWindowSize(200, 200);
- glutCreateWindow(argv[0]);
- init ();
- glutReshapeFunc (reshape);
- glutKeyboardFunc (keyboard);
- glutDisplayFunc (display);
- glutMainLoop();
- return 0;
- }
- */
- typedef GLdouble Coord;
- struct Point { Coord x, y, z; };
- Point lookFrom = { 2.0, 0.0, 0.0 };
- #define NFACE 6
- #define NVERT 8
- void drawCube(Point p0, Point p1) {
- static GLfloat v[4][3];
- // indices of front, top, left, bottom, right, back faces
- static GLubyte indices[NFACE][4] = { {0, 1, 2, 3} };
- v[0][0] = 0.0;
- v[0][0] = 0.0;
- v[0][0] = 0.0;
- v[0][0] = 0.0;
- v[0][0] = 0.0;
- v[0][0] = 1.0;
- v[0][0] = 1.0;
- v[0][0] = 0.0;
- v[0][0] = 1.0;
- v[0][0] = 1.0;
- v[0][0] = 0.0;
- v[0][0] = 0.0;
- glEnableClientState(GL_VERTEX_ARRAY);
- glEnableClientState(GL_NORMAL_ARRAY);
- glVertexPointer(3, GL_FLOAT, 0, v);
- glNormalPointer(GL_FLOAT, 0, v);
- glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, indices);
- glDisableClientState(GL_VERTEX_ARRAY);
- glDisableClientState(GL_NORMAL_ARRAY);
- /*
- static GLfloat v[8][3];
- static GLfloat c[8][4] = {
- {0.0, 0.0, 0.0, 1.0}, {1.0, 0.0, 0.0, 1.0},
- {0.0, 1.0, 0.0, 1.0}, {1.0, 1.0, 0.0, 1.0},
- {0.0, 0.0, 1.0, 1.0}, {1.0, 0.0, 1.0, 1.0},
- {0.0, 1.0, 1.0, 1.0}, {1.0, 1.0, 1.0, 1.0}
- };
- // indices of front, top, left, bottom, right, back faces
- static GLubyte indices[NFACE][4] = {
- {4, 5, 6, 7}, {2, 3, 7, 6}, {0, 4, 7, 3},
- {0, 1, 5, 4}, {1, 5, 6, 2}, {0, 3, 2, 1}
- };
- v[0][0] = v[3][0] = v[4][0] = v[7][0] = p0.x;
- v[1][0] = v[2][0] = v[5][0] = v[6][0] = p1.x;
- v[0][1] = v[1][1] = v[4][1] = v[5][1] = p0.y;
- v[2][1] = v[3][1] = v[6][1] = v[7][1] = p1.y;
- v[0][2] = v[1][2] = v[2][2] = v[3][2] = p0.z;
- v[4][2] = v[5][2] = v[6][2] = v[7][2] = p1.z;
- glEnableClientState(GL_VERTEX_ARRAY);
- glEnableClientState(GL_NORMAL_ARRAY);
- //glEnableClientState (GL_COLOR_ARRAY);
- glVertexPointer(3, GL_FLOAT, 0, v);
- glNormalPointer(GL_FLOAT, 0, v);
- //glColorPointer (4, GL_FLOAT, 0, c);
- glDrawElements(GL_QUADS, NFACE*4, GL_UNSIGNED_BYTE, indices);
- glDisableClientState(GL_VERTEX_ARRAY);
- glDisableClientState(GL_NORMAL_ARRAY);
- //glDisableClientState(GL_COLOR_ARRAY);
- */
- }
- void keyboard(unsigned char key, int x, int y) {
- float delta = 0.1;
- switch (key) {
- case 'h':
- lookFrom.x -= delta;
- glutPostRedisplay();
- glLoadIdentity();
- break;
- case 'l':
- lookFrom.x += delta;
- glutPostRedisplay();
- glLoadIdentity();
- break;
- case 'j':
- lookFrom.y -= delta;
- glutPostRedisplay();
- glLoadIdentity();
- break;
- case 'k':
- lookFrom.y += delta;
- glutPostRedisplay();
- glLoadIdentity();
- break;
- case 'u':
- lookFrom.z -= delta;
- glutPostRedisplay();
- glLoadIdentity();
- break;
- case 'o':
- lookFrom.z += delta;
- glutPostRedisplay();
- glLoadIdentity();
- break;
- case 27:
- exit(0);
- break;
- default:
- break;
- }
- }
- void init(void) {
- GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
- GLfloat mat_shininess[] = { 50.0 };
- GLfloat light_position[] = { 3.0, 0.0, 0.0, 1.0 };
- glClearColor (0.0, 0.0, 0.0, 0.0);
- glShadeModel (GL_SMOOTH);
- glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
- glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glEnable(GL_DEPTH_TEST);
- }
- void display(void) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- gluLookAt(lookFrom.x, lookFrom.y, lookFrom.z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- //glPushMatrix();
- //glRotatef(25.0, 1.0, 0.0, 0.0);
- //glRotatef(25.0, 0.0, 1.0, 0.0);
- Point p0 = {-0.5, 0.5, -0.5}, p1 = {0.5, -0.5, 0.5};
- drawCube(p0, p1);
- //glPopMatrix();
- glFlush();
- }
- void reshape (int w, int h) {
- glViewport (0, 0, (GLsizei) w, (GLsizei) h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- if (w <= h) {
- glOrtho(-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,
- 1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
- }
- else {
- glOrtho(-1.5*(GLfloat)w/(GLfloat)h, 1.5*(GLfloat)w/(GLfloat)h,
- -1.5, 1.5, -10.0, 10.0);
- }
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- int main(int argc, char** argv) {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize(500, 500);
- glutInitWindowPosition(100, 100);
- glutCreateWindow(argv[0]);
- init();
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutKeyboardFunc(keyboard);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement