Search code examples
user-interfaceopenglrenderer

Adding window options to my openGL renderer window


I've been spending like 3 hours installing GTK and it works but I don't know how to add it to the openGL renderer and I don't find any good tutorial with a simple example... Could someone tell me a good library to use and easy to install? I don't think it's difficult but I can't find anything that helps me. This is what I want http://gyazo.com/543b66d1d1da8c9e05bda62d2663778f I have everything in openGL now my objetive at the moment is something like this: http://tombraiders.net/stella/images/TRLE/trle_screenshot_lg.jpg But more simple to start... Thanks for read. PD: I don't want CEGUI because it is overlapped and it doesn't work for me...


Solution

  • I suppose you have version 3.16 of gtk. You need to use the gtk opengl renderer, insinde your gtk code, using a GtkGLArea widget. You can build your interface with Glade, but i don't know if last version already supports GtkGLArea.

    The gtk3 reference manual gives a small example of code. Here is a small tutorial: tutorial and the files of the example. Another simple code on this page.

    You can test the following code, to display a triangle, but i didn't tested it, since GtkGLArea doesn't work on my mingw system, but it may help you anyway, you may need to pass -lopengl or -lopengl32 or -lepoxy to compile:

    #include <gtk/gtk.h>
    #include <gdk/gdk.h>
    #include <GL/gl.h>
    #include <GL/glu.h>
    
    static gboolean
    render (GtkGLArea *area, GdkGLContext *context) {
      // inside this function it's safe to use GL; the given
      // #GdkGLContext has been made current to the drawable
      // surface used by the #GtkGLArea and the viewport has
      // already been set to be the size of the allocation
    
      // we can start by clearing the buffer
      glClearColor (0, 0, 0, 0);
      glClear (GL_COLOR_BUFFER_BIT);
    
      // draw your object
      // draw_an_object ();
      glBegin(GL_TRIANGLES);
        glColor3ub(255,0,0);    glVertex2d(-0.75,-0.75);
        glColor3ub(0,255,0);    glVertex2d(0,0.75);
        glColor3ub(0,0,255);    glVertex2d(0.75,-0.75);
      glEnd();
    
      glFlush();
    
      // we completed our drawing; the draw commands will be
      // flushed at the end of the signal emission chain, and
      // the buffers will be drawn on the window
      return TRUE;
    }
    
    GtkWidget * create_window1() {
      GtkWidget * window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      gtk_window_set_title (GTK_WINDOW (window1), "test gtk3");
      gtk_window_set_has_resize_grip(GTK_WINDOW (window1), false);
    
      GtkWidget * vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
      gtk_widget_show (vbox);
      gtk_container_add (GTK_CONTAINER (window1), vbox);
    
      // create a GtkGLArea instance:
      GtkWidget *gl_area = gtk_gl_area_new ();
      gtk_widget_show (gl_area);
      // add it to the vbox:
      gtk_container_add (GTK_CONTAINER (vbox), gl_area);
    
      // connect to the "render" signal:
      g_signal_connect (gl_area, "render", G_CALLBACK (render), NULL);
    
      return window1;
    }
    
    
    
    int main(int argc, char *argv[]) {
      gtk_init (&argc, &argv);
    
      GtkWidget * window1 = create_window1 ();
      gtk_widget_show (window1);
      g_signal_connect ((gpointer) window1, "destroy", G_CALLBACK(gtk_main_quit), NULL);
    
      gtk_main ();
    }