[retro-gtk/wip/aplazas/gl-display: 8/10] colorful blinking triangle
- From: Adrien Plazas <aplazas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [retro-gtk/wip/aplazas/gl-display: 8/10] colorful blinking triangle
- Date: Mon, 13 Nov 2017 14:11:50 +0000 (UTC)
commit 71eecc85da0d727c85186f1740ad435db4e9849c
Author: Adrien Plazas <kekun plazas laposte net>
Date: Mon Nov 13 11:49:22 2017 +0100
colorful blinking triangle
.editorconfig | 6 ++++++
retro-gtk/retro-gl-display-fragment.glsl | 14 ++++++++++----
retro-gtk/retro-gl-display-vertex.glsl | 14 ++++++++++----
retro-gtk/retro-gl-display.c | 30 ++++++++++++++++++++++++++++++
4 files changed, 56 insertions(+), 8 deletions(-)
---
diff --git a/.editorconfig b/.editorconfig
index 14415a2..58edaa8 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -11,6 +11,12 @@ tab_size = 2
indent_style = space
max_line_length = 80
+[*.glsl]
+indent_size = 2
+tab_size = 2
+indent_style = space
+max_line_length = 80
+
[*.{ui,xml,modules}]
indent_size = 2
tab_size = 2
diff --git a/retro-gtk/retro-gl-display-fragment.glsl b/retro-gtk/retro-gl-display-fragment.glsl
index f70065a..8747d24 100644
--- a/retro-gtk/retro-gl-display-fragment.glsl
+++ b/retro-gtk/retro-gl-display-fragment.glsl
@@ -1,6 +1,12 @@
-#version 400
+#version 150 core
-out vec4 frag_colour;
-void main() {
- frag_colour = vec4(0.5, 0.0, 0.5, 1.0);
+in vec3 Color;
+
+out vec4 outColor;
+
+uniform float triangleColor;
+
+void main ()
+{
+ outColor = vec4 (Color * triangleColor, 1.0);
}
diff --git a/retro-gtk/retro-gl-display-vertex.glsl b/retro-gtk/retro-gl-display-vertex.glsl
index d2107ad..aa8c092 100644
--- a/retro-gtk/retro-gl-display-vertex.glsl
+++ b/retro-gtk/retro-gl-display-vertex.glsl
@@ -1,6 +1,12 @@
-#version 400
+#version 150 core
-in vec3 vp;
-void main() {
- gl_Position = vec4(vp, 1.0);
+in vec2 position;
+in vec3 color;
+
+out vec3 Color;
+
+void main ()
+{
+ Color = color;
+ gl_Position = vec4 (position, 0.0, 1.0);
}
diff --git a/retro-gtk/retro-gl-display.c b/retro-gtk/retro-gl-display.c
index ebbcf0b..9b1e448 100644
--- a/retro-gtk/retro-gl-display.c
+++ b/retro-gtk/retro-gl-display.c
@@ -69,9 +69,17 @@ retro_gl_display_get_video_box (RetroGLDisplay *self,
*y = (h - *height) / 2;
}
+float vertices[] = {
+ 0.0f, 0.5f, 1.0f, 0.0f, 0.0f, // Vertex 1: Red
+ 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // Vertex 2: Green
+ -0.5f, -0.5f, 0.0f, 0.0f, 1.0f // Vertex 3: Blue
+};
+
static void
retro_gl_display_realize (RetroGLDisplay *self)
{
+ GLuint vertex_buffer_object;
+ GLuint vertex_array_object;
GLuint vertex_shader;
GLuint fragment_shader;
GLuint shader_program;
@@ -80,6 +88,13 @@ retro_gl_display_realize (RetroGLDisplay *self)
gtk_gl_area_make_current (GTK_GL_AREA (self));
+ glGenBuffers (1, &vertex_buffer_object);
+ glBindBuffer (GL_ARRAY_BUFFER, vertex_buffer_object);
+ glBufferData (GL_ARRAY_BUFFER, sizeof (vertices), vertices, GL_STATIC_DRAW);
+
+ glGenVertexArrays (1, &vertex_array_object);
+ glBindVertexArray (vertex_array_object);
+
// Prepare the vertex shader
shader_source_bytes = g_resources_lookup_data ("/org/gnome/Retro/retro-gl-display-vertex.glsl", 0, NULL);
shader_source = g_bytes_get_data (shader_source_bytes, NULL);
@@ -102,6 +117,17 @@ retro_gl_display_realize (RetroGLDisplay *self)
glAttachShader (shader_program, fragment_shader);
glLinkProgram (shader_program);
+ glUseProgram (shader_program);
+ GLint position_attrib = glGetAttribLocation (shader_program, "position");
+ glVertexAttribPointer (position_attrib, 2, GL_FLOAT, GL_FALSE,
+ 5 * sizeof (float), 0);
+ glEnableVertexAttribArray (position_attrib);
+
+ GLint colAttrib = glGetAttribLocation (shader_program, "color");
+ glEnableVertexAttribArray (colAttrib);
+ glVertexAttribPointer (colAttrib, 3, GL_FLOAT, GL_FALSE,
+ 5 * sizeof (float), (void *) (2 * sizeof(float)));
+
self->shader_program = shader_program;
}
@@ -141,6 +167,8 @@ retro_gl_display_render (RetroGLDisplay *self)
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram (self->shader_program);
+ GLint uniColor = glGetUniformLocation (self->shader_program, "triangleColor");
+ glUniform1f (uniColor, (g_get_monotonic_time () % 1000000) / 1000000.0f);
glBindFramebuffer (GL_READ_FRAMEBUFFER, self->framebuffer);
glBlitFramebuffer (0, 0,
@@ -151,6 +179,8 @@ retro_gl_display_render (RetroGLDisplay *self)
filter);
glBindFramebuffer (GL_READ_FRAMEBUFFER, 0);
+ glDrawArrays (GL_TRIANGLES, 0, 3);
+
return FALSE;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]