雾效果是就是是远处看的模糊,一种类似于大气效果。雾是在执行了矩阵变换,光照,纹理后才会应用,因此雾效果对上述内容都会产生印象。在大型项目中雾气可以提高性能,因为他可以选择不绘画因为雾气影响而看不见的物体
雾的效果做法:
1.设置雾气的模式 glFogi(GL_FOG_MODE, fogMode[fogfilter])
2.设置雾的颜色 glFogfv(GL_FOG_COLOR, fogColor);
3.设置雾的密度 glFogf(GL_FOG_DENSITY, 0.35f);
4.设置系统如何计算雾气 glHint(GL_FOG_HINT, GL_DONT_CARE);
5.雾气的开始位置 glFogf(GL_FOG_START, 1.0f);
6.雾气的结束位置 glFogf(GL_FOG_END, 5.0f);
7.使用雾气 glEnable(GL_FOG);
glFogi | GL_FOG_MODE | GL_EXP | 充满整个屏幕的基本渲染的雾。它能在较老的PC上工作,因此并不是特别像雾。 F=e-(density*z) EXP雾气只和GL_FOG_DENSITY有关 |
GL_EXP2 | 比GL_EXP更进一步。它也是充满整个屏幕,但它使屏幕看起来更有深度 F=e-(density*z)*e-(density*z) EXP2雾气只和GL_FOG_DENSITY有关 |
||
GL_LINEAR | 最好的渲染模式。物体淡入淡出的效果更自然。 F=end-z/end-start EXP雾气只和GL_FOG_START与GL_FOG_END有关 |
||
GL_FOG_COLOR | 雾气颜色 | ||
GL_FOG_DENSITY | 雾气密度 | ||
GL_FOG_START | 设定雾效距屏幕多近开始 | ||
GL_FOG_END | 设定雾效持续到距屏幕多远 |
雾气效果修正
glHint(GL_FOG_HINT,gl_nicest|gl_fastest|gl_dont_care)
GL_EXP GL_EXP2 GL_LINEAR
雾坐标
显示的为每个顶点指定雾气值。比如顶点1,2,3,4 Z值为-1.那么在雾气效果中这几个顶点的视觉效果应该是一致的,但是通过为每个顶点设置雾坐标。即使他们Z值相同,仍然可以有不同的效果
顶点Z值都为0,但是雾气坐标的分别为0,5,10 顶点Z值为0,-5,-10 不含雾气坐标
调用glFogi(GL_FOG_COORD_SRC, GL_FOG_COORD) 显示为每个顶点指定Z值(雾坐标Z值)可以使用 glFogCoordf (f1) 为每个顶点指定雾坐标
glBegin (GL_TRIANGLES);
glFogCoordf (f1);
glVertex3f (2.0f, -2.0f, 0.0f);
glFogCoordf (f2);
glVertex3f (-2.0f, 0.0f, -5.0f);
glFogCoordf (f3);
glVertex3f (0.0f, 2.0f, -10.0f);
glEnd();
雾DEMO
#include "header.h" static GLint fogMode; static void init(void) { GLfloat position[] = { 0.5, 0.5, 1.0, 0.0 }; glEnable(GL_DEPTH_TEST); glLightfv(GL_LIGHT0, GL_POSITION, position); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); { GLfloat mat[3] = {0.1745, 0.01175, 0.01175}; glMaterialfv (GL_FRONT, GL_AMBIENT, mat); mat[0] = 0.61424; mat[1] = 0.04136; mat[2] = 0.04136; glMaterialfv (GL_FRONT, GL_DIFFUSE, mat); mat[0] = 0.727811; mat[1] = 0.626959; mat[2] = 0.626959; glMaterialfv (GL_FRONT, GL_SPECULAR, mat); glMaterialf (GL_FRONT, GL_SHININESS, 0.6*128.0); } glEnable(GL_FOG); { GLfloat fogColor[4] = {0.5, 0.5, 0.5, 1.0}; fogMode = GL_EXP; glFogi (GL_FOG_MODE, fogMode); glFogfv (GL_FOG_COLOR, fogColor); glFogf (GL_FOG_DENSITY, 0.5); glHint (GL_FOG_HINT, GL_DONT_CARE); glFogf (GL_FOG_START, 6.0); glFogf (GL_FOG_END, 10.0); } glClearColor(0.5, 0.5, 0.5, 1.0); /* fog color */ } static void renderSphere (GLfloat x, GLfloat y, GLfloat z) { glPushMatrix(); glTranslatef (x, y, z); glutSolidSphere(0.4, 16, 16); glPopMatrix(); } void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); renderSphere (-2., -0.5, -1.0); renderSphere (-1., -0.5, -2.0); renderSphere (0., -0.5, -3.0); renderSphere (1., -0.5, -4.0); renderSphere (2., -0.5, -5.0); glFlush(); } void reshape(int w, int h) { glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= h) glOrtho (-2.5, 2.5, -2.5*(GLfloat)h/(GLfloat)w, 2.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0); else glOrtho (-2.5*(GLfloat)w/(GLfloat)h, 2.5*(GLfloat)w/(GLfloat)h, -2.5, 2.5, -10.0, 10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity (); } void keyboard(unsigned char key, int x, int y) { switch (key) { case 'f': case 'F': if (fogMode == GL_EXP) { fogMode = GL_EXP2; printf ("Fog mode is GL_EXP2\n"); } else if (fogMode == GL_EXP2) { fogMode = GL_LINEAR; printf ("Fog mode is GL_LINEAR\n"); } else if (fogMode == GL_LINEAR) { fogMode = GL_EXP; printf ("Fog mode is GL_EXP\n"); } glFogi (GL_FOG_MODE, fogMode); glutPostRedisplay(); break; case 27: exit(0); break; default: break; } } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow("雾"); init(); glutReshapeFunc (reshape); glutKeyboardFunc (keyboard); glutDisplayFunc (display); glutMainLoop(); return 0; }
雾坐标DEMO
#include "header.h" static GLfloat f1, f2, f3; /* Initialize fog */ static void init(void) { GLfloat fogColor[4] = {0.0, 0.25, 0.25, 1.0}; f1 = 1.0f; f2 = 5.0f; f3 = 10.0f; glEnable(GL_FOG); glFogi (GL_FOG_MODE, GL_EXP); glFogfv (GL_FOG_COLOR, fogColor); glFogf (GL_FOG_DENSITY, 0.25); glHint (GL_FOG_HINT, GL_DONT_CARE); glFogi(GL_FOG_COORD_SRC, GL_FOG_COORD); glClearColor(0.0, 0.25, 0.25, 1.0); /* fog color */ } /* display() draws a triangle at an angle. */ void display(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f (1.0f, 0.75f, 0.0f); glBegin (GL_TRIANGLES); glFogCoordf (f1); glVertex3f (2.0f, -2.0f, 0.0f); glFogCoordf (f2); glVertex3f (-2.0f, 0.0f, -5.0f); glFogCoordf (f3); glVertex3f (0.0f, 2.0f, -10.0f); glEnd(); glutSwapBuffers(); } void reshape(int w, int h) { glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective (45.0, 1.0, 0.25, 25.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity (); glTranslatef (0.0, 0.0, -5.0); } void keyboard(unsigned char key, int x, int y) { switch (key) { case 'c': glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT); glutPostRedisplay(); break; case 'C': glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT); glutPostRedisplay(); break; case '1': f1 = f1 + 0.25; glutPostRedisplay(); break; case '2': f2 = f2 + 0.25; glutPostRedisplay(); break; case '3': f3 = f3 + 0.25; glutPostRedisplay(); break; case '8': if (f1 > 0.25) { f1 = f1 - 0.25; glutPostRedisplay(); } break; case '9': if (f2 > 0.25) { f2 = f2 - 0.25; glutPostRedisplay(); } break; case '0': if (f3 > 0.25) { f3 = f3 - 0.25; glutPostRedisplay(); } break; case 'b': glMatrixMode (GL_MODELVIEW); glTranslatef (0.0, 0.0, -0.25); glutPostRedisplay(); break; case 'f': glMatrixMode (GL_MODELVIEW); glTranslatef (0.0, 0.0, 0.25); glutPostRedisplay(); break; case 27: exit(0); break; default: break; } } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(500, 500); glutCreateWindow("雾坐标"); init(); glutReshapeFunc (reshape); glutKeyboardFunc (keyboard); glutDisplayFunc (display); glewInit(); glutMainLoop(); return 0; }