2014-09-03 102 views
2

我使用的是Ubuntu 12.04,並且我安裝了OpenGL4。Ubuntu 12.04上的OpenGL 4教程

我也有一個支持CUDA的NVIDIA顯卡。請注意,我一直在使用CUDA在我的PC上進行並行計算,並且工作正常。

[[email protected] 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 
+0

請發佈您的代碼和您用於編譯的命令。 – erapert 2014-09-03 05:36:17

回答

0

警告:此問題已相當顯著,因爲這個答案作出

改變「我試着一些教程,但我不能得到一個簡單的程序工作「我假設,鑑於你之前說過,你的CUDA程序在Windows上工作,而不是在Ubuntu上?

1)嘗試先安裝一個較新的Ubuntu版本(如果您有該PC上的選項)。 12.04有點老了,如果你沒有足夠的理由(如公司PC,你會違反升級策略,沿着這些線路),你可能不應該使用它。

2)嘗試安裝NVIDIA®(英偉達™)NVIDIA®(英偉達™)NVIDIA®(英偉達™)驅動程序。您可能已安裝mesa。我想目前的mesa版本至少有一些GPGPU的支持,但我不確定他們是否支持CUDA(如果你不是特別喜歡CUDA,你也可以嘗試OpenCL,你可能有更好的機會。很有可能你需要專有的NVIDIA驅動程序)。


GLFW是創建窗口,在其中設置了OpenGL的一個輔助庫,並提供了訪問輸入功能等,這是類似於GLUT - 你似乎很熟悉 - 在這方面,但更現代的選擇。你當然需要安裝它才能使用它。您應該可以通過Ubuntus軟件包管理器按照慣例安裝它,請嘗試apt-cache search glfw,這應該可以幫助您找到確切的軟件包名稱。如果有多個「lib」版本,請安裝以-dev結尾的那個。

3

爲GLFW安裝「開發」包:sudo apt-get install libglfw3-dev

警告:你的包管理器獲取的版本可能會過時的。

您可以按照說明herehere

+0

你的意思是我應該從源代碼安裝libglfw? – mkuse 2014-09-03 05:49:25

+0

仍然不起作用。 main2.cpp:在函數'int main()'中: main2.cpp:18:2:錯誤:'GLFWwindow'未在此範圍內聲明 main2.cpp:18:14:錯誤:'窗口'未聲明在此範圍內 main2.cpp:18:79:錯誤:'glfwCreateWindow'未在此範圍內聲明 main2.cpp:24:32:錯誤:'glfwMakeContextCurrent'未在此範圍內聲明 – mkuse 2014-09-03 05:52:28

+0

我添加了另一個鏈接到堆棧溢出回答。在按照說明進行操作之前,在繼續之前卸載並刪除您當前擁有的任何版本的GLFW可能是明智的做法。 – erapert 2014-09-03 06:11:00

2

I tried compiling with -

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

爲什麼在使用GLFW時鏈接GLUT?你的程序也不需要GLU。試試這個:

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