1. 程式人生 > >OpenGL的安裝與配置

OpenGL的安裝與配置

小白所用的環境是visual studio 2012 express 版本,現在visual studio 都預設支援OpenGL,所以我們不需要再去官網下載OpenGL,但GLU是我們在學習過程中很有必要使用的.

GLUT is the OpenGL Utility Toolkit, awindow system independent toolkit for writing OpenGL programs. It implements asimple windowing application programming interface (API) for OpenGL. GLUT makesit considerably easier to learn about and explore OpenGL Programming.

GLUT其實對OpenGL進行了封裝,讓我們在使用時簡單了很多,避免了大量呼叫各種OpenGL的API。

閒話少說,下面是詳細過程:

1  下載GLUT

http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip

解壓後我們會發現5個檔案,這些都是我們需要的。

2  把5個檔案中的glut.h放在與glu.h相同的目錄下,大家可以搜尋檔名來找到該目錄,也可以在visual studio中引入glu.h標頭檔案後在外部依賴項中glut.h右鍵屬性來獲得其所在路徑。

   找到後ctrl_c  ctrl + v就好了

3  把解壓得到的glut.lib和glut32.lib放到靜態函式庫所在資料夾下面(例如我的visual studio 是C:\Program Files (x86)\Microsoft VisualStudio    11.0\VC\lib

4 把解壓得到的glut.dll和glut32.dll放到作業系統目錄下面的system32資料夾內(一般都是system32,有的系統可能在SysWOW64目錄下)

5 完成上述就已經完成了,下面來個測試的例子,用於測試一下配置有沒有問題:

<span style="font-size:14px;">// ConsoleApplication10.cpp : 定義控制檯應用程式的入口點。
//
#include"stdafx.h"
#include <stdio.h>
#include<gl/glut.h>
#include<math.h>
void init()
{
	glClearColor(1.0,1.0,1.0,0.0);
	glMatrixMode(GL_PROJECTION);//使用正投影
	gluOrtho2D(0.0,200.0,0.0,150.0);//座標範圍
}
void lineSegment()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0.0,0.4,0.2);
	glBegin(GL_LINES);
	glVertex2i(180,15);
	glVertex2i(10,145);
	glEnd();
	glFlush();
}
int _tmain(int argc,char** argv)
{
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
	glutInitWindowPosition(50,100);
	glutInitWindowSize(400,300);
	glutCreateWindow("An Example OpenGL Program");
	init();
	glutDisplayFunc(lineSegment);
	glutMainLoop();
}</span>

如果可以顯示出來一條直線就OK了,一起開始opengl吧。