summaryrefslogtreecommitdiff
path: root/tests/poc_c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/poc_c')
-rwxr-xr-xtests/poc_c/compil.sh10
-rw-r--r--tests/poc_c/main.c129
2 files changed, 139 insertions, 0 deletions
diff --git a/tests/poc_c/compil.sh b/tests/poc_c/compil.sh
new file mode 100755
index 0000000..f636aaf
--- /dev/null
+++ b/tests/poc_c/compil.sh
@@ -0,0 +1,10 @@
+#!/bin/bash -e
+CFLAGS="-Wall -g"
+LDFLAGS="-Wall -g"
+
+CFLAGS="$CFLAGS $(pkg-config --cflags gtkglextmm-1.2)"
+LIBS="$(pkg-config --libs gtkglextmm-1.2)"
+
+gcc $CFLAGS -c main.c -o main.o
+gcc $LDFLAGS -o main main.o $LIBS
+
diff --git a/tests/poc_c/main.c b/tests/poc_c/main.c
new file mode 100644
index 0000000..78eaf15
--- /dev/null
+++ b/tests/poc_c/main.c
@@ -0,0 +1,129 @@
+#include <stdlib.h>
+
+#include <gtk/gtk.h>
+#include <gtk/gtkgl.h>
+
+#include <GL/gl.h>
+#include <GL/glu.h>
+
+#include <gdk/x11/gdkglx.h> // X11 specific
+
+gboolean gl_area_on_draw (GtkObject* area, GdkEventExpose* event, gpointer data) {
+ GdkGLDrawable* drawable = gtk_widget_get_gl_drawable (GTK_WIDGET (area));
+ GdkGLContext* context = gtk_widget_get_gl_context (GTK_WIDGET (area));
+
+ gboolean status = gdk_gl_drawable_gl_begin (drawable, context);
+
+ if (status == FALSE) {
+ return FALSE;
+ }
+
+ GtkAllocation allocation;
+ gtk_widget_get_allocation (GTK_WIDGET (area), &allocation);
+ GLdouble viewport_width = (GLdouble) allocation.width;
+ GLdouble viewport_height = (GLdouble) allocation.height;
+
+ // Z negative forward oriented.
+
+ GLdouble aspect = viewport_width / viewport_height;
+ GLdouble fovy = 35.0; // The one which looks to most natural.
+ GLdouble zNear = 2.0; // Enough for a moderately sized sample model.
+ GLdouble zFar = -2.0; // Idem.
+
+ // Z positive forward oriented.
+
+ GLdouble projection_dx = 0.0;
+ GLdouble projection_dy = 0.0;
+ GLdouble projection_dz = -3.0;
+
+ // Reset picture.
+ glViewport (0, 0, (GLint) viewport_width, (GLint) viewport_height);
+ glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+ // Model defined in default coordinates.
+ glMatrixMode (GL_MODELVIEW);
+ glLoadIdentity ();
+
+ // Projection using perspective and a few units backward.
+ glMatrixMode (GL_PROJECTION);
+ glLoadIdentity ();
+ gluPerspective (fovy, aspect, zNear, zFar);
+ glTranslated (projection_dx, projection_dy, projection_dz);
+ // Use the line below instead of the two above, for non perspective view.
+ // glOrtho (-aspect, +aspect, -1.0, +1.0, zNear, zFar);
+
+ // Draw a 1x1 square center on origin and on the x*y plan.
+ // x*y*z, Z negative forward oriented.
+ glBegin (GL_QUADS);
+ glVertex3d ( 0.5, -0.5, 0.0);
+ glVertex3d ( 0.5, 0.5, 0.0);
+ glVertex3d (-0.5, 0.5, 0.0);
+ glVertex3d (-0.5, -0.5, 0.0);
+ glEnd ();
+
+ gdk_gl_drawable_swap_buffers (drawable);
+
+ gdk_gl_drawable_gl_end (drawable);
+
+ return TRUE; // Do not propagate the event.
+}
+
+
+int main(int argc, char *argv[]) {
+ GtkWidget* main_win;
+ GtkVBox* main_box;
+ GtkDrawingArea* gl_area;
+
+ GdkGLConfig* gl_config;
+ GdkGLContext* gl_context;
+ GLXContext glx_context; // X11 specific
+ gboolean res;
+
+ gtk_init(&argc, &argv);
+ if (gdk_gl_init_check(&argc, &argv) == FALSE ) {
+ fputs ("Failed to initialize GDKGLExt.\n", stderr);
+ return EXIT_FAILURE;
+ }
+
+ main_win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
+ gtk_window_set_default_size(GTK_WINDOW(main_win), 400, 300);
+ gtk_window_set_title(GTK_WINDOW(main_win), "GTK OpenCL Proof of Concept");
+ gtk_signal_connect(GTK_OBJECT(main_win), "destroy", G_CALLBACK(gtk_main_quit), NULL);
+
+ main_box = GTK_VBOX(gtk_vbox_new(FALSE, 0));
+ gtk_container_add(GTK_CONTAINER(main_win), GTK_WIDGET(main_box));
+
+ gl_area = GTK_DRAWING_AREA(gtk_drawing_area_new());
+ gl_config = gdk_gl_config_new_by_mode(GDK_GL_MODE_RGBA | GDK_GL_MODE_DOUBLE | GDK_GL_MODE_DEPTH);
+ res = gtk_widget_set_gl_capability(GTK_WIDGET(gl_area), gl_config,
+ /* GdkGLContext *share_list */ NULL,
+ /* gboolean direct */ TRUE,
+ /* int render_type */ GDK_GL_RGBA_TYPE);
+ if ( !res ) {
+ fputs ("Failed to set_gl_capability(gl_area)\n", stderr);
+ return EXIT_FAILURE;
+ }
+ gtk_signal_connect(GTK_OBJECT(gl_area), "expose-event", GTK_SIGNAL_FUNC(gl_area_on_draw), NULL);
+ gtk_box_pack_start(GTK_BOX(main_box), GTK_WIDGET(gl_area), TRUE, TRUE, 0);
+
+ gtk_widget_show_all(main_win);
+
+ /* gl_area needs to be realized to do that (so, it is after gtk_widget_show_all(main_win) )*/
+ gl_context=gtk_widget_get_gl_context(GTK_WIDGET(gl_area));
+ if ( !gl_context) {
+ fputs ("Failed to get_gl_context(gl_area)\n", stderr);
+ return EXIT_FAILURE;
+ }
+
+ /* BEGIN : X11 specific */
+ glx_context=gdk_x11_gl_context_get_glxcontext(gl_context);
+ if ( !glx_context) {
+ fputs ("Failed to gdk_x11_gl_context_get_glxcontext(gl_context)\n", stderr);
+ return EXIT_FAILURE;
+ }
+ printf("glx_context==%p\n", (void *)glx_context);
+ /* END : X11 specific */
+
+ gtk_main();
+ return EXIT_SUCCESS;
+}