[gtkmm/gtkmm-3-24] Update the GLArea demo



commit ce33c20732df9f3cbd235a7dd9d4fe69e7256a55
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Fri Dec 13 10:40:59 2019 +0100

    Update the GLArea demo
    
    Similar fixes have been made to the GLArea demo in gtk.
    The replacement of m_Vao by m_Buffer in some methods has been performed
    only in gtk4, but it would be appropriate to do it also in gtk3.

 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 00b71047..3baa68a2 100644
--- a/demos/Makefile.am
+++ b/demos/Makefile.am
@@ -74,8 +74,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 587368e8..912b1157 100644
--- a/demos/gtk-demo/demo.gresource.xml
+++ b/demos/gtk-demo/demo.gresource.xml
@@ -8,8 +8,10 @@
     <file preprocess="to-pixdata">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 b9b7b3b5..e0e73b2b 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();
 };
 
@@ -121,7 +121,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)
   {
@@ -138,7 +142,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)
@@ -257,9 +261,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;
@@ -276,7 +280,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;
@@ -380,7 +384,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..1bfe6433
--- /dev/null
+++ b/demos/gtk-demo/glarea-gles.fs.glsl
@@ -0,0 +1,7 @@
+precision mediump float;
+
+void main() {
+  float lerpVal = gl_FragCoord.y / 500.0;
+
+  gl_FragColor = mix(vec4(1.0, 0.85, 0.35, 1.0), vec4(0.2, 0.2, 0.2, 1.0), 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]