GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Oct 31, 2014 7:43 am

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Drawing Area with OpenGL
PostPosted: Wed Jul 16, 2014 9:27 am 
Offline
Familiar Face

Joined: Fri Apr 04, 2014 11:39 am
Posts: 10
Hi everyone,

I want to programmer a drawing area using OpenGL. I saw some GtkGLExt documentation and I seems that Gtk 3.0 doesn´t support this Gtk extension. do know anyone how i can draw with OpenGL into drawing area gtk?

Thank you very much.


Top
 Profile  
 
 Post subject: Re: Drawing Area with OpenGL
PostPosted: Sun Jul 27, 2014 5:52 pm 
Offline
Familiar Face

Joined: Sat Jul 26, 2014 11:43 pm
Posts: 48
I have been working on some OpenGL test code. With GTK3 I have only been able to get OpenGL to work by bringing in Xlib. Using the following code on Ubuntu 12.04 works. A partial solution but I hope it helps.

Eric

Code:
//gcc -Wall testGL.c -o testGL -lGL -lGLU -lX11 -lm `pkg-config --cflags --libs gtk+-3.0 gdk-x11-3.0`

#include<X11/Xlib.h>
#include<GL/glx.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<gtk/gtk.h>
#include<gdk/gdkx.h>
#include<stdio.h>

static GtkWidget *window=NULL;
static GtkWidget *da=NULL;
static GdkWindow *DrawingWindow=NULL;
static Window X_window;
static Display *X_display;
static GLXContext X_context;
static XVisualInfo *X_visual;
static XWindowAttributes X_attributes;
static GLint attributes[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None};
static float ang=0.0;
static guint timer_id;

static void drawGL(GtkWidget *da, gpointer data)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
    glRotatef(ang, 1.0, 1.0, 0.0);
    glShadeModel(GL_FLAT);
 
    //Axis lines
    glBegin(GL_LINES);
    glColor3f(1.0, 0.0, 0.0);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(1.8, 0.0, 0.0);
    glEnd();
   
    glBegin(GL_LINES);
    glColor3f(0.0, 1.0, 0.0);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(0.0, 1.8, 0.0);
    glEnd();
   
    glBegin(GL_LINES);
    glColor3f(1.0, 0.0, 1.0);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(0.0, 0.0, 1.8);
    glEnd();
   
    glPopMatrix ();
    glXSwapBuffers(X_display, X_window);
}
static void configureGL(GtkWidget *da, gpointer data)
{
   printf("Congigure GL\n");
   DrawingWindow=gtk_widget_get_window(GTK_WIDGET(da));

   X_window=gdk_x11_window_get_xid(GDK_WINDOW(DrawingWindow));
   X_display=gdk_x11_get_default_xdisplay();
   X_visual=glXChooseVisual(X_display, 0, attributes);
   X_context=glXCreateContext(X_display, X_visual, NULL, GL_TRUE);

   XGetWindowAttributes(X_display, X_window, &X_attributes);
   glXMakeCurrent(X_display, X_window, X_context);
   XMapWindow(X_display, X_window);
   printf("Viewport %i %i\n", (int)X_attributes.width, (int)X_attributes.height);
   glViewport(0, 0, X_attributes.width, X_attributes.height);
   glOrtho(-10,10,-10,10,-10,10);
   glScalef(5.0, 5.0, 5.0);
}
static gboolean rotate(gpointer data)
{
   ang++;
   gtk_widget_queue_draw_area(GTK_WIDGET(da), 0, 0, 500, 500); 
   return TRUE;
}
void close_program()
{
   //timer can trigger warnings when closing program.
   g_source_remove(timer_id);
   printf("Quit Program\n");
   gtk_main_quit();
}
int main(int argc, char **argv)
{
   gtk_init(&argc, &argv);
   window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
   gtk_window_set_default_size(GTK_WINDOW(window), 500, 500);
   da=gtk_drawing_area_new();
   gtk_widget_set_double_buffered(da, FALSE);

   gtk_container_add(GTK_CONTAINER(window), da);
   g_signal_connect_swapped(window, "destroy", G_CALLBACK(close_program), NULL);

   gtk_widget_show(window);

   g_signal_connect(da, "configure-event", G_CALLBACK(configureGL), NULL);
   g_signal_connect(da, "draw", G_CALLBACK(drawGL), NULL);

   gtk_widget_show_all(window);

   timer_id=g_timeout_add(1000/60, rotate, da);

   gtk_main();
   return 0;
}


Top
 Profile  
 
 Post subject: Re: Drawing Area with OpenGL
PostPosted: Thu Jul 31, 2014 9:28 am 
Offline
Familiar Face

Joined: Fri Apr 04, 2014 11:39 am
Posts: 10
Thank you for your answer.

However I consider that Gtk 3.0 doesn´t have a OpenGL support is very important.

Does Anyone know if it is developping some solution?


Top
 Profile  
 
 Post subject: Re: Drawing Area with OpenGL
PostPosted: Thu Jul 31, 2014 8:40 pm 
Offline
Familiar Face

Joined: Sat Jul 26, 2014 11:43 pm
Posts: 48
You can check this out. It is all that I have been able to find for a gtkglext port to GTK3.

https://github.com/tdz/gtkglext

The examples are for GTK2 though and I wasn't able to get it to work. Maybe just need to do a little more tinkering to figure it out.
I am not an OpenGL expert and got started because I wanted to look at some eigen vectors and values in 3-space. Put together some test code for Glut, which is pretty cool but it doesn't have the great widget collection GTK+ has. I put in a menu and scale in the GTK+ example I posted above and it seems to work good. If interested you can take a look in the Misc folder at

https://github.com/cecashon/OrderedSetVelociRaptor

The file is called gtk3GL.c.
GTK+ is very extensible so usually you can figure out something that will work.


Top
 Profile  
 
 Post subject: Re: Drawing Area with OpenGL
PostPosted: Tue Aug 05, 2014 8:59 am 
Offline
Familiar Face

Joined: Wed Jul 30, 2014 2:43 am
Posts: 9
g_signal_connect(da, "draw", G_CALLBACK(drawGL), NULL);
is the "draw" signal deprecated?


Top
 Profile  
 
 Post subject: Re: Drawing Area with OpenGL
PostPosted: Tue Aug 05, 2014 5:26 pm 
Offline
Familiar Face

Joined: Sat Jul 26, 2014 11:43 pm
Posts: 48
My understanding is the GTK2 signal for the drawing area is "expose-event" and the GTK3 signal is "draw."
The above code compiles with GTK3 version 3.4.2 on Ubuntu 12.04. I don't know that everything is correct with it. Even the OpenGL part might be better done with a different library if you are trying to map and animate millions of points and have a good graphics card. With GTK+ you should be able to compile your code with the needed libraries and get good performance though. Just have to test things out.


Top
 Profile  
 
 Post subject: Re: Drawing Area with OpenGL
PostPosted: Wed Aug 06, 2014 2:07 am 
Offline
Familiar Face

Joined: Wed Jul 30, 2014 2:43 am
Posts: 9
But the "expose-event" just implement once, and if I want to refresh the DrawArea as the OpenGL/Direct3D is involved there, So I add the actual draw function to the g_idel_add or g_timeout_add. Is it OK? or is there any good ways to do it?
Thank U!


Top
 Profile  
 
 Post subject: Re: Drawing Area with OpenGL
PostPosted: Wed Aug 06, 2014 4:42 pm 
Offline
Familiar Face

Joined: Sat Jul 26, 2014 11:43 pm
Posts: 48
The OpenGL and DirectX libraries basically are doing your linear algebra mappings of your points on the screen. Since each coordinate point gets mapped to a new position, a graphics card can parallelize the calculations in hardware and do the mappings very fast. So you need a bunch of 3-space points supplied by your program that can be drawn to the screen handed off to the graphics card.
Todays computers are very fast and unless you have a lot of points to draw, the grapics card calculations should be much faster than the screen refresh rate. If you have a lot of points you probably want to use a compiled language for performance reasons. With GTK+ you are just triggering a screen redraw at the rate you want. For a CAD/CAM program you may not need rapid redraws. For a game you want your redraws at around or a little above the screen refresh rate to keep it smooth.
The g_timeout_add probably should just trigger a gtk_widget_queue_draw_area which triggers the draw event. That way you can say if you want the screen redrawn or not. For example, I might want to stop the animation and movement of the drawing on the screen or rotate it degree by degree.
On https://github.com/cecashon/OrderedSetVelociRaptor in the Misc folder there is a file called plate_stack_viewGL.c that I have been playing around with trying to get a 3d view of microtiter plates. You can start and stop the movement of the drawing with a click event.
I am still trying to figure out how a lot of this works and have been having a little trouble with having more global varibles than I would like. I got started with the OpenGL because I wanted to take a look at some data arrangements in 3-space. Still learning how it works though. Definitly not an expert. I hope this helps.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC


Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group