C   69

rect2d

Guest on 10th June 2022 01:31:21 PM

  1. /* Compile me with cc -o rect2d rect2d.c -lGL -lGLU -lglut */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #include <GL/gl.h>
  6. #include <GL/glu.h>
  7. #include <GL/glut.h>
  8.  
  9. #include <png.h>
  10.  
  11. int window;
  12. GLfloat LightAmbient[4] = { 0.5,0.5,0.5,1.0};
  13. GLfloat LightDiffuse[4] = { 1,1,1,1};
  14. GLfloat lightpos[4] = { 0,0,2,1};
  15.  
  16. void draw_gl()
  17. {
  18.  
  19.         /* Clear screen */
  20.         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  21.  
  22.         /* Draw 3d objects in perspective */
  23.         glMatrixMode(GL_PROJECTION);
  24.         glLoadIdentity();
  25.         gluPerspective(45.0f,1.0,0.1f,100.0f);
  26.  
  27.         /* Orient ourselves by changing the modelview matrix
  28.          * with glTranslate and glRotate()
  29.          */
  30.         glMatrixMode(GL_MODELVIEW);
  31.         glLoadIdentity();
  32.  
  33.         glTranslatef(0,0,-2);
  34.         glRotatef(45,1,1,1);
  35.         /* Be blue. */
  36.         glColor3f(0,0,1);
  37.         /* Draw the cube */
  38.         glutSolidCube(1.0);
  39.  
  40.         /* Set modelview and projection matrices to unity */
  41.  
  42.         glMatrixMode(GL_PROJECTION);
  43.         glLoadIdentity();
  44.         glMatrixMode(GL_MODELVIEW);
  45.         glLoadIdentity();
  46.  
  47.         /* Be green. */
  48.         glColor3f(0,1,0);
  49.  
  50.         /* Turn off lighting since we don't want it to affect the HUD */
  51.  
  52.         glDisable(GL_LIGHTING);
  53.  
  54.         /* Draw a 2d square. */
  55.         glBegin(GL_LINE_STRIP);
  56.                 glVertex2f(-0.5, 0.5);
  57.                 glVertex2f( 0.5, 0.5);
  58.                 glVertex2f( 0.5,-0.5);
  59.                 glVertex2f(-0.5,-0.5);
  60.                 glVertex2f(-0.5, 0.5);
  61.         glEnd();
  62.  
  63.         /* Turn lighting back on */
  64.         glEnable(GL_LIGHTING);
  65.  
  66.  
  67.         /* display everything */
  68.         glutSwapBuffers();
  69.  
  70.         return ;
  71. }
  72.  
  73. void keypress(unsigned char key, int x, int y)
  74. {
  75.  
  76.         if (' '==key) {
  77.                 exit(0);
  78.         }
  79. }
  80.  
  81. int main(int argc, char *argv[])
  82. {
  83.  
  84.         /* Set up us the render window */
  85.         glutInit(&argc,argv);
  86.         glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
  87.         glutInitWindowSize(200,200);
  88.         glutInitWindowPosition(0,0);
  89.  
  90.         window = glutCreateWindow("GLUT Window");
  91.  
  92.         /* Set GL state */
  93.         glClearDepth(1.0);
  94.  
  95.         glDepthFunc(GL_LESS);
  96.         glEnable(GL_LIGHTING);
  97.  
  98.         glEnable(GL_DEPTH_TEST);
  99.         glShadeModel(GL_SMOOTH);
  100.  
  101.  
  102.         glLightfv(GL_LIGHT1,GL_AMBIENT,LightAmbient);
  103.         glLightfv(GL_LIGHT1,GL_DIFFUSE,LightDiffuse);
  104.         glLightfv(GL_LIGHT1,GL_POSITION,lightpos);
  105.         glEnable(GL_LIGHT1);
  106.  
  107.         glutDisplayFunc(&draw_gl);
  108.         glutKeyboardFunc(&keypress);
  109.  
  110.         glutMainLoop();
  111.  
  112.         return 0;
  113. }

Raw Paste


Login or Register to edit or fork this paste. It's free.