44 "precision highp float;\n"
45 "precision highp int;\n"
47 "#if __VERSION__ >= 130\n"
48 " #define attribute in\n"
49 " #define varying out\n"
52 "uniform mat4 mgl_PMVMatrix[2];\n"
53 "attribute vec4 mgl_Vertex;\n"
54 "attribute vec4 mgl_Color;\n"
55 "varying vec4 frontColor;\n"
59 " frontColor=mgl_Color;\n"
60 " gl_Position = mgl_PMVMatrix[0] * mgl_PMVMatrix[1] * mgl_Vertex;\n"
73 "precision mediump float;\n"
74 "precision mediump int;\n"
76 "#if __VERSION__ >= 130\n"
77 " #define varying in\n"
78 " out vec4 mgl_FragColor;\n"
80 " #define mgl_FragColor gl_FragColor\n"
83 "varying vec4 frontColor;\n"
87 " mgl_FragColor = frontColor;\n"
132 if( !m_initialized ) {
153 GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
154 glShaderSource(vertexShader, 1, &
vertexSource,
nullptr);
155 glCompileShader(vertexShader);
158 GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
160 glCompileShader(fragmentShader);
163 GLuint shaderProgram = glCreateProgram();
164 glAttachShader(shaderProgram, vertexShader);
165 glAttachShader(shaderProgram, fragmentShader);
166 glLinkProgram(shaderProgram);
167 glUseProgram(shaderProgram);
171 u_pmv = glGetUniformLocation(shaderProgram,
"mgl_PMVMatrix");
172 a_vertices = glGetAttribLocation(shaderProgram,
"mgl_Vertex");
173 a_colors = glGetAttribLocation(shaderProgram,
"mgl_Color");
178 glGenBuffers(2, vbos);
181 glBindBuffer(GL_ARRAY_BUFFER, vbos[0]);
184 glVertexAttribPointer(
a_vertices, 3, GL_FLOAT, GL_FALSE, 0,
nullptr);
187 glBindBuffer(GL_ARRAY_BUFFER, vbos[1]);
189 glEnableVertexAttribArray(
a_colors);
190 glVertexAttribPointer(
a_colors, 4, GL_FLOAT, GL_FALSE, 0,
nullptr);
193 m_pmv.getP().loadIdentity();
194 m_pmv.getMv().loadIdentity();
195 m_pmv.translateMv(0, 0, -10);
197 glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
198 glEnable(GL_DEPTH_TEST);
200 m_initialized = 0 != shaderProgram;
201 return m_initialized;
206 m_initialized =
false;
211 if( !m_initialized ) {
214 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
216 m_pmv.getMv().loadIdentity();
217 m_pmv.translateMv(0, 0, -10);
218 static float t_sum_ms = 0;
220 t_sum_ms += float( (when - m_tlast).to_ms() );
223 m_pmv.rotateMv(ang, 0, 0, 1);
224 m_pmv.rotateMv(ang, 0, 1, 0);
227 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
235 m_pmv.getP().loadIdentity();
237 const float aspect = 1.0f;
238 const float fovy_deg=45.0f;
239 const float aspect2 = ( (float) m_viewport.width() / (
float) m_viewport.height() ) / aspect;
240 const float zNear=1.0f;
241 const float zFar=100.0f;
249int main(
int argc,
char *argv[])
251 std::string_view sfile(__FILE__);
252 std::string demo_name = std::string(
"gamp ").append(
jau::fs::basename(sfile,
".cpp"));
253 printf(
"Demo: %s, source %s, exe %s\n", demo_name.c_str(), sfile.data(), argv[0]);
255 int win_width = 1920, win_height = 1000;
256 #if defined(__EMSCRIPTEN__)
257 win_width = 1024, win_height = 576;
260 for(
int i=1; i<argc; ++i) {
261 if( 0 == strcmp(
"-width", argv[i]) && i+1<argc) {
262 win_width = atoi(argv[i+1]);
264 }
else if( 0 == strcmp(
"-height", argv[i]) && i+1<argc) {
265 win_height = atoi(argv[i+1]);
267 }
else if( 0 == strcmp(
"-fps", argv[i]) && i+1<argc) {
281 printf(
"Exit (1): Failed to create window.\n");
285 printf(
"Exit (2): Failed to create context\n");
294 const float a = (float)w / (
float)h;
300 #if defined(__EMSCRIPTEN__)
int main(int argc, char *argv[])
static std::vector< Vec3f > vertices2
static const GLchar * vertexSource
std::shared_ptr< MyKeyListener > MyKeyListenerRef
static std::vector< Vec4f > colors2
static const GLchar * fragmentSource
void keyReleased(KeyEvent &e, const KeyboardTracker &kt) override
A key has been released, excluding auto-repeat modifier keys.
void keyPressed(KeyEvent &e, const KeyboardTracker &kt) override
A key has been pressed, excluding auto-repeat modifier keys.
bool init(const WindowRef &, const jau::fraction_timespec &when) override
Called by the drawable immediately after the render context is initialized.
Recti & viewport() noexcept
void dispose(const WindowRef &, const jau::fraction_timespec &) override
Notifies the listener to perform the release of all renderer resources per context,...
PMVMat4f & pmv() noexcept
void display(const WindowRef &, const jau::fraction_timespec &when) override
Called by the drawable to initiate rendering by the client.
const PMVMat4f & pmv() const noexcept
const Recti & viewport() const noexcept
void reshape(const WindowRef &, const jau::math::Recti &viewport, const jau::fraction_timespec &) override
Called by the drawable during the first repaint after the component has been resized.
bool animating() const noexcept
static GLContext & downcast(RenderContext *rc)
Downcast dereferenced given RenderContext* to GLContext&, throws exception if signature doesn't match...
std::string toString() const override
Specifies the OpenGL profile.
static constexpr std::string_view GLES2
The embedded OpenGL profile ES 2.x, with x >= 0.
constexpr RenderListener(Private) noexcept
Private ctor for shared_ptr<RenderListener> instance method w/o public ctor.
std::string toString() const noexcept
const gamp::render::RenderContext * renderContext() const noexcept
constexpr const Vec2i & surfaceSize() const noexcept
Returns the surface size of the client area excluding insets (window decorations) in pixel units.
bool createContext(const gamp::render::RenderProfile &profile, const gamp::render::RenderContextFlags &contextFlags)
void addRenderListener(const RenderListenerRef &l)
static WindowRef create(const char *title, int wwidth, int wheight, bool verbose=false)
Create an new instance using a native windowing toolkit.
constexpr const Recti & windowBounds() const noexcept
Returns the window client-area top-left position and size excluding insets (window decorations) in wi...
void dispose(const jau::fraction_timespec &when) noexcept override
std::string toString() const noexcept
void addKeyListener(const KeyListenerRef &l)
std::string toString() const noexcept
constexpr VKeyCode keySym() const noexcept
Returns the virtual key symbol reflecting the current keyboard layout.
Listener for multiple KeyEvent.
virtual const PressedKeyCodes & pressedKeyCodes() const noexcept=0
constexpr const WindowWeakPtr & source() const noexcept
constexpr const jau::fraction_timespec & when() const noexcept
size_t bitCount() const noexcept
std::string toString() const noexcept
static constexpr const size_t byte_size
static constexpr const size_t byte_size
SyncMatrices4< value_type, std::is_floating_point_v< float > > SyncMats4
constexpr const value_type * floats() const noexcept
Return the underlying float data buffer.
virtual size_t matrixCount() const noexcept=0
Return the number of Mat4 referenced by matrices()
std::string basename(const std::string_view &path) noexcept
Return stripped leading directory components from given path separated by /.
constexpr T adeg_to_rad(const T arc_degree) noexcept
Converts arc-degree to radians.
fraction_timespec getMonotonicTime() noexcept
Returns current monotonic time since Unix Epoch 00:00:00 UTC on 1970-01-01.
@ verbose
Verbose operations (debugging).
std::shared_ptr< Window > WindowRef
@ VK_ESCAPE
Constant for the ESCAPE function key.
@ VK_PAUSE
Constant for the PAUSE function key.
void mainloop_void() noexcept
Calls mainloop_default(), but exits application if returning false.
bool init_gfx_subsystem(const char *exe_path)
GFX Toolkit: Initialize the subsystem once.
bool mainloop_default() noexcept
Performs the whole tasks for all created gamp::wt::Window instances.
int gpu_forced_fps() noexcept
Returns optional forced frames per seconds or -1 if unset, set via set_gpu_forced_fps().
void set_gpu_forced_fps(int fps) noexcept
Optional forced frames per seconds, pass to swap_gpu_buffer() by default.
PMVMatrix4< float > PMVMat4f
Timespec structure using int64_t for its components in analogy to struct timespec_t on 64-bit platfor...
int printf(const char *format,...)
Operating Systems predefined macros.