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/types.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());
132 mDefaultClearColor.
redF(),
133 mDefaultClearColor.
greenF(),
134 mDefaultClearColor.
blueF(),
135 mDefaultClearColor.
alphaF());
146 mSize = {width, height};
159 if (mReadBufferCallback) {
160 DerivedRenderApp::CNV::drawContent(derived());
164 DerivedRenderApp::CNV::draw(derived());
165 DerivedRenderApp::CNV::postDraw(derived());
178 CallbackReadBuffer
callback =
nullptr)
180 if (point.
x() < 0 || point.
y() < 0 ||
181 point.
x() >= mSize.x() || point.
y() >= mSize.y()) {
185 mReadDepthPoint = point;
207 std::vector<std::uint8_t> buffer(mSize.x() * mSize.y() * 4);
222 saveImageData(
filename, mSize.x(), mSize.y(), buffer.data());
224 catch (
const std::exception& e) {
237 glGetFloatv(GL_DEPTH_RANGE, depthRange.data());
241 glGetIntegerv(GL_VIEWPORT, viewport);
244 GLfloat depth = depthRange[1];
246 GLint(mReadDepthPoint.
x()),
247 GLint(viewport[3] - mReadDepthPoint.
y() - 1),
255 depth = (depth - depthRange[0]) / (depthRange[1] - depthRange[0]);
257 ReadData rd = FloatData({depth});
260 mReadBufferCallback(rd);
263 mReadDepthPoint = {-1, -1};
264 mReadBufferCallback =
nullptr;
267 auto* derived() {
return static_cast<DerivedRenderApp*
>(
this); }
269 const auto* derived()
const
271 return static_cast<const DerivedRenderApp*
>(
this);
The Canvas class describes a canvas on which opengl2 can draw.
Definition canvas.h:74
bool onScreenshot(const std::string &filename, uint width=0, uint height=0)
Automatically called by the DerivedRenderApp when a drawer asks for a screenshot.
Definition canvas.h:199
void onInit()
Automatically called by the DerivedRenderApp when the window initializes. Initialization is requires ...
Definition canvas.h:128
void onResize(uint width, uint height)
Automatically called by the DerivedRenderApp when the window is resized.
Definition canvas.h:144
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:176
void onPaint()
Automatically called by the DerivedRenderApp when the window asks to repaint.
Definition canvas.h:154
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:207
float blueF() const
Returns the float blue component of this color [0-1].
Definition color.h:213
float alphaF() const
Returns the float alpha component of this color [0-1].
Definition color.h:219
float redF() const
Returns the float red component of this color [0-1].
Definition color.h:201
The Point class represents an N-dimensional point containing N scalar values.
Definition point.h:58
ScalarType & x()
Returns a reference to the x-component of the Point object.
Definition point.h:131
ScalarType & y()
Returns a reference to the y-component of the Point object.
Definition point.h:153
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
Definition render_app.h:31
Point2< int > Point2i
A convenience alias for a 2-dimensional Point with integer components.
Definition point.h:731