使用 OpenGL 编译 GTK+

Posted

技术标签:

【中文标题】使用 OpenGL 编译 GTK+【英文标题】:Compilation for GTK+ using OpenGL 【发布时间】:2016-12-03 13:15:51 【问题描述】:

上下文:我正在学习使用 GTK+ 开发 GUI。我还想在 GUI 上画线和圆。所以我从教程开始,我被 GtkGLArea 的部分困住了。我正在遵循GTK+ documentation 中给出的代码

错误

glTrial.cpp:32:13: error: variable or field ‘on_realize’ declared void
 on_realize (GtkGLarea *area)
             ^
glTrial.cpp:32:13: error: ‘GtkGLarea’ was not declared in this scope
glTrial.cpp:32:24: error: ‘area’ was not declared in this scope
 on_realize (GtkGLarea *area)

我相信我没有正确编译并且编译器无法找到正确的头文件。

编译

g++ -std=c++14 \`pkg-config --cflags gtk+-3.0\` -o glTrial glTrial.cpp \`pkg-config --libs gtk+-3.0\` 

代码

#include <gtk/gtk.h>
#include <gtkgl-2.0/gtkgl/gdkgl.h>
#include <gtkgl-2.0/gtkgl/gtkglarea.h>

static void
print_hello (GtkWidget *widget,
         gpointer user_data)

  g_print ("Hello World\n");


static gboolean
render (GtkGLArea *area, GdkGLContext *context)

  // inside this function it's safe to use GL; the given
  // #GdkGLContext has been made current to the drawable
  // surface used by the #GtkGLArea and the viewport has
  // already been set to be the size of the allocation

  // we can start by clearing the buffer
  //glClearColor (0, 0, 0, 0);
  // glClear (GL_COLOR_BUFFER_BIT);

  // draw your object
  // draw_an_object ();

  // we completed our drawing; the draw commands will be
  // flushed at the end of the signal emission chain, and
  // the buffers will be drawn on the window
  return TRUE;

static void
on_realize (GtkGLarea *area)

  // We need to make the context current if we want to
  // call GL API
  gtk_gl_area_make_current (area);

  // If there were errors during the initialization or
  // when trying to make the context current, this
  // function will return a #GError for you to catch
  if (gtk_gl_area_get_error (area) != NULL)
    return;

  // You can also use gtk_gl_area_set_error() in order
  // to show eventual initialization errors on the
  // GtkGLArea widget itself
  GError *internal_error = NULL;
  init_buffer_objects (&error);
  if (error != NULL)
    
      gtk_gl_area_set_error (area, error);
      g_error_free (error);
      return;
    

  init_shaders (&error);
  if (error != NULL)
    
      gtk_gl_area_set_error (area, error);
      g_error_free (error);
      return;
    


static void
activate (GtkApplication* app,
      gpointer user_data)

  GtkWidget *window;
  GtkWidget *grid;
  GtkWidget *button;
  GtkWidget *gl_area =gtk_gl_area_new();

  /* Create a new window, and set its title */
  window = gtk_application_window_new(app);
  gtk_window_set_title(GTK_WINDOW(window), "Window");
  gtk_container_set_border_width(GTK_CONTAINER(window), 10);
  //  gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);

  /* Here we construct the container that is going to pack the buttons*/
  grid = gtk_grid_new();

  /* Pack the container in the window */
  gtk_container_add (GTK_CONTAINER (window), grid);


  button = gtk_button_new_with_label ("Quit");
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);

  gtk_grid_attach (GTK_GRID (grid), button, 0, 0, 2, 1);

  button = gtk_button_new_with_label ("Hello");
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (print_hello), NULL);

  gtk_grid_attach (GTK_GRID (grid), button, 0, 1, 2, 1);

  /* Trial for GL area*/
  g_signal_connect (gl_area, "render", G_CALLBACK(render), NULL);
  gtk_grid_attach (GTK_GRID (grid), gl_area, 0, 2, 10, 10);

  gtk_widget_show_all (window);



int
main (int argc,
      char **argv)

  GtkApplication *app;
  int status;
  g_application_run (G_APPLICATION (app), argc, argv);
  //  app = gtk_application_new("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  g_signal_connect(app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);
  return status;


【问题讨论】:

如果将gtkglarea.h 标头替换为#include &lt;gtk/gtkgl.h&gt;,它会改变什么吗? @MarcoA。它说没有这样的文件或目录。我是否缺少一些标题? 根据快速谷歌,您正在寻找GtkGLArea,注意大写 A 【参考方案1】:

根据快速谷歌,您正在寻找GtkGLArea,注意大写 A。 – 德哈斯

【讨论】:

【参考方案2】:

你需要:

sudo apt install *epoxy*

c++ t.c --target=arm-linux-gnu  `pkg-config --libs --cflags  gtk+-3.0 epoxy ` -o op

【讨论】:

不,没有用。我仍然有原来的错误。现在有来自/usr/include/gtkgl-2.0/gtkgl/gdkgl.h:的新错误 是的,你不能编译这个源代码。它需要旧的 gtk 东西。看看 gtk3(内置 glarea)一个github.com/ebassi/glarea-example 已编辑找到它 不,这不适用于文档中给出的示例。我已经尝试过您发布的 github 链接中给出的示例,并且效果很好。我想我缺少一些东西。谢谢! libepoxy 是一个 GL 加载器,它与 gtk 完全无关,它不会为 GtkGLarea 类型提供声明。

以上是关于使用 OpenGL 编译 GTK+的主要内容,如果未能解决你的问题,请参考以下文章

windows下安装gtk并使用gcc编译?

GTK+ 3.0 编译错误:不能包含 errno.h

使用 gtkmm 编译 gtk 程序时出错

如何为原生窗口(而不是 X 窗口)编译 gtk+ 应用程序?

检查 OpenCV 是不是使用来自 C++ 代码的 GTK 支持进行编译

使用 cygwin 在 c 中编译 GTK-app:无效参数