Thanks to visit codestin.com
Credit goes to jausoft.com

aboutsummaryrefslogtreecommitdiffstats
path: root/examples/Primitives01.cpp
blob: c1f69ad7a782c9f878d385acd91c7e34551ffc44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
 * 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/.
 */

#include <cstdio>
#include <cmath>
#include <memory>

#include <jau/basic_types.hpp>
#include <jau/darray.hpp>
#include <jau/float_math.hpp>
#include <jau/float_types.hpp>
#include <jau/fraction_type.hpp>
#include <jau/io/file_util.hpp>
#include <jau/math/geom/aabbox3f.hpp>
#include <jau/math/geom/geom3f.hpp>
#include <jau/math/vec3f.hpp>
#include <jau/math/vec4f.hpp>
#include <jau/math/vec4f.hpp>
#include <jau/math/quaternion.hpp>

#include <gamp/Gamp.hpp>
#include <gamp/render/RenderContext.hpp>
#include <gamp/render/gl/data/GLArrayDataServer.hpp>
#include <gamp/render/gl/data/GLUniformData.hpp>
#include <gamp/render/gl/glsl/ShaderState.hpp>

#include "../demos/GLLauncher01.hpp"

using namespace jau::math;
using namespace jau::math::util;
using namespace jau::math::geom;

using namespace gamp::wt;
using namespace gamp::wt::event;

using namespace gamp::render::gl::glsl;
using namespace gamp::render::gl::data;

class Shape {
  private:
    Vec3f m_position;
    Quat4f m_rotation;
    Vec4f m_color = Vec4f(1, 0, 0, 1);

    GLenum m_type;
    ShaderState& m_st;
    GLUniformSyncPMVMat4f& m_pmvMatUni;
    GLUniformVec4f& m_staticColor;
    GLFloatArrayDataServerSRef m_array;

  public:
    Shape(GLenum type, ShaderState &st, GLUniformSyncPMVMat4f &pmvMatU, GLUniformVec4f &color)
    : m_type(type), m_st(st), m_pmvMatUni(pmvMatU), m_staticColor(color),
      m_array( GLFloatArrayDataServer::createGLSLInterleaved(2*3, false, 4, GL_STATIC_DRAW) )
    {
        m_array->addGLSLSubArray("gca_Vertex", 3, GL_ARRAY_BUFFER);
        m_array->addGLSLSubArray("gca_Normal", 3, GL_ARRAY_BUFFER);
        m_st.manage(m_array);
    }

    constexpr const Vec3f& position() const noexcept { return m_position; }
    constexpr Vec3f& position() noexcept { return m_position; }

    constexpr const Quat4f& rotation() const noexcept { return m_rotation; }
    constexpr Quat4f& rotation() noexcept { return m_rotation; }

    constexpr const GLFloatArrayDataServerSRef& vertices() const noexcept { return m_array; }
    constexpr GLFloatArrayDataServerSRef& vertices() noexcept { return m_array; }

    const Vec4f& color() const noexcept { return m_color; }
    void setColor(const Vec4f& c) noexcept { m_color=c; }

    void seal(GL&gl, bool seal) {
        m_array->seal(gl, seal);
    }
    void draw(GL &gl) {
        m_pmvMatUni.pmv().pushMv();
        m_pmvMatUni.pmv().translateMv(m_position); // identity + translate, scaled
        // Rotate shape around its scaled center
        m_pmvMatUni.pmv().rotateMv(m_rotation);
        m_st.send(gl, m_pmvMatUni); // automatic sync + update of Mvi + Mvit

        m_staticColor.vec4f() = m_color;
        m_st.send(gl, m_staticColor);

        m_array->enableBuffer(gl, true);
        ::glDrawArrays(m_type, 0, m_array->elemCount());
        m_array->enableBuffer(gl, false);
        m_pmvMatUni.pmv().popMv();
    }

};

class Primitives01 : public RenderListener {
  private:
    constexpr static jau::math::Vec3f lightPos = jau::math::Vec3f(0.0f, 5.0f, 10.0f);
    constexpr static float half = 0.5f;
    constexpr static float qter = 0.25f;
    constexpr static float zNear=  1.0f;
    constexpr static float zFar =100.0f;

    ShaderState m_st;
    Recti m_viewport;
    bool m_initialized;
    bool m_animating = true;
    jau::fraction_timespec m_tlast;
    Vec4f mgl_ColorStatic = Vec4f(0, 0, 0, 1);
    GLUniformSyncPMVMat4f m_pmvMatUni;
    GLUniformVec4f m_staticColor;
    GLUniformVec3f m_light0Pos;
    Shape m_shape1, m_shape2;
    bool m_once = true;


  public:
    Primitives01()
    : RenderListener(RenderListener::Private()),
      m_initialized(false),
      m_pmvMatUni("gcu_PMVMatrix", PMVData::inv_proj | PMVData::inv_mv | PMVData::inv_tps_mv), // P, Mv, Mvi and Mvit
      m_staticColor("gcu_StaticColor", Vec4f(1, 0, 0, 1)),
      m_light0Pos("gcu_Light0Pos", lightPos),
      m_shape1(GL_TRIANGLE_STRIP, m_st, m_pmvMatUni, m_staticColor),
      m_shape2(GL_LINE_STRIP, m_st, m_pmvMatUni, m_staticColor)
    {
        m_st.manage(m_pmvMatUni);
        m_st.manage(m_staticColor);
        m_st.manage(m_light0Pos);
    }

    Recti& viewport() noexcept { return m_viewport; }
    const Recti& viewport() const noexcept { return m_viewport; }

    PMVMat4f& pmv() noexcept { return m_pmvMatUni.pmv(); }
    const PMVMat4f& pmv() const noexcept { return m_pmvMatUni.pmv(); }
    bool animating() const noexcept { return m_animating; }
    bool& animating() noexcept { return m_animating; }

    bool init(const WindowSRef& 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());
        ShaderCodeSRef vp0 = ShaderCode::create(gl, GL_VERTEX_SHADER, "demos/glsl",
                "demos/glsl/bin", "SingleLight0");
        ShaderCodeSRef fp0 = ShaderCode::create(gl, GL_FRAGMENT_SHADER, "demos/glsl",
                "demos/glsl/bin", "SingleLight0");
        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;
        }
        {
            std::string custom = "#define MAX_TEXTURE_UNITS 0\n";
            size_t vsPos = vp0->defaultShaderCustomization(gl, true, true);
            size_t fsPos = fp0->defaultShaderCustomization(gl, true, true);
            vp0->insertShaderSource(0, vsPos, custom);
            fp0->insertShaderSource(0, fsPos, custom);
        }

        ShaderProgramSRef sp0 = ShaderProgram::create();
        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_pmvMatUni.pmv().getP().loadIdentity();
        m_pmvMatUni.pmv().getMv().loadIdentity();

        const Vec3f frontNormal(0, 0,  1);
        const Vec3f backNormal(0, 0, -1);
        {
            GLFloatArrayDataServerSRef& v = m_shape1.vertices();
            float dz = 0.001f;
            v->put3f(-half,  half, dz); v->put3f(frontNormal);
            v->put3f( half,  half, dz); v->put3f(frontNormal);
            v->put3f(-half, -half, dz); v->put3f(frontNormal);
            v->put3f( half, -half, dz); v->put3f(frontNormal);

            dz = -0.001f;
            v->put3f(-half,  half, dz); v->put3f(backNormal);
            v->put3f( half,  half, dz); v->put3f(backNormal);
            v->put3f(-half, -half, dz); v->put3f(backNormal);
            v->put3f( half, -half, dz); v->put3f(backNormal);
            m_shape1.seal(gl, true);
            m_shape1.setColor(Vec4f(0, 1, 0, 1));
            m_shape1.position().x =  1.5f;
        }
        {
            const float lineWidth = 1/10.0f;
            const float tw = 1.0f;
            const float th = 1.0f;

            float lwh = lineWidth/2.0f;

            float twh = tw/2.0f;
            float thh = th/2.0f;

            float ctrX = 0, ctrY = 0;
            float ctrZ = 0;

            GLFloatArrayDataServerSRef& v = m_shape2.vertices();
            // CCW
            v->put3f(ctrX-lwh, ctrY+thh, ctrZ); v->put3f(frontNormal); // vert: left-top
            v->put3f(ctrX-lwh, ctrY+lwh, ctrZ); v->put3f(frontNormal);
            v->put3f(ctrX-twh, ctrY+lwh, ctrZ); v->put3f(frontNormal); // horz: left-top
            v->put3f(ctrX-twh, ctrY-lwh, ctrZ); v->put3f(frontNormal); // horz: left-bottom
            v->put3f(ctrX-lwh, ctrY-lwh, ctrZ); v->put3f(frontNormal);
            v->put3f(ctrX-lwh, ctrY-thh, ctrZ); v->put3f(frontNormal); // vert: left-bottom
            v->put3f(ctrX+lwh, ctrY-thh, ctrZ); v->put3f(frontNormal); // vert: right-bottom
            v->put3f(ctrX+lwh, ctrY-lwh, ctrZ); v->put3f(frontNormal);
            v->put3f(ctrX+twh, ctrY-lwh, ctrZ); v->put3f(frontNormal); // horz: right-bottom
            v->put3f(ctrX+twh, ctrY+lwh, ctrZ); v->put3f(frontNormal); // horz: right-top
            v->put3f(ctrX+lwh, ctrY+lwh, ctrZ); v->put3f(frontNormal);
            v->put3f(ctrX+lwh, ctrY+thh, ctrZ); v->put3f(frontNormal); // vert: right-top
            v->put3f(ctrX-lwh, ctrY+thh, ctrZ); v->put3f(frontNormal); // vert: left-top
            m_shape2.seal(gl, true);
            m_shape2.setColor(Vec4f(0, 0, 1, 1));
            m_shape2.position().x = -1.5f;
        }

        ::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 WindowSRef& 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 WindowSRef& 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_pmvMatUni.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;
        m_pmvMatUni.pmv().perspectiveP(jau::adeg_to_rad(fovy_deg), aspect2, zNear, zFar);
        m_st.useProgram(gl, true);
        m_st.send(gl, m_pmvMatUni); // automatic sync + update of Mvi + Mvit
        m_st.useProgram(gl, true);
    }

    void display(const WindowSRef& 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_pmvMatUni.pmv().getMv().loadIdentity();
        m_pmvMatUni.pmv().translateMv(0, 0, -5);

        if( animating() ) {
            constexpr double angle_per_sec = 30;
            const float rad = (float) ( (when - m_tlast).to_double() * angle_per_sec );
            m_shape1.rotation().rotateByAngleY(jau::adeg_to_rad(  rad ));
            m_shape2.rotation().rotateByAngleY(jau::adeg_to_rad( -rad ));
        }
        m_shape1.draw(gl);
        m_shape2.draw(gl);

        if( m_once ) {
            m_once = false;
            std::cerr << "XXX draw " << m_st << "\n";
        }

        m_st.useProgram(gl, false);

        m_tlast = when;
    }

    std::string toStringImpl() const noexcept override { return "Primitives01"; }
};

class Example : public Primitives01 {
  private:
    class MyKeyListener : public KeyListener {
      private:
        Primitives01& m_parent;
      public:
        MyKeyListener(Primitives01& p) : m_parent(p) {}

        void keyPressed(KeyEvent& e, const KeyboardTracker& kt) override {
            jau::fprintf_td(e.when().to_ms(), stdout, "KeyPressed: %s; keys %zu\n", e.toString().c_str(), kt.pressedKeyCodes().count());
            if( e.keySym() == VKeyCode::VK_ESCAPE ) {
                WindowSRef win = e.source().lock();
                if( win ) {
                    win->dispose(e.when());
                }
            } else if( e.keySym() == VKeyCode::VK_PAUSE || e.keySym() == VKeyCode::VK_P ) {
                m_parent.animating() = !m_parent.animating();
            } else if( e.keySym() == VKeyCode::VK_W ) {
                WindowSRef win = e.source().lock();
                jau::fprintf_td(e.when().to_ms(), stdout, "Source: %s\n", win ? win->toString().c_str() : "null");
            }
        }
        void keyReleased(KeyEvent& e, const KeyboardTracker& kt) override {
            jau::fprintf_td(e.when().to_ms(), stdout, "KeyRelease: %s; keys %zu\n", e.toString().c_str(), kt.pressedKeyCodes().count());
        }
    };
    typedef std::shared_ptr<MyKeyListener> MyKeyListenerRef;
    MyKeyListenerRef m_kl;

  public:
    Example()
    : Primitives01(),
      m_kl(std::make_shared<MyKeyListener>(*this)) {  }

    bool init(const WindowSRef& win, const jau::fraction_timespec& when) override {
        if( !Primitives01::init(win, when) ) {
            return false;
        }
        win->addKeyListener(m_kl);
        return true;
    }
    void dispose(const WindowSRef& win, const jau::fraction_timespec& when) override {
        win->removeKeyListener(m_kl);
        Primitives01::dispose(win, when);
    }
};

int main(int argc, char *argv[]) // NOLINT(bugprone-exception-escape)
{
    ShaderCode::DEBUG_CODE = true;

    return launch("Primitives01.cpp",
                  GLLaunchProps{.profile=GLProfile(GLProfile::GLES2), .contextFlags=gamp::render::RenderContextFlags::verbose},
                  std::make_shared<Example>(), argc, argv);
}