-
Notifications
You must be signed in to change notification settings - Fork 0
/
glviewer.cpp
92 lines (75 loc) · 2.82 KB
/
glviewer.cpp
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
#include <iostream>
#include <algorithm>
#include "glviewer.h"
using namespace std;
using namespace MaxLib;
namespace GLViewer {
Viewer::Viewer()
: camera(Window::GetWidth(), Window::GetHeight(), glm::vec3(0.0f, 0.0f, 0.0f), 80.0f), buffers(this)
{
// Setup events
Event<Event_WindowResize>::RegisterPersistantHandler([&](Event_WindowResize& data) {
camera.SetViewport(0, 0, data.Width, data.Height);
});
Event<Event_MouseScroll>::RegisterPersistantHandler([&](Event_MouseScroll& data) {
if(!ActiveItem::IsViewportHovered(camera))
return;
camera.ProcessMouseScroll(data.OffsetY);
});
Event<Event_MouseMove>::RegisterPersistantHandler([&](Event_MouseMove& data) {
// we can just use the data already sent to mouse
(void)data;
// ignore if a window is hovered over
if(ImGui::GetIO().WantCaptureMouse)
return;
// rotate on mouse click
if(Mouse::IsLeftClicked() && ActiveItem::IsViewport(camera)) {
camera.ChangeDirection(Mouse::GetPositionDif());
}
// pan on mouse middle click
else if(Mouse::IsMiddleClicked() && ActiveItem::IsViewport(camera)) {
camera.Move(camera.GetWorldVector(Mouse::GetPositionDif()));
}
});
// blending (allows translucence)
GLCall(glEnable(GL_BLEND));
// what to do with source (overlapping item) / what to do with destination (item we are overlapping)
GLCall(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
// dont draw triangles facing the wrong way
glEnable(GL_CULL_FACE);
// dont draw vertices outside of our visible depth
glEnable(GL_DEPTH_TEST);
}
void Viewer::Render()
{
m_Proj = camera.GetProjectionMatrix();
m_View = camera.GetViewMatrix();
// Clear old render data
Renderer::Clear();
// Render each of the buffers
buffers.RenderBuffers(m_Proj, m_View);
}
void Viewer::RenderLoop(GLSystem& glsys, std::function<void()> cb_Configure, std::function<void()> cb_Draw_ImGui, std::function<void()> cb_Set_DynamicBuffers, std::function<void()> cb_Finalise)
{
// Loop until the user closes the window
while (!glfwWindowShouldClose(glsys.GetWindow()))
{
// Poll for and process glfw events
GLCall(glfwPollEvents());
// Configure using custon callback
cb_Configure();
// Draw ImGui
glsys.imgui_NewFrame();
cb_Draw_ImGui();
// Render viewer buffers
cb_Set_DynamicBuffers();
Render();
// Render ImGui
glsys.imgui_Render();
// Final functions using custon callback
if(cb_Finalise) { cb_Finalise(); }
// Swap front and back buffers
GLCall(glfwSwapBuffers(glsys.GetWindow()));
}
}
} // End namespace GLViewer