Gamp v0.0.7-54-gccdc599
Gamp: Graphics, Audio, Multimedia and Processing
Loading...
Searching...
No Matches
RedSquareES2.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright Gothel Software e.K.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * This Source Code Form is subject to the terms of the MIT License
8 * If a copy of the MIT was not distributed with this file,
9 * you can obtain one at https://opensource.org/license/mit/.
10 */
11
12#ifndef GAMP_DEMOS_REDSQUAREES2_HPP_
13#define GAMP_DEMOS_REDSQUAREES2_HPP_
14
15#include <cstdio>
16
21
22using namespace jau::math;
23using namespace jau::math::util;
24
25using namespace gamp::wt;
26using namespace gamp::wt::event;
27using namespace gamp::render::gl::glsl;
28using namespace gamp::render::gl::data;
29
30class RedSquareES2 : public RenderListener {
31 private:
32 ShaderState m_st;
33 Recti m_viewport;
34 GLUniformSyncPMVMat4fRef m_pmvMatUni;
35 bool m_initialized;
36 bool m_animating = true;
37 jau::fraction_timespec m_tlast;
38
39 public:
42 m_pmvMatUni(GLUniformSyncPMVMat4f::create("gcu_PMVMatrix")),
43 m_initialized(false) { }
44
45 Recti& viewport() noexcept { return m_viewport; }
46 const Recti& viewport() const noexcept { return m_viewport; }
47
48 PMVMat4f& pmv() noexcept { return m_pmvMatUni->pmv(); }
49 const PMVMat4f& pmv() const noexcept { return m_pmvMatUni->pmv(); }
50 bool animating() const noexcept { return m_animating; }
51 bool& animating() noexcept { return m_animating; }
52
53 bool init(const WindowRef& win, const jau::fraction_timespec& when) override {
54 jau::fprintf_td(when.to_ms(), stdout, "RL::init: %s\n", toString().c_str());
55 m_tlast = when;
56
58 ShaderCodeRef vp0 = ShaderCode::create(gl, GL_VERTEX_SHADER, "demos/glsl",
59 "demos/glsl/bin", "RedSquareShader");
60 ShaderCodeRef fp0 = ShaderCode::create(gl, GL_FRAGMENT_SHADER, "demos/glsl",
61 "demos/glsl/bin", "RedSquareShader");
62 if( !vp0 || !fp0 ) {
63 jau::fprintf_td(when.to_ms(), stdout, "ERROR %s:%d: %s\n", E_FILE_LINE, toString().c_str());
64 win->dispose(when);
65 return false;
66 }
67 vp0->defaultShaderCustomization(gl, true, true);
68 fp0->defaultShaderCustomization(gl, true, true);
70 if( !sp0->add(gl, vp0, true) || !sp0->add(gl, fp0, true) ) {
71 jau::fprintf_td(when.to_ms(), stdout, "ERROR %s:%d: %s\n", E_FILE_LINE, toString().c_str());
72 sp0->destroy(gl);
73 win->dispose(when);
74 return false;
75 }
76 m_st.attachShaderProgram(gl, sp0, true);
77
78 // setup mgl_PMVMatrix
79 PMVMat4f &pmv = m_pmvMatUni->pmv();
80 pmv.getP().loadIdentity();
81 pmv.getMv().loadIdentity();
82 m_st.ownUniform(m_pmvMatUni, true);
83
84 // Allocate Vertex Array
85 GLFloatArrayDataServerRef vertices = GLFloatArrayDataServer::createGLSL("gca_Vertex", 3, false, 4, GL_STATIC_DRAW);
86 vertices->reserve(4); // reserve 4 elements (4x3 components) upfront, otherwise growIfNeeded is used
87 vertices->put( { -2, 2, 0, // 1st vertex
88 2, 2, 0, // burst transfer, instead of 4x `put3f` for single vertice-value
89 -2, -2, 0,
90 2, -2, 0 } );
91 m_st.ownAttribute(vertices, true);
92 vertices->seal(gl, true);
93
94 // Allocate Color Array
95 GLFloatArrayDataServerRef colors = GLFloatArrayDataServer::createGLSL("gca_Color", 4, false, 4, GL_STATIC_DRAW);
96 assert(GL_FLOAT == vertices->compType()); // determined via template type jau::float32_t
97 colors->put( { 1, 0, 0, 1, // uses implied growIfNeeded
98 0, 0, 1, 1, // burst transfer, instead of 4x `put4f` for single color-value
99 1, 0, 0, 1,
100 1, 0, 0, 1 } );
101 m_st.ownAttribute(colors, true);
102 colors->seal(gl, true);
103
104 ::glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
105 ::glEnable(GL_DEPTH_TEST);
106
107 m_initialized = sp0->inUse();
108 if( !m_initialized ) {
109 jau::fprintf_td(when.to_ms(), stdout, "ERROR %s:%d: %s\n", E_FILE_LINE, toString().c_str());
110 m_st.destroy(gl);
111 win->dispose(when);
112 }
113 return m_initialized;
114 }
115
116 void dispose(const WindowRef& win, const jau::fraction_timespec& when) override {
117 jau::fprintf_td(when.to_ms(), stdout, "RL::dispose: %s\n", toString().c_str());
118 m_st.destroy(GL::downcast(win->renderContext()));
119 m_initialized = false;
120 }
121
122 void reshape(const WindowRef& win, const jau::math::Recti& viewport, const jau::fraction_timespec& when) override {
123 GL& gl = GL::downcast(win->renderContext());
124 jau::fprintf_td(when.to_ms(), stdout, "RL::reshape: %s\n", toString().c_str());
125 m_viewport = viewport;
126
127 PMVMat4f &pmv = m_pmvMatUni->pmv();
128 pmv.getP().loadIdentity();
129
130 const float aspect = 1.0f;
131 const float fovy_deg=45.0f;
132 const float aspect2 = ( (float) m_viewport.width() / (float) m_viewport.height() ) / aspect;
133 const float zNear=1.0f;
134 const float zFar=100.0f;
135 pmv.perspectiveP(jau::adeg_to_rad(fovy_deg), aspect2, zNear, zFar);
136 m_st.useProgram(gl, true);
137 m_st.pushAllUniforms(gl);
138 m_st.useProgram(gl, false);
139 }
140
141 void display(const WindowRef& win, const jau::fraction_timespec& when) override {
142 // jau::fprintf_td(when.to_ms(), stdout, "RL::display: %s, %s\n", toString().c_str(), win->toString().c_str());
143 if( !m_initialized ) {
144 return;
145 }
146 GL& gl = GL::downcast(win->renderContext());
147 ::glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
148
149 m_st.useProgram(gl, true);
150 PMVMat4f &pmv = m_pmvMatUni->pmv();
151 pmv.getMv().loadIdentity();
152 pmv.translateMv(0, 0, -10);
153 static float t_sum_ms = 0;
154 if( m_animating ) {
155 t_sum_ms += float( (when - m_tlast).to_ms() );
156 }
157 const float ang = jau::adeg_to_rad(t_sum_ms * 360.0f) / 4000.0f;
158 pmv.rotateMv(ang, 0, 0, 1);
159 pmv.rotateMv(ang, 0, 1, 0);
160 m_st.pushAllUniforms(gl);
161
162 // Draw a square
163 ::glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
164 m_st.useProgram(gl, false);
165
166 m_tlast = when;
167 }
168
169 std::string toStringImpl() const noexcept override { return "RedSquareES2"; }
170};
171
172#endif // #define GAMP_DEMOS_REDSQUAREES2_HPP_
#define E_FILE_LINE
bool init(const WindowRef &win, const jau::fraction_timespec &when) override
Called by the drawable immediately after the render context is initialized.
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.
void dispose(const WindowRef &win, const jau::fraction_timespec &when) override
Notifies the listener to perform the release of all renderer resources per context,...
Recti & viewport() noexcept
std::string toStringImpl() const noexcept override
void display(const WindowRef &win, const jau::fraction_timespec &when) override
Called by the drawable to initiate rendering by the client.
PMVMat4f & pmv() noexcept
const PMVMat4f & pmv() const noexcept
bool & animating() noexcept
const Recti & viewport() const noexcept
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(std::string_view name, GLsizei compsPerElement, bool normalized, GLsizei initialElementCount, GLenum vboUsage)
size_t defaultShaderCustomization(const GL &gl, bool preludeVersion=true, bool addDefaultPrecision=true, bool addDefaultDefines=true)
Default customization of this shader source code.
static ShaderCodeRef create(GLenum type, size_t count, const source_list_t &sources) noexcept
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
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
const gamp::render::RenderContext * renderContext() const noexcept
Definition Surface.hpp:129
void dispose(const jau::fraction_timespec &when) noexcept override
Definition Window.hpp:355
constexpr T adeg_to_rad(const T arc_degree) noexcept
Converts arc-degree to radians.
std::shared_ptr< GLUniformSyncPMVMat4f > GLUniformSyncPMVMat4fRef
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:146
PMVMatrix4< float > PMVMat4f
Definition pmvmat4.hpp:1463
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:276
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.