-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
152 lines (124 loc) · 2.76 KB
/
main.cpp
File metadata and controls
152 lines (124 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// Created by petar on 24.7.19..
//
#include <iostream>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
#include <vector>
#include <algorithm>
#include "figures.h"
void on_timer(int value);
void on_display();
void on_keyboard(unsigned char key, int x, int y);
void on_timer(int value);
void on_reshape(int w, int h);
void init();
int window_width = 800;
int window_height = 500;
std::vector<float> camera_pos {1.0, 1.0, 1.0};
float dz = 0;
float dx = 0;
float dy = 0;
bool engine_on;
bool in_reverse;
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(window_width, window_height);
glutInitWindowPosition(100, 100);
glutCreateWindow("Drive");
glutDisplayFunc(on_display);
glutKeyboardFunc(on_keyboard);
glutReshapeFunc(on_reshape);
init();
glutMainLoop();
return 0;
}
void on_display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(
60,
window_width/(float)window_height,
1, 25);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(
camera_pos[0], camera_pos[1], camera_pos[2],
0.0, 0.0, 0.0,
0.0, 1.0, 0.0
);
ground();
coordinates();
// car movement
glRotatef(dx, 0, 1, 0);
glTranslatef(0, 0, dz);
car();
glutSwapBuffers();
}
void on_reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(
60,
w/(float)h,
1, 25);
}
void on_keyboard(unsigned char key, int x, int y)
{
switch(key) {
case 27:
exit(0);
break;
case 'w':
in_reverse = false;
if (!engine_on) {
glutTimerFunc(10, on_timer, 0);
engine_on = true;
}
break;
case 's':
in_reverse = true;
break;
case 'a':
if (engine_on)
dx += 0.3;
break;
case 'd':
if (engine_on)
dx -= 0.3;
break;
case 32:
engine_on = false;
break;
case 'r':
glutTimerFunc(10, on_timer, 0);
engine_on = false;
dz = 0;
break;
}
}
void init()
{
glClearColor(0.75, 0.75, 0.75, 0);
glEnable(GL_DEPTH_TEST);
}
void on_timer(int value)
{
if (value != 0) {
return;
}
if (engine_on) {
dz += in_reverse ? -0.005 : 0.007;
}
glutPostRedisplay();
if (engine_on) {
glutTimerFunc(10, on_timer, 0);
}
}