用UBuntu開發OpenGL程式1

來源:互聯網
上載者:User

開發環境:
UBuntu9.10 Desktop x86,NetBeans6.8,OpenGL2.1

安裝開發需要的庫
OpenGL網站上有這麼一句話(http://www.opengl.org/wiki/Getting_started#Linux):
Linux comes with Mesa libraries, which implements the OpenGL API as a software rasterizer.
在Mesa網站上,目前最新的版本是7.7,支援OpenGL2.1,但是受限制於具體驅動程式,並不能保證完全支援所有的API.

可以下載原始碼編譯安裝,不過先從簡單的開始,在UBuntu上面安裝已經編譯好的Mesa庫和開發檔案。

首先確保編譯器等庫已經被安裝:
sudo apt-get install build-essential

執行下面的命令,將Mesa7.6.0安裝到系統中。
sudo apt-get install libgl1-mesa-dev

下面安裝兩個增強庫:
sudo apt-get install libglu1-mesa-dev
這個庫是對OpenGL的增強

sudo apt-get install libglut-dev
這個庫是對OpenGL視窗的增強和上面那個增強庫的增強

現在編寫代碼:(抄來的,參考http://wiki.ubuntu-tw.org/index.php?title=Howto_Install_OpenGL_Development_Environment)
#include <GL/glut.h>

void init();
void display();

int main(int argc, char* argv[]) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(0, 0);
    glutInitWindowSize(300, 300);

    glutCreateWindow("OpenGL 3D View");

    init();
    glutDisplayFunc(display);

    glutMainLoop();
    return 0;
}

void init() {
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    glOrtho(-5, 5, -5, 5, 5, 15);
    glMatrixMode(GL_MODELVIEW);
    gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
}

void display() {
    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f(1.0, 0, 0);
    glutWireTeapot(3);

    glFlush();
}

在NetBeans工程中設定link所需要的庫,可以參考:

繼續選中libGLU.so和libglut.so檔案。
然後編譯連結通過,
/usr/bin/make -f nbproject/Makefile-Debug.mk SUBPROJECTS= .build-conf
make[1]: 正在進入目錄 `/home/chenshu/svnclient/MyCodes/OpenGL1'
/usr/bin/make  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/opengl1
make[2]: 正在進入目錄 `/home/chenshu/svnclient/MyCodes/OpenGL1'
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/main.o.d
g++    -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.cpp
mkdir -p dist/Debug/GNU-Linux-x86
g++     -o dist/Debug/GNU-Linux-x86/opengl1 build/Debug/GNU-Linux-x86/main.o -lGL -lGLU -lglut
make[2]:正在離開目錄 `/home/chenshu/svnclient/MyCodes/OpenGL1'
make[1]:正在離開目錄 `/home/chenshu/svnclient/MyCodes/OpenGL1'
BUILD SUCCESSFUL (total time: 804ms)

運行程式:

今天就到這裡。明天會繼續探索這些API的功能。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.