[GtkGLExt] troubles with my first OpenGL app



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

---------------- CODE: example1_1.c  --------------------

/*
* example1_1.c on redBook (The White square)
*
* Problem: The app only display a black window
*
* Using GTK+ through libgtkglext
*
* Functions:
* main() create the widgets and connect the signals to their callback
* realize() is called only once on startup
* expose_event() is called when the drawingarea should be repainted.
*/

#include <stdlib.h>
#include <gtk/gtk.h>
#include <gtk/gtkgl.h>

#include <GL/gl.h>
#include <GL/glu.h>

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);
  glBegin(GL_POLYGON);
     glVertex2f(-0.5, -0.5);
     glVertex2f(-0.5, 0.5);
     glVertex2f(0.5, 0.5);
     glVertex2f(0.5, -0.5);
  glEnd();
  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;

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

int main (int argc, char *argv[]) {
 GdkGLConfig *glconfig;
 gint major, minor;

 GtkWidget *window;
 GtkWidget *vbox;
 GtkWidget *drawing_area;
 GtkWidget *button;

 /*
  * Init GTK.
  */

 gtk_init (&argc, &argv);

 /*
  * Init GtkGLExt.
  */

 gtk_gl_init (&argc, &argv);

 /*
  * Query OpenGL extension version.
  */

 gdk_gl_query_version (&major, &minor);
 g_print ("\nOpenGL extension version - %d.%d\n",
          major, minor);

 /*
  * Configure OpenGL-capable visual.
  */

 /* Try double-buffered visual */
 glconfig = gdk_gl_config_new_by_mode (GDK_GL_MODE_RGB |
GDK_GL_MODE_DEPTH | GDK_GL_MODE_DOUBLE);
 if (glconfig == NULL)
   {
     g_print ("*** Cannot find the double-buffered visual.\n");
     g_print ("*** Trying single-buffered visual.\n");

     /* Try single-buffered visual */
     glconfig = gdk_gl_config_new_by_mode (GDK_GL_MODE_RGB   |
                                           GDK_GL_MODE_DEPTH);
     if (glconfig == NULL)
       {
         g_print ("*** No appropriate OpenGL-capable visual found.\n");
         exit (1);
       }
   }


 /*
  * Top-level window.
  */

 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
 gtk_window_set_title (GTK_WINDOW (window), "simple");

 /* Get automatically redrawn if any of their children changed allocation. */
 gtk_container_set_reallocate_redraws (GTK_CONTAINER (window), TRUE);

 g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK
(gtk_main_quit), NULL);

 /*
  * VBox.
  */

 vbox = gtk_vbox_new (FALSE, 0);
 gtk_container_add (GTK_CONTAINER (window), vbox);
 gtk_widget_show (vbox);

 /*
  * Drawing area for drawing OpenGL scene.
  */

 drawing_area = gtk_drawing_area_new ();
 gtk_widget_set_size_request (drawing_area, 200, 200);

 /* Set OpenGL-capability to the widget. */
 gtk_widget_set_gl_capability (drawing_area, glconfig, NULL, TRUE,
GDK_GL_RGBA_TYPE);

 g_signal_connect_after (G_OBJECT (drawing_area), "realize",
G_CALLBACK (realize), NULL);
 g_signal_connect (G_OBJECT (drawing_area), "expose_event",
G_CALLBACK (expose_event), NULL);

 gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
 gtk_widget_show (drawing_area);

 /*
  * Simple quit button.
  */

 button = gtk_button_new_with_label ("Quit");

 g_signal_connect (G_OBJECT (button), "clicked",
                   G_CALLBACK (gtk_main_quit), NULL);

 gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);

 gtk_widget_show (button);

 /*
  * Show window.
  */

 gtk_widget_show (window);

 /*
  * Main loop.
  */

 gtk_main ();

 return 0;
}



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