-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[render] HelloTriangle example with ImGui
- Loading branch information
1 parent
b7b22e5
commit ab407f3
Showing
24 changed files
with
801 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#***************************************************************************** | ||
#* VCLib * | ||
#* Visual Computing Library * | ||
#* * | ||
#* Copyright(C) 2021-2025 * | ||
#* Visual Computing Lab * | ||
#* ISTI - Italian National Research Council * | ||
#* * | ||
#* All rights reserved. * | ||
#* * | ||
#* This program is free software; you can redistribute it and/or modify * | ||
#* it under the terms of the Mozilla Public License Version 2.0 as published * | ||
#* by the Mozilla Foundation; either version 2 of the License, or * | ||
#* (at your option) any later version. * | ||
#* * | ||
#* This program is distributed in the hope that it will be useful, * | ||
#* but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
#* Mozilla Public License Version 2.0 * | ||
#* (https://www.mozilla.org/en-US/MPL/2.0/) for more details. * | ||
#****************************************************************************/ | ||
|
||
cmake_minimum_required(VERSION 3.24) | ||
|
||
if (TARGET vclib-3rd-imgui) | ||
if (VCLIB_RENDER_BACKEND STREQUAL "bgfx") | ||
add_subdirectory(bgfx) | ||
elseif (VCLIB_RENDER_BACKEND STREQUAL "opengl2") | ||
add_subdirectory(opengl2) | ||
endif() | ||
endif() |
30 changes: 30 additions & 0 deletions
30
examples/render/04-hello-triangle-imgui/bgfx/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#***************************************************************************** | ||
#* VCLib * | ||
#* Visual Computing Library * | ||
#* * | ||
#* Copyright(C) 2021-2025 * | ||
#* Visual Computing Lab * | ||
#* ISTI - Italian National Research Council * | ||
#* * | ||
#* All rights reserved. * | ||
#* * | ||
#* This program is free software; you can redistribute it and/or modify * | ||
#* it under the terms of the Mozilla Public License Version 2.0 as published * | ||
#* by the Mozilla Foundation; either version 2 of the License, or * | ||
#* (at your option) any later version. * | ||
#* * | ||
#* This program is distributed in the hope that it will be useful, * | ||
#* but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
#* Mozilla Public License Version 2.0 * | ||
#* (https://www.mozilla.org/en-US/MPL/2.0/) for more details. * | ||
#****************************************************************************/ | ||
|
||
cmake_minimum_required(VERSION 3.24) | ||
|
||
if (TARGET vclib-3rd-glfw) | ||
add_subdirectory(glfw) | ||
endif() | ||
if (TARGET vclib-3rd-qt) | ||
#add_subdirectory(qt) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/***************************************************************************** | ||
* VCLib * | ||
* Visual Computing Library * | ||
* * | ||
* Copyright(C) 2021-2025 * | ||
* Visual Computing Lab * | ||
* ISTI - Italian National Research Council * | ||
* * | ||
* All rights reserved. * | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the Mozilla Public License Version 2.0 as published * | ||
* by the Mozilla Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
* This program is distributed in the hope that it will be useful, * | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
* Mozilla Public License Version 2.0 * | ||
* (https://www.mozilla.org/en-US/MPL/2.0/) for more details. * | ||
****************************************************************************/ | ||
|
||
#ifndef COMMON_H | ||
#define COMMON_H | ||
|
||
#include <bgfx/bgfx.h> | ||
|
||
#include <vclib/bgfx/context/load_program.h> | ||
#include <vclib/space/core/color.h> | ||
|
||
struct Vertex | ||
{ | ||
float pos[2]; | ||
uint32_t abgr; | ||
}; | ||
|
||
static const Vertex vertices[] { | ||
{{-1.0f, -1.0f}, vcl::Color(vcl::Color::Red).abgr() }, | ||
{{1.0f, -1.0f}, vcl::Color(vcl::Color::Green).abgr()}, | ||
{{0.0f, 1.0f}, vcl::Color(vcl::Color::Blue).abgr() }, | ||
}; | ||
|
||
inline void setUpBGFX( | ||
bgfx::ViewId viewId, | ||
bgfx::VertexBufferHandle& vbh, | ||
bgfx::ProgramHandle& program) | ||
{ | ||
vcl::Color backgroundColor = vcl::Color::Black; | ||
|
||
bgfx::setViewClear( | ||
viewId, | ||
BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, | ||
backgroundColor.rgba(), | ||
1.0f, | ||
0); | ||
|
||
bgfx::VertexLayout layout; | ||
|
||
layout.begin() | ||
.add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float) | ||
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true) | ||
.end(); | ||
|
||
vbh = bgfx::createVertexBuffer( | ||
bgfx::makeRef(vertices, sizeof(vertices)), layout); | ||
|
||
program = vcl::loadProgram( | ||
"shaders/vs_vertex_shader", "shaders/fs_fragment_shader"); | ||
|
||
bgfx::touch(viewId); | ||
} | ||
|
||
inline void drawOnView( | ||
bgfx::ViewId viewId, | ||
const bgfx::VertexBufferHandle& vbh, | ||
const bgfx::ProgramHandle& program) | ||
{ | ||
bgfx::setVertexBuffer(0, vbh); | ||
|
||
bgfx::submit(viewId, program); | ||
} | ||
|
||
#endif // COMMON_H |
48 changes: 48 additions & 0 deletions
48
examples/render/04-hello-triangle-imgui/bgfx/glfw/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#***************************************************************************** | ||
#* VCLib * | ||
#* Visual Computing Library * | ||
#* * | ||
#* Copyright(C) 2021-2025 * | ||
#* Visual Computing Lab * | ||
#* ISTI - Italian National Research Council * | ||
#* * | ||
#* All rights reserved. * | ||
#* * | ||
#* This program is free software; you can redistribute it and/or modify * | ||
#* it under the terms of the Mozilla Public License Version 2.0 as published * | ||
#* by the Mozilla Foundation; either version 2 of the License, or * | ||
#* (at your option) any later version. * | ||
#* * | ||
#* This program is distributed in the hope that it will be useful, * | ||
#* but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
#* Mozilla Public License Version 2.0 * | ||
#* (https://www.mozilla.org/en-US/MPL/2.0/) for more details. * | ||
#****************************************************************************/ | ||
|
||
cmake_minimum_required(VERSION 3.24) | ||
|
||
set(EXAMPLE_NAME 04-hello-triangle-imgui-glfw) | ||
project(vclib-render-example-${EXAMPLE_NAME}) | ||
|
||
### Build settings | ||
set(CMAKE_CXX_STANDARD 20) | ||
|
||
set(HEADERS | ||
../common.h | ||
../hello_triangle_drawer.h | ||
../../demo_imgui_drawer.h) | ||
|
||
set(SOURCES | ||
main.cpp) | ||
|
||
set(SHADERS | ||
shaders/vs_vertex_shader.sc | ||
shaders/fs_fragment_shader.sc | ||
shaders/varying.def.sc) | ||
|
||
vclib_add_example(${EXAMPLE_NAME} | ||
VCLIB_MODULE render | ||
SOURCES ${HEADERS} ${SOURCES}) | ||
|
||
target_add_bgfx_shaders(${PROJECT_NAME} ${SHADERS}) |
51 changes: 51 additions & 0 deletions
51
examples/render/04-hello-triangle-imgui/bgfx/glfw/main.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/***************************************************************************** | ||
* VCLib * | ||
* Visual Computing Library * | ||
* * | ||
* Copyright(C) 2021-2025 * | ||
* Visual Computing Lab * | ||
* ISTI - Italian National Research Council * | ||
* * | ||
* All rights reserved. * | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the Mozilla Public License Version 2.0 as published * | ||
* by the Mozilla Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
* This program is distributed in the hope that it will be useful, * | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
* Mozilla Public License Version 2.0 * | ||
* (https://www.mozilla.org/en-US/MPL/2.0/) for more details. * | ||
****************************************************************************/ | ||
|
||
#include "../../demo_imgui_drawer.h" | ||
#include "../hello_triangle_drawer.h" | ||
|
||
#include <vclib/render/canvas.h> | ||
#include <vclib/glfw/window_manager.h> | ||
#include <vclib/render/render_app.h> | ||
|
||
/** | ||
* This Demo is a simple example of how to use the ImGuiDrawer to draw the ImGui | ||
* demo window. | ||
* | ||
* The example is the same as 00-hello-triangle, but with the addition of the | ||
* ImGui demo window. | ||
*/ | ||
int main(int argc, char** argv) | ||
{ | ||
// The window is created with the RenderApp class: | ||
using WindowGLFW = vcl::RenderApp< | ||
vcl::glfw::WindowManager, // The WindowManager: GLFW | ||
vcl::Canvas, // The default Canvas | ||
DemoImGuiDrawer, // A Drawer that draws the ImGui demo window | ||
HelloTriangleDrawer>; // The Drawer that draws the triangle | ||
|
||
WindowGLFW tw("Hello Triangle ImGui with GLFW"); | ||
|
||
tw.show(); | ||
|
||
return 0; | ||
} |
8 changes: 8 additions & 0 deletions
8
examples/render/04-hello-triangle-imgui/bgfx/glfw/shaders/fs_fragment_shader.sc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
$input v_color | ||
|
||
#include <bgfx_shader.sh> | ||
|
||
void main() | ||
{ | ||
gl_FragColor = v_color; | ||
} |
4 changes: 4 additions & 0 deletions
4
examples/render/04-hello-triangle-imgui/bgfx/glfw/shaders/varying.def.sc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
vec2 a_position : POSITION; | ||
vec4 a_color0 : COLOR0; | ||
|
||
vec4 v_color : COLOR0; |
12 changes: 12 additions & 0 deletions
12
examples/render/04-hello-triangle-imgui/bgfx/glfw/shaders/vs_vertex_shader.sc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
$input a_position | ||
$input a_color0 | ||
|
||
$output v_color | ||
|
||
#include <bgfx_shader.sh> | ||
|
||
void main() | ||
{ | ||
gl_Position = vec4(a_position.x, a_position.y, 0.0, 1.0); | ||
v_color = a_color0; | ||
} |
71 changes: 71 additions & 0 deletions
71
examples/render/04-hello-triangle-imgui/bgfx/hello_triangle_drawer.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/***************************************************************************** | ||
* VCLib * | ||
* Visual Computing Library * | ||
* * | ||
* Copyright(C) 2021-2025 * | ||
* Visual Computing Lab * | ||
* ISTI - Italian National Research Council * | ||
* * | ||
* All rights reserved. * | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the Mozilla Public License Version 2.0 as published * | ||
* by the Mozilla Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
* This program is distributed in the hope that it will be useful, * | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
* Mozilla Public License Version 2.0 * | ||
* (https://www.mozilla.org/en-US/MPL/2.0/) for more details. * | ||
****************************************************************************/ | ||
|
||
#ifndef HELLO_TRIANGLE_DRAWER_H | ||
#define HELLO_TRIANGLE_DRAWER_H | ||
|
||
#include "common.h" | ||
|
||
#include <vclib/render/drawers/plain_drawer.h> | ||
|
||
template<typename DerivedDrawer> | ||
class HelloTriangleDrawer : public vcl::PlainDrawer<DerivedDrawer> | ||
{ | ||
public: | ||
HelloTriangleDrawer(vcl::uint width = 1024, vcl::uint height = 768) {} | ||
|
||
~HelloTriangleDrawer() | ||
{ | ||
if (bgfx::isValid(vbh)) | ||
bgfx::destroy(vbh); | ||
if (bgfx::isValid(program)) | ||
bgfx::destroy(program); | ||
} | ||
|
||
void onInit(vcl::uint viewId) override | ||
{ | ||
setUpBGFX(viewId, vbh, program); | ||
} | ||
|
||
void onResize(vcl::uint width, vcl::uint height) override | ||
{ | ||
std::cout << "Resize: " << width << "; " << height | ||
<< ". Nothing to do\n"; | ||
} | ||
|
||
void onDrawContent(vcl::uint viewId) override | ||
{ | ||
drawOnView(viewId, vbh, program); | ||
} | ||
|
||
void onDraw(vcl::uint viewId) override | ||
{ | ||
onDrawContent(viewId); | ||
} | ||
|
||
private: | ||
bgfx::VertexBufferHandle vbh; | ||
|
||
bgfx::ProgramHandle program; | ||
}; | ||
|
||
#endif // HELLO_TRIANGLE_DRAWER_H |
Oops, something went wrong.