Gamp v0.0.7-36-g24b1eb6
Gamp: Graphics, Audio, Multimedia and Processing
Loading...
Searching...
No Matches
RedSquareES2.hpp

This C++ demo showcases a simple gamp::wt::RenderListener, implementing rendering using gamp::render::gl::glsl::ShaderState setup with attributes, uniforms and gamp::render::gl::glsl::ShaderCode.

This C++ demo showcases a simple gamp::wt::RenderListener, implementing rendering using gamp::render::gl::glsl::ShaderState setup with attributes, uniforms and gamp::render::gl::glsl::ShaderCode.

/*
* Author: Sven Gothel <sgothel@jausoft.com>
* Copyright Gothel Software e.K.
*
* SPDX-License-Identifier: MIT
*
* This Source Code Form is subject to the terms of the MIT License
* If a copy of the MIT was not distributed with this file,
* you can obtain one at https://opensource.org/license/mit/.
*/
#ifndef GAMP_DEMOS_REDSQUAREES2_HPP_
#define GAMP_DEMOS_REDSQUAREES2_HPP_
#include <cstdio>
using namespace jau::math;
using namespace jau::math::util;
using namespace gamp::wt;
using namespace gamp::wt::event;
using namespace gamp::render::gl::glsl;
using namespace gamp::render::gl::data;
class RedSquareES2 : public RenderListener {
private:
Recti m_viewport;
PMVMat4f m_pmv;
bool m_initialized;
bool m_animating = true;
public:
m_pmv(), m_initialized(false) { }
Recti& viewport() noexcept { return m_viewport; }
const Recti& viewport() const noexcept { return m_viewport; }
PMVMat4f& pmv() noexcept { return m_pmv; }
const PMVMat4f& pmv() const noexcept { return m_pmv; }
bool animating() const noexcept { return m_animating; }
bool& animating() noexcept { return m_animating; }
bool init(const WindowRef& win, const jau::fraction_timespec& when) override {
jau::fprintf_td(when.to_ms(), stdout, "RL::init: %s\n", toString().c_str());
m_tlast = when;
GL& gl = GL::downcast(win->renderContext());
ShaderCodeRef vp0 = ShaderCode::create(gl, GL_VERTEX_SHADER, "demos/glsl",
"demos/glsl/bin", "RedSquareShader");
ShaderCodeRef fp0 = ShaderCode::create(gl, GL_FRAGMENT_SHADER, "demos/glsl",
"demos/glsl/bin", "RedSquareShader");
if( !vp0 || !fp0 ) {
jau::fprintf_td(when.to_ms(), stdout, "ERROR %s:%d: %s\n", E_FILE_LINE, toString().c_str());
win->dispose(when);
return false;
}
vp0->defaultShaderCustomization(gl, true, true);
fp0->defaultShaderCustomization(gl, true, true);
if( !sp0->add(gl, vp0, true) || !sp0->add(gl, fp0, true) ) {
jau::fprintf_td(when.to_ms(), stdout, "ERROR %s:%d: %s\n", E_FILE_LINE, toString().c_str());
sp0->destroy(gl);
win->dispose(when);
return false;
}
m_st.attachShaderProgram(gl, sp0, true);
// setup mgl_PMVMatrix
m_pmv.getP().loadIdentity();
m_pmv.getMv().loadIdentity();
m_st.ownUniform(GLUniformSyncMatrices4f::create("mgl_PMVMatrix", m_pmv.getSyncPMv()), true);
// Allocate Vertex Array
GLFloatArrayDataServerRef vertices = GLFloatArrayDataServer::createGLSL("mgl_Vertex", 3, false, 4, GL_STATIC_DRAW);
vertices->reserve(4); // reserve 4 elements (4x3 components) upfront, otherwise growIfNeeded is used
vertices->put3f(-2, 2, 0);
vertices->put3f( 2, 2, 0);
vertices->put3f(-2, -2, 0);
vertices->put3f( 2, -2, 0);
m_st.ownAttribute(vertices, true);
vertices->seal(gl, true);
// Allocate Color Array
GLFloatArrayDataServerRef colors = GLFloatArrayDataServer::createGLSL("mgl_Color", 4, false, 4, GL_STATIC_DRAW);
assert(GL_FLOAT == vertices->compType()); // determined via template type jau::float32_t
colors->put4f(1, 0, 0, 1); // used implied growIfNeeded
colors->put4f(0, 0, 1, 1);
colors->put4f(1, 0, 0, 1);
colors->put4f(1, 0, 0, 1);
m_st.ownAttribute(colors, true);
colors->seal(gl, true);
::glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
::glEnable(GL_DEPTH_TEST);
m_initialized = sp0->inUse();
if( !m_initialized ) {
jau::fprintf_td(when.to_ms(), stdout, "ERROR %s:%d: %s\n", E_FILE_LINE, toString().c_str());
m_st.destroy(gl);
win->dispose(when);
}
return m_initialized;
}
void dispose(const WindowRef& win, const jau::fraction_timespec& when) override {
jau::fprintf_td(when.to_ms(), stdout, "RL::dispose: %s\n", toString().c_str());
m_st.destroy(GL::downcast(win->renderContext()));
m_initialized = false;
}
void reshape(const WindowRef& win, const jau::math::Recti& viewport, const jau::fraction_timespec& when) override {
GL& gl = GL::downcast(win->renderContext());
jau::fprintf_td(when.to_ms(), stdout, "RL::reshape: %s\n", toString().c_str());
m_viewport = viewport;
m_pmv.getP().loadIdentity();
const float aspect = 1.0f;
const float fovy_deg=45.0f;
const float aspect2 = ( (float) m_viewport.width() / (float) m_viewport.height() ) / aspect;
const float zNear=1.0f;
const float zFar=100.0f;
m_pmv.perspectiveP(jau::adeg_to_rad(fovy_deg), aspect2, zNear, zFar);
m_st.useProgram(gl, true);
m_st.useProgram(gl, false);
}
void display(const WindowRef& win, const jau::fraction_timespec& when) override {
// jau::fprintf_td(when.to_ms(), stdout, "RL::display: %s, %s\n", toString().c_str(), win->toString().c_str());
if( !m_initialized ) {
return;
}
GL& gl = GL::downcast(win->renderContext());
::glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_st.useProgram(gl, true);
m_pmv.getMv().loadIdentity();
m_pmv.translateMv(0, 0, -10);
static float t_sum_ms = 0;
if( m_animating ) {
t_sum_ms += float( (when - m_tlast).to_ms() );
}
const float ang = jau::adeg_to_rad(t_sum_ms * 360.0f) / 4000.0f;
m_pmv.rotateMv(ang, 0, 0, 1);
m_pmv.rotateMv(ang, 0, 1, 0);
// Draw a square
::glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
m_st.useProgram(gl, false);
m_tlast = when;
}
std::string toStringImpl() const noexcept override { return "RedSquareES2"; }
};
#endif // #define GAMP_DEMOS_REDSQUAREES2_HPP_
#define E_FILE_LINE
Recti & viewport() noexcept
std::string toStringImpl() const noexcept override
void dispose(const WindowRef &, const jau::fraction_timespec &) override
Notifies the listener to perform the release of all renderer resources per context,...
PMVMat4f & pmv() noexcept
void display(const WindowRef &, const jau::fraction_timespec &when) override
Called by the drawable to initiate rendering by the client.
bool animating() const noexcept
static GLContext & downcast(RenderContext *rc)
Downcast dereferenced given RenderContext* to GLContext&, throws exception if signature doesn't match...
static server_ref createGLSL(const std::string &name, GLsizei compsPerElement, bool normalized, GLsizei initialElementCount, GLenum vboUsage)
static std::shared_ptr< GLUniformSyncMatrices4f > create(const string_t &name, SyncMats4f &data)
static ShaderCodeRef create(GLenum type, size_t count, const source_list_t &sources) noexcept
static ShaderProgramRef create() noexcept
ShaderState allows to sharing data between shader programs, while updating the attribute and uniform ...
void destroy(GL &gl)
Calls release(gl, true, true, true).
void ownUniform(const GLUniformDataRef &data, bool own)
Bind the GLUniform lifecycle to this ShaderState.
void useProgram(GL &gl, bool on)
Turns the shader program on or off.
void pushAllUniforms(const GL &gl)
Same as pushUniform(), but for all active uniforms.
void ownAttribute(const GLArrayDataRef &attr, bool own)
Binds or unbinds the GLArrayData lifecycle to this ShaderState.
bool attachShaderProgram(GL &gl, const ShaderProgramRef &prog, bool enable)
Attach or switch a shader program.
constexpr RenderListener(Private) noexcept
Private ctor for shared_ptr<RenderListener> instance method w/o public ctor.
Definition Window.hpp:60
std::string toString() const noexcept
Definition Window.hpp:112
constexpr Matrix4 & loadIdentity() noexcept
Set this matrix to identity.
Definition mat4f.hpp:240
constexpr value_type height() const noexcept
Definition recti.hpp:113
constexpr value_type width() const noexcept
Definition recti.hpp:112
constexpr Mat4 & getP() noexcept
Returns the projection matrix (P).
Definition pmvmat4f.hpp:284
constexpr SyncMats4f & getSyncPMv() noexcept
Returns SyncMatrices4f of 2 matrices within one FloatBuffer: P and Mv.
Definition pmvmat4f.hpp:323
constexpr Mat4 & getMv() noexcept
Returns the modelview matrix (Mv).
Definition pmvmat4f.hpp:305
constexpr_cxx26 PMVMatrix4 & rotateMv(const float ang_rad, const float x, const float y, const float z) noexcept
Rotate the modelview matrix by the given axis and angle in radians.
Definition pmvmat4f.hpp:720
constexpr PMVMatrix4 & translateMv(float x, float y, float z) noexcept
Translate the modelview matrix.
Definition pmvmat4f.hpp:633
PMVMatrix4 & perspectiveP(const float fovy_rad, const float aspect, const float zNear, const float zFar)
Multiply the projection matrix with the perspective/frustum matrix.
Definition pmvmat4f.hpp:861
constexpr T adeg_to_rad(const T arc_degree) noexcept
Converts arc-degree to radians.
GLArrayDataServerRef< float > GLFloatArrayDataServerRef
std::shared_ptr< ShaderProgram > ShaderProgramRef
std::shared_ptr< ShaderCode > ShaderCodeRef
std::shared_ptr< Window > WindowRef
Definition Event.hpp:36
RectI< int > Recti
Definition recti.hpp:139
PMVMatrix4< float > PMVMat4f
int fprintf_td(const uint64_t elapsed_ms, FILE *stream, const char *format,...) noexcept
Convenient fprintf() invocation, prepending the given elapsed_ms timestamp.
Definition debug.cpp:270
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.