-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.cpp
156 lines (126 loc) · 3.79 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
#include "scene.h"
#include <pxr/imaging/glf/glew.h>
#ifdef __APPLE__
#include <OpenGL/gl.h>
#else
#ifdef _WIN32
#include <windows.h>
#endif
#include <GL/gl.h>
#endif
#include <GLFW/glfw3.h>
#include <boost/predef/os.h>
#include <pxr/base/gf/camera.h>
#include <pxr/base/js/json.h>
#include <pxr/base/plug/plugin.h>
#include <pxr/base/tf/fileUtils.h>
#include <pxr/pxr.h>
#include <pxr/usd/usd/inherits.h>
#include <pxr/usd/usd/stage.h>
#include <pxr/usd/usdGeom/xformable.h>
#include <pxr/usdImaging/usdImagingGL/gl.h>
#include <fstream>
#include <streambuf>
PXR_NAMESPACE_USING_DIRECTIVE
// Window dimensions.
#define WIDTH 1280
#define HEIGHT 960
double scale = 150.0;
Scene* scene = NULL;
// Is called whenever a key is pressed/released via GLFW.
void key_callback(
GLFWwindow* window, int key, int scancode, int action, int mode)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
// Is called scrolled via GLFW.
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
scale *= pow(1.1, -yoffset);
}
void mouseButtonCallback(
GLFWwindow* window, int button, int action, int mods)
{
if (scene && action == GLFW_PRESS)
{
scene->click();
}
}
void cursorPosCallback(
GLFWwindow* window, double xpos, double ypos)
{
if (scene)
{
int width, height;
glfwGetWindowSize(window, &width, &height);
scene->cursor(xpos/width, ypos/height);
}
}
int main(int argc, char **argv)
{
printf("Starting GLFW context\n");
glfwInit();
// Set all the required options for GLFW.
#if (BOOST_OS_WINDOWS)
#else
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#if (BOOST_OS_MACOS)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
#else
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
#endif
glfwWindowHint(GLFW_SAMPLES, 16);
#endif
// Create a GLFWwindow object that we can use for GLFW's functions.
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Switch", NULL, NULL);
if (!window)
{
printf("Can't create GLFW context\n");
glfwTerminate();
return 1;
}
glfwMakeContextCurrent(window);
// Set the required callback functions.
glfwSetKeyCallback(window, key_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetMouseButtonCallback(window, mouseButtonCallback);
glfwSetCursorPosCallback(window, cursorPosCallback);
// Set this to true so GLEW knows to use a modern approach to retrieving
// function pointers and extensions.
glewExperimental = GL_TRUE;
// Initialize GLEW to setup the OpenGL Function pointers.
GlfGlewInit();
scene = new Scene;
double timer = glfwGetTime();
// Game loop
while (!glfwWindowShouldClose(window))
{
// Check if any events have been activiated (key pressed, mouse moved
// etc.) and call corresponding response functions.
glfwPollEvents();
double now = glfwGetTime();
scene->prepare(now - timer);
timer = now;
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_BLEND);
// Render
// Clear the colorbuffer.
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
scene->draw(width, height);
// Swap the screen buffers.
glfwSwapBuffers(window);
}
delete scene;
// Terminate GLFW, clearing any resources allocated by GLFW.
glfwTerminate();
return 0;
}