在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();
}