Skip to content

Commit 523ca1b

Browse files
committed
First iteration of Nuklear implementation.
* Need to render gui to fbo and blit it.
1 parent a8d9958 commit 523ca1b

File tree

15 files changed

+197
-17
lines changed

15 files changed

+197
-17
lines changed

MP-APS/.vs/MP-APS/v15/.suo

0 Bytes
Binary file not shown.

MP-APS/.vs/MP-APS/v15/Browse.VC.db

1.11 MB
Binary file not shown.

MP-APS/Core/GUISystem.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include "GUISystem.h"
2+
3+
#include <glad/glad.h>
4+
5+
#define NK_INCLUDE_FIXED_TYPES
6+
#define NK_INCLUDE_STANDARD_IO
7+
#define NK_INCLUDE_STANDARD_VARARGS
8+
#define NK_INCLUDE_DEFAULT_ALLOCATOR
9+
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
10+
#define NK_INCLUDE_FONT_BAKING
11+
#define NK_INCLUDE_DEFAULT_FONT
12+
#define NK_IMPLEMENTATION
13+
#define NK_GLFW_GL3_IMPLEMENTATION
14+
#include <nuklear/nuklear.h>
15+
#include <nuklear/nuklear_glfw_gl3.h>
16+
17+
//https://github.com/vurtun/nuklear/blob/master/demo/glfw_opengl3/main.c
18+
19+
/***********************************************************************************/
20+
void GUISystem::Init(GLFWwindow* windowPtr) {
21+
m_nuklearContext = nk_glfw3_init(windowPtr, NK_GLFW3_INSTALL_CALLBACKS);
22+
23+
/* Load Fonts: if none of these are loaded a default font will be used */
24+
/* Load Cursor: if you uncomment cursor loading please hide the cursor */
25+
{
26+
struct nk_font_atlas* atlas;
27+
nk_glfw3_font_stash_begin(&atlas);
28+
nk_glfw3_font_stash_end();
29+
}
30+
31+
int width, height;
32+
glfwGetWindowSize(windowPtr, &width, &height);
33+
34+
m_guiFBO.Init("GUI FBO", width, height);
35+
}
36+
37+
/***********************************************************************************/
38+
void GUISystem::Update() {
39+
}
40+
41+
/***********************************************************************************/
42+
void GUISystem::Render() {
43+
44+
struct nk_colorf bg;
45+
bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
46+
47+
nk_glfw3_new_frame();
48+
49+
if (nk_begin(m_nuklearContext, "Demo", nk_rect(50, 50, 230, 250),
50+
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
51+
NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
52+
{
53+
enum {EASY, HARD};
54+
static int op = EASY;
55+
static int property = 20;
56+
nk_layout_row_static(m_nuklearContext, 30, 80, 1);
57+
if (nk_button_label(m_nuklearContext, "button")) {
58+
fprintf(stdout, "button pressed\n");
59+
}
60+
61+
nk_layout_row_dynamic(m_nuklearContext, 30, 2);
62+
if (nk_option_label(m_nuklearContext, "easy", op == EASY)) op = EASY;
63+
if (nk_option_label(m_nuklearContext, "hard", op == HARD)) op = HARD;
64+
65+
nk_layout_row_dynamic(m_nuklearContext, 25, 1);
66+
nk_property_int(m_nuklearContext, "Compression:", 0, &property, 100, 10, 1);
67+
68+
nk_layout_row_dynamic(m_nuklearContext, 20, 1);
69+
nk_label(m_nuklearContext, "background:", NK_TEXT_LEFT);
70+
nk_layout_row_dynamic(m_nuklearContext, 25, 1);
71+
if (nk_combo_begin_color(m_nuklearContext, nk_rgb_cf(bg), nk_vec2(nk_widget_width(m_nuklearContext),400))) {
72+
nk_layout_row_dynamic(m_nuklearContext, 120, 1);
73+
bg = nk_color_picker(m_nuklearContext, bg, NK_RGBA);
74+
nk_layout_row_dynamic(m_nuklearContext, 25, 1);
75+
bg.r = nk_propertyf(m_nuklearContext, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f);
76+
bg.g = nk_propertyf(m_nuklearContext, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f);
77+
bg.b = nk_propertyf(m_nuklearContext, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f);
78+
bg.a = nk_propertyf(m_nuklearContext, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f);
79+
nk_combo_end(m_nuklearContext);
80+
}
81+
}
82+
83+
nk_end(m_nuklearContext);
84+
85+
86+
// Todo: render to different framebuffer and blit onto default
87+
//glClear(GL_COLOR_BUFFER_BIT);
88+
nk_glfw3_render(NK_ANTI_ALIASING_ON, m_maxVertexBuffer, m_maxElementBuffer);
89+
}
90+
91+
/***********************************************************************************/
92+
void GUISystem::Shutdown() {
93+
m_guiFBO.Delete();
94+
nk_glfw3_shutdown();
95+
}

MP-APS/Core/GUISystem.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
3+
#include "../Graphics/GLFramebuffer.h"
4+
5+
#include <cstddef>
6+
7+
/***********************************************************************************/
8+
// Forward Declarations
9+
struct nk_context;
10+
struct GLFWwindow;
11+
12+
/***********************************************************************************/
13+
class GUISystem {
14+
public:
15+
GUISystem() = default;
16+
GUISystem(const GUISystem&) = delete;
17+
GUISystem& operator=(const GUISystem&) = delete;
18+
19+
void Init(GLFWwindow* windowPtr);
20+
void Update();
21+
void Render();
22+
void Shutdown();
23+
24+
private:
25+
nk_context* m_nuklearContext;
26+
27+
std::size_t m_maxVertexBuffer = 512 * 1024;
28+
std::size_t m_maxElementBuffer = 128 * 1024;
29+
30+
GLFramebuffer m_guiFBO;
31+
};

MP-APS/Core/RenderSystem.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,8 @@ void RenderSystem::Init(const pugi::xml_node& rendererNode) {
6363
glEnable(GL_DEBUG_OUTPUT);
6464
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
6565
#endif
66-
glFrontFace(GL_CCW);
67-
glCullFace(GL_BACK);
68-
glEnable(GL_CULL_FACE);
69-
glEnable(GL_DEPTH_TEST);
70-
glDepthMask(GL_TRUE);
71-
glDepthFunc(GL_LEQUAL);
72-
glEnable(GL_BLEND);
66+
setDefaultState();
7367
glEnable(GL_MULTISAMPLE);
74-
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
7568

7669
// Create uniform buffer object for projection and view matrices
7770
// so same data shared to multiple shader programs.
@@ -105,6 +98,7 @@ void RenderSystem::Shutdown() const {
10598

10699
/***********************************************************************************/
107100
void RenderSystem::Render(const SceneBase& scene, const bool wireframe) {
101+
setDefaultState();
108102
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
109103
glBindBuffer(GL_UNIFORM_BUFFER, m_uboMatrices);
110104

@@ -201,6 +195,18 @@ void RenderSystem::Update(const Camera& camera, const double delta) {
201195
glBufferSubData(GL_UNIFORM_BUFFER, sizeof(glm::mat4), sizeof(glm::mat4), glm::value_ptr(view));
202196
}
203197

198+
/***********************************************************************************/
199+
void RenderSystem::setDefaultState() {
200+
glFrontFace(GL_CCW);
201+
glCullFace(GL_BACK);
202+
glEnable(GL_CULL_FACE);
203+
glEnable(GL_DEPTH_TEST);
204+
glDepthMask(GL_TRUE);
205+
glDepthFunc(GL_LEQUAL);
206+
glEnable(GL_BLEND);
207+
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
208+
}
209+
204210
/***********************************************************************************/
205211
void RenderSystem::renderModelsWithTextures(GLShaderProgram& shader, const std::vector<ModelPtr>& renderList) const {
206212
glBindSampler(m_samplerPBRTextures, 3);

MP-APS/Core/RenderSystem.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class RenderSystem {
3939
private:
4040
// Helper functions
4141

42+
// Sets the default state required for rendering
43+
void setDefaultState();
4244
// Render models contained in the renderlist
4345
void renderModelsWithTextures(GLShaderProgram& shader, const std::vector<ModelPtr>& renderList) const;
4446
// Render models without binding textures (for a depth or shadow pass perhaps)

MP-APS/Core/WindowSystem.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ void WindowSystem::SetVsync(const bool vsync) const {
9595
void WindowSystem::Update() {
9696
glfwPollEvents();
9797

98+
if (Input::GetInstance().IsKeyPressed(GLFW_KEY_TAB)) {
99+
m_showCursor = !m_showCursor;
100+
if (m_showCursor) {
101+
EnableCursor();
102+
} else {
103+
DisableCursor();
104+
}
105+
106+
}
107+
98108
// Check if the window needs to be closed
99109
if (Input::GetInstance().IsKeyPressed(GLFW_KEY_ESCAPE) || glfwWindowShouldClose(m_window)) {
100110
m_shouldWindowClose = true;

MP-APS/Core/WindowSystem.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
namespace pugi {
88
class xml_node;
99
}
10-
10+
class Engine;
1111
struct GLFWwindow;
1212

1313
/***********************************************************************************/
1414
class WindowSystem {
15+
friend class Engine;
1516
public:
1617
WindowSystem() = default;
1718
WindowSystem(const WindowSystem&) = delete;
@@ -29,12 +30,14 @@ class WindowSystem {
2930

3031
void SetVsync(const bool vsync) const;
3132

32-
bool ShouldClose() const noexcept { return m_shouldWindowClose; }
33-
bool IsFocused() const noexcept { return m_focused; }
33+
auto ShouldClose() const noexcept { return m_shouldWindowClose; }
34+
auto IsCursorVisible() const noexcept { return m_showCursor; }
35+
auto IsFocused() const noexcept { return m_focused; }
3436

3537
private:
3638
GLFWwindow* m_window = nullptr;
3739

3840
bool m_shouldWindowClose;
3941
bool m_focused = true;
42+
bool m_showCursor = false;
4043
};

0 commit comments

Comments
 (0)