Guest User

Untitled

a guest
Aug 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | None | 0 0
  1. // Transform.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <math.h>
  6. #include <GL/glut.h>   
  7.  
  8. #ifndef M_PI
  9. #define M_PI 3.141592
  10. #endif
  11.  
  12. int width = 600;
  13. int height = 600;
  14.  
  15. float worldAngle = 0;
  16. float radiusWorld = 10;
  17. float radiusMoon = 5;
  18. float moonAngle = 0;
  19.  
  20. /**
  21.  * Draws a circle on X-Z with radius and a specific
  22.  * number of Line Segments (countLineSegments)
  23.  */
  24. void drawCircle(float radius, int countLineSegments){
  25.     glBegin(GL_LINES);
  26.     glColor3f(0,0,0);
  27.     const float angleStep = (2*M_PI)/countLineSegments;
  28.     float angle = 0.0;
  29.     float startX = 0.0;
  30.     float startY = radius;
  31.     for(int step = 0; step<=countLineSegments;step++){
  32.         glVertex3f(startX,0.0f,startY);
  33.         startX = sinf(angle)*radius;
  34.         startY = cos(angle)*radius;
  35.         glVertex3f(startX,0.0f,startY);
  36.  
  37.         angle += angleStep;
  38.     }
  39.     glEnd();
  40. }
  41.  
  42.  
  43. /**
  44.  * Draws a qubic Moon
  45.  * with its global orbit radius(radiusMoon) and angle(moonAngle).
  46.  */
  47. void drawMoon(){
  48.     glPushMatrix();
  49.         glRotatef(moonAngle,0,1,0);
  50.         glTranslatef(radiusMoon,0,0);
  51.  
  52.         glutSolidCube(1);
  53.  
  54.     glPopMatrix();
  55. }
  56.  
  57. void display(void) 
  58. {
  59.     // Farb- und Tiefenbuffer leeren
  60.     glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  61.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  62.  
  63.     // Teekanne rendern.
  64.     glColor3f(1,1,0);
  65.     glutSolidTeapot(1);
  66.  
  67.     //Kreis malen
  68.     drawCircle(10, 80);
  69.  
  70.     //World
  71.     glPushMatrix();
  72.  
  73.         //glTranslatef(sin(worldAngle)*radiusWorld,0,cos(worldAngle)*radiusWorld);
  74.         glRotatef(worldAngle,0,1,0);
  75.         glTranslatef(radiusWorld,0,0);
  76.         glutSolidSphere(1,30,30);
  77.  
  78.         //Moon Circle X-Z
  79.         drawCircle(5,42);
  80.         glColor3f(0,0,255);
  81.         drawMoon();
  82.  
  83.  
  84.         //Moon Circle X-Y
  85.         glPushMatrix();
  86.             glRotatef(90,0,0,1);
  87.             drawCircle(5,42);
  88.             glColor3f(255,0,0);
  89.             drawMoon();
  90.         glPopMatrix();
  91.  
  92.         //Moon Circle Y-Z
  93.         glPushMatrix();
  94.             glRotatef(90,1,0,0);
  95.             drawCircle(5,42);
  96.             glColor3f(0,255,0);
  97.             drawMoon();
  98.         glPopMatrix();
  99.  
  100.     glPopMatrix();
  101.  
  102.     glutSwapBuffers();
  103. }
  104.  
  105. void timer(int val)
  106. {
  107.     glutTimerFunc(val,timer,val);
  108.     glutPostRedisplay();
  109.  
  110.     //Move Moon and World (Moon at double speed)
  111.     worldAngle += M_PI/10;
  112.     moonAngle += M_PI/5;
  113. }
  114.  
  115. void init()
  116. {
  117.     glEnable(GL_DEPTH_TEST);
  118.  
  119.     glViewport(0,0,width,height);                  
  120.     glMatrixMode(GL_PROJECTION);                   
  121.     glLoadIdentity();                              
  122.     glOrtho(-15.0, 15.0, -15.0, 15.0, 0.0, 200);
  123.  
  124.     glMatrixMode(GL_MODELVIEW);
  125.  
  126.     glLoadIdentity();
  127.     gluLookAt(25.0, 11.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  128. }
  129.  
  130. int _tmain(int argc, _TCHAR* argv[])
  131. {
  132.     glutInit(&argc, (char**)argv);
  133.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  134.     glutInitWindowSize(width, height);
  135.     glutCreateWindow("Transformationen");
  136.  
  137.     glutDisplayFunc(display);
  138.     glutTimerFunc(10,timer,10);
  139.  
  140.     init();
  141.     glutMainLoop();
  142.     return 0;
  143. }
Add Comment
Please, Sign In to add comment