Skip to content

Commit 045e12b

Browse files
committed
Merge pull request #486 from RyanGordon/bug/protonect_fullwindow_render
Viewer Scaling Fix
2 parents 4162721 + 188f6bb commit 045e12b

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

examples/viewer.cpp

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#include "viewer.h"
22
#include <cstdlib>
33

4-
Viewer::Viewer() : shader_folder("src/shader/")
4+
5+
Viewer::Viewer() : shader_folder("src/shader/"),
6+
win_width(600),
7+
win_height(400)
58
{
69
// init glfw - if already initialized nothing happens
710
int init = glfwInit();
@@ -32,7 +35,7 @@ void Viewer::initialize()
3235
#endif
3336
//glfwWindowHint(GLFW_VISIBLE, debug ? GL_TRUE : GL_FALSE);
3437

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);
3639
if (window == NULL)
3740
{
3841
std::cerr << "Failed to create opengl window." << std::endl;
@@ -106,10 +109,23 @@ void Viewer::initialize()
106109

107110
glfwSetWindowUserPointer(window, this);
108111
glfwSetKeyCallback(window, Viewer::key_callbackstatic);
112+
glfwSetWindowSizeCallback(window, Viewer::winsize_callbackstatic);
109113

110114
shouldStop = false;
111115
}
112116

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+
113129
void Viewer::key_callbackstatic(GLFWwindow* window, int key, int scancode, int action, int mods)
114130
{
115131
Viewer* viewer = reinterpret_cast<Viewer*>(glfwGetWindowUserPointer(window));
@@ -135,25 +151,38 @@ bool Viewer::render()
135151
// wipe the drawing surface clear
136152
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
137153

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;
139156

140157
std::map<std::string, libfreenect2::Frame*>::iterator iter;
141158

142159
for (iter = frames.begin(); iter != frames.end(); ++iter)
143160
{
144161
libfreenect2::Frame* frame = iter->second;
145162

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))
149171
{
150172
x = 0;
151-
y += height;
173+
y += fb_height_half;
152174
}
153175

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 };
155183
Vertex vertices[] = {
156-
bl, tl, tr, tr, br, bl
184+
bl, tl, tr,
185+
tr, br, bl
157186
};
158187

159188
gl()->glGenBuffers(1, &triangle_vbo);

examples/viewer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,18 @@ class Viewer : WithOpenGLBindings {
271271
std::map<std::string,libfreenect2::Frame*> frames;
272272
Texture<F8C4> rgb;
273273
Texture<F32C1> ir;
274+
int win_height;
275+
int win_width;
274276
public:
275277
Viewer();
276278
void initialize();
277279
virtual void onOpenGLBindingsChanged(OpenGLBindings *b);
278280
bool render();
279281
void addFrame(std::string id,libfreenect2::Frame* frame);
280282
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
283+
void winsize_callback(GLFWwindow* window, int w, int h);
281284
static void key_callbackstatic(GLFWwindow* window, int key, int scancode, int action, int mods);
285+
static void winsize_callbackstatic(GLFWwindow* window, int w, int h);
282286
};
283287

284288
#endif

0 commit comments

Comments
 (0)