Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <list>
- #include<cmath>
- #include <algorithm>
- #include <vector>
- #include <GL/glut.h>
- #include <GL/gl.h>
- #include <GL/glu.h>
- using namespace std;
- double scale = 1.0;
- float update_time = 1000;
- float angle_step = M_PI / 30.0 / 1000.0 * update_time;
- class vector2d {
- public:
- float x ;
- float y;
- vector2d() : x(0) , y(0){}
- vector2d (float a ,float b ) : x(a) , y(b) {}
- void print(){
- cout << x <<'\t' << y << endl;
- }
- float dot(vector2d other){
- return (x * other.x + y * other.y);
- }
- vector2d operator + (vector2d other){
- return vector2d(x + other.x, y + other.y);
- }
- vector2d operator - (vector2d other){
- return vector2d(x - other.x, y - other.y);
- }
- vector2d operator * (float c){
- return vector2d (x * c , y * c);
- }
- vector2d operator / (float c){
- return vector2d (x / c , y / c);
- }
- };
- 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();
- }
- void draw_arrow(vector2d a){
- draw_line(0, 0, a.x, a.y);
- vector2d n1(-a.y, a.x);
- vector2d n2(a.y, -a.x);
- vector2d ar_1 = (n1 - a * 3) / 10.0;
- vector2d ar_2 = (n2 - a * 3) / 10.0;
- draw_line(a.x, a.y, a.x + ar_1.x, a.y + ar_1.y);
- draw_line(a.x, a.y , a.x + ar_2.x, a.y + ar_2.y);
- }
- void Render() {
- static float arrow_angle = -0.5 * M_PI;
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- glTranslatef(0, 0, -10);
- glColor3d(1, 1, 1);
- glScalef(scale, scale, scale);
- vector2d a(cos(-arrow_angle),sin(-arrow_angle));
- arrow_angle += angle_step;
- if (arrow_angle > 2 * M_PI) arrow_angle -= 2 * M_PI;
- //draw_line(0,0,1,1);
- draw_arrow(a);
- glFlush();
- }
- void update(int t) {
- Render();
- glutTimerFunc(update_time, 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