-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplay.cpp
More file actions
218 lines (183 loc) · 6.28 KB
/
Display.cpp
File metadata and controls
218 lines (183 loc) · 6.28 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
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
#include <chrono>
#include <thread>
#include "../gl.h"
#include "Display.h"
#include "igl/igl_inline.h"
#include <igl/get_seconds.h>
static void glfw_error_callback(int error, const char* description)
{
fputs(description, stderr);
}
Display::Display(int windowWidth, int windowHeight, const std::string& title)
{
bool resizable = true, fullscreen = false;
glfwSetErrorCallback(glfw_error_callback);
if (!glfwInit())
{
exit(EXIT_FAILURE);
}
glfwWindowHint(GLFW_SAMPLES, 8);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
//#ifdef __APPLE__
// glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
//#endif
// if (fullscreen)
// {
// GLFWmonitor* monitor = glfwGetPrimaryMonitor();
// const GLFWvidmode* mode = glfwGetVideoMode(monitor);
// window = glfwCreateWindow(mode->width, mode->height, title.c_str(), monitor, nullptr);
// windowWidth = mode->width;
// windowHeight = mode->height;
// }
// else
// {
// Set default windows width
//if (windowWidth <= 0 & core_list.size() == 1 && renderer->core().viewport[2] > 0)
// windowWidth = renderer->core().viewport[2];
//else
// if (windowWidth <= 0)
// windowWidth = 1280;
//// Set default windows height
//if (windowHeight <= 0 & core_list.size() == 1 && renderer->core().viewport[3] > 0)
// windowHeight = renderer->core().viewport[3];
//else if (windowHeight <= 0)
// windowHeight = 800;
// window = glfwCreateWindow(windowWidth, windowHeight, title.c_str(), nullptr, nullptr);
// }
window = glfwCreateWindow(windowWidth, windowHeight, title.c_str(), nullptr, nullptr);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
// Load OpenGL and its extensions
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
printf("Failed to load OpenGL and its extensions\n");
exit(EXIT_FAILURE);
}
#if defined(DEBUG) || defined(_DEBUG)
printf("OpenGL Version %d.%d loaded\n", GLVersion.major, GLVersion.minor);
int major, minor, rev;
major = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
minor = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
rev = glfwGetWindowAttrib(window, GLFW_CONTEXT_REVISION);
printf("OpenGL version received: %d.%d.%d\n", major, minor, rev);
printf("Supported OpenGL is %s\n", (const char*)glGetString(GL_VERSION));
printf("Supported GLSL is %s\n", (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION));
#endif
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
//Tamir: changes from here
// Initialize FormScreen
// __viewer = this;
// Register callbacks
//glfwSetKeyCallback(window, glfw_key_callback);
//glfwSetCursorPosCallback(window,glfw_mouse_move);
//glfwSetScrollCallback(window, glfw_mouse_scroll);
//glfwSetMouseButtonCallback(window, glfw_mouse_press);
//glfwSetWindowSizeCallback(window,glfw_window_size);
//glfwSetCharModsCallback(window,glfw_char_mods_callback);
//glfwSetDropCallback(window,glfw_drop_callback);
// Handle retina displays (windows and mac)
//int width, height;
//glfwGetFramebufferSize(window, &width, &height);
//int width_window, height_window;
//glfwGetWindowSize(window, &width_window, &height_window);
//highdpi = windowWidth/width_window;
//glfw_window_size(window,width_window,height_window);
//opengl.init();
// core().align_camera_center(data().V, data().F);
// Initialize IGL viewer
// init();
}
bool Display::launch_rendering(bool loop)
{
// glfwMakeContextCurrent(window);
// Rendering loop
const int num_extra_frames = 5;
int frame_counter = 0;
int windowWidth, windowHeight;
//main loop
Renderer* renderer = (Renderer*)glfwGetWindowUserPointer(window);
igl::opengl::glfw::Viewer* scn= renderer->GetScene();
glfwGetWindowSize(window, &windowWidth, &windowHeight);
renderer->post_resize(window, windowWidth, windowHeight);
while (!glfwWindowShouldClose(window))
{
double tic = igl::get_seconds();
if(renderer->shouldIK)
renderer->animateIK();
// music check
renderer->GetScene()->player.checks();
renderer->moveSpheres();
renderer->draw(window);
renderer->select_hovered_core(window);
glfwSwapBuffers(window);
if (renderer->core().is_animating || frame_counter++ < num_extra_frames)
{//motion
glfwPollEvents();
// In microseconds
double duration = 1000000. * (igl::get_seconds() - tic);
const double min_duration = 1000000. / renderer->core().animation_max_fps;
if (duration < min_duration)
{
std::this_thread::sleep_for(std::chrono::microseconds((int)(min_duration - duration)));
}
}
else
{
//
glfwPollEvents();
frame_counter = 0;
}
if (!loop)
return !glfwWindowShouldClose(window);
#ifdef __APPLE__
static bool first_time_hack = true;
if (first_time_hack) {
glfwHideWindow(window);
glfwShowWindow(window);
first_time_hack = false;
}
#endif
}
return EXIT_SUCCESS;
}
void Display::AddKeyCallBack(void(*keyCallback)(GLFWwindow*, int, int, int, int))
{
glfwSetKeyCallback(window, (void(*)(GLFWwindow*, int, int, int, int))keyCallback);//{
}
void Display::AddMouseCallBacks(void (*mousebuttonfun)(GLFWwindow*, int, int, int), void (*scrollfun)(GLFWwindow*, double, double), void (*cursorposfun)(GLFWwindow*, double, double))
{
glfwSetMouseButtonCallback(window, mousebuttonfun);
glfwSetScrollCallback(window, scrollfun);
glfwSetCursorPosCallback(window, cursorposfun);
}
void Display::AddResizeCallBack(void (*windowsizefun)(GLFWwindow*, int, int))
{
glfwSetWindowSizeCallback(window, windowsizefun);
}
void Display::SetRenderer(void* userPointer)
{
glfwSetWindowUserPointer(window, userPointer);
}
void* Display::GetScene()
{
return glfwGetWindowUserPointer(window);
}
void Display::SwapBuffers()
{
glfwSwapBuffers(window);
}
void Display::PollEvents()
{
glfwPollEvents();
}
Display::~Display()
{
glfwDestroyWindow(window);
glfwTerminate();
}