【问题标题】:Tutorial OpenGL 4 on Ubuntu 12.04Ubuntu 12.04 上的教程 OpenGL 4
【发布时间】:2014-09-04 23:13:22
【问题描述】:

我使用的是 Ubuntu 12.04,并且我已经安装了 OpenGL4。

我还有一个支持 CUDA 的 NVIDIA 显卡。请注意,我一直在我的 PC 上使用 CUDA 进行并行计算,这很有效。

[eeuser@roadrunner sample_opengl]$ glxinfo | grep gl
server glx vendor string: NVIDIA Corporation
server glx version string: 1.4
server glx extensions:
client glx vendor string: NVIDIA Corporation
client glx version string: 1.4
client glx extensions:
    GL_ARB_texture_query_lod, GL_ARB_texture_rectangle, GL_ARB_texture_rg, 
    GL_NV_texture_multisample, GL_NV_texture_rectangle, GL_NV_texture_shader, 

我无法让一个简单的程序运行。谁能提供一个可以在我的电脑上运行的示例程序

这是我正在使用的代码:

#include <GL/glew.h> // include GLEW and new version of GL on Windows
#include <GLFW/glfw3.h> // GLFW helper library
#include <stdio.h>

int main () {
  // start GL context and O/S window using the GLFW helper library
  if (!glfwInit ()) {
    fprintf (stderr, "ERROR: could not start GLFW3\n");
    return 1;
  } 

    // uncomment these lines if on Apple OS X
  /*glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3);
  glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 2);
  glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);*/

  GLFWwindow* window = glfwCreateWindow (640, 480, "Hello Triangle", NULL, NULL);
  if (!window) {
    fprintf (stderr, "ERROR: could not open window with GLFW3\n");
    glfwTerminate();
    return 1;
  }
  glfwMakeContextCurrent (window);

  // start GLEW extension handler
  glewExperimental = GL_TRUE;
  glewInit ();

  // get version info
  const GLubyte* renderer = glGetString (GL_RENDERER); // get renderer string
  const GLubyte* version = glGetString (GL_VERSION); // version as a string
  printf ("Renderer: %s\n", renderer);
  printf ("OpenGL version supported %s\n", version);

  // tell GL to only draw onto a pixel if the shape is closer to the viewer
  glEnable (GL_DEPTH_TEST); // enable depth-testing
  glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer"

  /* OTHER STUFF GOES HERE NEXT */

  // close GL context and any other GLFW resources
  glfwTerminate();
  return 0;
}

我尝试使用 - 进行编译

 g++ main2.cpp  -lglut -lGL -lGLEW -lGLU 

我收到一个错误:

main2.cpp:2:47: fatal error: GLFW/glfw3.h: No such file or directory
compilation terminated.

我现在想知道这个 GLFW 是什么?更确切地说,我该如何安装它?关注@eapert 我将 libglfw 安装为 -

sudo apt-get install libglfw-dev

还是出现以下错误

main2.cpp: In function ‘int main()’:
main2.cpp:18:2: error: ‘GLFWwindow’ was not declared in this scope
main2.cpp:18:14: error: ‘window’ was not declared in this scope
main2.cpp:18:79: error: ‘glfwCreateWindow’ was not declared in this scope
main2.cpp:24:32: error: ‘glfwMakeContextCurrent’ was not declared in this scope

【问题讨论】:

  • 请发布您的代码和您用于编译的命令。

标签: c++ opengl ubuntu


【解决方案1】:

为 GLFW 安装“dev”包:sudo apt-get install libglfw3-dev

警告:您从包管理器获得的版本可能已过期。

您可以按照herehere 的说明进行操作。

【讨论】:

  • 你的意思是我应该从源代码安装 libglfw 吗?
  • 还是不行。 main2.cpp:在函数'int main()'中:main2.cpp:18:2:错误:在此范围内未声明'GLFWwindow' main2.cpp:18:14:错误:未在此声明'窗口'范围 main2.cpp:18:79:错误:未在此范围内声明“glfwCreateWindow” main2.cpp:24:32:错误:未在此范围内声明“glfwMakeContextCurrent”
  • 我添加了另一个链接到堆栈溢出的答案。在按照说明操作之前,最好先卸载并删除您当前拥有的任何 GLFW 版本,然后再继续。
  • 这里正确的包是libglfw3-dev(注意3)。
【解决方案2】:

我尝试使用 - 进行编译

g++ main2.cpp  -lglut -lGL -lGLEW -lGLU

当您想使用 GLFW 时,为什么要链接 GLUT?此外,您的程序不需要 GLU。试试这个:

g++ main2.cpp -lGL -lGLEW -lglfw

【讨论】:

    【解决方案3】:

    警告:自从做出此答案后,问题发生了很大变化

    “我尝试了一些教程,但无法让一个简单的程序运行”我假设,鉴于您之前所说,您的 CUDA 程序可以在 Windows 上运行,而不是在 Ubuntu 上运行?

    1) 尝试先安装更新的 Ubuntu 版本(如果您在该 PC 上有选项)。 12.04 有点旧,如果您没有充分的理由,您可能不应该使用它(即,它是公司 PC,您会违反升级政策,类似这些)

    2) 尝试为您的显卡安装专有的 NVIDIA 驱动程序(这也应该为您提供 NVIDIA OpenGL 实现);你可能已经安装了mesa。我想当前的mesa 版本至少有一些 GPGPU 支持,但我不确定它们是否支持 CUDA(如果你不是特别喜欢 CUDA,你也可以尝试 OpenCL,你可能会有更好的机会。虽然有您仍然很有可能需要专有的 NVIDIA 驱动程序)。


    GLFW 是一个帮助库,用于创建窗口、在其中设置 OpenGL 并提供对输入函数的访问等。在这方面它类似于 GLUT(您似乎很熟悉),但它是一种更现代的替代方案。您当然需要安装它才能使用它。你应该可以像往常一样通过 Ubuntus 包管理器安装它,试试apt-cache search glfw,它应该可以帮助你找到确切的包名称。如果有多个“lib”版本,请安装以-dev 结尾的那个。

    【讨论】:

      猜你喜欢
      • 2012-12-16
      • 2011-03-03
      • 1970-01-01
      • 2012-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-26
      相关资源
      最近更新 更多