Re: [GtkGLExt] troubles with my first OpenGL app



Thu, 03 Aug 2006, 19:36 jesus ruiz wrote:
> Thank you very much! :D
>
> Now everything works fine.
The changes, that I proposed, still don't make the code 100% correct.
Let's try to figure out how the error-free version should look like.

There are some essential points that you are probably not aware of yet:
1. You should handle the "configure" event which is invoked every time
a viewport is resized. It should be something like this:

static gboolean configure_event (GtkWidget *widget, GdkEventConfigure
*event, gpointer data) {
   GdkGLContext *glcontext = gtk_widget_get_gl_context (widget);
   GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (widget);

   /*** OpenGL BEGIN ***/
   if (!gdk_gl_drawable_gl_begin (gldrawable, glcontext))
      return FALSE;

   glViewport (0, 0, widget->allocation.width, widget->allocation.height);

   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho (-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();

   gdk_gl_drawable_gl_end (gldrawable);
   /*** OpenGL END ***/

   return TRUE;
}

Be advised, that we modify the projection matrix only in this handler.
We clearly identify that the current matrix is the projection matrix by
glMatrixMode (). After glOrtho () we get back to the model-view matrix.
It is very important to point out which matrix you are working on at the
moment. Try to avoid superfluous projection matrix transformations
either.

Don't forget to connect the event with the handler in the main function:
g_signal_connect_after (G_OBJECT (drawing_area), "configure_event",
G_CALLBACK (configure_event), NULL);

2. In the "realize" event handler you should place all GL calls that
define the initial state of the rendering context. In this particular
example it may be reduced to this:

static void realize (GtkWidget *widget, gpointer data) {
   GdkGLContext *glcontext = gtk_widget_get_gl_context (widget);
   GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (widget);

   /*** OpenGL BEGIN ***/
   if (!gdk_gl_drawable_gl_begin (gldrawable, glcontext))
      return;

   /*** Begin code from example-1.1 ***/

   glClearColor (0.0, 0.0, 0.0, 0.0);

   /*** End code from example-1.1 ***/

   gdk_gl_drawable_gl_end (gldrawable);
   /*** OpenGL END ***/
}

The handler is called only once, when the widget is realized.

3. In the "expose" handler do all the drawing and a cleanup prior to it
(if needed). I left only these calls:

static gboolean expose_event (GtkWidget *widget, GdkEventExpose
*event, gpointer data) {
   GdkGLContext *glcontext = gtk_widget_get_gl_context (widget);
   GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (widget);

   /*** OpenGL BEGIN ***/
   if (!gdk_gl_drawable_gl_begin (gldrawable, glcontext))
      return FALSE;

   glClear (GL_COLOR_BUFFER_BIT);

   glColor3f (1.0, 1.0, 1.0);

   glBegin(GL_POLYGON);
      glVertex2f(-0.5, -0.5);
      glVertex2f(-0.5, 0.5);
      glVertex2f(0.5, 0.5);
      glVertex2f(0.5, -0.5);
   glEnd();

   if (gdk_gl_drawable_is_double_buffered (gldrawable))
      gdk_gl_drawable_swap_buffers (gldrawable);
   else
      glFlush ();

   gdk_gl_drawable_gl_end (gldrawable);
   /*** OpenGL END ***/

   return TRUE;
}

> Note: Is the code hardware independent? The previous code (the first I
> posted) works fine on other pcs, but not on my laptop. It has a Intel
> 82852/855GM integrated graphics device.
I can't tell for sure whether it's hardware-, driver- or OS-dependent.
Neither your nor mine implementations were correct, because we didn't
specify the current type of matrix. I assume that the initial state
of the context is undefined (i.e. you don't know what is the current
matrix, if you don't specify it explicitly), so we both ended up
messing up with matrices in the realize () function. That led to
undefined behaviour. With proper handling of matrices in the "configure"
handler, it should be 100% predictable.

> On 8/3/06, Vladimir Bashkardin <bash deg gubkin ru> wrote:
>>
>> Hi.
>>
>> I suggest to move the following code from realize () to expose_event ():
>> >    glBegin(GL_POLYGON);
>> >       glVertex2f(-0.5, -0.5);
>> >       glVertex2f(-0.5, 0.5);
>> >       glVertex2f(0.5, 0.5);
>> >       glVertex2f(0.5, -0.5);
>> >    glEnd();
>>
>> It should be like this:
>> static void realize (GtkWidget *widget, gpointer data) {
>>    GdkGLContext *glcontext = gtk_widget_get_gl_context (widget);
>>    GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (widget);
>>
>>    /*** OpenGL BEGIN ***/
>>    if (!gdk_gl_drawable_gl_begin (gldrawable, glcontext))
>>       return;
>>
>>    /*** Begin code from example-1.1 ***/
>>
>>    glClearColor (0.0, 0.0, 0.0, 0.0);
>>    glClear (GL_COLOR_BUFFER_BIT);
>>    glColor3f (1.0, 1.0, 1.0);
>>    glOrtho (-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
>>    glFlush();
>>
>>    /*** End code from example-1.1 ***/
>>
>>    glViewport (0, 0, widget->allocation.width,
>> widget->allocation.height);
>>
>>    gdk_gl_drawable_gl_end (gldrawable);
>>    /*** OpenGL END ***/
>> }
>>
>> static gboolean expose_event (GtkWidget *widget, GdkEventExpose
>> *event, gpointer data) {
>>    GdkGLContext *glcontext = gtk_widget_get_gl_context (widget);
>>    GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (widget);
>>
>>    /*** OpenGL BEGIN ***/
>>    if (!gdk_gl_drawable_gl_begin (gldrawable, glcontext))
>>       return FALSE;
>>
>>    glBegin(GL_POLYGON);
>>       glVertex2f(-0.5, -0.5);
>>       glVertex2f(-0.5, 0.5);
>>       glVertex2f(0.5, 0.5);
>>       glVertex2f(0.5, -0.5);
>>    glEnd();
>>
>>    if (gdk_gl_drawable_is_double_buffered (gldrawable))
>>       gdk_gl_drawable_swap_buffers (gldrawable);
>>    else
>>       glFlush ();
>>
>>    gdk_gl_drawable_gl_end (gldrawable);
>>    /*** OpenGL END ***/
>>
>>    return TRUE;
>> }
>>
>> Now works OK on my configuration: RedHat Enterprise Linux 4.0,
>> nVidia QuadroFX 1300, nVidia propietary driver version 87.62
>>
>> PS. Instead of the "realize" event, I would handle the "configure"
>> event.
>> Currently, it doesn't react properly to resize/maximize/minimize window
>> events.
>>
>> Thu, 03 Aug 2006, 06:49 jesus ruiz wrote:
>> > Hello
>> >
>> > First of all, sorry for my English.
>> >
>> > I want to learn developing OpenGL applications, so I try gtkglext. The
>> > examples are great! :) But I would like to follow the redbook from
>> > OpenGL to learn it from scratch.
>> >
>> > So, I try the example1-1 [1] (A white square into a black screen), but
>> > i didnt get it working. The code (based on simple.c) mentioned is
>> > commented and posted below.
>> >
>> > Thanks in advance
>> >
>> > P.S. If this is off topic, please tell me and sorry.
>> >
>> > [1] http://www.rush3d.com/reference/opengl-redbook-1.1/chapter01.html
>> >

-- 
Vladimir Bashkardin
Department of Exploration Geophysics
Gubkin's Russian State University of Oil and Gas




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]