ubuntu下openGL的配置方法

This is a simple tutorial to show a new linux user (such as myself) how to setup freeglut and OpenGl. 

OS: ubuntu 12.1

I have just recently become a linux user and wanted to install freeglut to do my graphics assignments on my laptop. Although the install did not turn out to be very difficult it took me a while to do. So I have written this tutorial in case another young linux user comes along and decides he/she wants to install freeglut and OpenGL.

**This tutorial assumes that you have your operating system installed and you have access to the internet** 

Steps:

From a terminal

1) sudo apt-get update 

-This will update your apt database to the most recent available packages.

2) sudo apt-get install build-essential

- This installs the necessary development tools for building source code.

3) sudo apt-get install freeglut3-dev

- This installs the development libraries and headers for freeglut.

Your done! Extremely simple! However you must remember that when compiling you must add a '-lglut' as a comand line argument to gcc. If you don't it cannot find the library's and you will get undefined reference errors.

example command line: gcc simple.c -lglut

At this point if your program compiles and runs then you are finished. However the first time I tried to run mine I received a 'libGL warning: 3D driver claims to not support visual 0x42'. This error means I cannot display the required colors to run the program. In my case I had the most recent drivers for my video card. So I did some research on my monitor and found out it can display a color depth of 24 instead of the 16 it was set at. To fix this problem you must edit the /etc/X11/xorg.conf file as root and set the 'DefaultDepth 24'. Reboot and the problem is solved.

This is my first post (and tutorial) on the ubuntuforums. If people feel that this tutorial is not needed they can feel free to remove it. If anyone wants to add anything related to freeglut or OpenGl please feel free.
css

 完成上述操做,能夠經過如下程序進行驗證環境安裝的正確性。linux

 

測試ubuntu

 


 

示例test.c源碼:ide

 

  1. #include <GL/glut.h>

  2. void init(void)
  3. {
  4.     glClearColor(0.0, 0.0, 0.0, 0.0);
  5.     glMatrixMode(GL_PROJECTION);
  6.     glOrtho(-5, 5, -5, 5, 5, 15);
  7.     glMatrixMode(GL_MODELVIEW);
  8.     gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);

  9.     return;
  10. }

  11. void display(void)
  12. {
  13.     glClear(GL_COLOR_BUFFER_BIT);
  14.     glColor3f(1.0, 0, 0);
  15.     glutWireTeapot(3);
  16.     glFlush();

  17.     return;
  18. }

  19. int main(int argc, char *argv[])
  20. {
  21.     glutInit(&argc, argv);
  22.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
  23.     glutInitWindowPosition(0, 0);
  24.     glutInitWindowSize(300, 300);
  25.     glutCreateWindow("OpenGL 3D View");
  26.     init();
  27.     glutDisplayFunc(display);
  28.     glutMainLoop();

  29.     return 0;
  30. }

編譯程式時,執行如下指令:
oop

  1. $ gcc -o test test.c -lGL -lGLU -lglut

執行:
post

  1. $ ./test
  2. 效果圖以下:
  3. codeblocks IDE 要使用的話,須要在創建了控制檯程序後,對內部調試環境進行配置。
  4. 具體步驟以下:
  5. 1.新建一個Console Application 工程

2.使工程包含OpenGL/Glut 等相關的連接庫測試

菜單的「project」 ----- 「Build Options」 ---------「 Linkersettings」中進行設置ui

添加文件:libGL.so  libglut.so libGLU.sothis

以下圖spa

選擇「加入」按鈕,根據本身ubuntu中的具體位置進行添加。

完成上述步驟後,就能夠進行Debug了。

 

也能夠進行下面這幾個步驟,能夠實如今codeblocks下直接建立openGL工程。

$ sudo apt-get install build-essential gdb subversion
$ sudo apt-get install automake autoconf libtool
$ sudo apt-get install libgtk2.0-dev libxmu-dev libxxf86vm-dev

這樣,在codeblocks裏面新建工程就能夠了,新建glut工程的話,會自帶一個例子。若是隻作了前兩步,編譯的時候就會報錯,說找不到Xxf86vm。去Ubuntu論壇逛了一圈,回來,作了後3個步驟,就順利的編譯運行了。

創 建 GLUT 工程時,選擇 GULT‘s Location時,直接輸入 /usr 便可。由於/usr下面就有include和lib兩個子文件夾。但是在Windows下面,我就搞不定如何能有這兩個子文件夾的文件夾。每次都說不能包 含進去glut.h頭文件。沒有辦法,只能到linux下面來折騰。不過仍是要研究一下。

這個是codeblocks自帶的glut工程的例子。新建工程的時候出來的。編譯運行後的圖:

相關文章
相關標籤/搜索