下面将对VS2010中配置OpenGL进行简单介绍。
学习OpenGL前的准备工作
第一步,选择一个编译环境
现在Windows系统的主流编译环境有Visual Studio,Broland C++ Builder,Dev-C++等,它们都是支持OpenGL的。
我选择Visual Studio 2010和VC6++作为学习OpenGL的环境。
第二步,安装GLUT工具包
GLUT不是OpenGL所必须的,但它会给我们的学习带来一定的方便,推荐安装。
Windows环境下的GLUT下载地址:(大小约为150k)
http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip
无法从以上地址下载的话请使用下面的连接:
http://upload.programfan.com/upfile/200607311626279.zip
Windows环境下安装GLUT的步骤:
1、将下载的压缩包解开,将得到5个文件,分别是glut.h,glut.lib,glut32.lib,glut.dll,glut32.dll
2、将其中的glut.h放到C:\Program Files\Microsoft SDKs\Windows\v7.0A\Include\gl里面,VS2010会自动搜索这个路径。
3. 将glut.lib和glut32.lib复制到F:\vs2010\VC\lib下,根据VS2010的具体安装路径确定。
4. 将glut.dll和glut32.dll复制到C:\WINDOWS\system32下。
至此,环境已经配置好了。
下面来开发一个简单的c++程序,环境vs2010中的vc
1. 新建->项目->win32控制台应用程序。
2. 输入项目名:simpleGL,其他默认即可。
3. 输入源代码:
// Rotation amountsstatic GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
// Called to draw scene
void RenderScene(void)
{
// Angle of revolution around the nucleus
static float fElect1 = 0.0f;
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Red Nucleus
glColor3ub(255, 0, 0);
glutSolidSphere(10.0f, 15, 15);
// Yellow Electrons
glColor3ub(255,255,0);
// First Electron Orbit
// Save viewing transformation
glPushMatrix();
// Rotate by angle of revolution
glRotatef(fElect1, 0.0f, 1.0f, 0.0f);
// Translate out from origin to orbit distance
glTranslatef(90.0f, 0.0f, 0.0f);
// Draw the electron
glutSolidSphere(6.0f, 15, 15);
// Restore the viewing transformation
glPopMatrix();
// Second Electron Orbit
glPushMatrix();
glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
glRotatef(fElect1, 0.0f, 1.0f, 0.0f);
glTranslatef(-70.0f, 0.0f, 0.0f);
glutSolidSphere(6.0f, 15, 15);
glPopMatrix();
// Third Electron Orbit
glPushMatrix();
glRotatef(360.0f-45.0f,0.0f, 0.0f, 1.0f);
glRotatef(fElect1, 0.0f, 1.0f, 0.0f);
glTranslatef(0.0f, 0.0f, 60.0f);
glutSolidSphere(6.0f, 15, 15);
glPopMatrix();
// Increment the angle of revolution
fElect1 += 10.0f;
if(fElect1 > 360.0f)
fElect1 = 0.0f;
// Show the image
glutSwapBuffers();
}
// This function does any needed initialization on the rendering
// context.
void SetupRC()
{
glEnable(GL_DEPTH_TEST); // Hidden surface removal
glFrontFace(GL_CCW); // Counter clock-wise polygons face out
glEnable(GL_CULL_FACE); // Do not calculate inside of jet
// Black background
glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
}
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
xRot-= 5.0f;
if(key == GLUT_KEY_DOWN)
xRot += 5.0f;
if(key == GLUT_KEY_LEFT)
yRot -= 5.0f;
if(key == GLUT_KEY_RIGHT)
yRot += 5.0f;
if(key > 356.0f)
xRot = 0.0f;
if(key < -1.0f)
xRot = 355.0f;
if(key > 356.0f)
yRot = 0.0f;
if(key < -1.0f)
yRot = 355.0f;
// Refresh the Window
glutPostRedisplay();
}
void TimerFunc(int value)
{
glutPostRedisplay();
glutTimerFunc(100, TimerFunc, 1);
}
void ChangeSize(int w, int h)
{
GLfloat fAspect;
// Prevent a divide by zero
if(h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
// Reset coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
fAspect = (float)w/(float)h;
gluPerspective(45.0, fAspect, 1.0, 500.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -250.0f);
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("OpenGL Atom - Part Duex");
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(RenderScene);
glutTimerFunc(500, TimerFunc, 1);
SetupRC();
glutMainLoop();
return 0;
}
4. 选择项目属性,选择C/C++->预处理器,在右侧的“预处理器定义”处,删除“_CONSOLE“,如图
5. 在项目属性中,切换到“链接器”->系统,右侧窗口的子系统选择:窗口 (/SUBSYSTEM:WINDOWS)
6. 在项目属性中,再切换到“链接器”->命令行,在“其他选项”中输入:/ENTRY:mainCRTStartup (可以去除程序运行时出现的dos窗口)
编译运行,就能看到小球的动态运行图”了!
手动卸载 Visual Studio
1. 删除 Visual Studio 2010产品的所有实例。
2. 按下列顺序卸载任何支持产品。使用非英语安装时,还必须删除列出的某些产品的语言包。在您删除列表上的最后一个包之前,没有必要重新启动。
a. Microsoft Visual Studio 2010 ADO.NET实体框架工具
b. Microsoft SQL Server 2008 R2 Transact-SQL语言服务
c. Microsoft SQL Server 2008 R2数据层应用程序项目
d. Microsoft SQL Server 2008 R2数据层应用程序框架
e. Microsoft Sync Framework SDK 1.0版 SP1
f. Microsoft Sync Framework Services 1.0版 SP1
g. Microsoft Sync Framework Runtime 1.0版 SP1
h. Microsoft Silverlight 3 SDK
i. Microsoft ASP.NET MVC 2
j. Microsoft ASP.NET MVC 2 - Visual Studio 2010工具
k. Microsoft .NET Framework 4 Multi-Targeting Pack
l. Microsoft TFS对象模型
m. Microsoft Visual F# 2.0运行时
n. Microsoft Visual Studio x64系统必备