Skip to content

Commit af1480b

Browse files
committed
Fixed logic to render the 4 frames in the full window. Also handle window resizing.
1 parent 40ba27c commit af1480b

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

examples/viewer.cpp

Lines changed: 31 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(1280),
7+
win_height(800)
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, win_height, "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,22 @@ 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; win_height = h;
126+
}
127+
113128
void Viewer::key_callbackstatic(GLFWwindow* window, int key, int scancode, int action, int mods)
114129
{
115130
Viewer* viewer = reinterpret_cast<Viewer*>(glfwGetWindowUserPointer(window));
@@ -135,25 +150,32 @@ bool Viewer::render()
135150
// wipe the drawing surface clear
136151
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
137152

138-
GLint x = 0, y = 0, width = 600, height = 400;
153+
GLint x = 0, y = 0;
139154

140155
std::map<std::string, libfreenect2::Frame*>::iterator iter;
141156

142157
for (iter = frames.begin(); iter != frames.end(); ++iter)
143158
{
144159
libfreenect2::Frame* frame = iter->second;
145160

146-
glViewport(x, y, width, height);
147-
x += width;
148-
if (x >= 1024)
161+
glViewport(x, y, win_width, win_height);
162+
x += win_width;
163+
if (x >= (win_width * 2))
149164
{
150165
x = 0;
151-
y += height;
166+
y += win_height;
152167
}
153168

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) };
169+
float w = static_cast<float>(frame->width);
170+
float h = static_cast<float>(frame->height);
171+
172+
Vertex bl = { -1.0f, -1.0f, 0.0f, 0.0f };
173+
Vertex br = { 1.0f, -1.0f, w, 0.0f };
174+
Vertex tl = { -1.0f, 1.0f, 0.0f, h };
175+
Vertex tr = { 1.0f, 1.0f, w, h };
155176
Vertex vertices[] = {
156-
bl, tl, tr, tr, br, bl
177+
bl, tl, tr,
178+
tr, br, bl
157179
};
158180

159181
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)