简体   繁体   English

Ubuntu 12.04上的OpenGL 4教程

[英]Tutorial OpenGL 4 on Ubuntu 12.04

I am using Ubuntu 12.04 and I have installed OpenGL4. 我正在使用Ubuntu 12.04,并且已经安装了OpenGL4。

I also have a CUDA-enable NVIDIA graphics card. 我也有启用CUDA的NVIDIA图形卡。 Note that, I have been doing parallel computation with CUDA on my PC and that works. 请注意,我一直在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, 

I cannot get a simple program to work. 我无法获得一个简单的程序来工作。 Can anyone give a sample program that can work on my PC 任何人都可以提供可以在我的PC上运行的示例程序吗

Here is the code I am using: 这是我正在使用的代码:

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

I tried compiling with - 我尝试用-

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

I get an error : 我得到一个错误:

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

I am now wondering what is this GLFW? 我现在想知道这是什么GLFW? More precisely how do I install it? 更确切地说,我该如何安装? Following @eapert I installed libglfw as - 在@eapert之后,我将libglfw安装为-

sudo apt-get install libglfw-dev

Still get the following errors 仍然出现以下错误

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

Install the "dev" package for GLFW: sudo apt-get install libglfw3-dev 安装GLFW的“ dev”软件包: sudo apt-get install libglfw3-dev

Warning: the version you get from the package manager may be out of date. 警告:您从软件包管理器获得的版本可能已过时。

You can follow the instructions here or here . 您可以按照此处此处的说明进行操作。

I tried compiling with - 我尝试用-

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

Why are you linking GLUT when you want to use GLFW? 为什么要使用GLFW时要链接GLUT? Also you don't need GLU for your program. 另外,您的程序不需要GLU。 Try this: 尝试这个:

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

WARNING : The question was changed quite significantly since this answer was made 警告 :自做出此答案以来,问题已发生了很大变化

"I tried a few tutorials but I cannot get a simple program to work" I assume, given what you said earlier, your CUDA programs work on Windows, just not on Ubuntu? “我尝试了一些教程,但我无法获得一个简单的程序来工作”,我假设您已经说过,您的CUDA程序可以在Windows上运行,而不能在Ubuntu上运行?

1) Try installing a newer Ubuntu version first (if you have the option on that PC). 1)尝试首先安装较新的Ubuntu版本(如果您在该PC上具有该选件)。 12.04 is a bit old and you probably shouldn't be using it if you don't have a good reason to (ie it's a company PC and you would violate upgrade policy, something along those lines) 12.04有点旧,如果您没有充分的理由(例如,它是公司PC,并且您会违反升级政策,可能会违反这些原则),则可能不应该使用它。

2) Try installing the proprietary NVIDIA drivers for your card (this should also give you the NVIDIA OpenGL implementation); 2)尝试为您的卡安装专有的NVIDIA驱动程序(这也将为您提供NVIDIA OpenGL实现); You probably have mesa installed. 您可能已安装mesa I suppose current mesa version have at least some GPGPU support, but I'm not sure if they support CUDA (if you aren't particularly fond of CUDA you can also try OpenCL, you might have better chances with that. Although there's still a good chance you'll need the proprietary NVIDIA drivers). 我想当前的mesa版本至少支持GPGPU,但我不确定它们是否支持CUDA(如果您不特别喜欢CUDA,也可以尝试OpenCL,但您可能会有更好的机会。您将需要专有的NVIDIA驱动程序)。


GLFW is a helper library for creating windows, setting up OpenGL in them and provides access to input functions etc. It's similar to GLUT - which you seem to be familiar with - in that regard, but a much more modern alternative. GLFW是一个用于创建窗口,在其中设置OpenGL并提供对输入函数的访问的帮助程序库。在这方面,它类似于GLUT(您似乎很熟悉),但是它是一种更现代的替代方法。 You'll of course need to install it to use it. 您当然需要安装它才能使用它。 You should be able to install it through Ubuntus package manager as per usual, try apt-cache search glfw which should help you find the exact package name. 您应该能够照常通过Ubuntus软件包管理器进行安装,请尝试apt-cache search glfw ,它可以帮助您找到确切的软件包名称。 If there are multiple "lib" versions, install the one ending in -dev . 如果有多个“ lib”版本,请安装以-dev结尾的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM