使用GTK2 +和OpenGL时的编译错误?

时间:2016-09-13 07:52:58

标签: c opengl gtk

我正在编写一个使用固定管道OpenGL和FreeGLUT的C程序。但是,我想使用像GTK +这样的完整工具包而不是FreeGLUT。我不能使用GTK3 +,因为它与旧的OpenGL代码不兼容,所以我想使用GTK2 +。

我发现this link有一个名为 simple.c 的示例程序:

#include <math.h>
#include <gtk/gtk.h>
#include <GL/gl.h>
#include <gtkgl/gtkglarea.h>

int init (GtkWidget *widget)
{
if (gtk_gl_area_make_current (GTK_GL_AREA(widget)))
  {
  glViewport(0,0, widget->allocation.width, widget->allocation.height);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0,100, 100,0, -1,1);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  }

return TRUE;
}

int draw (GtkWidget *widget, GdkEventExpose *event)
{
if (event->count > 0) return TRUE;

if (gtk_gl_area_make_current (GTK_GL_AREA(widget)))
  {
  glClearColor(0,0,0,1);
  glClear(GL_COLOR_BUFFER_BIT);
  glColor3f(1,1,1);
  glBegin(GL_TRIANGLES);
  glVertex2f(10,10);
  glVertex2f(10,90);
  glVertex2f(90,90);
  glEnd();
  gtk_gl_area_swap_buffers (GTK_GL_AREA(widget));
  }

return TRUE;
}

int reshape (GtkWidget *widget, GdkEventConfigure *event)
{
if (gtk_gl_area_make_current (GTK_GL_AREA(widget)))
  glViewport(0,0, widget->allocation.width, widget->allocation.height);

return TRUE;
}

int main (int argc, char **argv)
{
GtkWidget *window, *glarea;

int attrlist[] = {
GDK_GL_RGBA,
GDK_GL_RED_SIZE,1,
GDK_GL_GREEN_SIZE,1,
GDK_GL_BLUE_SIZE,1,
GDK_GL_DOUBLEBUFFER,
GDK_GL_NONE };

gtk_init (&argc, &argv);
if (gdk_gl_query () == FALSE) return 0;

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW(window), "Simple");
gtk_container_set_border_width (GTK_CONTAINER(window), 10);
g_signal_connect (window, "delete_event",
G_CALLBACK (gtk_main_quit), NULL);

/* You should always delete gtk_gl_area widgets before exit or else
   GLX contexts are left undeleted, this may cause problems (=core dump)
   in some systems.
   Destroy method of objects is not automatically called on exit.
   You need to manually enable this feature. Do gtk_quit_add_destroy()
   for all your top level windows unless you are certain that they get
   destroy signal by other means. */

gtk_quit_add_destroy (1, GTK_OBJECT(window));

glarea = GTK_WIDGET(gtk_gl_area_new (attrlist));
gtk_widget_set_size_request (GTK_WIDGET(glarea), 100, 100);

gtk_widget_set_events (GTK_WIDGET(glarea), GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK);

g_signal_connect (glarea, "expose_event", G_CALLBACK(draw), NULL);
g_signal_connect (glarea, "configure_event", G_CALLBACK(reshape), NULL);
g_signal_connect (glarea, "realize", G_CALLBACK(init), NULL);

gtk_container_add (GTK_CONTAINER(window), GTK_WIDGET(glarea));
gtk_widget_show (GTK_WIDGET(glarea));
gtk_widget_show (GTK_WIDGET(window));

gtk_main ();
return 0;
}

但不幸的是,它重新查询了一个我不知道从哪里得到的文件, gtk_opengl.h

我尝试使用

编译此 simple.c 代码失败了
gcc simple.c -o helloworld `pkg-config --libs gtk+-2.0`

当然,出现以下错误:

fatal error: gtk_opengl.h: No such file or directory

有关如何编译 simple.c 的任何想法?或者如何将OpenGL与GTK2 +结合起来?

1 个答案:

答案 0 :(得分:2)

在我的Debian Stable框中,我必须安装libgtkgl2.0-dev并添加--cflags&amp; gtkgl-2.0调用pkg-config

gcc simple.c -o helloworld `pkg-config --cflags --libs gtk+-2.0 gtkgl-2.0`