Advertisement
zaquest

gllab

Sep 11th, 2012
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.70 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. #include <GL/gl.h>
  6. #include <GL/glu.h>
  7. #include <GL/glut.h>
  8.  
  9. /*  Note:  polygons must be drawn from front to back
  10.  *  for proper blending.
  11.  */
  12. /*
  13. GLboolean polySmooth = GL_TRUE;
  14.  
  15. static void init(void) {
  16.     //glCullFace (GL_BACK);
  17.     //glEnable (GL_CULL_FACE);
  18.     //glBlendFunc (GL_SRC_ALPHA_SATURATE, GL_ONE);
  19.  
  20.     GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  21.     GLfloat mat_shininess[] = { 50.0 };
  22.     GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  23.     glClearColor (0.0, 0.0, 0.0, 0.0);
  24.     glShadeModel (GL_SMOOTH);
  25.  
  26.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  27.     glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  28.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  29.  
  30.     glEnable(GL_LIGHTING);
  31.     glEnable(GL_LIGHT0);
  32.     glEnable(GL_DEPTH_TEST);
  33.  
  34.     glClearColor (0.0, 0.0, 0.0, 0.0);
  35. }
  36.  
  37. #define NFACE 6
  38. #define NVERT 8
  39. void drawCube(GLdouble x0, GLdouble x1, GLdouble y0,
  40.         GLdouble y1, GLdouble z0, GLdouble z1)
  41. {
  42.     static GLfloat v[8][3];
  43.     static GLfloat c[8][4] = {
  44.         {0.0, 0.0, 0.0, 1.0}, {1.0, 0.0, 0.0, .5},
  45.         {0.0, 1.0, 0.0, 1.0}, {1.0, 1.0, 0.0, .5},
  46.         {0.0, 0.0, 1.0, 1.0}, {1.0, 0.0, 1.0, .5},
  47.         {0.0, 1.0, 1.0, 1.0}, {1.0, 1.0, 1.0, .5}
  48.     };
  49.  
  50.     //  indices of front, top, left, bottom, right, back faces
  51.     static GLubyte indices[NFACE][4] = {
  52.         {4, 5, 6, 7}, {2, 3, 7, 6}, {0, 4, 7, 3},
  53.         {0, 1, 5, 4}, {1, 5, 6, 2}, {0, 3, 2, 1}
  54.     };
  55.  
  56.     v[0][0] = v[3][0] = v[4][0] = v[7][0] = x0;
  57.     v[1][0] = v[2][0] = v[5][0] = v[6][0] = x1;
  58.     v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
  59.     v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
  60.     v[0][2] = v[1][2] = v[2][2] = v[3][2] = z0;
  61.     v[4][2] = v[5][2] = v[6][2] = v[7][2] = z1;
  62.  
  63. #ifdef GL_VERSION_1_1
  64.     glEnableClientState (GL_VERTEX_ARRAY);
  65.     glEnableClientState (GL_COLOR_ARRAY);
  66.     glVertexPointer (3, GL_FLOAT, 0, v);
  67.     glColorPointer (4, GL_FLOAT, 0, c);
  68.     glDrawElements(GL_QUADS, NFACE*4, GL_UNSIGNED_BYTE, indices);
  69.     glDisableClientState (GL_VERTEX_ARRAY);
  70.     glDisableClientState (GL_COLOR_ARRAY);
  71. #else
  72.     std::cout << "If this is GL Version 1.0, ";
  73.     std::cout << "vertex arrays are not supported.\n";
  74.     exit(1);
  75. #endif
  76. }
  77. void display(void)
  78. {
  79.     if (polySmooth) {
  80.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  81.         glEnable(GL_BLEND);
  82.         glEnable(GL_POLYGON_SMOOTH);
  83.         glDisable(GL_DEPTH_TEST);
  84.     }
  85.     else {
  86.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  87.         glDisable(GL_BLEND);
  88.         glDisable(GL_POLYGON_SMOOTH);
  89.         glEnable(GL_DEPTH_TEST);
  90.     }
  91.  
  92.     glPushMatrix ();
  93.     glTranslatef (0.0, 0.0, -8.0);
  94.     glRotatef (30.0, 1.0, 0.0, 0.0);
  95.     glRotatef (60.0, 0.0, 1.0, 0.0);
  96.     drawCube(-0.5, 0.5, -0.5, 0.5, -0.5, 0.5);
  97.     glPopMatrix ();
  98.  
  99.     glFlush ();
  100. }
  101.  
  102. void reshape(int w, int h)
  103. {
  104.     glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  105.     glMatrixMode(GL_PROJECTION);
  106.     glLoadIdentity();
  107.     gluPerspective(30.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
  108.     glMatrixMode(GL_MODELVIEW);
  109.     glLoadIdentity();
  110. }
  111. void keyboard(unsigned char key, int x, int y)
  112. {
  113.     switch (key) {
  114.         case 't':
  115.         case 'T':
  116.             polySmooth = !polySmooth;
  117.             glutPostRedisplay();
  118.             break;
  119.         case 27:
  120.             exit(0);
  121.             break;
  122.         default:
  123.             break;
  124.     }
  125. }
  126.  
  127. int main(int argc, char** argv) {
  128.     glutInit(&argc, argv);
  129.     glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_ALPHA | GLUT_DEPTH);
  130.     glEnable(GL_DEPTH_TEST);
  131.  
  132.     glutInitWindowSize(200, 200);
  133.     glutCreateWindow(argv[0]);
  134.  
  135.     init ();
  136.     glutReshapeFunc (reshape);
  137.     glutKeyboardFunc (keyboard);
  138.     glutDisplayFunc (display);
  139.     glutMainLoop();
  140.     return 0;
  141. }
  142. */
  143.  
  144.  
  145. typedef GLdouble Coord;
  146. struct Point { Coord x, y, z; };
  147.  
  148. Point lookFrom = { 2.0, 0.0, 0.0 };
  149.  
  150.  
  151. #define NFACE 6
  152. #define NVERT 8
  153. void drawCube(Point p0, Point p1) {
  154.     static GLfloat v[4][3];
  155.  
  156.     //  indices of front, top, left, bottom, right, back faces
  157.     static GLubyte indices[NFACE][4] = { {0, 1, 2, 3} };
  158.  
  159.     v[0][0] = 0.0;
  160.     v[0][0] = 0.0;
  161.     v[0][0] = 0.0;
  162.  
  163.     v[0][0] = 0.0;
  164.     v[0][0] = 0.0;
  165.     v[0][0] = 1.0;
  166.  
  167.     v[0][0] = 1.0;
  168.     v[0][0] = 0.0;
  169.     v[0][0] = 1.0;
  170.  
  171.     v[0][0] = 1.0;
  172.     v[0][0] = 0.0;
  173.     v[0][0] = 0.0;
  174.  
  175.     glEnableClientState(GL_VERTEX_ARRAY);
  176.     glEnableClientState(GL_NORMAL_ARRAY);
  177.  
  178.     glVertexPointer(3, GL_FLOAT, 0, v);
  179.     glNormalPointer(GL_FLOAT, 0, v);
  180.  
  181.     glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, indices);
  182.  
  183.     glDisableClientState(GL_VERTEX_ARRAY);
  184.     glDisableClientState(GL_NORMAL_ARRAY);
  185.  
  186.     /*
  187.     static GLfloat v[8][3];
  188.     static GLfloat c[8][4] = {
  189.         {0.0, 0.0, 0.0, 1.0}, {1.0, 0.0, 0.0, 1.0},
  190.         {0.0, 1.0, 0.0, 1.0}, {1.0, 1.0, 0.0, 1.0},
  191.         {0.0, 0.0, 1.0, 1.0}, {1.0, 0.0, 1.0, 1.0},
  192.         {0.0, 1.0, 1.0, 1.0}, {1.0, 1.0, 1.0, 1.0}
  193.     };
  194.  
  195.     //  indices of front, top, left, bottom, right, back faces
  196.     static GLubyte indices[NFACE][4] = {
  197.         {4, 5, 6, 7}, {2, 3, 7, 6}, {0, 4, 7, 3},
  198.         {0, 1, 5, 4}, {1, 5, 6, 2}, {0, 3, 2, 1}
  199.     };
  200.  
  201.     v[0][0] = v[3][0] = v[4][0] = v[7][0] = p0.x;
  202.     v[1][0] = v[2][0] = v[5][0] = v[6][0] = p1.x;
  203.     v[0][1] = v[1][1] = v[4][1] = v[5][1] = p0.y;
  204.     v[2][1] = v[3][1] = v[6][1] = v[7][1] = p1.y;
  205.     v[0][2] = v[1][2] = v[2][2] = v[3][2] = p0.z;
  206.     v[4][2] = v[5][2] = v[6][2] = v[7][2] = p1.z;
  207.  
  208.     glEnableClientState(GL_VERTEX_ARRAY);
  209.     glEnableClientState(GL_NORMAL_ARRAY);
  210.     //glEnableClientState (GL_COLOR_ARRAY);
  211.  
  212.     glVertexPointer(3, GL_FLOAT, 0, v);
  213.     glNormalPointer(GL_FLOAT, 0, v);
  214.     //glColorPointer (4, GL_FLOAT, 0, c);
  215.  
  216.     glDrawElements(GL_QUADS, NFACE*4, GL_UNSIGNED_BYTE, indices);
  217.  
  218.     glDisableClientState(GL_VERTEX_ARRAY);
  219.     glDisableClientState(GL_NORMAL_ARRAY);
  220.     //glDisableClientState(GL_COLOR_ARRAY);
  221.     */
  222. }
  223.  
  224. void keyboard(unsigned char key, int x, int y) {
  225.     float delta = 0.1;
  226.     switch (key) {
  227.         case 'h':
  228.             lookFrom.x -= delta;
  229.             glutPostRedisplay();
  230.             glLoadIdentity();
  231.             break;
  232.         case 'l':
  233.             lookFrom.x += delta;
  234.             glutPostRedisplay();
  235.             glLoadIdentity();
  236.             break;
  237.         case 'j':
  238.             lookFrom.y -= delta;
  239.             glutPostRedisplay();
  240.             glLoadIdentity();
  241.             break;
  242.         case 'k':
  243.             lookFrom.y += delta;
  244.             glutPostRedisplay();
  245.             glLoadIdentity();
  246.             break;
  247.         case 'u':
  248.             lookFrom.z -= delta;
  249.             glutPostRedisplay();
  250.             glLoadIdentity();
  251.             break;
  252.         case 'o':
  253.             lookFrom.z += delta;
  254.             glutPostRedisplay();
  255.             glLoadIdentity();
  256.             break;
  257.         case 27:
  258.             exit(0);
  259.             break;
  260.         default:
  261.             break;
  262.     }
  263. }
  264.  
  265.  
  266. void init(void) {
  267.    GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  268.    GLfloat mat_shininess[] = { 50.0 };
  269.    GLfloat light_position[] = { 3.0, 0.0, 0.0, 1.0 };
  270.  
  271.    glClearColor (0.0, 0.0, 0.0, 0.0);
  272.    glShadeModel (GL_SMOOTH);
  273.  
  274.    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  275.    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  276.  
  277.    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  278.  
  279.    glEnable(GL_LIGHTING);
  280.    glEnable(GL_LIGHT0);
  281.    glEnable(GL_DEPTH_TEST);
  282. }
  283.  
  284. void display(void) {
  285.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  286.  
  287.     gluLookAt(lookFrom.x, lookFrom.y, lookFrom.z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  288.  
  289.     //glPushMatrix();
  290.     //glRotatef(25.0, 1.0, 0.0, 0.0);
  291.     //glRotatef(25.0, 0.0, 1.0, 0.0);
  292.  
  293.     Point p0 = {-0.5, 0.5, -0.5}, p1 = {0.5, -0.5, 0.5};
  294.     drawCube(p0, p1);
  295.  
  296.     //glPopMatrix();
  297.  
  298.     glFlush();
  299. }
  300.  
  301. void reshape (int w, int h) {
  302.     glViewport (0, 0, (GLsizei) w, (GLsizei) h);
  303.     glMatrixMode(GL_PROJECTION);
  304.     glLoadIdentity();
  305.     if (w <= h) {
  306.         glOrtho(-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,
  307.                 1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
  308.     }
  309.     else {
  310.         glOrtho(-1.5*(GLfloat)w/(GLfloat)h, 1.5*(GLfloat)w/(GLfloat)h,
  311.                 -1.5, 1.5, -10.0, 10.0);
  312.     }
  313.     glMatrixMode(GL_MODELVIEW);
  314.     glLoadIdentity();
  315. }
  316.  
  317.  
  318. int main(int argc, char** argv) {
  319.     glutInit(&argc, argv);
  320.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  321.     glutInitWindowSize(500, 500);
  322.     glutInitWindowPosition(100, 100);
  323.  
  324.     glutCreateWindow(argv[0]);
  325.  
  326.     init();
  327.  
  328.     glutDisplayFunc(display);
  329.     glutReshapeFunc(reshape);
  330.     glutKeyboardFunc(keyboard);
  331.  
  332.     glutMainLoop();
  333.  
  334.     return 0;
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement