aboutsummaryrefslogtreecommitdiffstats
path: root/dev/MinGfx/src/graphics_app.cc
blob: 57405f52c06775c4b04a1ce49d36c7beae00d773 (plain) (blame)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
 Copyright (c) 2017,2018 Regents of the University of Minnesota.
 All Rights Reserved.
 See corresponding header file for details.
 */

#include "graphics_app.h"


namespace mingfx {



GraphicsApp::GraphicsApp(int width, int height, const std::string &caption) :
    graphicsInitialized_(false), width_(width), height_(height), caption_(caption), lastDrawT_(0.0),
    leftDown_(false), middleDown_(false), rightDown_(false), screen_(NULL), window_(NULL)
{
}

GraphicsApp::~GraphicsApp() {
}

void GraphicsApp::InitGraphicsContext() {
    
    glfwInit();
    
    glfwSetTime(0);
    
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    
    glfwWindowHint(GLFW_SAMPLES, 0);
    glfwWindowHint(GLFW_RED_BITS, 8);
    glfwWindowHint(GLFW_GREEN_BITS, 8);
    glfwWindowHint(GLFW_BLUE_BITS, 8);
    glfwWindowHint(GLFW_ALPHA_BITS, 8);
    glfwWindowHint(GLFW_STENCIL_BITS, 8);
    glfwWindowHint(GLFW_DEPTH_BITS, 24);
    glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
    
    
    // on OSX, glfwCreateWindow bombs if we pass caption_.c_str() in for the 3rd
    // parameter perhaps because it's const.  so, copying to a tmp char array here.
    char *title = new char[caption_.size() + 1];
    for (int i = 0; i < caption_.size(); i++) {
        title[i] = caption_[i];
    }
    title[caption_.size()] = '\0';
    std::cout << caption_ << std::endl;
    
    // Create a GLFWwindow object
    window_ = glfwCreateWindow(width_, height_, title, NULL, NULL);
    if (window_ == nullptr) {
        std::cerr << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        exit(-1);
    }
    glfwMakeContextCurrent(window_);
    glfwSetWindowUserPointer(window_, this);

    // cleanup tmp title hack
    delete [] title;
    
    
#if defined(NANOGUI_GLAD)
    if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress))
       throw std::runtime_error("Could not initialize GLAD!");
    glGetError(); // pull and ignore unhandled errors like GL_INVALID_ENUM
#endif
    
    glClearColor(0.2f, 0.25f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    
    // Create a nanogui screen and pass the glfw pointer to initialize
    screen_ = new nanogui::Screen();
    screen_->initialize(window_, true);
    
    glfwGetFramebufferSize(window_, &width_, &height_);
    glViewport(0, 0, width_, height_);
    glfwSwapInterval(0);
    glfwSwapBuffers(window_);
    
    screen_->setVisible(true);
    screen_->performLayout();

    glfwSetCursorPosCallback(window_,
        [](GLFWwindow *window, double x, double y) {
            GraphicsApp *app = (GraphicsApp*)glfwGetWindowUserPointer(window);
             app->cursor_pos_glfw_cb(x, y);
         }
    );

    glfwSetMouseButtonCallback(window_,
        [](GLFWwindow *window, int button, int action, int modifiers) {
            GraphicsApp *app = (GraphicsApp*)glfwGetWindowUserPointer(window);
            app->mouse_button_glfw_cb(button, action, modifiers);
        }
    );

    glfwSetKeyCallback(window_,
        [](GLFWwindow *window, int key, int scancode, int action, int mods) {
            GraphicsApp *app = (GraphicsApp*)glfwGetWindowUserPointer(window);
             app->key_glfw_cb(key, scancode, action, mods);
        }
    );

    glfwSetCharCallback(window_,
        [](GLFWwindow *window, unsigned int codepoint) {
            GraphicsApp *app = (GraphicsApp*)glfwGetWindowUserPointer(window);
            app->char_glfw_cb(codepoint);
        }
    );

    glfwSetDropCallback(window_,
        [](GLFWwindow *window, int count, const char **filenames) {
            GraphicsApp *app = (GraphicsApp*)glfwGetWindowUserPointer(window);
            app->drop_glfw_cb(count, filenames);
        }
    );

    glfwSetScrollCallback(window_,
        [](GLFWwindow *window, double x, double y) {
            GraphicsApp *app = (GraphicsApp*)glfwGetWindowUserPointer(window);
            app->scroll_glfw_cb(x, y);
        }
    );

    glfwSetFramebufferSizeCallback(window_,
        [](GLFWwindow *window, int width, int height) {
            GraphicsApp *app = (GraphicsApp*)glfwGetWindowUserPointer(window);
            app->resize_glfw_cb(width, height);
        }
    );

    graphicsInitialized_ = true;
 }


    
void GraphicsApp::Run() {

    if (!graphicsInitialized_) {
        InitGraphicsContext();        
    }

    InitNanoGUI();

    InitOpenGL();
    
    // Main program loop
    glfwSetTime(0.0);
    while (!glfwWindowShouldClose(window_)) {

        // Poll for new user input events and call callbacks
        glfwPollEvents();

        // Update the simulation, i.e., perform all non-graphics updates that
        // should happen each frame.
        double now = glfwGetTime();
        UpdateSimulation(now-lastDrawT_);
        lastDrawT_ = now;
        
        // Clear is handled in this mainloop so that drawing works even for
        // users who do not want to fill in DrawUsingOpenGL()
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

        // NanoGUI sets these to something other than the OpenGL defaults, which
        // screws up most OpenGL programs, so we need to reset them each frame here.
        glEnable(GL_CULL_FACE);
        glCullFace(GL_BACK);
        glEnable(GL_DEPTH_TEST);
        
        // Users may fill this in to do raw OpenGL rendering
        DrawUsingOpenGL();

        // This renders the nanogui widgets created on screen_
        screen_->drawContents();
        screen_->drawWidgets();
        
        // Users may fill this in to do additional 2D rendering with the NanoVG library
        DrawUsingNanoVG(screen_->nvgContext());
        
        glfwSwapBuffers(window_);
    }
    
    glfwTerminate();
}
    


bool GraphicsApp::cursor_pos_glfw_cb(double x, double y) {
    
    if (screen_->cursorPosCallbackEvent(x,y)) {
        // event was handled by nanogui
        lastMouse_ = Point2((float)x, (float)y);
        return true;
    }
    else {
        Point2 cur((float)x, (float)y);
        Vector2 delta = cur - lastMouse_;

        // if no buttons are down, generate a mouse move event
        if (!leftDown_ && !middleDown_ && !rightDown_) {
            mouse_move(cur, delta);
        }
        
        // if a button is down, generate a corresponding mouse drag event
        if (leftDown_) {
            left_mouse_drag(cur, delta);
        }
        if (middleDown_) {
            middle_mouse_drag(cur, delta);
        }
        if (rightDown_) {
            right_mouse_drag(cur, delta);
        }
        
        lastMouse_ = cur;
        return false;
    }
}

bool GraphicsApp::mouse_button_glfw_cb(int button, int action, int modifiers) {
    if (screen_->mouseButtonCallbackEvent(button, action, modifiers)) {
        return true;
    }
    else if (button == GLFW_MOUSE_BUTTON_LEFT) {
        double x,y;
        glfwGetCursorPos(window_, &x, &y);
        if (action == 1) {
            left_mouse_down(Point2((float)x, (float)y));
            leftDown_ = true;
        }
        else {
            left_mouse_up(Point2((float)x, (float)y));
            leftDown_ = false;
        }
        return true;
    }
    else if (button == GLFW_MOUSE_BUTTON_MIDDLE) {
        double x,y;
        glfwGetCursorPos(window_, &x, &y);
        if (action == 1) {
            middle_mouse_down(Point2((float)x, (float)y));
            middleDown_ = true;
        }
        else {
            middle_mouse_up(Point2((float)x, (float)y));
            middleDown_ = false;
        }
        return true;
    }
    else if (button == GLFW_MOUSE_BUTTON_RIGHT) {
        double x,y;
        glfwGetCursorPos(window_, &x, &y);
        if (action == 1) {
            right_mouse_down(Point2((float)x, (float)y));
            rightDown_ = true;
        }
        else {
            right_mouse_up(Point2((float)x, (float)y));
            rightDown_ = false;
        }
        return true;
    }
    return false;
}


bool GraphicsApp::key_glfw_cb(int key, int scancode, int action, int modifiers) {
    if (screen_->keyCallbackEvent(key, scancode, action, modifiers)) {
        return true;
    }
    else {
    if (glfwGetKeyName(key, scancode) != NULL) {
        if (action == GLFW_PRESS) {
            key_down(glfwGetKeyName(key, scancode), modifiers);
        }
        else if (action == GLFW_REPEAT) {
            key_repeat(glfwGetKeyName(key, scancode), modifiers);
        }
        else {
            key_up(glfwGetKeyName(key, scancode), modifiers);
        }
        return true;
    }
    else {
        if (action == GLFW_PRESS) {
            special_key_down(key, scancode, modifiers);
        }
        else if (action == GLFW_REPEAT) {
            special_key_repeat(key, scancode, modifiers);
        }
        else {
            special_key_up(key, scancode, modifiers);
        }
        return true;
    }
    }

    return false;
}


bool GraphicsApp::char_glfw_cb(unsigned int codepoint) {
    if (screen_->charCallbackEvent(codepoint)) {
        return true;
    }
    else {
        // TODO: could add another virtual function to GraphicsApp to
        // respond to this if needed
    }
    return false;
}


bool GraphicsApp::drop_glfw_cb(int count, const char **filenames) {
    if (screen_->dropCallbackEvent(count, filenames)) {
        return true;
    }
    else {
        // TODO: could add another virtual function to GraphicsApp to
        // respond to this if needed
    }
    return false;
}


bool GraphicsApp::scroll_glfw_cb(double x, double y) {
    if (screen_->scrollCallbackEvent(x,y)) {
        return true;
    }
    else {
        // TODO: could add another virtual function to GraphicsApp to
        // respond to this if needed
    }
    return false;
}


bool GraphicsApp::resize_glfw_cb(int width, int height) {
    if (screen_->resizeCallbackEvent(width, height)) {
        return true;
    }
    else {
        // the width and height reported here are the new framebuffer size
        // we will query/save/report the new window size instead
        width_ = window_width();
        height_ = window_height();
        OnWindowResize(width_, height_);
    }
    return false;
}


bool GraphicsApp::IsKeyDown(int key) {
    return (glfwGetKey(window_, key) == GLFW_PRESS);
}

bool GraphicsApp::IsLeftMouseDown() {
    return (glfwGetMouseButton(window_, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS);
}


bool GraphicsApp::IsMiddleMouseDown() {
    return (glfwGetMouseButton(window_, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS);
}


bool GraphicsApp::IsRightMouseDown() {
    return (glfwGetMouseButton(window_, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS);
}

    

float GraphicsApp::aspect_ratio() {
    int width, height;
    glfwGetFramebufferSize(window_, &width, &height);
    return (float)width/(float)height;
}
    
int GraphicsApp::window_width() {
    int width, height;
    glfwGetWindowSize(window_, &width, &height);
    return width;
}

int GraphicsApp::framebuffer_width() {
    int width, height;
    glfwGetFramebufferSize(window_, &width, &height);
    return width;
}

int GraphicsApp::window_height() {
    int width, height;
    glfwGetWindowSize(window_, &width, &height);
    return height;
}

int GraphicsApp::framebuffer_height() {
    int width, height;
    glfwGetFramebufferSize(window_, &width, &height);
    return height;
}
    
Point2 GraphicsApp::PixelsToNormalizedDeviceCoords(const Point2 &pointInPixels) {
    float x = (pointInPixels[0] / window_width()) * 2.0f - 1.0f;
    float y = (1.0f - (pointInPixels[1] / window_height())) * 2.0f - 1.0f;
    return Point2(x,y);
}

Point2 GraphicsApp::NormalizedDeviceCoordsToPixels(const Point2 &pointInNDC) {
    float x = 0.5f * (pointInNDC[0] + 1.0f) * window_width();
    float y = (1.0f - (0.5f * (pointInNDC[1] + 1.0f))) * window_height();
    return Point2(x,y);
}

Vector2 GraphicsApp::PixelsToNormalizedDeviceCoords(const Vector2 &vectorInPixels) {
    float x = (2.0f/window_width()) * vectorInPixels[0];
    float y = (-2.0f/window_height()) * vectorInPixels[1];
    return Vector2(x,y);
}

Vector2 GraphicsApp::NormalizedDeviceCoordsToPixels(const Vector2 &vectorInNDC) {
    float x = (window_width()/2.0f) * vectorInNDC[0];
    float y = (-window_height()/2.0f) * vectorInNDC[1];
    return Vector2(x,y);
}

float GraphicsApp::ReadZValueAtPixel(const Point2 &pointInPixels, unsigned int whichBuffer) {
    // scale screen points to framebuffer size, since they are not the same on retina displays
    float x01 = pointInPixels[0] / window_width();
    float y01 = pointInPixels[1] / window_height();
    y01 = 1.0f - y01;
    
    float x = x01 * (float)framebuffer_width();
    float y = y01 * (float)framebuffer_height();

    float z;
    glReadPixels((int)x, (int)y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);
    return z;
}
    
    
nanogui::Screen* GraphicsApp::screen() {
    return screen_;
}

GLFWwindow* GraphicsApp::window() {
    return window_;
}
    
    
void GraphicsApp::ResizeWindow(int new_width, int new_height) {
    glfwSetWindowSize(window_, new_width, new_height);
    width_ = new_width;
    height_ = new_height;
    OnWindowResize(new_width, new_height);
}

    
    


} // end namespace