Gtk GLArea Widget in C not displaying gl commands

问题内容:

I have asked a question earlier (Use of the Gtk.GLArea in Pygobject GTK3) about trying to use the GLArea widget in PyGObject. I never did get the python version to draw commands so I thought I’d give the c version a try to understand it better. Using Ubuntu 16 since Gtk3 is all included, I gave it a shot. The following code compiles and runs without errors or warning but does not draw the included GL_LINES instructions. The really odd thing is changing the glClearColor does change the background and the GL_LINES are in the same function. As you can see I’m using the Ubuntu included epoxy/gl library, which I believe would be equivalent to GL/gl. Any ideas?

/*to compile -> 
gcc `pkg-config --cflags gtk+-3.0` -o gl_area gl_area.c `pkg-config --libs gtk+-3.0 epoxy`*/
#include <math.h>
#include <gtk/gtk.h>
#include <epoxy/gl.h>
#include <stdio.h>


gint init(GtkWidget *widget)
{
  /* Setup the viewport*/
      glViewport(0, 0,  gtk_widget_get_allocated_width (widget),
                        gtk_widget_get_allocated_height(widget));
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glOrtho(0,100, 100,0, -1,1);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();

      printf("ran init method\n");
      return TRUE;
}



/* When widget is exposed it's contents are redrawn. */
static gboolean
render (GtkGLArea *area, GdkGLContext *context)
{
  /* OpenGL functions can be called only if make_current returns true */

      /* Draw simple triangle */
      glClearColor(.3,.3,.3,1);
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glColor4f(1,1,1,1);
      glBegin(GL_LINES);
{
      glVertex2f(0,0);
      glVertex2f(10000,10000);
      glVertex2f(0,0);
      glVertex2f(-10000,-10000);
      glVertex2f(0,0);
      glVertex2f(10000,-10000);
      glVertex2f(0,0);
      glVertex2f(-10000,10000);
}
      glEnd();
      printf("ran render method\n");
  return TRUE;
}

/* When glarea widget size changes, viewport size is set to match the new size */
gint reshape(GtkWidget *widget, GdkEventConfigure *event)
{
  /* OpenGL functions can be called if context is current */
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      printf("ran rehsape method\n");
    glViewport(0, 0, gtk_widget_get_allocated_width (widget),
                        gtk_widget_get_allocated_height(widget));
  return TRUE;
}


int main(int argc, char **argv)
{
/* initialize gtk */
gtk_init(&argc, &argv);

/* Create new top level window. */
GtkWidget *window = gtk_window_new( GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "GL Area Test");
gtk_window_set_default_size (GTK_WINDOW(window),
                             640,
                            480);
  gtk_container_set_border_width(GTK_CONTAINER(window), 10);

  /* Quit main if got delete event */
  g_signal_connect(G_OBJECT(window),
                    "delete-event",
                    G_CALLBACK(gtk_main_quit), 
                    NULL);

  /* Create new OpenGL widget. */
GtkWidget *glarea = gtk_gl_area_new ();

  /* Do initialization when widget has been realized. */
 g_signal_connect(glarea, "realize",  G_CALLBACK(init), NULL);
  /* When window is resized viewport needs to be resized also. */
 g_signal_connect(glarea, "configure-event",  G_CALLBACK(reshape), NULL);

  /* Render signal should be sent once the context is set to current. */
g_signal_connect (glarea, "render", G_CALLBACK (render), NULL);


  /* set minimum size */
 gtk_widget_set_size_request(glarea, 100,100);

  /* put glarea into window and show it all */
 gtk_container_add(GTK_CONTAINER(window), glarea);
 gtk_widget_show_all (window);
 gtk_main();
 return 0;
}

问题评论:

1  
IMHO, probably not the error but: after glOrtho(0,100, 100,0, -1,1); you call glMatrixMode(GL_PROJECTION); again and glLoadIdentity();. This overrides the previous call of glOrtho(). (If I remember right this is like glOrtho(-1, 1, -1, 1, -1, 1);.) However, I assume you intended to write glMatrixMode(GL_MODELVIEW); (for the second call of glMatrixMode()) instead.
    
Completeley unrelated but you’re supposed to connect to destroy, not delete-event to call gtk_main_quit.
    
Please run gtk-demo, give a look to the gl example there, maybe you’ll see what you’re doing wrong? The example is here too: git.gnome.org/browse/gtk+/tree/demos/gtk-demo/glarea.c

原文地址:

https://stackoverflow.com/questions/47746674/gtk-glarea-widget-in-c-not-displaying-gl-commands

添加评论