在Windows上開發OpenGL, 通常都會選擇Visual Studio做爲開發工具,不過我更喜歡Eclipse。 在Windows上開發OpenGL所需的庫通常會帶有32這個後綴, 跟Linux上的還不太同樣。
如今開始搭建環境:
1.首先下載Eclipse, 開發C/C++應用程序的話選擇」Eclipse IDE for C/C++ Developers「,http://www.eclipse.org/downloads/。光有開發工具還不行, 還須要編譯器。eclipse
2.配合Eclipse最好的莫過於gcc了, 下載TDM-GCChttp://tdm-gcc.tdragon.net/, 安裝完後會在C盤(默認安裝的話)有個叫MinGW32的文件夾。
3.Windows自帶了Opengl的dll了, 因此若是隻用OpenGL的話,已經足夠了,不過我如今要提供一個窗口管理工具給OpenGL, 經常使用的有SDL,GLUT(或freeglut)等等。這些都是跨平臺的。固然,也可使用MFC,我我的是很討厭MFC的, 因此通常不拿來用。由於glut的版權問題, 並且已經好久沒有更新了, 因此用開源版本的freeglut。 在本篇文章的附件中有下載。下載完後解壓會發現裏面有個include文件夾和lib文件夾, 將這兩個文件夾拷貝到諸如:C:\MinGW32\freeglut, 後面在Eclipse裏會用到該路徑, 固然,有個簡單的方法,就是將這兩個文件夾中的內容分別拷貝到MinGW32裏的include和lib文件夾中, 注意, 拷貝freeglut中include的文件時,只要拷貝include/GL裏的.h文件到MinGW32的include/GL下。ide
4. 環境搭建完了, 下面就能夠開始新建工程了,工具
在Eclipse中 New-->C++ Project, 選擇Hello World C++ Project, 取名爲OpenGLDemo,新建工程完成後, 在左側的Project Explorer中選擇OpenGLDemo,右鍵選擇Properties,選擇C/C++ Build--> Settings-->MinGW C++ Linker, 點擊Add,以下圖所示,增長opengl32,glu32,freeglut,而後點擊肯定oop
5.如今修改OpenGLDemo.cpp 文件
開發工具
#include <GL/glut.h>ui
#define window_width 640spa
#define window_height 480.net
// Main loopblog
void main_loop_function()ip
{
// Z angle
static float angle;
// Clear color (screen)
// And depth (used internally to block obstructed objects)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Load identity matrix
glLoadIdentity();
// Multiply in translation matrix
glTranslatef(0, 0, -10);
// Multiply in rotation matrix
glRotatef(angle, 0, 0, 1);
// Render colored quad
glBegin( GL_QUADS);
glColor3ub(255, 000, 000);
glVertex2f(-1, 1);
glColor3ub(000, 255, 000);
glVertex2f(1, 1);
glColor3ub(000, 000, 255);
glVertex2f(1, -1);
glColor3ub(255, 255, 000);
glVertex2f(-1, -1);
glEnd();
// Swap buffers (color buffers, makes previous render visible)
glutSwapBuffers();
// Increase angle to rotate
angle += 0.25;
}
// Initialze OpenGL perspective matrix
void GL_Setup(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode( GL_PROJECTION);
glEnable( GL_DEPTH_TEST);
gluPerspective(45, (float) width / height, .1, 100);
glMatrixMode( GL_MODELVIEW);
}
// Initialize GLUT and start main loop
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(window_width, window_height);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("GLUT Example!!!");
glutIdleFunc(main_loop_function);
GL_Setup(window_width, window_height);
glutMainLoop();
}