86 bool iMatIdent =
true;
87 bool iMatDirty =
false;
89 struct Private{
explicit Private() =
default; };
93 : m_st(st), m_pmvMat(pmvMatU), m_oshape(3, 16),
96 m_array->addGLSLSubArray(
"mgl_Vertex", 3, GL_ARRAY_BUFFER);
97 m_array->addGLSLSubArray(
"mgl_Normal", 3, GL_ARRAY_BUFFER);
98 m_st.ownAttribute(m_array,
true);
101 m_st.ownUniform(m_uColor,
true);
105 return std::make_shared<Shape>(Private(), st, pmvMatU);
111 constexpr const float&
zOffset() const noexcept {
return m_zOffset; }
112 constexpr float&
zOffset() noexcept { iMatDirty=
true;
return m_zOffset; }
120 constexpr const Vec3f&
scale() const noexcept {
return m_scale; }
121 constexpr Vec3f&
scale() noexcept { iMatDirty=
true;
return m_scale; }
126 const Vec4f&
color() const noexcept {
return m_uColor->vec4f(); }
132 m_array->seal(
gl,
true);
137 applyMatToMv(m_pmvMat.m);
138 m_st.pushUniform(
gl, m_pmvMat.u);
140 m_st.pushUniform(
gl, m_uColor);
141 m_array->enableBuffer(
gl,
true);
143 ::glDrawArrays(s.type, s.first, s.count);
145 m_array->enableBuffer(
gl,
false);
164 void applyMatToMv(
PMVMat4f& pmvMat)
noexcept {
172 void updateMat() noexcept {
173 bool hasPos = !m_position.is_zero();
175 bool hasRotate = !m_rotation.isIdentity();
176 bool hasRotPivot =
false;
177 const Vec3f& ctr = m_oshape.bounds().center();
178 bool sameScaleRotatePivot = hasScale && hasRotate && ( !hasRotPivot || m_rotPivot == ctr );
180 if( sameScaleRotatePivot ) {
182 iMat.setToTranslation(m_position);
184 iMat.translate(
Vec3f(ctr).mul(m_scale));
185 iMat.rotate(m_rotation);
187 iMat.translate(-ctr);
188 }
else if( hasRotate || hasScale ) {
190 iMat.setToTranslation(m_position);
194 iMat.translate(
Vec3f(m_rotPivot).mul(m_scale));
195 iMat.rotate(m_rotation);
196 iMat.translate(
Vec3f(-m_rotPivot).mul(m_scale));
199 iMat.translate(
Vec3f(ctr).mul(m_scale));
200 iMat.rotate(m_rotation);
201 iMat.translate(
Vec3f(-ctr).mul(m_scale));
206 iMat.translate(
Vec3f(ctr).mul(m_scale));
208 iMat.translate(
Vec3f(-ctr).mul(m_scale));
210 }
else if( hasPos ) {
212 iMat.setToTranslation(m_position);
225 constexpr static float zNear= 1.0f;
226 constexpr static float zFar =100.0f;
231 bool m_animating =
true;
232 bool m_oneframe =
false;
235 std::vector<ShapeRef> m_shapes;
251 void setOneFrame() noexcept { m_animating=
false; m_oneframe=
true; }
259 "demos/glsl/bin",
"SingleLight0");
261 "demos/glsl/bin",
"SingleLight0");
268 std::string custom =
"#define MAX_TEXTURE_UNITS 0\n";
276 if( !sp0->
add(
gl, vp0,
true) || !sp0->
add(
gl, fp0,
true) ) {
282 m_st.attachShaderProgram(
gl, sp0,
true);
285 m_pmvMat.m.getP().loadIdentity();
286 m_pmvMat.m.getMv().loadIdentity();
287 m_st.ownUniform(m_pmvMat.u,
true);
290 m_st.ownUniform(lightU,
true);
292 m_st.pushAllUniforms(
gl);
294 const float lineWidth = 1/2.5f;
295 const float dz = 0.001f;
298 const float width = 1.5f;
299 const float height = 1.5f;
300 const float x1 = -width/2.0f;
301 const float y1 = -height/2.0f;
302 const float x2 = x1 + width;
303 const float y2 = y1 + height;
306 m_shapes.push_back(frontShape);
320 const float dxy = lineWidth;
321 oshape.
moveTo(x1+dxy, y1+dxy, z);
322 oshape.
lineTo(x1+dxy, y2-dxy, z);
323 oshape.
lineTo(x2-dxy, y2-dxy, z);
324 oshape.
lineTo(x2-dxy, y1+dxy, z);
325 oshape.
lineTo(x1+dxy, y1+dxy, z);
328 frontShape->update(
gl);
329 frontShape->setColor(
Vec4f(0.05f, 0.05f, 0.5f, 1));
330 frontShape->position().x = 1.5f;
333 m_shapes.push_back(backShape);
334 backShape->outlines() = oshape.
flipFace();
335 backShape->update(
gl);
336 backShape->setColor(
Vec4f(0.4f, 0.4f, 0.1f, 1));
337 backShape->position().x = 1.5f;
341 const float width = 1.5f;
342 const float height = 1.5f;
344 float lwh = lineWidth/2.0f;
346 float twh = width/2.0f;
347 float thh = height/2.0f;
349 float ctrX = 0, ctrY = 0, ctrZ = dz;
351 m_shapes.push_back(frontShape);
354 oshape.
moveTo(ctrX-lwh, ctrY+thh, ctrZ);
355 oshape.
lineTo(ctrX-lwh, ctrY+lwh, ctrZ);
356 oshape.
lineTo(ctrX-twh, ctrY+lwh, ctrZ);
357 oshape.
lineTo(ctrX-twh, ctrY-lwh, ctrZ);
358 oshape.
lineTo(ctrX-lwh, ctrY-lwh, ctrZ);
359 oshape.
lineTo(ctrX-lwh, ctrY-thh, ctrZ);
360 oshape.
lineTo(ctrX+lwh, ctrY-thh, ctrZ);
361 oshape.
lineTo(ctrX+lwh, ctrY-lwh, ctrZ);
362 oshape.
lineTo(ctrX+twh, ctrY-lwh, ctrZ);
363 oshape.
lineTo(ctrX+twh, ctrY+lwh, ctrZ);
364 oshape.
lineTo(ctrX+lwh, ctrY+lwh, ctrZ);
365 oshape.
lineTo(ctrX+lwh, ctrY+thh, ctrZ);
366 oshape.
lineTo(ctrX-lwh, ctrY+thh, ctrZ);
369 frontShape->update(
gl);
370 frontShape->setColor(
Vec4f(0.5f, 0.05f, 0.05f, 1));
371 frontShape->position().x = -1.5f;
374 m_shapes.push_back(backShape);
375 backShape->outlines() = oshape.
flipFace();
376 backShape->update(
gl);
377 backShape->setColor(
Vec4f(0.2f, 0.2f, 0.2f, 1));
378 backShape->position().x = -1.5f;
381 ::glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
382 ::glEnable(GL_DEPTH_TEST);
383 ::glEnable(GL_CULL_FACE);
385 m_initialized = sp0->
inUse();
386 if( !m_initialized ) {
391 return m_initialized;
397 m_initialized =
false;
405 m_pmvMat.m.getP().loadIdentity();
406 const float aspect = 1.0f;
407 const float fovy_deg=45.0f;
408 const float aspect2 = ( (float) m_viewport.width() / (
float) m_viewport.height() ) / aspect;
410 m_st.useProgram(
gl,
true);
411 m_st.pushUniform(
gl, m_pmvMat.u);
417 if( !m_initialized ) {
421 ::glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
423 m_st.useProgram(
gl,
true);
424 m_pmvMat.m.getMv().loadIdentity();
425 m_pmvMat.m.translateMv(0, 0, -5);
429 constexpr double angle_per_sec = 30;
430 const float rad = (float) ( (when - m_tlast).to_double() * angle_per_sec );
441 std::string
toStringImpl() const noexcept
override {
return "Primitives02"; }
446 class MyKeyListener :
public KeyListener {
452 void keyPressed(KeyEvent& e,
const KeyboardTracker& kt)
override {
454 if( e.
keySym() == VKeyCode::VK_ESCAPE ) {
459 }
else if( e.
keySym() == VKeyCode::VK_PAUSE || e.
keySym() == VKeyCode::VK_P ) {
460 m_parent.animating() = !m_parent.animating();
461 }
else if( e.
keySym() == VKeyCode::VK_PERIOD ) {
462 m_parent.setOneFrame();
463 }
else if( e.
keySym() == VKeyCode::VK_W ) {
468 void keyReleased(KeyEvent& e,
const KeyboardTracker& kt)
override {
472 typedef std::shared_ptr<MyKeyListener> MyKeyListenerRef;
473 MyKeyListenerRef m_kl;
478 m_kl(
std::make_shared<MyKeyListener>(*this)) { }
493int main(
int argc,
char *argv[])
497 return launch(
"Primitives02.cpp",
499 std::make_shared<Example>(), argc, argv);
int launch(std::string_view sfile, const GLLaunchProps &props, const RenderListenerRef &demo, int argc, char *argv[])
std::shared_ptr< Shape > ShapeRef
int main(int argc, char *argv[])
std::shared_ptr< Shape > ShapeRef
bool init(const WindowRef &win, const jau::fraction_timespec &when) override
Called by the drawable immediately after the render context is initialized.
void dispose(const WindowRef &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
Recti & viewport() noexcept
void dispose(const WindowRef &win, const jau::fraction_timespec &when) override
Notifies the listener to perform the release of all renderer resources per context,...
bool animating() const noexcept
void display(const WindowRef &win, const jau::fraction_timespec &when) override
Called by the drawable to initiate rendering by the client.
PMVMat4f & pmv() noexcept
void reshape(const WindowRef &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.
bool init(const WindowRef &win, const jau::fraction_timespec &when) override
Called by the drawable immediately after the render context is initialized.
const PMVMat4f & pmv() const noexcept
const Recti & viewport() const noexcept
constexpr const float & zOffset() const noexcept
void setColor(const Vec4f &c) noexcept
constexpr Vec3f & position() noexcept
static ShapeRef create(ShaderState &st, PMVMat4fUniform &pmvMatU, GraphRenderer &renderer)
constexpr OutlineShape & outlines() noexcept
constexpr const Quat4f & rotation() const noexcept
static ShapeRef create(ShaderState &st, PMVMat4fUniform &pmvMatU)
constexpr const Vec3f & rotationPivot() const noexcept
constexpr float & zOffset() noexcept
constexpr const Vec3f & position() const noexcept
constexpr Vec3f & scale() noexcept
constexpr Vec3f & rotationPivot() noexcept
Shape(Private, ShaderState &st, PMVMat4fUniform &pmvMatU)
const Vec4f & color() const noexcept
constexpr const Vec3f & scale() const noexcept
constexpr const OutlineShape & outlines() 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() and all outlines() vertices()'s z-axis sign-flipped,...
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
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, bool addDefaultPrecision)
Default customization of this shader source code.
static ShaderCodeRef create(GLenum type, size_t count, const source_list_t &sources) noexcept
size_t insertShaderSource(size_t shaderIdx, stringview_t tag, size_t fromIndex, stringview_t data) noexcept
Adds data after the line containing tag.
void destroy(GL &gl) noexcept
Detaches all shader codes and deletes the program.
static ShaderProgramRef create() noexcept
bool add(const ShaderCodeRef &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
size_t removeKeyListener(const KeyListenerRef &l)
void dispose(const jau::fraction_timespec &when) noexcept override
std::string toString() const noexcept
void addKeyListener(const KeyListenerRef &l)
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_t bitCount() const noexcept
static constexpr const value_type one
constexpr T adeg_to_rad(const T arc_degree) noexcept
Converts arc-degree to radians.
GLArrayDataServer< float > GLFloatArrayDataServer
GLArrayDataServerRef< float > GLFloatArrayDataServerRef
std::shared_ptr< GLUniformData > GLUniformDataRef
std::shared_ptr< GLUniformVec4f > GLUniformVec4fRef
std::shared_ptr< GLUniformVec3f > GLUniformVec3fRef
std::shared_ptr< ShaderProgram > ShaderProgramRef
std::shared_ptr< ShaderCode > ShaderCodeRef
@ verbose
Verbose operations (debugging).
std::shared_ptr< Window > WindowRef
Quaternion< float > Quat4f
PMVMatrix4< float > PMVMat4f
Gamp: Graphics, Audio, Multimedia and Processing Framework (Native C++, WebAssembly,...
void INFO_PRINT(const char *format,...) noexcept
Use for unconditional informal messages, prefix '[elapsed_time] Info: '.
int fprintf_td(const uint64_t elapsed_ms, FILE *stream, const char *format,...) noexcept
Convenient fprintf() invocation, prepending the given elapsed_ms timestamp.
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.