[gtkmm] Update the GLArea demo



commit 22da8dd537ca464dbe474779c2a2a759b51b4224
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Fri Dec 13 10:30:46 2019 +0100

    Update the GLArea demo
    
    Similar fixes have been made to the GLArea demo in gtk4.

 demos/Makefile.am                                      |  6 ++++--
 demos/gtk-demo/demo.gresource.xml                      |  6 ++++--
 demos/gtk-demo/example_glarea.cc                       | 18 +++++++++++-------
 .../{glarea-fragment.glsl => glarea-gl.fs.glsl}        |  0
 .../gtk-demo/{glarea-vertex.glsl => glarea-gl.vs.glsl} |  0
 demos/gtk-demo/glarea-gles.fs.glsl                     |  7 +++++++
 demos/gtk-demo/glarea-gles.vs.glsl                     |  7 +++++++
 7 files changed, 33 insertions(+), 11 deletions(-)
---
diff --git a/demos/Makefile.am b/demos/Makefile.am
index ba3b8939..0fa482de 100644
--- a/demos/Makefile.am
+++ b/demos/Makefile.am
@@ -73,8 +73,10 @@ GTK_DEMO_RESOURCES = \
   gtk-demo/apple-red.png \
   gtk-demo/background.jpg \
   gtk-demo/floppybuddy.gif \
-  gtk-demo/glarea-fragment.glsl \
-  gtk-demo/glarea-vertex.glsl \
+  gtk-demo/glarea-gl.fs.glsl \
+  gtk-demo/glarea-gl.vs.glsl \
+  gtk-demo/glarea-gles.fs.glsl \
+  gtk-demo/glarea-gles.vs.glsl \
   gtk-demo/gnome-applets.png \
   gtk-demo/gnome-calendar.png \
   gtk-demo/gnome-foot.png \
diff --git a/demos/gtk-demo/demo.gresource.xml b/demos/gtk-demo/demo.gresource.xml
index f66cb847..b800b455 100644
--- a/demos/gtk-demo/demo.gresource.xml
+++ b/demos/gtk-demo/demo.gresource.xml
@@ -8,8 +8,10 @@
     <file>gnome-fs-regular.png</file>
   </gresource>
   <gresource prefix="/glarea">
-    <file>glarea-fragment.glsl</file>
-    <file>glarea-vertex.glsl</file>
+    <file>glarea-gl.fs.glsl</file>
+    <file>glarea-gl.vs.glsl</file>
+    <file>glarea-gles.fs.glsl</file>
+    <file>glarea-gles.vs.glsl</file>
   </gresource>
   <gresource prefix="/images">
     <file>alphatest.png</file>
diff --git a/demos/gtk-demo/example_glarea.cc b/demos/gtk-demo/example_glarea.cc
index b6f4d8a6..08597a29 100644
--- a/demos/gtk-demo/example_glarea.cc
+++ b/demos/gtk-demo/example_glarea.cc
@@ -57,7 +57,7 @@ protected:
 
   Gtk::Box* create_axis_slider_box(int axis);
   void init_buffers();
-  void init_shaders();
+  void init_shaders(const std::string& vertex_path, const std::string& fragment_path);
   void draw_triangle();
 };
 
@@ -119,7 +119,11 @@ void Example_GLArea::realize()
   {
     m_GLArea.throw_if_error();
     init_buffers();
-    init_shaders();
+
+    const bool use_es = m_GLArea.get_context()->get_use_es();
+    const std::string vertex_path = use_es ? "/glarea/glarea-gles.vs.glsl" : "/glarea/glarea-gl.vs.glsl";
+    const std::string fragment_path = use_es ? "/glarea/glarea-gles.fs.glsl" : "/glarea/glarea-gl.fs.glsl";
+    init_shaders(vertex_path, fragment_path);
   }
   catch(const Gdk::GLError& gle)
   {
@@ -136,7 +140,7 @@ void Example_GLArea::unrealize()
     m_GLArea.throw_if_error();
 
     // Delete buffers and program
-    glDeleteBuffers(1, &m_Vao);
+    glDeleteBuffers(1, &m_Buffer);
     glDeleteProgram(m_Program);
   }
   catch(const Gdk::GLError& gle)
@@ -255,9 +259,9 @@ static GLuint create_shader(int type, const char *src)
   return shader;
 }
 
-void Example_GLArea::init_shaders()
+void Example_GLArea::init_shaders(const std::string& vertex_path, const std::string& fragment_path)
 {
-  auto vshader_bytes = Gio::Resource::lookup_data_global("/glarea/glarea-vertex.glsl");
+  auto vshader_bytes = Gio::Resource::lookup_data_global(vertex_path);
   if(!vshader_bytes)
   {
     cerr << "Failed fetching vertex shader resource" << endl;
@@ -274,7 +278,7 @@ void Example_GLArea::init_shaders()
     return;
   }
 
-  auto fshader_bytes = Gio::Resource::lookup_data_global("/glarea/glarea-fragment.glsl");
+  auto fshader_bytes = Gio::Resource::lookup_data_global(fragment_path);
   if(!fshader_bytes)
   {
     cerr << "Failed fetching fragment shader resource" << endl;
@@ -378,7 +382,7 @@ void Example_GLArea::draw_triangle()
 
   glUniformMatrix4fv(m_Mvp, 1, GL_FALSE, &mvp[0]);
 
-  glBindBuffer(GL_ARRAY_BUFFER, m_Vao);
+  glBindBuffer(GL_ARRAY_BUFFER, m_Buffer);
   glEnableVertexAttribArray(0);
   glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, nullptr);
 
diff --git a/demos/gtk-demo/glarea-fragment.glsl b/demos/gtk-demo/glarea-gl.fs.glsl
similarity index 100%
rename from demos/gtk-demo/glarea-fragment.glsl
rename to demos/gtk-demo/glarea-gl.fs.glsl
diff --git a/demos/gtk-demo/glarea-vertex.glsl b/demos/gtk-demo/glarea-gl.vs.glsl
similarity index 100%
rename from demos/gtk-demo/glarea-vertex.glsl
rename to demos/gtk-demo/glarea-gl.vs.glsl
diff --git a/demos/gtk-demo/glarea-gles.fs.glsl b/demos/gtk-demo/glarea-gles.fs.glsl
new file mode 100644
index 00000000..39f71439
--- /dev/null
+++ b/demos/gtk-demo/glarea-gles.fs.glsl
@@ -0,0 +1,7 @@
+precision highp float;
+
+void main() {
+  float lerpVal = gl_FragCoord.y / 500.0f;
+
+  gl_FragColor = mix(vec4(1.0f, 0.85f, 0.35f, 1.0f), vec4(0.2f, 0.2f, 0.2f, 1.0f), lerpVal);
+}
diff --git a/demos/gtk-demo/glarea-gles.vs.glsl b/demos/gtk-demo/glarea-gles.vs.glsl
new file mode 100644
index 00000000..63064da4
--- /dev/null
+++ b/demos/gtk-demo/glarea-gles.vs.glsl
@@ -0,0 +1,7 @@
+attribute vec4 position;
+
+uniform mat4 mvp;
+
+void main() {
+  gl_Position = mvp * position;
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]