opengl 雾效

// fog.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"
#include <iostream>
#include <gl/glew.h>
#include <gl/glut.h>




#pragma comment(lib, "glew32.lib")
#pragma comment(lib, "glut32.lib")


float light_pos[] = {0.5, 0, 3};


void init()
{
glClearColor(0.5,0.5,0.5,1.0);
glEnable(GL_DEPTH_TEST);

float fogColor[] = {0.5, 0.5, 0.5, 1.0};//same to clear Color
glFogi(GL_FOG_MODE, GL_EXP);
glFogfv(GL_COLOR, fogColor);
glFogf(GL_FOG_DENSITY, 0.3);
glEnable(GL_FOG);


glFogf(GL_FOG_START, -3);
glFogf(GL_FOG_END, 3);


glLightfv(GL_LIGHT0,GL_POSITION, light_pos);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

}


void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


//glPushMatrix();
//glTranslatef(light_pos[0], light_pos[1], light_pos[2]);
//glutWireCube(0.2);
//glPopMatrix();


static float mat_color[] = {0, 0, 0.2, 1.0};
static float mat_diff[] = {0.6, 0.06, 0.004};
static float mat_specular[] = {0.7, 0.6, 0.62};
static float mat_shiness[] = {50};
for (int i=0; i<4; ++i)
{
glPushMatrix();
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_color);
glMaterialf(GL_FRONT, GL_SHININESS, mat_shiness[0]);


glTranslatef(-2.0 + 1.2*i, 0,  -2+i);
//glutSolidSphere(0.4, 512, 16);
glutSolidTeapot(.4);
glPopMatrix();
}


glutSwapBuffers();
}


void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w > h)
{
float factor = (float)w/h;
glOrtho(-3 *factor, 3 *factor, -3, 3, -3, 3);
}
else
{
float factor = (float)h/w;
glOrtho(-3, 3, -3*factor, 3*factor, -3, 3);
}


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}


void keyboard(unsigned char key, int x, int y)
{
static int mode = 0;
switch (key)
{
case 'm':
case 'M':
{
GLint value = 0;
mode = (++mode) % 3;
switch (mode)
{
case 0:
value = GL_EXP2;
break;
case 1:
value = GL_EXP;
break;
case 2:
value = GL_LINEAR;
break;
default:
break;
}


glFogi(GL_FOG_MODE, value);
glutPostRedisplay();
break;
}
case 27:
exit(0);
break;
default:
break;
}
}


int _tmain(int argc, _TCHAR* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH);
glutInitWindowPosition(300, 300);
glutInitWindowSize(600, 600);
glutCreateWindow("Fog");

GLenum ret = glewInit();
if (ret != GLEW_NO_ERROR)
return EXIT_FAILURE;
init();


glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();


return 0;

}


opengl 雾效_第1张图片


你可能感兴趣的:(OpenGL,雾效)