Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Mine
Thursday, December 31st, 2009 at 2:20:50pm UTC 

  1. #include <GL/gl.h>
  2. #include <GL/glu.h>
  3. #include <GL/glut.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. GLboolean polySmooth = GL_TRUE;
  9. static void init(void)
  10. {
  11.    glCullFace (GL_BACK);
  12.    glEnable (GL_CULL_FACE);
  13.    glBlendFunc (GL_SRC_ALPHA_SATURATE, GL_ONE);
  14.    glClearColor (0.0, 0.0, 0.0, 0.0);
  15. }
  16.  
  17. #define NFACE 6
  18. #define NVERT 8
  19. void drawCube(GLdouble x0, GLdouble x1, GLdouble y0,
  20.               GLdouble y1, GLdouble z0, GLdouble z1)
  21. {
  22.    static GLfloat v[8][3];
  23.    static GLfloat c[8][4] = {
  24.       {0.0, 0.0, 0.0, 1.0}, {1.0, 0.0, 0.0, 1.0},
  25.       {0.0, 1.0, 0.0, 1.0}, {1.0, 1.0, 0.0, 1.0},
  26.       {0.0, 0.0, 1.0, 1.0}, {1.0, 0.0, 1.0, 1.0},
  27.       {0.0, 1.0, 1.0, 1.0}, {1.0, 1.0, 1.0, 1.0}
  28.    };
  29.  
  30. /*  indices of front, top, left, bottom, right, back faces  */
  31.    static GLubyte indices[NFACE][4] = {
  32.       {4, 5, 6, 7}, {2, 3, 7, 6}, {0, 4, 7, 3},
  33.       {0, 1, 5, 4}, {1, 5, 6, 2}, {0, 3, 2, 1}
  34.    };
  35.  
  36.    v[0][0] = v[3][0] = v[4][0] = v[7][0] = x0;
  37.    v[1][0] = v[2][0] = v[5][0] = v[6][0] = x1;
  38.    v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
  39.    v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
  40.    v[0][2] = v[1][2] = v[2][2] = v[3][2] = z0;
  41.    v[4][2] = v[5][2] = v[6][2] = v[7][2] = z1;
  42.  
  43. #ifdef GL_VERSION_1_1
  44.    glEnableClientState (GL_VERTEX_ARRAY);
  45.    glEnableClientState (GL_COLOR_ARRAY);
  46.    glVertexPointer (3, GL_FLOAT, 0, v);
  47.    glColorPointer (4, GL_FLOAT, 0, c);
  48.    glDrawElements(GL_QUADS, NFACE*4, GL_UNSIGNED_BYTE, indices);
  49.    glDisableClientState (GL_VERTEX_ARRAY);
  50.    glDisableClientState (GL_COLOR_ARRAY);
  51. #else
  52.    printf ("If this is GL Version 1.0, ");
  53.    printf ("vertex arrays are not supported.\n");
  54.    exit(1);
  55. #endif
  56. }
  57. /*  Note:  polygons must be drawn from front to back
  58. *  for proper blending.
  59. */
  60. void display(void)
  61. {
  62.    if (polySmooth) {
  63.       glClear (GL_COLOR_BUFFER_BIT);
  64.       glEnable (GL_BLEND);
  65.       glEnable (GL_POLYGON_SMOOTH);
  66.       glDisable (GL_DEPTH_TEST);
  67.    }
  68.    else {
  69.       glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  70.       glDisable (GL_BLEND);
  71.       glDisable (GL_POLYGON_SMOOTH);
  72.       glEnable (GL_DEPTH_TEST);
  73.    }
  74.  
  75.    glPushMatrix ();
  76.       glTranslatef (0.0, 0.0, -8.0);   
  77.       glRotatef (30.0, 1.0, 0.0, 0.0);
  78.       glRotatef (60.0, 0.0, 1.0, 0.0);
  79.       drawCube(-0.5, 0.5, -0.5, 0.5, -0.5, 0.5);
  80.    glPopMatrix ();
  81.  
  82.    glFlush ();
  83. }
  84.  
  85. void reshape(int w, int h)
  86. {
  87.    glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  88.    glMatrixMode(GL_PROJECTION);
  89.    glLoadIdentity();
  90.    gluPerspective(30.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
  91.    glMatrixMode(GL_MODELVIEW);
  92.    glLoadIdentity();
  93. }
  94. void keyboard(unsigned char key, int x, int y)
  95. {
  96.    switch (key) {
  97.       case 't':
  98.       case 'T':
  99.          polySmooth = !polySmooth;
  100.          glutPostRedisplay();
  101.          break;
  102.       case 27:
  103.          exit(0)/*  Escape key  */
  104.          break;
  105.       default:
  106.          break;
  107.    }
  108. }
  109.  
  110. int main(int argc, char** argv)
  111. {
  112.    glutInit(&argc, argv);
  113.    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB
  114.                         | GLUT_ALPHA | GLUT_DEPTH);
  115.    glutInitWindowSize(200, 200);
  116.    glutCreateWindow(argv[0]);
  117.    init ();
  118.    glutReshapeFunc (reshape);
  119.    glutKeyboardFunc (keyboard);
  120.    glutDisplayFunc (display);
  121.    glutMainLoop();
  122.    return 0;
  123. }

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

comments powered by Disqus
worth-right
worth-right
worth-right