经历了几个小时的钻研后,我终于弄明白了应该如何在VC6.0环境下配置OpenGl类库~~~
开心ing~~~现在就和大家分享一下我的心得~~~
首先,下载OpenGL类库的常用文件,配置应用环境。
Windows环境下的GLUT下载地址:(大小约为150k)
http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip
windows下glut的配置步骤:
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。
首先,在stdafx.h文件中加入
#include
#include
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 //
#include
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;
}