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;
- double scale = 1.0;
- double random(double a, double b ) {
- double r = (rand() % 10000) / 10000.0;
- return a + r * (b - a);
- }
- void draw_line(double x, double y, double x1, double y1) {
- glBegin(GL_LINES);
- glVertex2d(x, y);
- glVertex2d(x1, y1);
- glEnd();
- }
- void star(float sc, float x, float y) {
- draw_line(5*sc+x,10*sc+y, 8*sc+x,0*sc+y);
- draw_line(8*sc+x,0*sc+y, 0*sc+x,6*sc+y);
- draw_line(0*sc+x,6*sc+y, 10*sc+x,6*sc+y);
- draw_line(10*sc+x,6*sc+y, 2*sc+x,0*sc+y);
- draw_line(2*sc+x,0*sc+y, 5*sc+x,10*sc+y);
- }
- double sx1 = 0;
- double sx = 0;
- double sy1 = 0;
- double sy = 0;
- void Render() {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- glTranslatef(0, 0, -10);
- glColor3d(1, 0.843, 0);
- glScalef(scale, scale, scale);
- //draw_line(0,0, 0,1);
- //draw_line(0,1, 1,1);
- //draw_line(1,1, 1,0);
- //draw_line(1,0, 0,0);
- //star(0.1, -3, -3);
- //star(0.2, -1, 0);
- //star(0.3, -3, -2);
- star (random(0.4, 0.41),sx+sx1+random(-0.02, 0.02), sy+sy1+ random(-0.02, 0.02));
- glFlush();
- }
- void update(int t) {
- glutTimerFunc(50, update, 0);
- Render();
- }
- void keyb(unsigned char key, int x, int y) {
- cout << key << endl;
- if(key == 'w') {
- sy1 += 0.1;
- }
- if(key == 'a') {
- sx1 -= 0.1;
- }
- if(key == 's') {
- sy -= 0.1;
- }
- if(key == 'd') {
- sx += 0.1;
- }
- }
- 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(1920, 980);
- glutCreateWindow("Physics Engine");
- glEnable(GL_DEPTH_TEST);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glClearColor(1.0, 0.05, 0.5, 0.4);
- glutTimerFunc(50, update, 0);
- glutReshapeFunc(reshape);
- glutDisplayFunc(Render);
- glutKeyboardFunc(keyb);
- glutMainLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement