Remove iTime from shaders, render each only once

This commit is contained in:
blackle 2019-04-02 10:05:02 -04:00
parent 9508ba5fde
commit 9a9d9a31b3
5 changed files with 17 additions and 25 deletions

View File

@ -14,7 +14,7 @@ gtk-webkit.elf : gtk-webkit.c index.html.inc Makefile
gcc -o $@ $< `pkg-config --cflags webkit2gtk-4.0` -lgobject-2.0 -lgtk-3 -lwebkit2gtk-4.0 -no-pie -fno-plt -Os -std=gnu11 -nostartfiles -nostdlib
gtk-opengl.elf : gtk-opengl.c shader.h Makefile
gcc -o $@ $< `pkg-config --cflags gtk+-3.0` -lglib-2.0 -lGL -lgtk-3 -lgdk-3 -lgobject-2.0 -no-pie -fno-plt -Os -std=gnu11 -nostartfiles -nostdlib
gcc -o $@ $< `pkg-config --cflags gtk+-3.0` -lGL -lgtk-3 -lgdk-3 -lgobject-2.0 -no-pie -fno-plt -Os -std=gnu11 -nostartfiles -nostdlib
xlib-opengl.elf : xlib-opengl.c shader.h Makefile
gcc -o $@ $< -lX11 -lGL -lcairo -lXrandr -no-pie -fno-plt -Os -std=gnu11 -nostartfiles -nostdlib

View File

@ -1,15 +1,15 @@
# Linux-OpenGL-Examples
Some code examples for opening windows for various purposes in very few bytes. Unfortunately each example does its own thing, so comparing the sizes is an apple to oranges thing.
Some code examples for opening windows for various purposes in very few bytes. The examples mostly do the same thing, the only difference being gtk-webkit uses a different shader source, and is also heavily unoptimized.
## gtk-opengl - 1585 bytes
## gtk-opengl - 1292 bytes
Example code for opening a glsl shader fullscreen with gtk. Closes with the standard ALT+F4 on Ubuntu. Shaders get passed an iTime uniform in seconds. App closes automatically after 10 seconds.
Example code for opening a glsl shader fullscreen with gtk. Closes with the standard ALT+F4 on Ubuntu. Renders the shader once on load.
## gtk-webkit - 1482 bytes
Example code for opening an html page fullscreen with gtk. Closes with the standard ALT+F4 on Ubuntu. WebGL is enabled and initialized to display a shader full screen with some very unoptimized javascript. Optimizing the js for size will probably take off another 100 bytes or two.
Example code for opening a webgl enabled html page fullscreen with gtk and webkit. Closes with the standard ALT+F4 on Ubuntu. WebGL is used to display a shader full screen with some very unoptimized javascript. Like gtk-opengl, the shader is rendered once. Optimizing the js for size will probably take off another 100 bytes or two.
## xlib-opengl - 1498 bytes
## xlib-opengl - 1474 bytes
Example code for opening a glsl shader fullscreen with vanilla xlib. Unlike gtk-opengl, this does not pass in an iTime or render the shader more than once, and also must be closed with ESC. This code is based on the code for Cenotaph For Soda, a 4k gfx demo for Revision 2018. OPINION: xlib feels the most fragile of the bunch here...
Example code for opening a glsl shader fullscreen with vanilla xlib. Like gtk-opengl and gtk-webkit, the shader is rendered once. Unlike gtk-opengl, the window must be closed with ESC. This code is based on the code for Cenotaph For Soda, a 4k gfx demo for Revision 2018. OPINION: xlib feels the most fragile of the bunch here...

View File

@ -22,17 +22,11 @@ static char* vshader = "#version 450\nvec2 y=vec2(1.,-1);\nvec4 x[4]={y.yyxx,y.x
GLuint vao;
GLuint p;
GTimer* gtimer;
static gboolean
on_render (GtkGLArea *glarea, GdkGLContext *context)
{
float itime = g_timer_elapsed(gtimer, NULL);
if (itime > 10.0) gtk_main_quit();
glUseProgram(p);
glUniform1f(0, itime);
glBindVertexArray(vao);
glVertexAttrib1f(0, 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
@ -106,15 +100,16 @@ static void on_realize(GtkGLArea *glarea)
glGenVertexArrays(1, &vao);
GdkGLContext *context = gtk_gl_area_get_context(glarea);
GdkWindow *glwindow = gdk_gl_context_get_window(context);
GdkFrameClock *frame_clock = gdk_window_get_frame_clock(glwindow);
// if you want to continuously render the shader once per frame
// GdkGLContext *context = gtk_gl_area_get_context(glarea);
// GdkWindow *glwindow = gdk_gl_context_get_window(context);
// GdkFrameClock *frame_clock = gdk_window_get_frame_clock(glwindow);
// Connect update signal:
g_signal_connect_swapped(frame_clock, "update", G_CALLBACK(gtk_gl_area_queue_render), glarea);
// // Connect update signal:
// g_signal_connect_swapped(frame_clock, "update", G_CALLBACK(gtk_gl_area_queue_render), glarea);
// Start updating:
gdk_frame_clock_begin_updating(frame_clock);
// // Start updating:
// gdk_frame_clock_begin_updating(frame_clock);
}
void _start() {
@ -124,8 +119,6 @@ void _start() {
voidWithOneParam gtk_init_one_param = (voidWithOneParam)gtk_init;
(*gtk_init_one_param)(NULL);
gtimer = g_timer_new();
GtkWidget *win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
GtkWidget *glarea = gtk_gl_area_new();
gtk_container_add(GTK_CONTAINER(win), glarea);

View File

@ -36,7 +36,7 @@ void _start() {
GtkWidget *win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
WebKitSettings *webSettings = webkit_settings_new_with_settings("enable-webgl", TRUE, NULL);
WebKitWebView *webView = WEBKIT_WEB_VIEW(webkit_web_view_new_with_settings(webSettings));
webkit_web_view_load_html(webView, html, NULL);
webkit_web_view_load_html(webView, html, NULL);
gtk_container_add(GTK_CONTAINER(win), GTK_WIDGET(webView));

View File

@ -1,6 +1,5 @@
#version 450
uniform float iTime;
out vec4 fragColor;
void main()
@ -10,5 +9,5 @@ void main()
vec2 uv = (gl_FragCoord.xy/iResolution.xy)*2.0 - vec2(1.0,1.0);
uv.y *= iResolution.y/iResolution.x;
fragColor = abs(uv.yxyx + sin(iTime)/2.0);
fragColor = abs(uv.yxyx);
}