一、下载并安装glut库
opengl的glut库 GLUT不是OpenGL所必须的,但它会给学习带来一定的方便,推荐安装。
Windows环境下的GLUT下载地址:(大小约为150k)
http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip
Windows环境下安装GLUT的步骤:
1、将下载的压缩包解开,将得到5个文件
2、在“我的电脑”中搜索“gl.h”,并找到其所在文件夹(Program Files\Microsoft Visual Studio\VC98\Include\GL文件夹”)。把解压得到的glut.h放到这个文件夹。
3、把解压得到的glut.lib和glut32.lib放到静态函数库所在文件夹(Program Files\Microsoft Visual Studio\VC98\lib”文件夹)。
4、把解压得到的glut.dll和glut32.dll放到操作系统目录下面的system32文件夹内。(典型的位置为:C:\Windows\System32)
二、VC工程配置:
1)创建一个Win32 Console Application。
2)链接OpenGL libraries。单击Project,再单击Settings,再找到Link单击,最后在Object/library modules 的最前面加上opengl32.lib glu32.lib glut.lib glaux.lib
3)修改代码如下:
![](/Images/OutliningIndicators/None.gif)
#include "stdafx.h"
#include <GL/glut.h>
![](/Images/OutliningIndicators/None.gif)
void display(void)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
glClear (GL_COLOR_BUFFER_BIT);/**//* clear all pixels */
glColor3f (1.0, 1.0, 1.0);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
glBegin(GL_POLYGON);/**//* draw white polygon with corners at(0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)*/
glVertex3f (0.25, 0.25, 0.0);
glVertex3f (0.75, 0.25, 0.0);
glVertex3f (0.75, 0.75, 0.0);
glVertex3f (0.25, 0.75, 0.0);
glEnd();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
glFlush ();/**//* start processing buffered OpenGL routines */
}
![](/Images/OutliningIndicators/None.gif)
void init (void)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
glClearColor (0.0, 0.0, 0.0, 0.0);/**//* select clearing color */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);/**//* initialize viewing values */
}
![](/Images/OutliningIndicators/None.gif)
int main(int argc, char** argv)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
glutInit(&argc, argv);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);/**//*Declare initial display mode(single buffer and RGBA).*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
glutInitWindowSize (250, 250); /**//*Declare initial window size.*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
glutInitWindowPosition (100, 100);/**//*Declare initial window position.*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
glutCreateWindow ("hello");/**//*Open window with "hello"in its title bar.*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
init ();/**//*Call initialization routines.*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
glutDisplayFunc(display); /**//*Register callback function to display graphics.*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
glutMainLoop();/**//*Enter main loop and process events.*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
return 0; /**//* ANSI C requires main to return int. */
}