Gamp v0.0.7-36-g24b1eb6
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 PMVMat4f m_pmv;
35 bool m_initialized;
36 bool m_animating = true;
37 jau::fraction_timespec m_tlast;
38
39 public:
42 m_pmv(), m_initialized(false) { }
43
44 Recti& viewport() noexcept { return m_viewport; }
45 const Recti& viewport() const noexcept { return m_viewport; }
46
47 PMVMat4f& pmv() noexcept { return m_pmv; }
48 const PMVMat4f& pmv() const noexcept { return m_pmv; }
49 bool animating() const noexcept { return m_animating; }
50 bool& animating() noexcept { return m_animating; }
51
52 bool init(const WindowRef& win, const jau::fraction_timespec& when) override {
53 jau::fprintf_td(when.to_ms(), stdout, "RL::init: %s\n", toString().c_str());
54 m_tlast = when;
55
57 ShaderCodeRef vp0 = ShaderCode::create(gl, GL_VERTEX_SHADER, "demos/glsl",
58 "demos/glsl/bin", "RedSquareShader");
59 ShaderCodeRef fp0 = ShaderCode::create(gl, GL_FRAGMENT_SHADER, "demos/glsl",
60 "demos/glsl/bin", "RedSquareShader");
61 if( !vp0 || !fp0 ) {
62 jau::fprintf_td(when.to_ms(), stdout, "ERROR %s:%d: %s\n", E_FILE_LINE, toString().c_str());
63 win->dispose(when);
64 return false;
65 }
66 vp0->defaultShaderCustomization(gl, true, true);
67 fp0->defaultShaderCustomization(gl, true, true);
69 if( !sp0->add(gl, vp0, true) || !sp0->add(gl, fp0, true) ) {
70 jau::fprintf_td(when.to_ms(), stdout, "ERROR %s:%d: %s\n", E_FILE_LINE, toString().c_str());
71 sp0->destroy(gl);
72 win->dispose(when);
73 return false;
74 }
75 m_st.attachShaderProgram(gl, sp0, true);
76
77 // setup mgl_PMVMatrix
78 m_pmv.getP().loadIdentity();
79 m_pmv.getMv().loadIdentity();
80 m_st.ownUniform(GLUniformSyncMatrices4f::create("mgl_PMVMatrix", m_pmv.getSyncPMv()), true);
81
82 // Allocate Vertex Array
83 GLFloatArrayDataServerRef vertices = GLFloatArrayDataServer::createGLSL("mgl_Vertex", 3, false, 4, GL_STATIC_DRAW);
84 vertices->reserve(4); // reserve 4 elements (4x3 components) upfront, otherwise growIfNeeded is used
85 vertices->put3f(-2, 2, 0);
86 vertices->put3f( 2, 2, 0);
87 vertices->put3f(-2, -2, 0);
88 vertices->put3f( 2, -2, 0);
89 m_st.ownAttribute(vertices, true);
90 vertices->seal(gl, true);
91
92 // Allocate Color Array
93 GLFloatArrayDataServerRef colors = GLFloatArrayDataServer::createGLSL("mgl_Color", 4, false, 4, GL_STATIC_DRAW);
94 assert(GL_FLOAT == vertices->compType()); // determined via template type jau::float32_t
95 colors->put4f(1, 0, 0, 1); // used implied growIfNeeded
96 colors->put4f(0, 0, 1, 1);
97 colors->put4f(1, 0, 0, 1);
98 colors->put4f(1, 0, 0, 1);
99 m_st.ownAttribute(colors, true);
100 colors->seal(gl, true);
101
102 ::glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
103 ::glEnable(GL_DEPTH_TEST);
104
105 m_initialized = sp0->inUse();
106 if( !m_initialized ) {
107 jau::fprintf_td(when.to_ms(), stdout, "ERROR %s:%d: %s\n", E_FILE_LINE, toString().c_str());
108 m_st.destroy(gl);
109 win->dispose(when);
110 }
111 return m_initialized;
112 }
113
114 void dispose(const WindowRef& win, const jau::fraction_timespec& when) override {
115 jau::fprintf_td(when.to_ms(), stdout, "RL::dispose: %s\n", toString().c_str());
116 m_st.destroy(GL::downcast(win->renderContext()));
117 m_initialized = false;
118 }
119
120 void reshape(const WindowRef& win, const jau::math::Recti& viewport, const jau::fraction_timespec& when) override {
121 GL& gl = GL::downcast(win->renderContext());
122 jau::fprintf_td(when.to_ms(), stdout, "RL::reshape: %s\n", toString().c_str());
123 m_viewport = viewport;
124
125 m_pmv.getP().loadIdentity();
126
127 const float aspect = 1.0f;
128 const float fovy_deg=45.0f;
129 const float aspect2 = ( (float) m_viewport.width() / (float) m_viewport.height() ) / aspect;
130 const float zNear=1.0f;
131 const float zFar=100.0f;
132 m_pmv.perspectiveP(jau::adeg_to_rad(fovy_deg), aspect2, zNear, zFar);
133 m_st.useProgram(gl, true);
134 m_st.pushAllUniforms(gl);
135 m_st.useProgram(gl, false);
136 }
137
138 void display(const WindowRef& win, const jau::fraction_timespec& when) override {
139 // jau::fprintf_td(when.to_ms(), stdout, "RL::display: %s, %s\n", toString().c_str(), win->toString().c_str());
140 if( !m_initialized ) {
141 return;
142 }
143 GL& gl = GL::downcast(win->renderContext());
144 ::glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
145
146 m_st.useProgram(gl, true);
147 m_pmv.getMv().loadIdentity();
148 m_pmv.translateMv(0, 0, -10);
149 static float t_sum_ms = 0;
150 if( m_animating ) {
151 t_sum_ms += float( (when - m_tlast).to_ms() );
152 }
153 const float ang = jau::adeg_to_rad(t_sum_ms * 360.0f) / 4000.0f;
154 m_pmv.rotateMv(ang, 0, 0, 1);
155 m_pmv.rotateMv(ang, 0, 1, 0);
156 m_st.pushAllUniforms(gl);
157
158 // Draw a square
159 ::glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
160 m_st.useProgram(gl, false);
161
162 m_tlast = when;
163 }
164
165 std::string toStringImpl() const noexcept override { return "RedSquareES2"; }
166};
167
168#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(const std::string &name, GLsizei compsPerElement, bool normalized, GLsizei initialElementCount, GLenum vboUsage)
static std::shared_ptr< GLUniformSyncMatrices4f > create(const string_t &name, SyncMats4f &data)
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
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.
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.