-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
161 lines (136 loc) · 3.69 KB
/
main.cpp
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
152
153
154
155
156
157
158
159
160
161
#include <iostream>
#include "src/Point.h"
#include "src/primitives.h"
#include "src/Plate.h"
#include "src/Object.h"
#include "src/scene.h"
#include "src/Camera.h"
#include <math.h>
#include <unistd.h>
#include <thread>
#include <chrono>
#include <cstring>
#include "extras/mouse.h"
#include "src/objects/Cube.h"
#include <cstdlib>
using namespace std;
Point3D camRotation(0.7, -1, 0);
Point3D camPosition(200, -100, -200);
int fps = 0;
clock_t msBefore = 0;
void renderAll(Scene *scene)
{
int currentMs = clock() - msBefore;
scene->render();
scene->display();
XSetForeground(di, gc, 0xffffff);
char fpsStr[16];
sprintf(fpsStr, "FPS: %.0f", (float)1000/currentMs);
XDrawString (di, double_buffer, gc, 16, 16, fpsStr, strlen(fpsStr));
char positionStr[32];
sprintf(positionStr, "X: %.2f, Y: %.2f, Z: %.2f", camPosition.x, camPosition.y, camPosition.z);
XDrawString (di, double_buffer, gc, 16, 30, positionStr, strlen(positionStr));
redrawBuf();
msBefore = clock();
}
int main()
{
srand((unsigned) time(NULL));
Scene *scene = new Scene();
Camera *camera = new Camera(PERSPECTIVE, 0);
scene->addCamera(camera);
Point3D position1(0, 0, 0);
Point3D rotation0(0, 0, 0);
for(int i = 0; i < 64; i++)
{
position1.x = -rand()%800;
position1.y = -rand()%800;
position1.z = -rand()%800;
Cube *cube = new Cube(&camRotation, &camPosition, rotation0, position1, Point3D(100, 100, 100));
cube->setColor(CUBE_PX, 0x663300);
cube->setColor(CUBE_NX, 0x663300);
cube->setColor(CUBE_PY, 0x00FF00);
cube->setColor(CUBE_NY, 0x663300);
cube->setColor(CUBE_PZ, 0x663300);
cube->setColor(CUBE_NZ, 0x663300);
scene->addObject(cube);
}
Mouse *mouse = new Mouse("/dev/input/mice");
mouse->init();
msBefore = clock();
Point3D motion;
motion.x = 0; motion.y = 0; motion.z = 0;
initGraphics(1600, 900);
cout << "TC3L Test" << endl;
XSelectInput(di, wi, KeyPressMask | KeyReleaseMask | ExposureMask | PointerMotionMask);
XEvent ev;
XMotionEvent myszelel;
XKeyEvent bieg;
bool autoRepeatSupported = false;
int quit = 0;
while (!quit)
{
while(XPending(di))
{
Point2D mouseData = mouse->getParsed();
camera->rotate(Point3D(mouseData.x/200, mouseData.y/200, 0));
int a = XNextEvent(di, &ev);
if (ev.type == Expose)
{
//renderAll();
}
if (ev.type == KeyPress)
{
/* exit on ESC key press */
if ( ev.xkey.keycode == 0x09 )
quit = 1;
switch(ev.xkey.keycode)
{
case 25:
motion.z = 1;
break;
case 39:
motion.z = -1;
break;
case 65:
motion.y = 1;
break;
case 50:
motion.y = -1;
break;
case 38:
motion.x = 1;
break;
case 40:
motion.x = -1;
break;
}
}
else if (ev.type == KeyRelease)
{
switch(ev.xkey.keycode)
{
case 25:
case 39:
motion.z = 0;
break;
case 38:
case 40:
motion.x = 0;
break;
case 50:
case 65:
motion.y = 0;
break;
}
//XClearWindow(di, wi) ;
}
}
renderAll(scene);
usleep(10000);
camera->moveForward(motion.z*10);
camera->moveLeft(motion.x*10);
camera->move(Point3D(0, motion.y*10, 0));
}
return 0;
}