71 float m_zOffset = 0.0f;
77 bool iMatIdent =
true;
78 bool iMatDirty =
false;
80 struct Private{
explicit Private() =
default; };
84 : m_st(st), m_pmvMatUni(pmvMatU), m_staticColor(
color),
88 m_array->addGLSLSubArray(
"gca_Vertex", 3, GL_ARRAY_BUFFER);
89 m_array->addGLSLSubArray(
"gca_Normal", 3, GL_ARRAY_BUFFER);
94 return std::make_shared<Shape>(Private(), st, pmvMatU,
color);
98 constexpr Vec3f&
position() noexcept { iMatDirty=
true;
return m_position; }
100 constexpr const float&
zOffset() const noexcept {
return m_zOffset; }
101 constexpr float&
zOffset() noexcept { iMatDirty=
true;
return m_zOffset; }
109 constexpr const Vec3f&
scale() const noexcept {
return m_scale; }
110 constexpr Vec3f&
scale() noexcept { iMatDirty=
true;
return m_scale; }
121 m_array->seal(
gl,
true);
125 m_pmvMatUni.pmv().pushMv();
126 applyMatToMv(m_pmvMatUni.pmv());
127 m_st.send(
gl, m_pmvMatUni);
129 m_staticColor.vec4f() = m_color;
130 m_st.send(
gl, m_staticColor);
131 m_array->enableBuffer(
gl,
true);
133 ::glDrawArrays(s.type, s.first, s.count);
135 m_array->enableBuffer(
gl,
false);
136 m_pmvMatUni.pmv().popMv();
154 void applyMatToMv(
PMVMat4f& pmvMat)
noexcept {
162 void updateMat() noexcept {
163 bool hasPos = !m_position.is_zero();
165 bool hasRotate = !m_rotation.isIdentity();
166 bool hasRotPivot =
false;
167 const Vec3f& ctr = m_oshape.bounds().center();
168 bool sameScaleRotatePivot = hasScale && hasRotate && ( !hasRotPivot || m_rotPivot == ctr );
170 if( sameScaleRotatePivot ) {
172 iMat.setToTranslation(m_position);
174 iMat.translate(
Vec3f(ctr).mul(m_scale));
175 iMat.rotate(m_rotation);
177 iMat.translate(-ctr);
178 }
else if( hasRotate || hasScale ) {
180 iMat.setToTranslation(m_position);
184 iMat.translate(
Vec3f(m_rotPivot).mul(m_scale));
185 iMat.rotate(m_rotation);
186 iMat.translate(
Vec3f(-m_rotPivot).mul(m_scale));
189 iMat.translate(
Vec3f(ctr).mul(m_scale));
190 iMat.rotate(m_rotation);
191 iMat.translate(
Vec3f(-ctr).mul(m_scale));
196 iMat.translate(
Vec3f(ctr).mul(m_scale));
198 iMat.translate(
Vec3f(-ctr).mul(m_scale));
200 }
else if( hasPos ) {
202 iMat.setToTranslation(m_position);
215 constexpr static float zNear= 1.0f;
216 constexpr static float zFar =100.0f;
221 bool m_animating =
true;
222 bool m_oneframe =
false;
227 std::vector<ShapeRef> m_shapes;
233 m_initialized(false),
235 m_light0Pos(
"gcu_Light0Pos", lightPos),
236 m_staticColor(
"gcu_StaticColor",
Vec4f(0, 0, 0, 1))
238 m_st.manage(m_pmvMat);
239 m_st.manage(m_light0Pos);
240 m_st.manage(m_staticColor);
250 void setOneFrame() noexcept { m_animating=
false; m_oneframe=
true; }
258 "demos/glsl/bin",
"SingleLight0");
260 "demos/glsl/bin",
"SingleLight0");
267 std::string custom =
"#define MAX_TEXTURE_UNITS 0\n";
275 if( !sp0->
add(
gl, vp0,
true) || !sp0->
add(
gl, fp0,
true) ) {
281 m_st.attachShaderProgram(
gl, sp0,
true);
284 m_pmvMat.pmv().getP().loadIdentity();
285 m_pmvMat.pmv().getMv().loadIdentity();
287 const float lineWidth = 1/2.5f;
288 const float dz = 0.001f;
291 const float width = 1.5f;
292 const float height = 1.5f;
293 const float x1 = -width/2.0f;
294 const float y1 = -height/2.0f;
295 const float x2 = x1 + width;
296 const float y2 = y1 + height;
299 m_shapes.push_back(frontShape);
313 const float dxy = lineWidth;
314 oshape.
moveTo(x1+dxy, y1+dxy, z);
315 oshape.
lineTo(x1+dxy, y2-dxy, z);
316 oshape.
lineTo(x2-dxy, y2-dxy, z);
317 oshape.
lineTo(x2-dxy, y1+dxy, z);
318 oshape.
lineTo(x1+dxy, y1+dxy, z);
326 m_shapes.push_back(backShape);
334 const float width = 1.5f;
335 const float height = 1.5f;
337 float lwh = lineWidth/2.0f;
339 float twh = width/2.0f;
340 float thh = height/2.0f;
342 float ctrX = 0, ctrY = 0, ctrZ = dz;
344 m_shapes.push_back(frontShape);
347 oshape.
moveTo(ctrX-lwh, ctrY+thh, ctrZ);
348 oshape.
lineTo(ctrX-lwh, ctrY+lwh, ctrZ);
349 oshape.
lineTo(ctrX-twh, ctrY+lwh, ctrZ);
350 oshape.
lineTo(ctrX-twh, ctrY-lwh, ctrZ);
351 oshape.
lineTo(ctrX-lwh, ctrY-lwh, ctrZ);
352 oshape.
lineTo(ctrX-lwh, ctrY-thh, ctrZ);
353 oshape.
lineTo(ctrX+lwh, ctrY-thh, ctrZ);
354 oshape.
lineTo(ctrX+lwh, ctrY-lwh, ctrZ);
355 oshape.
lineTo(ctrX+twh, ctrY-lwh, ctrZ);
356 oshape.
lineTo(ctrX+twh, ctrY+lwh, ctrZ);
357 oshape.
lineTo(ctrX+lwh, ctrY+lwh, ctrZ);
358 oshape.
lineTo(ctrX+lwh, ctrY+thh, ctrZ);
359 oshape.
lineTo(ctrX-lwh, ctrY+thh, ctrZ);
367 m_shapes.push_back(backShape);
374 ::glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
375 ::glEnable(GL_DEPTH_TEST);
376 ::glEnable(GL_CULL_FACE);
378 m_initialized = sp0->
inUse();
379 if( !m_initialized ) {
384 return m_initialized;
390 m_initialized =
false;
398 m_pmvMat.pmv().getP().loadIdentity();
399 const float aspect = 1.0f;
400 const float fovy_deg=45.0f;
401 const float aspect2 = ( (float) m_viewport.width() / (
float) m_viewport.height() ) / aspect;
402 m_pmvMat.pmv().perspectiveP(
jau::adeg_to_rad(fovy_deg), aspect2, zNear, zFar);
403 m_st.useProgram(
gl,
true);
404 m_st.send(
gl, m_pmvMat);
410 if( !m_initialized ) {
414 ::glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
416 m_st.useProgram(
gl,
true);
417 m_pmvMat.pmv().getMv().loadIdentity();
418 m_pmvMat.pmv().translateMv(0, 0, -5);
422 constexpr double angle_per_sec = 30;
423 const float rad = (float) ( (when - m_tlast).to_double() * angle_per_sec );
433 std::cerr <<
"XXX draw " << m_st <<
"\n";
439 std::string
toStringImpl() const noexcept
override {
return "Primitives02"; }
444 class MyKeyListener :
public KeyListener {
450 void keyPressed(KeyEvent& e,
const KeyboardTracker& kt)
override {
452 if( e.
keySym() == VKeyCode::VK_ESCAPE ) {
457 }
else if( e.
keySym() == VKeyCode::VK_PAUSE || e.
keySym() == VKeyCode::VK_P ) {
458 m_parent.animating() = !m_parent.animating();
459 }
else if( e.
keySym() == VKeyCode::VK_PERIOD ) {
460 m_parent.setOneFrame();
461 }
else if( e.
keySym() == VKeyCode::VK_W ) {
466 void keyReleased(KeyEvent& e,
const KeyboardTracker& kt)
override {
470 typedef std::shared_ptr<MyKeyListener> MyKeyListenerRef;
471 MyKeyListenerRef m_kl;
476 m_kl(
std::make_shared<MyKeyListener>(*this)) { }
491int main(
int argc,
char *argv[])
495 return launch(
"Primitives02.cpp",
497 std::make_shared<Example>(), argc, argv);
int launch(std::string_view sfile, GLLaunchProps props, const RenderListenerSRef &demo, int argc, char *argv[])
std::shared_ptr< Shape > ShapeRef
int main(int argc, char *argv[])
std::shared_ptr< Shape > ShapeRef
bool init(const WindowSRef &win, const jau::fraction_timespec &when) override
Called by the drawable immediately after the render context is initialized.
void dispose(const WindowSRef &win, const jau::fraction_timespec &when) override
Notifies the listener to perform the release of all renderer resources per context,...
std::string toStringImpl() const noexcept override
void setOneFrame() noexcept
bool & animating() noexcept
void reshape(const WindowSRef &win, const jau::math::Recti &viewport, const jau::fraction_timespec &when) override
Called by the drawable during the first repaint after the component has been resized.
Recti & viewport() noexcept
void dispose(const WindowSRef &win, const jau::fraction_timespec &when) override
Notifies the listener to perform the release of all renderer resources per context,...
void display(const WindowSRef &win, const jau::fraction_timespec &when) override
Called by the drawable to initiate rendering by the client.
bool animating() const noexcept
bool init(const WindowSRef &win, const jau::fraction_timespec &when) override
Called by the drawable immediately after the render context is initialized.
PMVMat4f & pmv() noexcept
const PMVMat4f & pmv() const noexcept
const Recti & viewport() const noexcept
constexpr const float & zOffset() const noexcept
void setColor(const Vec4f &c) noexcept
constexpr const OutlineShape & outlineShape() const noexcept
constexpr Vec3f & position() noexcept
static ShapeRef createShared(ShaderState &st, GraphRenderer &renderer)
constexpr const Quat4f & rotation() const noexcept
Shape(Private, ShaderState &st, GLUniformSyncPMVMat4f &pmvMatU, GLUniformVec4f &color)
constexpr OutlineShape & outlineShape() noexcept
static ShapeRef createShared(ShaderState &st, GLUniformSyncPMVMat4f &pmvMatU, GLUniformVec4f &color)
constexpr const Vec3f & rotationPivot() const noexcept
constexpr float & zOffset() noexcept
constexpr const Vec3f & position() const noexcept
constexpr Vec3f & scale() noexcept
constexpr Vec3f & rotationPivot() noexcept
const Vec4f & color() const noexcept
constexpr const Vec3f & scale() const noexcept
constexpr Quat4f & rotation() noexcept
A Generic shape objects which is defined by a list of Outlines.
void moveTo(float x, float y, float z)
Start a new position for the next line segment at given point x/y (P1).
void closePath()
Closes the current sub-path segment by drawing a straight line back to the coordinates of the last mo...
OutlineShape flipFace(float zoffset=0) const
Returns a copy of this instance with normal() pointing to the opposite direction and all outlines() v...
void lineTo(float x, float y, float z)
Add a line segment, intersecting the last point and the given point x/y (P1).
std::vector< Segment > SegmentList
static SegmentList tesselate(int flags, GLFloatArrayDataServer &array, OutlineShape &outlines)
static constexpr int FLAG_NORMAL
static constexpr int FLAG_VERBOSE
Specifies a set of OpenGL capabilities.
static GLContext & downcast(RenderContext *rc)
Downcast dereferenced given RenderContext* to GLContext&, throws exception if signature doesn't match...
Specifies the OpenGL profile.
static constexpr std::string_view GLES2
The embedded OpenGL profile ES 2.x, with x >= 0.
size_t defaultShaderCustomization(const GL &gl, bool preludeVersion=true, bool addDefaultPrecision=true, bool addDefaultDefines=true)
Default customization of this shader source code.
static ShaderCodeSRef create(GLenum type, size_t count, const source_list_t &sources)
size_t insertShaderSource(size_t shaderIdx, stringview_t tag, size_t fromIndex, stringview_t data) noexcept
Adds data after the line containing tag.
static ShaderProgramSRef create() noexcept
void destroy(GL &gl) noexcept
Detaches all shader codes and deletes the program.
bool add(const ShaderCodeSRef &shaderCode) noexcept
Adds a new shader to this program.
constexpr bool inUse() const noexcept
ShaderState allows to sharing data between shader programs, while updating the attribute and uniform ...
constexpr RenderListener(Private) noexcept
Private ctor for shared_ptr<RenderListener> instance method w/o public ctor.
std::string toString() const noexcept
const gamp::render::RenderContext * renderContext() const noexcept
void addKeyListener(const KeyListenerSRef &l)
void dispose(const jau::fraction_timespec &when) noexcept override
size_t removeKeyListener(const KeyListenerSRef &l)
std::string toString() const noexcept
std::string toString() const noexcept
constexpr VKeyCode keySym() const noexcept
Returns the virtual key symbol reflecting the current keyboard layout.
virtual const PressedKeyCodes & pressedKeyCodes() const noexcept=0
constexpr const WindowWeakPtr & source() const noexcept
constexpr const jau::fraction_timespec & when() const noexcept
size_type count() const noexcept
static constexpr const value_type one
#define jau_INFO_PRINT(fmt,...)
Use for unconditional informal messages, prefix '[elapsed_time] Info: '.
std::shared_ptr< Shape > ShapeRef
constexpr T adeg_to_rad(const T arc_degree) noexcept
Converts arc-degree to radians.
GLArrayDataServer< float > GLFloatArrayDataServer
GLArrayDataServerSRef< float > GLFloatArrayDataServerSRef
std::shared_ptr< ShaderCode > ShaderCodeSRef
std::shared_ptr< ShaderProgram > ShaderProgramSRef
@ verbose
Verbose operations (debugging).
std::shared_ptr< Window > WindowSRef
PMVData
PMVMatrix4 derived matrices and values.
Quaternion< float > Quat4f
PMVMatrix4< float > PMVMat4f
@ inv_mv
Bit value for inverse modelview matrix (Mvi), updated via update().
@ inv_proj
Bit value for inverse projection matrix (Pi), updated via update().
@ inv_tps_mv
Bit value for inverse transposed modelview matrix (Mvit), updated via update().
Gamp: Graphics, Audio, Multimedia and Processing Framework (Native C++, WebAssembly,...
ssize_t fprintf_td(const uint64_t elapsed_ms, FILE *stream, std::string_view format, const Args &...args) noexcept
Convenient secure fprintf() invocation, prepending the given elapsed_ms timestamp and using jau:forma...
static std::string toString(const std::string &pre, const SegmentList &segments) noexcept
Timespec structure using int64_t for its components in analogy to struct timespec_t on 64-bit platfor...
constexpr uint64_t to_ms() const noexcept
Returns time in milliseconds.