Advertisement
Garey

OpenGL Transformations

Apr 10th, 2019
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.04 KB | None | 0 0
  1. //https://www.geeksforgeeks.org/basic-transformations-opengl/
  2. // C code to implement basic
  3. // transformations in OPENGL
  4. #include <stdio.h>
  5. #include <math.h>
  6. #include <time.h>
  7. #include <GL/glut.h>
  8.  
  9. // window size
  10. #define maxWD 640
  11. #define maxHT 480
  12.  
  13. // rotation speed
  14. #define thetaSpeed 0.05
  15.  
  16. // this creates delay between two actions
  17. void delay(unsigned int mseconds)
  18. {
  19.     clock_t goal = mseconds + clock();
  20.     while (goal > clock())
  21.         ;
  22. }
  23.  
  24. // this is a basic init for the glut window
  25. void myInit(void)
  26. {
  27.     glClearColor(1.0, 1.0, 1.0, 0.0);
  28.     glMatrixMode(GL_PROJECTION);
  29.     glLoadIdentity();
  30.     gluOrtho2D(0.0, maxWD, 0.0, maxHT);
  31.     glClear(GL_COLOR_BUFFER_BIT);
  32.     glFlush();
  33. }
  34.  
  35. // this function just draws a point
  36. void drawPoint(int x, int y)
  37. {
  38.     glPointSize(7.0);
  39.     glColor3f(0.0f, 0.0f, 1.0f);
  40.     glBegin(GL_POINTS);
  41.     glVertex2i(x, y);
  42.     glEnd();
  43. }
  44.  
  45. void rotateAroundPt(int px, int py, int cx, int cy)
  46. {
  47.     float theta = 0.0;
  48.     while (1) {
  49.         glClear(GL_COLOR_BUFFER_BIT);
  50.         int xf, yf;
  51.  
  52.         // update theta anticlockwise rotation
  53.         theta = theta + thetaSpeed;
  54.  
  55.         // check overflow
  56.         if (theta >= (2.0 * 3.14159))
  57.             theta = theta - (2.0 * 3.14159);
  58.  
  59.         // actual calculations..
  60.         xf = cx + (int)((float)(px - cx) * cos(theta))
  61.              - ((float)(py - cy) * sin(theta));
  62.         yf = cy + (int)((float)(px - cx) * sin(theta))
  63.              + ((float)(py - cy) * cos(theta));
  64.  
  65.         // drawing the centre point
  66.         drawPoint(cx, cy);
  67.  
  68.         // drawing the rotating point
  69.         drawPoint(xf, yf);
  70.         glFlush();
  71.         // creating a delay
  72.         // so that the point can be noticed
  73.         delay(10);
  74.     }
  75. }
  76.  
  77. // this function will translate the point
  78. void translatePoint(int px, int py, int tx, int ty)
  79. {
  80.     int fx = px, fy = py;
  81.     while (1) {
  82.         glClear(GL_COLOR_BUFFER_BIT);
  83.  
  84.         // update
  85.         px = px + tx;
  86.         py = py + ty;
  87.  
  88.         // check overflow to keep point in screen
  89.         if (px > maxWD || px < 0 || py > maxHT || py < 0) {
  90.             px = fx;
  91.             py = fy;
  92.         }
  93.  
  94.         drawPoint(px, py); // drawing the point
  95.  
  96.         glFlush();
  97.         // creating a delay
  98.         // so that the point can be noticed
  99.         delay(10);
  100.     }
  101. }
  102.  
  103. // this function draws
  104. void scalePoint(int px, int py, int sx, int sy)
  105. {
  106.     int fx, fy;
  107.     while (1) {
  108.         glClear(GL_COLOR_BUFFER_BIT);
  109.  
  110.         // update
  111.         fx = px * sx;
  112.         fy = py * sy;
  113.  
  114.         drawPoint(fx, fy); // drawing the point
  115.  
  116.         glFlush();
  117.         // creating a delay
  118.         // so that the point can be noticed
  119.         delay(500);
  120.  
  121.         glClear(GL_COLOR_BUFFER_BIT);
  122.  
  123.         // update
  124.         fx = px;
  125.         fy = py;
  126.  
  127.         // drawing the point
  128.         drawPoint(fx, fy);
  129.         glFlush();
  130.         // creating a delay
  131.         // so that the point can be noticed
  132.         delay(500);
  133.     }
  134. }
  135.  
  136. // Actual display function
  137. void myDisplay(void)
  138. {
  139.     int opt;
  140.     printf("\nEnter\n\t<1> for translation"
  141.            "\n\t<2> for rotation"
  142.            "\n\t<3> for scaling\n\t:");
  143.     scanf("%d", &opt);
  144.     printf("\nGo to the window...");
  145.     switch (opt) {
  146.     case 1:
  147.         translatePoint(100, 200, 1, 5);
  148.         break;
  149.     case 2:
  150.         rotateAroundPt(200, 200, maxWD / 2, maxHT / 2);
  151.         // point will circle around
  152.         // the centre of the window
  153.         break;
  154.     case 3:
  155.         scalePoint(10, 20, 2, 3);
  156.         break;
  157.     }
  158. }
  159.  
  160. void main(int argc, char** argv)
  161. {
  162.     glutInit(&argc, argv);
  163.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  164.     glutInitWindowSize(maxWD, maxHT);
  165.     glutInitWindowPosition(100, 150);
  166.     glutCreateWindow("Transforming point");
  167.     glutDisplayFunc(myDisplay);
  168.     myInit();
  169.     glutMainLoop();
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement