1
1
#include " viewer.h"
2
2
#include < cstdlib>
3
3
4
- Viewer::Viewer () : shader_folder(" src/shader/" )
4
+
5
+ Viewer::Viewer () : shader_folder(" src/shader/" ),
6
+ win_width(600 ),
7
+ win_height(400 )
5
8
{
6
9
// init glfw - if already initialized nothing happens
7
10
int init = glfwInit ();
@@ -32,7 +35,7 @@ void Viewer::initialize()
32
35
#endif
33
36
// glfwWindowHint(GLFW_VISIBLE, debug ? GL_TRUE : GL_FALSE);
34
37
35
- window = glfwCreateWindow (1280 , 800 , " Viewer (press ESC to exit)" , 0 , NULL );
38
+ window = glfwCreateWindow (win_width* 2 , win_height* 2 , " Viewer (press ESC to exit)" , 0 , NULL );
36
39
if (window == NULL )
37
40
{
38
41
std::cerr << " Failed to create opengl window." << std::endl;
@@ -106,10 +109,23 @@ void Viewer::initialize()
106
109
107
110
glfwSetWindowUserPointer (window, this );
108
111
glfwSetKeyCallback (window, Viewer::key_callbackstatic);
112
+ glfwSetWindowSizeCallback (window, Viewer::winsize_callbackstatic);
109
113
110
114
shouldStop = false ;
111
115
}
112
116
117
+ void Viewer::winsize_callbackstatic (GLFWwindow* window, int w, int h)
118
+ {
119
+ Viewer* viewer = reinterpret_cast <Viewer*>(glfwGetWindowUserPointer (window));
120
+ viewer->winsize_callback (window, w, h);
121
+ }
122
+
123
+ void Viewer::winsize_callback (GLFWwindow* window, int w, int h)
124
+ {
125
+ win_width = w/2 ;
126
+ win_height = h/2 ;
127
+ }
128
+
113
129
void Viewer::key_callbackstatic (GLFWwindow* window, int key, int scancode, int action, int mods)
114
130
{
115
131
Viewer* viewer = reinterpret_cast <Viewer*>(glfwGetWindowUserPointer (window));
@@ -135,25 +151,38 @@ bool Viewer::render()
135
151
// wipe the drawing surface clear
136
152
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
137
153
138
- GLint x = 0 , y = 0 , width = 600 , height = 400 ;
154
+ GLint x = 0 , y = 0 ;
155
+ int fb_width, fb_width_half, fb_height, fb_height_half;
139
156
140
157
std::map<std::string, libfreenect2::Frame*>::iterator iter;
141
158
142
159
for (iter = frames.begin (); iter != frames.end (); ++iter)
143
160
{
144
161
libfreenect2::Frame* frame = iter->second ;
145
162
146
- glViewport (x, y, width, height);
147
- x += width;
148
- if (x >= 1024 )
163
+ // Using the frame buffer size to account for screens where window.size != framebuffer.size, e.g. retina displays
164
+ glfwGetFramebufferSize (window, &fb_width, &fb_height);
165
+ fb_width_half = (fb_width + 1 ) / 2 ;
166
+ fb_height_half = (fb_height + 1 ) / 2 ;
167
+
168
+ glViewport (x, y, fb_width_half, fb_height_half);
169
+ x += fb_width_half;
170
+ if (x >= (fb_width - 1 ))
149
171
{
150
172
x = 0 ;
151
- y += height ;
173
+ y += fb_height_half ;
152
174
}
153
175
154
- Vertex bl = { -1 .0f , -1 .0f , 0 .0f , 0 .0f }, br = { 1 .0f , -1 .0f , static_cast <float >(frame->width ), 0 .0f }, tl = { -1 .0f , 1 .0f , 0 .0f , static_cast <float >(frame->height ) }, tr = { 1 .0f , 1 .0f , static_cast <float >(frame->width ), static_cast <float >(frame->height ) };
176
+ float w = static_cast <float >(frame->width );
177
+ float h = static_cast <float >(frame->height );
178
+
179
+ Vertex bl = { -1 .0f , -1 .0f , 0 .0f , 0 .0f };
180
+ Vertex br = { 1 .0f , -1 .0f , w, 0 .0f };
181
+ Vertex tl = { -1 .0f , 1 .0f , 0 .0f , h };
182
+ Vertex tr = { 1 .0f , 1 .0f , w, h };
155
183
Vertex vertices[] = {
156
- bl, tl, tr, tr, br, bl
184
+ bl, tl, tr,
185
+ tr, br, bl
157
186
};
158
187
159
188
gl ()->glGenBuffers (1 , &triangle_vbo);
0 commit comments