Gamp v0.0.8
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
22
23using namespace jau::math;
24using namespace jau::math::util;
25
26using namespace gamp::wt;
27using namespace gamp::wt::event;
28using namespace gamp::render::gl::glsl;
29using namespace gamp::render::gl::data;
30
31class RedSquareES2 : public RenderListener {
32 private:
33 ShaderState m_st;
34 Recti m_viewport;
35 GLUniformSyncPMVMat4f m_pmvMatUni;
36 bool m_initialized;
37 bool m_animating = true;
38 jau::fraction_timespec m_tlast;
39
40 public:
43 m_pmvMatUni("gcu_PMVMatrix"),
44 m_initialized(false)
45 {
46 m_st.manage(m_pmvMatUni);
47 }
48
49 Recti& viewport() noexcept { return m_viewport; }
50 const Recti& viewport() const noexcept { return m_viewport; }
51
52 PMVMat4f& pmv() noexcept { return m_pmvMatUni.pmv(); }
53 const PMVMat4f& pmv() const noexcept { return m_pmvMatUni.pmv(); }
54 bool animating() const noexcept { return m_animating; }
55 bool& animating() noexcept { return m_animating; }
56
57 bool init(const WindowSRef& win, const jau::fraction_timespec& when) override {
59
60 jau::fprintf_td(when.to_ms(), stdout, "RL::init: %s\n", toString().c_str());
61 m_tlast = when;
62
64 ShaderCodeSRef vp0 = ShaderCode::create(gl, GL_VERTEX_SHADER, "demos/glsl",
65 "demos/glsl/bin", "RedSquareShader");
66 ShaderCodeSRef fp0 = ShaderCode::create(gl, GL_FRAGMENT_SHADER, "demos/glsl",
67 "demos/glsl/bin", "RedSquareShader");
68 if( !vp0 || !fp0 ) {
69 jau::fprintf_td(when.to_ms(), stdout, "ERROR %s:%d: %s\n", E_FILE_LINE, toString().c_str());
70 win->dispose(when);
71 return false;
72 }
73 vp0->defaultShaderCustomization(gl, true, true);
74 fp0->defaultShaderCustomization(gl, true, true);
76 if( !sp0->add(gl, vp0, true) || !sp0->add(gl, fp0, true) ) {
77 jau::fprintf_td(when.to_ms(), stdout, "ERROR %s:%d: %s\n", E_FILE_LINE, toString().c_str());
78 sp0->destroy(gl);
79 win->dispose(when);
80 return false;
81 }
82 m_st.attachShaderProgram(gl, sp0, true);
83
84 // setup mgl_PMVMatrix
85 PMVMat4f &pmv = m_pmvMatUni.pmv();
86 pmv.getP().loadIdentity();
87 pmv.getMv().loadIdentity();
88
89 // Allocate Vertex Array
90 GLFloatArrayDataServerSRef vertices = GLFloatArrayDataServer::createGLSL("gca_Vertex", 3, false, 4, GL_STATIC_DRAW);
91 vertices->reserve(4); // reserve 4 elements (4x3 components) upfront, otherwise growIfNeeded is used
92 vertices->put( { -2, 2, 0, // 1st vertex
93 2, 2, 0, // burst transfer, instead of 4x `put3f` for single vertice-value
94 -2, -2, 0,
95 2, -2, 0 } );
96 m_st.manage(vertices);
97 vertices->seal(gl, true);
98
99 // Allocate Color Array
100 GLFloatArrayDataServerSRef colors = GLFloatArrayDataServer::createGLSL("gca_Color", 4, false, 4, GL_STATIC_DRAW);
101 assert(GL_FLOAT == vertices->compType()); // determined via template type jau::float32_t
102 colors->put( { 1, 0, 0, 1, // uses implied growIfNeeded
103 0, 0, 1, 1, // burst transfer, instead of 4x `put4f` for single color-value
104 1, 0, 0, 1,
105 1, 0, 0, 1 } );
106 m_st.manage(colors);
107 colors->seal(gl, true);
108
109 ::glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
110 ::glEnable(GL_DEPTH_TEST);
111
112 m_initialized = sp0->inUse();
113 if( !m_initialized ) {
114 jau::fprintf_td(when.to_ms(), stdout, "ERROR %s:%d: %s\n", E_FILE_LINE, toString().c_str());
115 m_st.destroy(gl);
116 win->dispose(when);
117 }
118 std::cout << "Init:\n";
119 std::cout << m_st.toString(true) << "\n";
120 return m_initialized;
121 }
122
123 void dispose(const WindowSRef& win, const jau::fraction_timespec& when) override {
124 jau::fprintf_td(when.to_ms(), stdout, "RL::dispose: %s\n", toString().c_str());
125 m_st.destroy(GL::downcast(win->renderContext()));
126 m_initialized = false;
127 }
128
129 void reshape(const WindowSRef& win, const jau::math::Recti& viewport, const jau::fraction_timespec& when) override {
130 GL& gl = GL::downcast(win->renderContext());
131 jau::fprintf_td(when.to_ms(), stdout, "RL::reshape: %s\n", toString().c_str());
132 m_viewport = viewport;
133
134 PMVMat4f &pmv = m_pmvMatUni.pmv();
135 pmv.getP().loadIdentity();
136
137 const float aspect = 1.0f;
138 const float fovy_deg=45.0f;
139 const float aspect2 = ( (float) m_viewport.width() / (float) m_viewport.height() ) / aspect;
140 const float zNear=1.0f;
141 const float zFar=100.0f;
142 pmv.perspectiveP(jau::adeg_to_rad(fovy_deg), aspect2, zNear, zFar);
143 m_st.useProgram(gl, true);
144 m_st.send(gl, m_pmvMatUni);
145 m_st.useProgram(gl, false);
146 }
147
148 void display(const WindowSRef& win, const jau::fraction_timespec& when) override {
149 // jau::fprintf_td(when.to_ms(), stdout, "RL::display: %s, %s\n", toString().c_str(), win->toString().c_str());
150 if( !m_initialized ) {
151 return;
152 }
153 GL& gl = GL::downcast(win->renderContext());
154 ::glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
155
156 m_st.useProgram(gl, true);
157 PMVMat4f &pmv = m_pmvMatUni.pmv();
158 pmv.getMv().loadIdentity();
159 pmv.translateMv(0, 0, -10);
160 static float t_sum_ms = 0;
161 if( m_animating ) {
162 t_sum_ms += float( (when - m_tlast).to_ms() );
163 }
164 const float ang = jau::adeg_to_rad(t_sum_ms * 360.0f) / 4000.0f;
165 pmv.rotateMv(ang, 0, 0, 1);
166 pmv.rotateMv(ang, 0, 1, 0);
167 m_st.send(gl, m_pmvMatUni);
168
169 // Draw a square
170 ::glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
171 m_st.useProgram(gl, false);
172
173 m_tlast = when;
174 }
175
176 std::string toStringImpl() const noexcept override { return "RedSquareES2"; }
177};
178
179#endif // #define GAMP_DEMOS_REDSQUAREES2_HPP_
void dispose(const WindowSRef &win, const jau::fraction_timespec &when) override
Notifies the listener to perform the release of all renderer resources per context,...
bool init(const WindowSRef &win, const jau::fraction_timespec &when) override
Called by the drawable immediately after the render context is initialized.
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.
void display(const WindowSRef &win, const jau::fraction_timespec &when) override
Called by the drawable to initiate rendering by the client.
Recti & viewport() noexcept
std::string toStringImpl() const noexcept override
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_sref 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 ShaderCodeSRef create(GLenum type, size_t count, const source_list_t &sources)
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
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:131
void dispose(const jau::fraction_timespec &when) noexcept override
Definition Window.hpp:355
#define E_FILE_LINE
constexpr T adeg_to_rad(const T arc_degree) noexcept
Converts arc-degree to radians.
GLArrayDataServerSRef< float > GLFloatArrayDataServerSRef
std::shared_ptr< ShaderCode > ShaderCodeSRef
std::shared_ptr< ShaderProgram > ShaderProgramSRef
std::shared_ptr< Window > WindowSRef
Definition Event.hpp:36
RectI< int > Recti
Definition recti.hpp:146
PMVMatrix4< float > PMVMat4f
Definition pmvmat4.hpp:1463
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...
Definition debug.hpp:188
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.