aboutsummaryrefslogtreecommitdiffstats
path: root/examples/redsquare01.cpp
blob: 901390d7cf2cd4d40310b03cb5e7fe780cf39acf (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
/*
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 2022 Gothel Software e.K.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include <gamp/gamp.hpp>

#include <random>
#include <cstdio>
#include <cmath>
#include <iostream>

#include <GLES2/gl2.h>
#include <SDL2/SDL_opengles2.h>

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

// Vertex shader
static GLint u_pmv;
static GLint a_vertices, a_colors;

static_assert(sizeof(GLfloat) == sizeof(float) );

// 2 <= ES < 3: #version 100
// ES >= 3: #version 300 es
//
// ES2/ES3 precision:
//   precision highp float;
//   precision highp int;
static const GLchar* vertexSource =
    "#version 100\n"
    "precision highp float;\n"
    "precision highp int;\n"
    "\n"
    "#if __VERSION__ >= 130\n"
    "  #define attribute in\n"
    "  #define varying out\n"
    "#endif\n"
    "\n"
    "uniform mat4    mgl_PMVMatrix[2];\n"
    "attribute vec4    mgl_Vertex;\n"
    "attribute vec4    mgl_Color;\n"
    "varying vec4    frontColor;\n"
    "\n"
    "void main(void)\n"
    "{\n"
    "  frontColor=mgl_Color;\n"
    "  gl_Position = mgl_PMVMatrix[0] * mgl_PMVMatrix[1] * mgl_Vertex;\n"
    "}\n";

// Fragment/pixel shader
//
// ES2 precision
//   precision mediump float;
//   precision mediump int;
// ES3 precision:
//   precision highp float;
//   precision highp int;
static const GLchar* fragmentSource =
    "#version 100\n"
    "precision mediump float;\n"
    "precision mediump int;\n"
    "\n"
    "#if __VERSION__ >= 130\n"
    "  #define varying in\n"
    "  out vec4 mgl_FragColor;\n"
    "#else\n"
    "  #define mgl_FragColor gl_FragColor\n"   
    "#endif\n"
    "\n"
    "varying vec4 frontColor;\n"
    "\n"
    "void main (void)\n"
    "{\n"
    "    mgl_FragColor = frontColor;\n"
    "}\n";

static std::vector<Vec3f> vertices2 = { Vec3f(-2,  2, 0),
                                        Vec3f( 2,  2, 0),
                                        Vec3f(-2, -2, 0),
                                        Vec3f( 2, -2, 0) };

static std::vector<Vec4f> colors2 = { Vec4f( 1, 0, 0, 1),
                                      Vec4f( 0, 0, 1, 1),
                                      Vec4f( 1, 0, 0, 1),
                                      Vec4f( 1, 0, 0, 1) };

class RenderContext {
  public:
    typedef bool (*init_func)(RenderContext& pmv);
    
  private:    
    Recti& m_viewport;
    PMVMat4f m_pmv;
    bool m_initialized;
    
  public:    
    RenderContext(init_func init)
    : m_viewport(gamp::viewport), m_pmv(), m_initialized(false) {
        m_initialized = init(*this);
    }
    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 initialized() const noexcept { return m_initialized; }
};

void updatePMv(const PMVMat4f& pmv)
{
    const PMVMat4f::SyncMats4& spmv = pmv.getSyncPMv();
    glUniformMatrix4fv(u_pmv, (GLsizei)spmv.matrixCount(), false, spmv.floats());
}

void reshape(RenderContext& rc) {
    rc.pmv().getP().loadIdentity();

    const float aspect = 1.0f;
    const float fovy_deg=45.0f;
    const float aspect2 = ( (float) rc.viewport().width() / (float) rc.viewport().height() ) / aspect;
    const float zNear=1.0f;
    const float zFar=100.0f;
    rc.pmv().perspectiveP(gamp::adeg_to_rad(fovy_deg), aspect2, zNear, zFar);
    
    updatePMv(rc.pmv());
}

bool initialize(RenderContext& rc)
{
    // Create and compile vertex shader
    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexSource, nullptr);
    glCompileShader(vertexShader);

    // Create and compile fragment shader
    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentSource, nullptr);
    glCompileShader(fragmentShader);

    // Link vertex and fragment shader into shader program and use it
    GLuint shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);
    glUseProgram(shaderProgram);

    // Get shader variables and initialize them
    
    u_pmv = glGetUniformLocation(shaderProgram, "mgl_PMVMatrix");
    a_vertices = glGetAttribLocation(shaderProgram, "mgl_Vertex");
    a_colors = glGetAttribLocation(shaderProgram, "mgl_Color");
    
    {
        // Create vertex buffer object and copy vertex data into it
        GLuint vbos[2];
        glGenBuffers(2, vbos);
        
        // Specify the layout of the shader vertex data (positions only, 3 floats)
        glBindBuffer(GL_ARRAY_BUFFER, vbos[0]);
        glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(vertices2.size() * Vec3f::byte_size), vertices2.data(), GL_STATIC_DRAW);
        glEnableVertexAttribArray(a_vertices);
        glVertexAttribPointer(a_vertices, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
        
        // Specify the layout of the shader vertex data (positions only, 4 floats)
        glBindBuffer(GL_ARRAY_BUFFER, vbos[1]);
        glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(colors2.size() * Vec4f::byte_size), colors2.data(), GL_STATIC_DRAW);
        glEnableVertexAttribArray(a_colors);
        glVertexAttribPointer(a_colors, 4, GL_FLOAT, GL_FALSE, 0, nullptr);
    }
        
    PMVMat4f& pmv = rc.pmv();
    pmv.getP().loadIdentity();
    pmv.getMv().loadIdentity();
    pmv.translateMv(0, 0, -10);
    reshape(rc);    

    glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
    glEnable(GL_DEPTH_TEST);
    
    return shaderProgram;
}

void mainloop() {
    static uint64_t t_sum = 0;
    static uint64_t t_last = jau::environment::getElapsedMillisecond(); // [ms]
    static gamp::input_event_t event;
    static RenderContext renderContext(initialize);

    gamp::handle_events(event);
    if( event.pressed_and_clr( gamp::input_event_type_t::WINDOW_CLOSE_REQ ) ) {
        printf("Exit Application\n");
        #if defined(__EMSCRIPTEN__)
            emscripten_cancel_main_loop();
        #else
            exit(0);
        #endif
    } else if( event.pressed_and_clr( gamp::input_event_type_t::WINDOW_RESIZED ) ) {
        reshape(renderContext);
    }
    const bool animating = !event.paused();

    const uint64_t t1 = jau::environment::getElapsedMillisecond(); // [ms]
    const uint64_t dt = animating ? t1 - t_last : 0; // [ms]
    t_sum += dt;
    t_last = t1;

    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    
        
        PMVMat4f& pmv = renderContext.pmv();
        pmv.getMv().loadIdentity();
        pmv.translateMv(0, 0, -10);
        
        const float ang = gamp::adeg_to_rad(static_cast<float>(t_sum) * 360.0f) / 4000.0f;
        pmv.rotateMv(ang, 0, 0, 1);
        pmv.rotateMv(ang, 0, 1, 0);
            
        updatePMv(pmv);
        
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    }

    gamp::swap_gpu_buffer();
}

int main(int argc, char *argv[])
{
    printf("%s\n", jau::os::get_platform_info().c_str());
    
    int win_width = 1920, win_height = 1000;
    #if defined(__EMSCRIPTEN__)
        win_width = 1024, win_height = 576; // 16:9
    #endif
    {
        for(int i=1; i<argc; ++i) {
            if( 0 == strcmp("-width", argv[i]) && i+1<argc) {
                win_width = atoi(argv[i+1]);
                ++i;
            } else if( 0 == strcmp("-height", argv[i]) && i+1<argc) {
                win_height = atoi(argv[i+1]);
                ++i;
            } else if( 0 == strcmp("-fps", argv[i]) && i+1<argc) {
                gamp::forced_fps = atoi(argv[i+1]);
                ++i;
            }
        }
        printf("-fps: %d\n", gamp::forced_fps);
    }
    gamp::set_gpu_stats_show(true);
    
    if( !gamp::init_gfx_subsystem("gamp example01", win_width, win_height, true /* vsync */) ) {
        printf("Exit...");
        return 1;
    }    
    {
        const int w = gamp::viewport.width();
        const int h = gamp::viewport.width();
        const float a = (float)w / (float)h;
        printf("FB %d x %d [w x h], aspect %f [w/h]; Win %d x %d\n", w, h, a, gamp::win_width, gamp::win_height);
    }

    #if defined(__EMSCRIPTEN__)
        emscripten_set_main_loop(mainloop, 0, 1);
    #else
        while( true ) { mainloop(); }
    #endif
}