23#ifndef VCL_OPENGL2_CANVAS_H
24#define VCL_OPENGL2_CANVAS_H
26#include <vclib/io/image.h>
27#include <vclib/render/concepts/render_app.h>
28#include <vclib/render/read_buffer_types.h>
29#include <vclib/space/core/color.h>
30#include <vclib/space/core/point.h>
31#include <vclib/base.h>
72template<
typename DerivedRenderApp>
76 using FloatData = ReadBufferTypes::FloatData;
77 using ByteData = ReadBufferTypes::ByteData;
78 using ReadData = ReadBufferTypes::ReadData;
81 using CallbackReadBuffer = ReadBufferTypes::CallbackReadBuffer;
84 void* mWinId =
nullptr;
88 vcl::Color mDefaultClearColor = vcl::Color::Black;
90 CallbackReadBuffer mReadBufferCallback =
nullptr;
98 void* displayId =
nullptr) : mWinId(winId), mSize(width, height)
102 "The DerivedRenderApp must satisfy the RenderAppConcept.");
109 uint viewId()
const {
return 0; }
111 void setDefaultClearColor(
const Color& color)
113 mDefaultClearColor = color;
115 mDefaultClearColor.
redF(),
116 mDefaultClearColor.
greenF(),
117 mDefaultClearColor.
blueF(),
118 mDefaultClearColor.
alphaF());
144 mDefaultClearColor.
redF(),
145 mDefaultClearColor.
greenF(),
146 mDefaultClearColor.
blueF(),
147 mDefaultClearColor.
alphaF());
158 mSize = {width, height};
171 if (mReadBufferCallback) {
172 DerivedRenderApp::CNV::drawContent(derived());
176 DerivedRenderApp::CNV::draw(derived());
177 DerivedRenderApp::CNV::postDraw(derived());
190 CallbackReadBuffer
callback =
nullptr)
192 if (point.
x() < 0 || point.
y() < 0 ||
193 point.
x() >= mSize.x() || point.
y() >= mSize.y()) {
197 mReadDepthPoint = point;
215 std::vector<std::uint8_t> buffer(mSize.x() * mSize.y() * 4);
230 saveImageData(
filename, mSize.x(), mSize.y(), buffer.data());
232 catch (
const std::exception& e) {
252 CallbackReadBuffer
callback =
nullptr)
262 glGetFloatv(GL_DEPTH_RANGE, depthRange.data());
266 glGetIntegerv(GL_VIEWPORT, viewport);
269 GLfloat depth = depthRange[1];
271 GLint(mReadDepthPoint.
x()),
272 GLint(viewport[3] - mReadDepthPoint.
y() - 1),
280 depth = (depth - depthRange[0]) / (depthRange[1] - depthRange[0]);
282 ReadData rd = FloatData({depth});
285 mReadBufferCallback(rd);
288 mReadDepthPoint = {-1, -1};
289 mReadBufferCallback =
nullptr;
292 auto* derived() {
return static_cast<DerivedRenderApp*
>(
this); }
294 const auto* derived()
const
296 return static_cast<const DerivedRenderApp*
>(
this);
A class representing a box in N-dimensional space.
Definition box.h:46
The Canvas class describes a canvas on which opengl2 can draw.
Definition canvas.h:74
void onInit()
Automatically called by the DerivedRenderApp when the window initializes. Initialization is requires ...
Definition canvas.h:140
bool onReadId(const Point2i &point, CallbackReadBuffer callback=nullptr)
Automatically called by the DerivedRenderApp when a drawer asks to read the ID at a specific point.
Definition canvas.h:250
void onResize(uint width, uint height)
Automatically called by the DerivedRenderApp when the window is resized.
Definition canvas.h:156
bool screenshot(const std::string &filename, uint multiplier=1)
Request a screenshot of the canvas. The screenshot will be saved asynchronously.
Definition canvas.h:128
bool onScreenshot(const std::string &filename, uint multiplier=1)
Automatically called by the DerivedRenderApp when a drawer asks for a screenshot.
Definition canvas.h:211
bool onReadDepth(const Point2i &point, CallbackReadBuffer callback=nullptr)
Automatically called by the DerivedRenderApp when a drawer asks to read the depth buffer at a specifi...
Definition canvas.h:188
void onPaint()
Automatically called by the DerivedRenderApp when the window asks to repaint.
Definition canvas.h:166
The Color class represents a 32 bit color.
Definition color.h:48
float greenF() const
Returns the float green component of this color [0-1].
Definition color.h:210
float blueF() const
Returns the float blue component of this color [0-1].
Definition color.h:216
float alphaF() const
Returns the float alpha component of this color [0-1].
Definition color.h:222
float redF() const
Returns the float red component of this color [0-1].
Definition color.h:204
The Point class represents an N-dimensional point containing N scalar values.
Definition point.h:55
ScalarType & x()
Returns a reference to the x-component of the Point object.
Definition point.h:128
ScalarType & y()
Returns a reference to the y-component of the Point object.
Definition point.h:150
Definition render_app.h:31
Point2< int > Point2i
A convenience alias for a 2-dimensional Point with integer components.
Definition point.h:706