-
Notifications
You must be signed in to change notification settings - Fork 0
/
EdvsRiftApp.cpp
232 lines (179 loc) · 6.04 KB
/
EdvsRiftApp.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "Common.h"
#include "EdvsRiftApp.h"
#include "InputEvent.hpp"
#include <random>
#include <cstring>
#include <glm/gtx/rotate_vector.hpp>
using namespace oglplus;
void EdvsRiftApp::initGl()
{
RiftApp::initGl();
vao = VertexArrayPtr(new VertexArray());
vbo_position = BufferPtr(new Buffer());
vbo_color = BufferPtr(new Buffer());
vbo_camera_id = BufferPtr(new Buffer());
}
void EdvsRiftApp::update()
{
RiftApp::update();
mutex_->lock();
parity_ = edvs_event_handler_->parity();
position_ = edvs_event_handler_->position();
camera_id_ = edvs_event_handler_->camera_id();
time_ = edvs_event_handler_->time();
mutex_->unlock();
ovrTrackingState tracking_state = ovrHmd_GetTrackingState(hmd, 0.0);
auto orientation = glm::vec4(
tracking_state.CameraPose.Orientation.x,
tracking_state.CameraPose.Orientation.y,
tracking_state.CameraPose.Orientation.z,
tracking_state.CameraPose.Orientation.w
);
auto position = glm::vec3(
tracking_state.CameraPose.Position.x,
tracking_state.CameraPose.Position.y,
tracking_state.CameraPose.Position.z
);
tracking_info_t info = {
orientation,
position
};
std::vector<unsigned char> data;
data.resize(sizeof(tracking_info_t));
std::memcpy(&data[0], &info, sizeof(tracking_info_t));
auto e = DispatcherEvent(type_id, &data);
dispatcher_->dispatch(&e);
}
void EdvsRiftApp::drawEvents()
{
static ProgramPtr program = oria::loadProgram("./resources/event_pixel.vs", "./resources/event_pixel.fs");
// Reset before application exit
Platform::addShutdownHook([]
{
program.reset();
});
MatrixStack & mv = Stacks::modelview();
mv.withPush([&]
{
// Binds the program
program->Use();
// Matrices as uniforms
Mat4Uniform(*program, "ModelView").Set(Stacks::modelview().top());
Mat4Uniform(*program, "Projection").Set(Stacks::projection().top());
// Manual set for azimuth and elevation
oglplus::Uniform<float>(*program, "ManAzimuth").Set(azimuth);
oglplus::Uniform<float>(*program, "ManElevation").Set(elevation);
// Manual set for field of view
oglplus::Uniform<float>(*program, "FovX").Set(60.0 * DEGREES_TO_RADIANS);
oglplus::Uniform<float>(*program, "FovY").Set(60.0 * DEGREES_TO_RADIANS);
// Bind VAO
vao->Bind();
// Bind VBO "Positions"
vbo_position->Bind(Buffer::Target::Array);
{
Buffer::Data(Buffer::Target::Array, position_);
VertexArrayAttrib vao_attr(*program, "Position");
vao_attr.Setup<Vec2f>();
vao_attr.Enable();
}
// Bind VBO "CameraId"
vbo_camera_id->Bind(Buffer::Target::Array);
{
Buffer::Data(Buffer::Target::Array, camera_id_);
VertexArrayAttrib vao_attr(*program, "CameraId");
vao_attr.Setup<Vec1f>();
vao_attr.Enable();
}
// Bind VBO "Color"
vbo_color->Bind(Buffer::Target::Array);
{
Buffer::Data(Buffer::Target::Array, parity_); //, BufferUsage::StreamDraw);
VertexArrayAttrib vao_attr(*program, "Color");
vao_attr.Setup<Vec1f>();
vao_attr.Enable();
}
// Draw all elements
Context::DrawArrays(PrimitiveType::Points, 0, camera_id_.size());
});
// Unbind
oglplus::NoProgram().Bind();
oglplus::NoVertexArray().Bind();
}
void EdvsRiftApp::drawSphereBackground(int camera_id)
{
static ProgramPtr program = oria::loadProgram("./resources/sphere_background.vs", "./resources/sphere_background.fs");
static ShapeWrapperPtr geometry = ShapeWrapperPtr(new shapes::ShapeWrapper({ "Position" }, shapes::ObjMesh(mesh_input.stream), *program));
// Reset before application exit
Platform::addShutdownHook([]
{
program.reset();
geometry.reset();
});
MatrixStack & mv = Stacks::modelview();
mv.withPush([&]
{
// Binds the program
program->Use();
// Matrices as uniforms
Mat4Uniform(*program, "ModelView").Set(Stacks::modelview().top());
Mat4Uniform(*program, "Projection").Set(Stacks::projection().top());
// Draw this
geometry->Use();
geometry->Draw();
});
// Unbind
oglplus::NoProgram().Bind();
oglplus::NoVertexArray().Bind();
}
void EdvsRiftApp::renderScene()
{
using namespace oglplus;
Context::ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Context::ClearDepth(1.0f);
Context::Clear().ColorBuffer();
Context::Clear().DepthBuffer();
MatrixStack & mv = Stacks::modelview();
Context::Disable(Capability::CullFace);
glDisable(GL_CULL_FACE);
mv.withPush([&]
{
mv.rotate(90 * DEGREES_TO_RADIANS, glm::vec3(1.0, 0.0, 0.0));
mv.scale(10.1f);
if (camera_id_.size() > 0)
{
drawEvents();
}
});
Context::Enable(Capability::CullFace);
Context::Enable(Capability::ProgramPointSize);
}
void EdvsRiftApp::onKey(int key, int scancode, int action, int mods)
{
if (GLFW_PRESS == action)
{
switch (key)
{
case GLFW_KEY_UP:
elevation -= 10 * DEGREES_TO_RADIANS;
elevation = elevation < -90 * DEGREES_TO_RADIANS ? -90 : elevation;
return;
case GLFW_KEY_DOWN:
elevation += 10 * DEGREES_TO_RADIANS;
elevation = elevation > 90 * DEGREES_TO_RADIANS ? 90 : elevation;
return;
case GLFW_KEY_LEFT:
azimuth -= 10 * DEGREES_TO_RADIANS;
return;
case GLFW_KEY_RIGHT:
azimuth += 10 * DEGREES_TO_RADIANS;
return;
}
}
RiftApp::onKey(key, scancode, action, mods);
// Dispatch all over events
auto input_event = InputEvent(key, scancode, action, mods);
auto data = input_event.serialize();
// Pack new event and dispatch it
auto e = DispatcherEvent(InputEvent::type_id, &data);
dispatcher_->dispatch(&e);
}