Skip to content

Commit

Permalink
[render] HelloTriangle example with ImGui
Browse files Browse the repository at this point in the history
  • Loading branch information
alemuntoni committed Jan 10, 2025
1 parent b7b22e5 commit ab407f3
Show file tree
Hide file tree
Showing 24 changed files with 801 additions and 3 deletions.
6 changes: 3 additions & 3 deletions examples/render/00-hello-triangle/bgfx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

cmake_minimum_required(VERSION 3.24)

if (TARGET vclib-3rd-qt)
add_subdirectory(qt)
endif()
if (TARGET vclib-3rd-glfw)
add_subdirectory(glfw)
endif()
if (TARGET vclib-3rd-qt)
add_subdirectory(qt)
endif()
31 changes: 31 additions & 0 deletions examples/render/04-hello-triangle-imgui/CMakeLists.txt
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 examples/render/04-hello-triangle-imgui/bgfx/CMakeLists.txt
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()
83 changes: 83 additions & 0 deletions examples/render/04-hello-triangle-imgui/bgfx/common.h
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 examples/render/04-hello-triangle-imgui/bgfx/glfw/CMakeLists.txt
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 examples/render/04-hello-triangle-imgui/bgfx/glfw/main.cpp
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;
}
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;
}
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;
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;
}
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
Loading

0 comments on commit ab407f3

Please sign in to comment.