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;
- double scale = 1.0;
- 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:
- float x, y, radius, vx, vy;
- Circle(float cx, float cy, float vx, float vy, float rad) : x(cx), y(cy), radius(rad), vx(vx), vy(vy) {}
- void draw(){
- float step = 0.04;
- for (float f = 0; f <= M_PI * 2; f += step){
- draw_line(x + radius * cos(f), y + radius * sin(f), x + radius * cos(f+step), y + radius * sin(f+step));
- }
- }
- void update(){
- if (x > 4.0 || x < -4.0){
- vx = -vx;
- }
- x += vx;
- y += vy;
- }
- };
- Circle c1(0,0,0.1,0.0,0.2);
- Circle c2(0,1,-0.2,0.0,0.5);
- static float TIME = 0.0;
- void draw_circle(float x0, float y0, float vx, float vy, float r, int n){
- float step = M_PI*2.0f / n;
- if (x0 + vx * TIME < -5.0){
- vx = -vx;
- }
- for (float f = 0; f <= M_PI * 2; f += step){
- draw_line(x0 + vx * TIME + r * cos(f), y0 + vy * TIME + r * sin(f), x0 + vx * TIME + r * cos(f+step), y0 + vy * TIME + r * sin(f+step));
- }
- }
- void Render() {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- glTranslatef(0, 0, -10);
- glColor3d(1, 1, 1);
- glScalef(scale, scale, scale);
- //draw_circle(0.0, 0.2, 0.01, 0.0, 0.2, 8);
- //draw_circle(0.0, 0.0, -0.01, 0.0, 0.4, 6);
- //draw_circle(0.0, -0.2, 0.000, 0.0, 0.6, 18);
- //TIME += 2.5;
- c1.draw();
- c1.update();
- c2.draw();
- c2.update();
- glFlush();
- }
- void update(int t) {
- Render();
- glutTimerFunc(50, update, 0);
- }
- void keyb(unsigned char key, int x, int y) {
- if(key == '+') {
- scale *= 1.1;
- }
- if(key == '-') {
- scale *= 0.9;
- }
- 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, 128.0, 0.0);
- glutTimerFunc(50, update, 0);
- glutReshapeFunc(reshape);
- glutDisplayFunc(Render);
- glutKeyboardFunc(keyb);
- glutMainLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment