Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <list>
- #include <cmath>
- #include <algorithm>
- #include <GL/glut.h>
- #include <GL/gl.h>
- #include <GL/glu.h>
- using namespace std;
- int msec = 10;
- double scale = 1.0;
- double vx=0,vy=0;
- double p=3.14,r_1=0;
- double kx=5,ky=5;
- double angle_step = p/30/100.0 * msec;
- void render_square(double x, double y) {
- glPushMatrix();
- double dx = 0.5;
- double dy = 0.5;
- glBegin(GL_POLYGON);
- glVertex2f(x, y);
- glVertex2f(x + dx, y);
- glVertex2f(x + dx, y - dy);
- glVertex2f(x, y - dy);
- glEnd();
- glPopMatrix();
- }
- void draw_line(double x, double y, double x1, double y1) {
- glBegin(GL_LINES);
- glVertex2d(x, y);
- glVertex2d(x1, y1);
- glEnd();
- }
- class circle{
- public:
- double x,y,r,vx,vy;
- circle(double x_, double y_, double r_, double vx_, double vy_):r(r_), x(x_), y(y_){
- vx = vx_ / 1000.0 * msec;
- vy = vy_ / 1000.0 * msec;
- }
- void print_circle(){
- int n=40;
- double cx=x + r;
- double cy=y;
- double step = p/n;
- for (float f=step; f<=p*2;f+=step){
- draw_line(cx, cy ,cos(f)*r+x ,sin(f)*r+y);
- cx=x + cos(f)*r;
- cy=y + sin(f)*r;
- }
- }
- void update(){
- if(x+r>kx || x-r<-kx){
- vx*=-1;
- }
- if(y+r>ky || y-r<-ky){
- vy*=-1;
- }
- x+=vx;
- y+=vy;
- }
- };
- class Vector{
- public:
- double x,y;
- Vector(float a,float b):x(a),y(b){
- }
- Vector operator +(Vector other){
- return Vector(x+other.x, y+other.y);
- }
- Vector operator -(Vector other){
- return Vector(x-other.x, y-other.y);
- }
- Vector operator *(float a){
- return Vector(x*a,y*a);
- }
- Vector operator /(float a){
- return Vector(x/a,y/a);
- }
- float dot (Vector other){
- return x*other.x+y*other.y;
- }
- void print(){
- cout<<'('<<x<<','<<y<<')'<<endl;
- }
- void draw(){
- static double d = 6.0;
- Vector n1(-y, x);
- Vector n2(y, -x);
- Vector a(x,y);
- Vector ar_1 = (n1 - a*2)/d;
- Vector ar_2 = (n2 - a*2)/d;
- draw_line(0,0,x,y);
- draw_line(x, y, ar_1.x + x, ar_1.y + y);
- draw_line(x, y, ar_2.x + x, ar_2.y + y);
- }
- };
- class Liniya{
- public:
- double x,y;
- Liniya(float a,float b):x(a),y(b){
- }
- Liniya operator +(Liniya other){
- return Liniya(x+other.x, y+other.y);
- }
- Liniya operator -(Liniya other){
- return Liniya(x-other.x, y-other.y);
- }
- Liniya operator *(float a){
- return Liniya (x*a,y*a);
- }
- Liniya operator /(float a){
- return Liniya (x/a,y/a);
- }
- float dot (Liniya other){
- return x*other.x+y*other.y;
- }
- void print(){
- cout<<'('<<x<<','<<y<<')'<<endl;
- }
- void draw(){
- static double d = 6.0;
- Liniya n1(-y, x);
- Liniya n2(y, -x);
- Liniya a(x,y);
- //Liniya ar_1 = (n1 - a*2)/d;
- //Liniya ar_2 = (n2 - a*2)/d;
- draw_line(0,0,x,y);
- //draw_line(x, y, ar_1.x + x, ar_1.y + y);
- //draw_line(x, y, ar_2.x + x, ar_2.y + y);
- }
- };
- Liniya c(2,2);
- Vector b(2,2);
- circle a(0, 0, 0.4, 20, 40);
- circle n(0, 0, 1, 0, 0);
- void draw_clocks(float stop_angle){
- int j = 0;
- float coef = 0.8;
- for(float angle = 0; angle < 2 * p; angle += p/30.0, j++){
- float x = cos(angle);
- float y = sin(angle);
- if(j % 5 == 0) coef = 0.6;
- else coef = 0.8;
- draw_line(x, y, x * coef, y * coef);
- }
- }
- void Render() {
- static double cur_angle = 1.5*p;
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- glTranslatef(0, 0, -10);
- glColor3d(1, 1, 1);
- glScalef(scale, scale, scale);
- a.print_circle();
- draw_clocks(cur_angle);
- Vector b(cos(-cur_angle),sin(-cur_angle));
- // Liniya c()
- cur_angle += angle_step;
- if (cur_angle - 1.5 * p > 2 * p) cur_angle -= 2*p;
- draw_line(-kx,-ky,kx,-ky);
- draw_line(kx,-ky,kx,ky);
- draw_line(kx,ky,-kx,ky);
- draw_line(-kx,ky,-kx,-ky);
- b.draw();
- n.print_circle();
- a.update();
- glFlush();
- }
- void update(int t) {
- Render();
- glutTimerFunc(msec, update, 0);
- }
- void keyb(unsigned char key, int x, int y) {
- if(key == '+') {
- scale *= 1.1;
- }
- if(key == '-') {
- scale *= 0.9;
- }
- if(key == 'w') {
- kx *= 1.1;
- }
- Render();
- }
- void reshape(int w, int h) {
- // предупредим деление на ноль
- // если окно сильно перетянуто будет
- if(h == 0)
- h = 1;
- float ratio = 1.0* w / h;
- // используем матрицу проекции
- glMatrixMode(GL_PROJECTION);
- // Reset матрицы
- glLoadIdentity();
- // определяем окно просмотра
- glViewport(0, 0, w, h);
- // установить корректную перспективу.
- gluPerspective(45,ratio,1,1000);
- // вернуться к модели
- glMatrixMode(GL_MODELVIEW);
- }
- int main(int argc, char **argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize(800, 600);
- glutCreateWindow("Physics Engine");
- glEnable(GL_DEPTH_TEST);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glClearColor(0, 0, 0.0, 0.0);
- glutTimerFunc(50, update, 0);
- glutReshapeFunc(reshape);
- glutDisplayFunc(Render);
- glutKeyboardFunc(keyb);
- glutMainLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement