-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
299 lines (239 loc) · 8.51 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <iostream>
#include <mutex> // For std::unique_lock
#include <shared_mutex>
#ifdef _WIN32
#include "GL/glew.h"
#include "GL/wglew.h"
#elif (!defined(__APPLE__))
#include "GL/glxew.h"
#endif
// OpenGL Headers
#if defined(WIN32)
#define NOMINMAX
#include <windows.h>
#include <GL/gl.h>
#elif defined(__APPLE__)
#define GL_GLEXT_PROTOTYPES
#include <OpenGL/gl3.h>
#include <OpenGL/glext.h>
#else
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#endif
// MinVR header
#include <api/MinVR.h>
using namespace MinVR;
// Just included for some simple Matrix math used below
// This is not required for use of MinVR in general
#include <math/VRMath.h>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/epsilon.hpp>
#include <glm/gtx/matrix_decompose.hpp>
#include <glm/gtx/quaternion.hpp>
#include <glm/gtx/projection.hpp>
#include <glm/gtx/rotate_vector.hpp>
using namespace glm;
#include "VRMultithreadedApp.h"
#include "4DUtils.h"
struct CameraInfo {
mat4 previousRealWorldViewMatrix;
vec4 pos;
vec4 forwardDir;
vec4 upDir;
vec4 rightDir;
};
/**
* MyVRApp is an example of a modern OpenGL using VBOs, VAOs, and shaders. MyVRApp inherits
* from VRGraphicsApp, which allows you to override onVREvent to get input events, onRenderContext
* to setup context sepecific objects, and onRenderScene that renders to each viewport.
*/
class MyVRApp : public VRMultithreadedApp {
public:
MyVRApp(int argc, char** argv) : VRMultithreadedApp(argc, argv) {
}
void onAnalogChange(const VRAnalogEvent &state) {}
void onButtonDown(const VRButtonEvent &state) {
if (state.getName() == "KbdEsc_Down") {
shutdown();
}
}
void onButtonUp(const VRButtonEvent &state) {}
void onCursorMove(const VRCursorEvent &state) {}
bool firstTime = true;
bool moved = false;
void onTrackerMove(const VRTrackerEvent &state) {
if (state.getName().substr(0, 4) == headTrackingEventName) {
curHeadMatrix = make_mat4(state.getTransform());
if (firstTime) {
firstTime = false;
prevHeadMatrix = curHeadMatrix;
}
else {
moved = true;
}
}
}
void onRenderAudio(const VRAudioState& state) {}
void onRenderConsole(const VRConsoleState& state) {}
void updateWorld(double currentTime) {
if (moved) {
changeByMatrixDifference(prevHeadMatrix, curHeadMatrix, USER_SCALE, &userState);
prevHeadMatrix = curHeadMatrix;
moved = false;
}
}
void onRenderGraphicsContext(const VRGraphicsState& state) {
// If this is the inital call, initialize context variables
if (state.isInitialRenderCall()) {
#ifndef __APPLE__
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (GLEW_OK != err)
{
std::cout << "Error initializing GLEW." << std::endl;
}
#endif
// Init GL
glClearColor(0, 0, 0, 0);
std::vector<vec3> cpuVertexArray;
std::vector<int> cpuIndexArray;
cpuVertexArray.push_back({ -1, -1, 0 });
cpuVertexArray.push_back({ 1, -1, 0 });
cpuVertexArray.push_back({ -1, 1, 0 });
cpuVertexArray.push_back({ 1, 1, 0 });
cpuIndexArray.push_back(0);
cpuIndexArray.push_back(1);
cpuIndexArray.push_back(2);
cpuIndexArray.push_back(3);
const int cpuVertexByteSize = sizeof(float[3]) * cpuVertexArray.size();
const int cpuIndexByteSize = sizeof(int) * cpuIndexArray.size();
_numIndices = cpuIndexArray.size();
glGenVertexArrays(1, &_vaoID);
glBindVertexArray(_vaoID);
// create the vbo
glGenBuffers(1, &_vertexVBO);
glBindBuffer(GL_ARRAY_BUFFER, _vertexVBO);
// initialize size
glBufferData(GL_ARRAY_BUFFER, cpuVertexByteSize, NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, cpuVertexByteSize, &cpuVertexArray[0]);
// set up vertex attributes
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float[3]), (void*)0);
// Create indexstream
glGenBuffers(1, &_indexVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexVBO);
// copy data into the buffer object
glBufferData(GL_ELEMENT_ARRAY_BUFFER, cpuIndexByteSize, NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, cpuIndexByteSize, &cpuIndexArray[0]);
// Create shaders
GLuint vshader = loadAndCompileShader("shader.vert", GL_VERTEX_SHADER);
GLuint fshader = loadAndCompileShader("shader.frag", GL_FRAGMENT_SHADER);
// Create shader program
_programHandle = glCreateProgram();
glAttachShader(_programHandle, vshader);
glAttachShader(_programHandle, fshader);
linkShaderProgram(_programHandle);
glUseProgram(_programHandle);
viewportResolutionLocation = glGetUniformLocation(_programHandle, "viewportResolution");
projectionMatLocation = glGetUniformLocation(_programHandle, "projectionMat");
userPosLocation = glGetUniformLocation(_programHandle, "userPos");
userForwardDirLocation = glGetUniformLocation(_programHandle, "userForwardDir");
userUpDirLocation = glGetUniformLocation(_programHandle, "userUpDir");
userRightDirLocation = glGetUniformLocation(_programHandle, "userRightDir");
testRotationMethods();
}
}
void onRenderGraphicsScene(const VRGraphicsState& state) {
//changeMatrix is a view matrix from the old matrix to the new one
mat4 viewMatrix = make_mat4(state.getViewMatrix());
CurvedWorldPosAndRot thisViewPosAndRot = userState;
changeByMatrixDifference(curHeadMatrix, inverse(viewMatrix), USER_SCALE, &thisViewPosAndRot);
// Setup uniforms
GLfloat windowHeight = state.index().getValue("FramebufferHeight");
GLfloat windowWidth = state.index().getValue("FramebufferWidth");
glUniform2f(viewportResolutionLocation, windowWidth, windowHeight);
mat4 projectionMat = make_mat4(state.getProjectionMatrix());
setUniform(projectionMatLocation, projectionMat, GL_FALSE);
setUniform(userPosLocation, thisViewPosAndRot.pos);
setUniform(userForwardDirLocation, thisViewPosAndRot.forwardDir);
setUniform(userUpDirLocation, thisViewPosAndRot.upDir);
setUniform(userRightDirLocation, thisViewPosAndRot.rightDir);
// Render
glDrawElements(GL_TRIANGLE_STRIP, _numIndices, GL_UNSIGNED_INT, 0);
}
void onRenderHaptics(const VRHapticsState& state) {}
void setUniform(GLint location, glm::mat4& matrix, GLboolean transpose) {
glUniformMatrix4fv(location, 1, transpose, &(matrix[0][0]));
}
void setUniform(GLint location, vec4 vector) {
glUniform4f(location, vector.x, vector.y, vector.z, vector.w);
}
GLuint loadAndCompileShader(const std::string& pathToFile, GLuint shaderType) {
std::ifstream inFile(pathToFile, std::ios::in);
if (!inFile) {
throw std::runtime_error("could not load file");
}
// Get file contents
std::stringstream code;
code << inFile.rdbuf();
inFile.close();
std::string asString = code.str();
return compileShader(asString, shaderType);
}
/// Compiles shader
GLuint compileShader(const std::string& shaderText, GLuint shaderType) {
const char* source = shaderText.c_str();
int length = (int)shaderText.size();
GLuint shader = glCreateShader(shaderType);
glShaderSource(shader, 1, &source, &length);
glCompileShader(shader);
GLint status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if(status == GL_FALSE) {
GLint length;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
std::vector<char> log(length);
glGetShaderInfoLog(shader, length, &length, &log[0]);
std::cerr << &log[0];
}
return shader;
}
/// links shader program
void linkShaderProgram(GLuint shaderProgram) {
glLinkProgram(shaderProgram);
GLint status;
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &status);
if(status == GL_FALSE) {
GLint length;
glGetProgramiv(shaderProgram, GL_INFO_LOG_LENGTH, &length);
std::vector<char> log(length);
glGetProgramInfoLog(shaderProgram, length, &length, &log[0]);
std::cerr << "Error compiling program: " << &log[0] << std::endl;
}
}
private:
GLuint _vaoID;
GLuint _vertexVBO;
GLuint _indexVBO;
GLsizei _numIndices;
GLuint _programHandle;
GLint viewportResolutionLocation;
GLint projectionMatLocation;
GLint userPosLocation;
GLint userForwardDirLocation;
GLint userUpDirLocation;
GLint userRightDirLocation;
float USER_SCALE = 1;
mat4 curHeadMatrix = mat4(1.0);
mat4 prevHeadMatrix = mat4(1.0);
CurvedWorldPosAndRot userState = { vec4(0,1,0,0), vec4(1,0,0,0), vec4(0,0,0,1), vec4(0,0,1,0) };
};
/// Main method which creates and calls application
int main(int argc, char **argv) {
MyVRApp app(argc, argv);
app.run();
app.shutdown();
return 0;
}