tgui::CanvasOpenGl3 widget

Started by naylinntu, 12 October 2024, 21:42:45

naylinntu

I am using TGUI compiled with GLFW, it works well for common widgets like buttons and message boxes.

How can I render an OpenGL scene on tgui::CanvasOpenGl3 widget?
My code looks like below::

`#include <gl/glew.h>

auto canvas = tgui::CanvasOpenGL3::create({200,200});
canvas->setPosition(300, 200);
gui.add(canvas); canvas->bindFramebuffer();
'

`auto canvas_size_button = tgui::Button::create("Get canvas size");
gui.add(canvas_size_button);
// few other widgets added here

while (!glfwWindowShouldClose(window)){
glUseProgram(shaderProgram);
gui.draw();
glDrawArrays(GL_LINES, 0, 2);
}`


I am using GLEW with this.
but it does not work. It only show other widgets placed into `gui` and they work well.

Appreciate if an example show how to render a simple triangle on Canvas along side with few simple widgets like buttons. or point to the correct way.
Thanks

texus

The minimal information on how to use the canvas is located at https://tgui.eu/tutorials/latest-stable/canvas/#canvasopengl3

After you created the canvas and added it to the gui, you would have the following code to render to the canvas. The contents of the canvas is then later drawn to the window when you call gui.draw().
Code (cpp) Select
canvas->bindFramebuffer();  // Calls glBindFramebuffer with the id of the canvas framebuffer
glClear(GL_COLOR_BUFFER_BIT);
// OpenGL calls placed here will draw on the canvas
glBindFramebuffer(GL_FRAMEBUFFER, 0);

The purpose of the CanvasOpenGl3 widget is to render custom OpenGL stuff in the middle of TGUI widgets. It thus makes no sense to render TGUI widgets on the canvas.