//sample.cpp
#include <gl\glos.h>
#include <gl\gl.h>//OpenGL32库的头文件
#include <gl\glu.h>//Glu32库的头文件
#include <gl\glut.h>//OpenGL实用库的头文件
#include <gl\glaux.h>//GLaux库的头文件
#include "windows.h"
void myinit(void);
void CALLBACK display(void);
void CALLBACK reshape(GLsizei w,GLsizei h);
void myinit(void)
{
auxInitDisplayMode(AUX_SINGLE|AUX_RGBA);
auxInitPosition(0,0,500,500);
auxInitWindow((LPCWSTR)"sample1");
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
GLfloat mat_ambient[]={0.0,0.1,0.8,1.0};
GLfloat mat_diffuse[]={0.0,0.3,0.6,1.0};
GLfloat mat_specular[]={1.0,0.0,1.0,1.0};
GLfloat mat_shininess[]={15.0};
GLfloat position[]={5.0,5.0,5.0,0.0};
GLfloat fogColor[4]={0.6,0.6,0.0,1.0};
glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
glLightfv(GL_LIGHT0,GL_POSITION,position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glFrontFace(GL_CW);
// glEnable(GL_POLYGON_SMOOTH);
// glEnable(GL_BLEND);
// glBlendFunc(GL_SRC_ALPHA,GL_ONE);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
//启用雾化处理
glEnable(GL_FOG);
{
//采用线性变化的雾化效果
glFogi(GL_FOG_MODE,GL_LINEAR);
//指定雾化颜色(黄色)
glFogfv(GL_FOG_COLOR,fogColor);
//指定按线性变化时计算公式的参量
glFogf(GL_FOG_START,3.0);
glFogf(GL_FOG_END,15.0);
//规定雾化效果的质量
glHint(GL_FOG_HINT,GL_DONT_CARE);
}
// glShadeModel(GL_FLAT);
}
void CALLBACK reshape(GLsizei w,GLsizei h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h*3)
glOrtho(-6.0,6.0,-2.0*(GLfloat)h*3/(GLfloat)w,
2.0*(GLfloat)h*3/(GLfloat)w,0.0,10.0);
else
glOrtho(-6.0*(GLfloat)h/(GLfloat)w,
6.0*(GLfloat)h/(GLfloat)w,-2.0,2.0,0.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void CALLBACK display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//在不同远近(Z方向)绘制同样大小、颜色的环,显示雾化的效果
glPushMatrix();
glTranslatef(-3.0,-1.5,-3.0);
auxSolidTorus(0.6,1.5);
glPopMatrix();
glPushMatrix();
glTranslatef(-0.5,-0.5,-6.0);
auxSolidTorus(0.6,1.5);
glPopMatrix();
glPushMatrix();
glTranslatef(2.0,0.5,-8.0);
auxSolidTorus(0.6,1.5);
glPopMatrix();
glFlush();
}
void main(void)
{
myinit();
auxReshapeFunc(reshape);
auxMainLoop(display);
}
//end of sample