Facebook
From TwojTATKO, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 224
  1.  
  2. #include <GL/glut.h>
  3.  int color1=1;
  4. double angle=0;
  5. //myszunia
  6. int mucka = -1;
  7. GLdouble click_x = 0, click_y = 0;
  8. float na_x, na_y;
  9. float zoom = 1;
  10. static GLdouble coordX = 0.0, coordY = 0.0;
  11. void InitGL(void)
  12. {
  13.     glClearColor(0.0, 0.8, 0.8, 0.0);
  14.     glClearDepth( 1.0 );
  15.     glEnable( GL_DEPTH_TEST ); //rysowanie z glebią ekranu
  16. }
  17. void Idle(void) {
  18.     angle +=5;
  19.     glutPostRedisplay();
  20. }
  21. void text(char*string)
  22. {
  23.     char*p;
  24.     for(p=string;*p;p++)
  25.                 glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *p);
  26. }
  27.  
  28. void reshape(int w, int h)
  29. {
  30.     GLdouble aspect = (GLdouble) w / h;
  31.     glViewport (0, 0, (GLsizei) w, (GLsizei) h);
  32. glMatrixMode(GL_PROJECTION);    //macierz projekcji
  33. glLoadIdentity();               //zerowanie macierzy
  34. if (w <= h)
  35. glOrtho(-250.0, 250, -250./ aspect, 250.0/ aspect, -100, 100);
  36. else
  37.     glOrtho (-250.*aspect, 250.*aspect, -250, 250, -100.0, 100);
  38. glMatrixMode(GL_MODELVIEW);     //macierz wyglądu
  39. glLoadIdentity();
  40.  
  41. }
  42.  
  43. void displayCallBack(void)
  44. {
  45.  
  46.        
  47.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); //czyszczenie buforów
  48.  glTranslatef( coordX,coordY,0);
  49.         glRotatef(na_x,0,1,0);
  50.         glRotatef(na_y,1,0,0);
  51.         glScalef(zoom,zoom,zoom);
  52.     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  53.  if(color1 == 1)glColor3d(1,0,0);
  54.     if(color1 == 2)glColor3d(1,1,0);
  55.      glRotatef(angle, 1.0, 0.0, 0.0);
  56. glRotatef(15, 1, 1, 0);
  57. glRotatef(15,0,1,0);
  58. glutSolidCube(100);
  59.        
  60.  glLoadIdentity();
  61.  glColor3d(0,0,0);
  62.  glRasterPos2f(-200,200);
  63.  text("jgklfjagljfalgjfa");
  64.    
  65.     glFlush();
  66.  
  67.     glutSwapBuffers();  //zamienia bufory
  68.  
  69. }
  70.  
  71.  
  72. void myMouse(int button, int state, int mouseX, int mouseY)
  73. {
  74.         if(state == GLUT_DOWN) {
  75.                 switch(button){
  76.                 case GLUT_LEFT_BUTTON:
  77.                         mucka=0;
  78.                         break;
  79.                 case GLUT_MIDDLE_BUTTON:
  80.                         mucka=1;
  81.                         break;
  82.                 case GLUT_RIGHT_BUTTON:
  83.                         mucka=2;
  84.                         break;
  85.                 } }
  86.         click_x = mouseX;
  87.         click_y = mouseY;
  88. }
  89. void myMotion(int mouseX, int mouseY)
  90. {
  91.         if(mucka==0)
  92.         {
  93.                 coordX -=(click_x - mouseX);
  94.                 coordY += (click_y - mouseY);
  95.         }
  96.         else if (mucka==1)
  97.         {
  98.                 zoom +=(click_x - mouseX)*0.05;
  99.         }
  100.         else if (mucka==2)
  101.         {
  102.                 na_x -=(click_x - mouseX);
  103.                 na_y +=(click_y - mouseY);
  104.         }
  105.         click_x = mouseX;
  106.         click_y = mouseY;
  107.         glutPostRedisplay();
  108. }
  109.  
  110. void Klawiatura(unsigned char key, int x, int y)
  111. {
  112.     switch(key)
  113.     {
  114.         case'1':
  115.                 color1=1;
  116.                 break;
  117.         case'2':
  118.                 color1=2;
  119.                 break;
  120.         case 27: case'q':
  121.             exit(0);
  122.             break;
  123.     }
  124. }
  125.  
  126.        
  127.    
  128. void main(int argc, char** argv)
  129. {  
  130.        
  131.   glutInit(&argc, argv);
  132.  
  133.   glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );  
  134.   glutInitWindowSize(600, 600);
  135.   glutCreateWindow("Program_1");
  136.  
  137.  
  138.   InitGL();
  139.  
  140.   float pos[4] = { 0.90F,0.90F,2.25F, 0.00F };
  141.   float dir[3] = {-1, -1, -1};
  142.   glEnable(GL_COLOR_MATERIAL);
  143.   glEnable(GL_LIGHTING);
  144.   glEnable(GL_LIGHT0);
  145.  
  146.   glLightfv(GL_LIGHT0, GL_POSITION, pos);
  147.   glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, dir);
  148.  
  149.   glutMouseFunc(myMouse);
  150.   glutMotionFunc(myMotion);
  151.   glutIdleFunc(Idle);
  152.  
  153.  
  154.   glutReshapeFunc(reshape);
  155.   glutDisplayFunc(displayCallBack);
  156.   glutKeyboardFunc(Klawiatura);
  157.   glutIdleFunc(Idle);
  158.   glutMainLoop();
  159.  
  160.  
  161. }