在Windows上开发OpenGL, 一般都会选择Visual Studio作为开发工具,不过我更喜欢Eclipse。 在Windows上开发OpenGL所需的库一般会带有32这个后缀, 跟Linux上的还不太一样。
现在开始搭建环境:
1.首先下载Eclipse, 开发C/C++应用程序的话选择”Eclipse IDE for C/C++ Developers“,http://www.eclipse.org/downloads/。光有开发工具还不行, 还需要编译器。

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下。

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,然后点击确定


5.现在修改OpenGLDemo.cpp 文件

#include <GL/glut.h>

#define window_width  640

#define window_height 480

// Main loop

void main_loop_function()

{

// 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();

}

输入完成后点击编译,运行, 是不是就成功了!