Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Transform.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <math.h>
- #include <GL/glut.h>
- #ifndef M_PI
- #define M_PI 3.141592
- #endif
- int width = 600;
- int height = 600;
- float worldAngle = 0;
- float radiusWorld = 10;
- float radiusMoon = 5;
- float moonAngle = 0;
- /**
- * Draws a circle on X-Z with radius and a specific
- * number of Line Segments (countLineSegments)
- */
- void drawCircle(float radius, int countLineSegments){
- glBegin(GL_LINES);
- glColor3f(0,0,0);
- const float angleStep = (2*M_PI)/countLineSegments;
- float angle = 0.0;
- float startX = 0.0;
- float startY = radius;
- for(int step = 0; step<=countLineSegments;step++){
- glVertex3f(startX,0.0f,startY);
- startX = sinf(angle)*radius;
- startY = cos(angle)*radius;
- glVertex3f(startX,0.0f,startY);
- angle += angleStep;
- }
- glEnd();
- }
- /**
- * Draws a qubic Moon
- * with its global orbit radius(radiusMoon) and angle(moonAngle).
- */
- void drawMoon(){
- glPushMatrix();
- glRotatef(moonAngle,0,1,0);
- glTranslatef(radiusMoon,0,0);
- glutSolidCube(1);
- glPopMatrix();
- }
- void display(void)
- {
- // Farb- und Tiefenbuffer leeren
- glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // Teekanne rendern.
- glColor3f(1,1,0);
- glutSolidTeapot(1);
- //Kreis malen
- drawCircle(10, 80);
- //World
- glPushMatrix();
- //glTranslatef(sin(worldAngle)*radiusWorld,0,cos(worldAngle)*radiusWorld);
- glRotatef(worldAngle,0,1,0);
- glTranslatef(radiusWorld,0,0);
- glutSolidSphere(1,30,30);
- //Moon Circle X-Z
- drawCircle(5,42);
- glColor3f(0,0,255);
- drawMoon();
- //Moon Circle X-Y
- glPushMatrix();
- glRotatef(90,0,0,1);
- drawCircle(5,42);
- glColor3f(255,0,0);
- drawMoon();
- glPopMatrix();
- //Moon Circle Y-Z
- glPushMatrix();
- glRotatef(90,1,0,0);
- drawCircle(5,42);
- glColor3f(0,255,0);
- drawMoon();
- glPopMatrix();
- glPopMatrix();
- glutSwapBuffers();
- }
- void timer(int val)
- {
- glutTimerFunc(val,timer,val);
- glutPostRedisplay();
- //Move Moon and World (Moon at double speed)
- worldAngle += M_PI/10;
- moonAngle += M_PI/5;
- }
- void init()
- {
- glEnable(GL_DEPTH_TEST);
- glViewport(0,0,width,height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-15.0, 15.0, -15.0, 15.0, 0.0, 200);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(25.0, 11.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- glutInit(&argc, (char**)argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize(width, height);
- glutCreateWindow("Transformationen");
- glutDisplayFunc(display);
- glutTimerFunc(10,timer,10);
- init();
- glutMainLoop();
- return 0;
- }
Add Comment
Please, Sign In to add comment