Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //planet.h
- #include <cmath>
- #include <GL/gl.h>
- #include "vec2d.h"
- using namespace std;
- struct planet {
- double r, m, speed, red, green, blue;
- Vec2D pos;
- planet () {
- m = 1;
- r = 1;
- speed = 0;
- red = 1;
- green = 1;
- blue = 1;
- }
- planet (Vec2D pos_, Vec2D shift_, double r_, double m_, double speed_) {
- m = m_;
- r = r_;
- pos = pos_ + shift_;
- speed = speed_;
- red = 1;
- green = 1;
- blue = 1;
- }
- void set_color (double r, double g, double b) {
- red = r;
- green = g;
- blue = b;
- }
- void show () {
- double Phi, x, y;
- glColor3d (red, green, blue);
- glBegin(GL_POLYGON);
- for (int i = 0; i < 360; i++) {
- Phi = i * M_PI / 180;
- x = pos.x + r * cos(Phi);
- y = pos.y + r * sin(Phi);
- glVertex2d(x, y);
- }
- glEnd();
- }
- };
- //Vec2D.h
- #include <cmath>
- #include <GL/gl.h>
- using namespace std;
- struct Vec2D {
- double x, y, red, green, blue;
- Vec2D () {
- x = 0;
- y = 0;
- red = 1;
- green = 1;
- blue = 1;
- }
- Vec2D (double x_, double y_) {
- x = x_;
- y = y_;
- red = 1;
- green = 1;
- blue = 1;
- }
- Vec2D (double r) {
- x = r;
- y = r;
- red = 1;
- green = 1;
- blue = 1;
- }
- double len () {
- return sqrt(x*x + y*y);
- }
- void rotate (double alpha) {
- double Phi = atan2(y, x);
- double r = len();
- Phi += alpha;
- x = r * cos(Phi);
- y = r * sin(Phi);
- }
- void rotate_g (double alpha) {
- double Alpha = alpha / 180.0 * M_PI;
- rotate (Alpha);
- }
- Vec2D operator + (Vec2D other) {
- Vec2D ret = Vec2D(x + other.x, y + other.y);
- ret.set_color(0.5 * (red + other.red), 0.5 * (green + other.green), 0.5 * (blue + other.blue));
- return ret;
- }
- Vec2D operator - (Vec2D other) {
- Vec2D ret = Vec2D(x - other.x, y - other.y);
- ret.set_color(0.5 * (red - other.red), 0.5 * (green - other.green), 0.5 * (blue - other.blue));
- return ret;
- }
- double operator * (Vec2D other) {
- return double (x * other.x + y * other.y);
- }
- Vec2D operator * (double alpha) {
- return Vec2D(x * alpha, y * alpha);
- }
- Vec2D& operator += (Vec2D shift) {
- x += shift.x;
- y += shift.y;
- return *this;
- }
- Vec2D& operator -= (Vec2D shift) {
- x -= shift.x;
- y -= shift.y;
- return *this;
- }
- void set_color (double r, double g, double b) {
- red = r;
- green = g;
- blue = b;
- }
- void arrow (Vec2D beta) {
- Vec2D A, B(x,y), C, D, c, d, alpha;
- double num = -0.1;
- alpha = B * num;
- c = alpha;
- c.rotate_g(30);
- d = alpha;
- d.rotate_g(-30);
- C = B + c;
- D = B + d;
- shift_arrow(beta, A, B, C, D);
- show_arrow(A, B, C, D);
- }
- void shift_arrow (Vec2D beta, Vec2D &A, Vec2D &B, Vec2D &C, Vec2D &D) {
- A = A + beta;
- B = B + beta;
- C = C + beta;
- D = D + beta;
- }
- void show_arrow (Vec2D A, Vec2D B, Vec2D C, Vec2D D) {
- glColor3d (red, green, blue);
- glBegin(GL_LINES);
- glVertex2d(A.x, A.y);
- glVertex2d(B.x, B.y);
- glEnd();
- glBegin(GL_POLYGON);
- glVertex2d(B.x, B.y);
- glVertex2d(C.x, C.y);
- glVertex2d(D.x, D.y);
- glEnd();
- }
- void show () {
- glColor3d (red, green, blue);
- glBegin(GL_LINES);
- glVertex2d(0, 0);
- glVertex2d(x, y);
- glEnd();
- }
- };
- //main.cpp
- #include <iostream>
- #include <GL/glut.h>
- #include <GL/gl.h>
- #include "vec2d.h"
- #include "planet.h"
- using namespace std;
- planet sun(Vec2D(0,0), Vec2D(), 3, 10, 0);
- planet earth(Vec2D(6,5), Vec2D(), 0.5, 2, 0.5);
- planet moon(Vec2D(6,5), Vec2D(0.75,-0.75), 0.125, 0.5, 1);
- void draw() {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- sun.set_color(0.9, 0.9, 0.0);
- earth.set_color(0.0, 0.3, 0.8);
- moon.set_color(0.9, 0.9, 0.9);
- sun.show();
- earth.show();
- moon.show();
- glutSwapBuffers();
- }
- void update(int t) {
- Vec2D es1, es2, es;
- es1 = earth.pos;
- es1.rotate_g(earth.speed);
- es2 = es1;
- es1 = earth.pos;
- es = es2 - es1;
- earth.pos = earth.pos + es;
- moon.pos = moon.pos + es;
- Vec2D me;
- me = moon.pos - earth.pos;
- me.rotate_g(moon.speed);
- moon.pos = earth.pos + me;
- glutPostRedisplay();
- glutTimerFunc(50, update, 0);
- }
- void keyb(unsigned char key, int x, int y) {
- glutPostRedisplay();
- }
- void reshape(int w, int h) {
- // предупредим деление на ноль
- // если окно сильно перетянуто будет
- if(h == 0)
- h = 1;
- // используем матрицу проекции
- glMatrixMode(GL_PROJECTION);
- // Reset матрицы
- glLoadIdentity();
- // определяем окно просмотра
- glViewport(0, 0, w, h);
- // установить корректную перспективу.
- glOrtho(-10, 10, -10, 10, -10, 10);
- // вернуться к модели
- glMatrixMode(GL_MODELVIEW);
- }
- int main(int argc, char **argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize(800, 800);
- glutCreateWindow("GL2D");
- glEnable(GL_DEPTH_TEST);
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glutTimerFunc(50, update, 0);
- glutReshapeFunc(reshape);
- glutDisplayFunc(draw);
- glutKeyboardFunc(keyb);
- glutMainLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement