VC6下载
http://blog.csdn.net/bcbobo21cn/article/details/44200205
demoproject和glut库下载
http://pan.baidu.com/s/1pLSpKiZ
一 环境配置
參阅
http://blog.itpub.net/17267437/viewspace-545635/
解压glut库
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" >
拷贝glut.h到例如以下文件夹;
拷贝图1的两个lib到例如以下文件夹。
拷贝图1的两个dll到例如以下文件夹(偶是64位系统)。
project设置加入四个lib
二样例1
#include <GL/glut.h>
#include <math.h>
const int n = 200;
const GLfloat R = 0.5f;
const GLfloat Pi = 3.1415926536f;
void myDisplay(void)
{
int i;
// 画一个绿色正方形
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f,1.0f,0.0f);
glRectf(-0.5f, 0.4f, 0.0f, 0.9f); //画一个绿色的正方形
//glFlush();
//glClearColor(0.0,0.0,0.0,0.0);
//glClear(GL_COLOR_BUFFER_BIT);
glColor4f(1.0,0.0,0.0,1.0); // set the quad color
glBegin(GL_QUADS);
glVertex3f(-0.2,-0.2,0.0);
glVertex3f(0.2,-0.2,0.0);
glVertex3f(0.2,0.2,0.0);
glVertex3f(-0.2,0.2,0.0);
glEnd();
glColor4f(1.0,0.0,0.0,1.0); // set the point color
glPointSize(10);
glBegin(GL_POINTS);
glVertex3f(-0.5,-0.5,0.0);
glVertex3f(0.5,-0.5,0.0);
glVertex3f(0.5,0.5,0.0);
glVertex3f(-0.5,0.5,0.0);
glEnd();
glColor4f(1.0,0.0,0.0,1.0); // set the line color
glLineWidth(5);
glBegin(GL_LINES);
glVertex3f(-0.5,-0.5,0.0);
glVertex3f(0.5,-0.5,0.0);
glVertex3f(0.5,0.5,0.0);
glVertex3f(-0.5,0.5,0.0);
glEnd();
/*glBegin(GL_POLYGON);
for(i=0; i<n; ++i)
glVertex2f(R*cos(2*Pi/n*i), R*sin(2*Pi/n*i));
glEnd();*/
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 0);
glutInitWindowSize(1024, 768);
glutCreateWindow("OpenGL基本绘制");
glutDisplayFunc(&myDisplay);
glutMainLoop();
return 0;
}
网上说要改下图的_CONSOLE,我没改也能在开发环境下执行。
三 样例2
#include <GL/glut.h>
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("第一个OpenGL程序");
glutDisplayFunc(&myDisplay);
glutMainLoop();
return 0;
}
四 样例3
#include <GL/glut.h>
//Called to draw scene
void RenderSence(void)
{
//Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT);
//Flush drawing commands
glFlush();
}
//Set up the rendering state
void SetupRC(void)
{
glClearColor(0.0f,0.0f,1.0f,1.0f); //此时背景色为蓝色
}
int main(int argc, char *argv[])
{
//glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_SINGLE|GLUT_RGB);
glutCreateWindow("Simple"); //窗体名为“Simple”
glutDisplayFunc(RenderSence);
SetupRC();
glutMainLoop();
return 0;
}
编译,出现一堆下述错误;
demo4.cpp
y:\dddd10\vc6opengldemo\demo4\demo4.cpp(5) : error C2018: unknown character ‘0xa1‘
y:\dddd10\vc6opengldemo\demo4\demo4.cpp(5) : error C2018: unknown character ‘0xa1‘
y:\dddd10\vc6opengldemo\demo4\demo4.cpp(10) : error C2018: unknown character ‘0xa1‘
......
是由于代码中包括全角字符;寻找发现该全角字符是空格;用替换功能,把全角空格所有替换为半角空格再编译就可以;
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" >
五 样例4
#include <GL/glut.h>
void background(void)
{
glClearColor(0.0,0.0,0.0,0.0);//设置背景颜色为黑色
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);//buffer设置为颜色可写
glBegin(GL_TRIANGLES);//開始画三角形
glShadeModel(GL_SMOOTH);//设置为光滑明暗模式
glColor3f(1.0,0.0,0.0);//设置第一个顶点为红色
glVertex2f(-1.0,-1.0);//设置第一个顶点的坐标为(-1.0,-1.0)
glColor3f(0.0,1.0,0.0);//设置第二个顶点为绿色
glVertex2f(0.0,-1.0);//设置第二个顶点的坐标为(0.0,-1.0)
glColor3f(0.0,0.0,1.0);//设置第三个顶点为蓝色
glVertex2f(-0.5,1.0);//设置第三个顶点的坐标为(-0.5。1.0)
glEnd();//三角形结束
glFlush();//强制OpenGL函数在有限时间内执行
}
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);
}
六 opengl资源链接
http://www.cnblogs.com/phinecos/category/90224.html
基于MFC的opengl编程