Browse Source

Opengl with gtk example

master
blackle 1 year ago
parent
commit
574c83edee
5 changed files with 186 additions and 2 deletions
  1. +4
    -0
      .gitignore
  2. +14
    -2
      Makefile
  3. +154
    -0
      gtk-opengl.c
  4. +14
    -0
      shader.frag
  5. BIN
     

+ 4
- 0
.gitignore View File

@@ -0,0 +1,4 @@
*.elf
gtk-webkit
gtk-opengl
shader.h

+ 14
- 2
Makefile View File

@@ -1,17 +1,29 @@

all : gtk-webkit
all : gtk-webkit gtk-opengl

.PHONY: clean

packer : vondehi/vondehi.asm
cd vondehi; nasm -fbin -o vondehi vondehi.asm

shader.h : shader.frag Makefile
mono ./shader_minifier.exe --preserve-externals shader.frag -o shader.h

# not using `pkg-config --libs` here because it will include too many libs
gtk-webkit.elf : gtk-webkit.c 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

gtk-webkit : gtk-webkit_opt.elf.packed
mv $< $@

gtk-opengl : gtk-opengl_opt.elf.packed
mv $< $@


#all the rest of these rules just takes a compiled elf file and generates a packed version of it with vondehi
%_opt.elf : %.elf Makefile
cp $< $@
strip $@
@@ -38,4 +50,4 @@ gtk-webkit : gtk-webkit_opt.elf.packed
wc -c $@

clean :
-rm *.elf gtk-webkit
-rm *.elf shader.h gtk-webkit

+ 154
- 0
gtk-opengl.c View File

@@ -0,0 +1,154 @@
#define GL_GLEXT_PROTOTYPES why

#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include<stdint.h>

#include <glib.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <GL/gl.h>
#include <GL/glx.h>
#include <GL/glu.h>
#include <GL/glext.h>

#include "shader.h"
static char* vshader = "#version 450\nvec2 y=vec2(1.,-1);\nvec4 x[4]={y.yyxx,y.xyxx,y.yxxx,y.xxxx};void main(){gl_Position=x[gl_VertexID];}";

#define CANVAS_WIDTH 1920
#define CANVAS_HEIGHT 1080
// #define DEBUG

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);

return TRUE;
}

static void on_realize(GtkGLArea *glarea)
{
gtk_gl_area_make_current(glarea);

// compile shader
GLuint f = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(f, 1, &shader_frag, NULL);
glCompileShader(f);

#ifdef DEBUG
GLint isCompiled = 0;
glGetShaderiv(f, GL_COMPILE_STATUS, &isCompiled);
if(isCompiled == GL_FALSE) {
GLint maxLength = 0;
glGetShaderiv(f, GL_INFO_LOG_LENGTH, &maxLength);

char* error = malloc(maxLength);
glGetShaderInfoLog(f, maxLength, &maxLength, error);
printf("%s\n", error);

exit(-10);
}
#endif

GLuint v = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(v, 1, &vshader, NULL);
glCompileShader(v);

#ifdef DEBUG
GLint isCompiled2 = 0;
glGetShaderiv(v, GL_COMPILE_STATUS, &isCompiled2);
if(isCompiled2 == GL_FALSE) {
GLint maxLength = 0;
glGetShaderiv(v, GL_INFO_LOG_LENGTH, &maxLength);

char* error = malloc(maxLength);
glGetShaderInfoLog(v, maxLength, &maxLength, error);
printf("%s\n", error);

exit(-10);
}
#endif

// link shader
p = glCreateProgram();
glAttachShader(p,v);
glAttachShader(p,f);
glLinkProgram(p);

#ifdef DEBUG
GLint isLinked = 0;
glGetProgramiv(p, GL_LINK_STATUS, (int *)&isLinked);
if (isLinked == GL_FALSE) {
GLint maxLength = 0;
glGetProgramiv(p, GL_INFO_LOG_LENGTH, &maxLength);

char* error = malloc(maxLength);
glGetProgramInfoLog(p, maxLength, &maxLength,error);
printf("%s\n", error);

exit(-10);
}
#endif

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);

// 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);
}

void _start() {
asm volatile("sub $8, %rsp\n");

typedef void (*voidWithOneParam)(int*);
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);

g_signal_connect(win, "destroy", gtk_main_quit, NULL);
g_signal_connect(glarea, "realize", G_CALLBACK(on_realize), NULL);
g_signal_connect(glarea, "render", G_CALLBACK(on_render), NULL);

gtk_widget_show_all (win);

gtk_window_fullscreen((GtkWindow*)win);
GdkWindow* window = gtk_widget_get_window(win);
GdkCursor* Cursor = gdk_cursor_new(GDK_BLANK_CURSOR);
gdk_window_set_cursor(window, Cursor);

gtk_main();

asm volatile(".intel_syntax noprefix");
asm volatile("push 231"); //exit_group
asm volatile("pop rax");
// asm volatile("xor edi, edi");
asm volatile("syscall");
asm volatile(".att_syntax prefix");
__builtin_unreachable();
// return 0;
}

+ 14
- 0
shader.frag View File

@@ -0,0 +1,14 @@
#version 450

uniform float iTime;
out vec4 fragColor;

void main()
{
// Normalized pixel coordinates (from -1 to 1)
vec2 iResolution = vec2(1920.0, 1080.0);
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);
}

BIN
View File


Loading…
Cancel
Save