您的位置:首页 > 其它

在VC6.0环境下配置glut.h文件

2012-04-06 21:54 393 查看
经历了几个小时的钻研后,我终于弄明白了应该如何在VC6.0环境下配置OpenGl类库~~~

开心ing~~~现在就和大家分享一下我的心得~~~

首先,下载OpenGL类库的常用文件,配置应用环境。

Windows环境下的GLUT下载地址:(大小约为150k)
http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip
windows下glut的配置步骤:

1、将下载的压缩包解开,将得到5个文件

2、把解压得到的glut.h放到“Program Files\Microsoft VisualStudio\VC98\Include\GL“这个文件夹。

3、把解压得到的glut.lib和glut32.lib放到“Program Files\Microsoft Visual Studio\VC98\lib”文件夹。

4、把解压得到的glut.dll和glut32.dll放到操作系统目录下面的system32文件夹内。

(典型的位置为:C:\Windows\System32)

其次,配置VC6.0中的工程。

1、新建一个Win32 Console Application工程,选择simple application,点击Finish

2、 按照如下顺序选择:
Project → Settings → Link选项卡
然后,在Object/library modules下面的文本框的最前面添加如下库文件内容:
Opengl32.lib glut32.lib GLAUX.LIB Glu32.lib
最后,在Project Options中修改subsystem:console修改为subsystem:windows。点击OK。
3、再按照如下顺序选择:
Project → Settings → C/C++选项卡
将Preprocessor definitions 中的_CONSOLE修改为_WINDOWS。点击OK。

完成配置,即可测试OpenGL库是否配置成功。

这里附上一个网上的测试程序~~

经过测试,很好用哦~~~

首先,在stdafx.h文件中加入

#include <windows.h>

#include <GL/glu.h>

#include <GL/gl.h>
#include <GL/glut.h>

#include <GL/glaux.h>

注意:#include <windows.h>是需要的,不引入的话有时会报错。

其次, 编写主文件,例如 test.cpp

#include <stdafx.h>
void background(void)
{
//设置背景颜色为黑色
glClearColor(0.0,0.0,0.0,0.0);
}

void myDisplay(void)
{
//buffer设置为颜色可写
glClear(GL_COLOR_BUFFER_BIT);
//开始画三角形
glBegin(GL_TRIANGLES);
//设置为光滑明暗模式
glShadeModel(GL_SMOOTH);
//设置第一个顶点为红色
glColor3f(1.0,0.0,0.0);
//设置第一个顶点的坐标为(-1.0,-1.0)
glVertex2f(-1.0,-1.0);
//设置第二个顶点为绿色
glColor3f(0.0,1.0,0.0);
//设置第二个顶点的坐标为(0.0,-1.0)
glVertex2f(0.0,-1.0);
//设置第三个顶点为蓝色
glColor3f(0.0,0.0,1.0);
//设置第三个顶点的坐标为(-0.5,1.0)
glVertex2f(-0.5,1.0);
//三角形结束
glEnd();
//强制OpenGL函数在有限时间内运行
glFlush();
}

void myReshape(GLsizei w,GLsizei h)
{
glViewport(0,0,w,h);
//设置视口

glMatrixMode(GL_PROJECTION);
//指明当前矩阵为GL_PROJECTION
glLoadIdentity();
//将当前矩阵置换为单位阵

if(w <= h)
gluOrtho2D(-1.0,1.5,-1.5,1.5*(GLfloat)h/(GLfloat)w);
//定义二维正视投影矩阵
else
gluOrtho2D(-1.0,1.5*(GLfloat)w/(GLfloat)h,-1.5,1.5);
glMatrixMode(GL_MODELVIEW);
//指明当前矩阵为GL_MODELVIEW
}

int main(int argc, char* argv[])
{
// 初始化
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(400,400);
glutInitWindowPosition(200,200);

//创建窗口
glutCreateWindow("Triangle");

//绘制与显示
background();
glutReshapeFunc(myReshape);
glutDisplayFunc(myDisplay);

glutMainLoop();
return(0);
}


再附上一个测试程序~~~~

#include "stdafx.h"
#include <stdlib.h>  //<math.h>
#include <GL/glut.h>
void myinit()
{
glClearColor(0.0,0.0,0.0,0.0); // white background
glColor3f(1.0,1.0,1.0); // draw in red

// set up viewing
// 50.0 x 50.0 camera coordinate window with origin lower left

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,50.0,0.0,50.0);
glMatrixMode(GL_MODELVIEW);
}

void display()
{
// a triangle
GLfloat vertices[3][2]={{0.0,0.0},{25.0,50.0},{50.0,0.0}};

int j,k;
int rand(); // rand number generator
GLfloat p[2]={7.5,5.0}; // arbitrary initial point inside triangle
glClear(GL_COLOR_BUFFER_BIT); // clear the window
glBegin(GL_POINTS);

// compute and plot 5000 new points

for( k=0; k<5000; k++)
{
j=rand()%3; //pick a vertex at random

// compute point halfway between selected vertex and old point

p[0]= (p[0] + vertices[j][0])/2.0;
p[1]= (p[1] + vertices[j][1])/2.0;

// plot new point
glVertex2fv(p);

}

glEnd();
glFlush(); // clear buffers

}
int main(int argc, char *argv[])

{
glutInit(&argc, argv); // if no , still ok
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  // if no , still ok
glutInitWindowPosition(100, 100);
glutInitWindowSize(500, 500); //if no, windows goes to up-left corner
glutCreateWindow("Sierpinski Gasket");  //error, if no
glutDisplayFunc(&display);  //error, if no
myinit();  // set attributes
glutMainLoop();  //error, if no

return 0;
}


测试成功就完成啦~~~


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: