Vous êtes sur la page 1sur 3

So now that we have the basics of 3d design and some simple 3d manipulations...

Lets learn how to set


up an environment for our objects. The first thing we will learn is how to create your own fog. Fog in
OpenGL is very... very simple to setup and use. This is how we go about doing that...

glFogi(GL_FOG_MODE, GL_EXP)
glFogfv(GL_FOG_COLOR, vec4(.5, .5, .5, 1))
glFogf(GL_FOG_DENSITY, 0.4)
glEnable(GL_FOG)

Thats it... Basically, all you need to know are the fog color, and density parts... The vec4(.5, .5, .5, 0) is
similar to how we colored our objects in the first tutorial. vec4(red, green, blue, alpha). Dont worry
about the alpha right now, you can keep that at 0. So, that is how we tell OpenGL what color we want
the fog to be. The density is even simpler... 0.4 is our density, the higher this number is, the more dense
the fog is, hence the harder it is to see through. The lower that number is... The clearer everything will
become. Fog is a very effective method of showing depth in a 3d application because people can
quickly determine distance by how far into the fog it is. Where as there is no way of telling how far
away an object without fog or light is without other objects to judge by. So now... Another thing you
need to add (well, you don't NEED to) is a clear color... This is the color of our background. The reason
we should always define this is because we almost always want the fog to be the color of the
background. You can try it without, but you simply will not get the same results. Dont worry it is very
simple...
glClearColor(.5, .5, .5, 1)

That just sets our background to the color of the fog. Obviously, if you change your fog color, be sure
to change the background color as well. I can't make you or anything, but you will most likely find that
to be the best looking method.

Lets just add that fog routine into our previous example (from tutorial 2) and view our cube with some
nice looking fog.
dim Texture1
dim Texture2
dim Texture3
dim Texture4
dim Texture5
dim Texture6

dim Yangle#, Xangle#

Texture1=LoadTexture("textures\00001.jpg")
Texture2=LoadTexture("textures\00002.jpg")
Texture3=LoadTexture("textures\00003.jpg")
Texture4=LoadTexture("textures\00004.jpg")
Texture5=LoadTexture("textures\00005.jpg")
Texture6=LoadTexture("textures\00006.jpg")

glEnable(GL_TEXTURE_2D)

''' [Here is the clear color


glClearColor(.5, .5, .5, 1)
''' [Here is the fog]
glFogi(GL_FOG_MODE, GL_EXP)
glFogfv(GL_FOG_COLOR, vec4(.5, .5, .5, 1))
glFogf(GL_FOG_DENSITY, 0.4)
glEnable(GL_FOG)

do
glClear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glTranslatef(0,0,-4)
glRotatef(YAngle#, 0, 1, 0)
glRotatef(XAngle#, 1, 0, 0)

glBindTexture(GL_TEXTURE_2D, Texture1)
glBegin(GL_QUADS)
glTexCoord2f(0,1)
glVertex3f(-1, 1, -1)
glTexCoord2f(1,1)
glVertex3f( 1, 1, -1)
glTexCoord2f(1,0)
glVertex3f( 1,-1, -1)
glTexCoord2f(0,0)
glVertex3f(-1,-1, -1)
glEnd()

glBindTexture(GL_TEXTURE_2D, Texture2)
glBegin(GL_QUADS)
glTexCoord2f(0,1)
glVertex3f(-1, 1, 1)
glTexCoord2f(1,1)
glVertex3f( 1, 1, 1)
glTexCoord2f(1,0)
glVertex3f( 1,-1, 1)
glTexCoord2f(0,0)
glVertex3f(-1,-1, 1)
glEnd()

glBindTexture(GL_TEXTURE_2D, Texture3)
glBegin(GL_QUADS)
glTexCoord2f(0,1)
glVertex3f(-1,-1, 1)
glTexCoord2f(1,1)
glVertex3f(-1, 1, 1)
glTexCoord2f(1,0)
glVertex3f(-1, 1,-1)
glTexCoord2f(0,0)
glVertex3f(-1,-1,-1)
glEnd()

glBindTexture(GL_TEXTURE_2D, Texture4)
glBegin(GL_QUADS)
glTexCoord2f(0,1)
glVertex3f(1,-1, 1)
glTexCoord2f(1,1)
glVertex3f(1, 1, 1)
glTexCoord2f(1,0)
glVertex3f(1, 1,-1)
glTexCoord2f(0,0)
glVertex3f(1,-1,-1)
glEnd()

glBindTexture(GL_TEXTURE_2D, Texture5)
glBegin(GL_QUADS)
glTexCoord2f(0,1)
glVertex3f(-1, 1, 1)
glTexCoord2f(1,1)
glVertex3f( 1, 1, 1)
glTexCoord2f(1,0)
glVertex3f( 1, 1,-1)
glTexCoord2f(0,0)
glVertex3f(-1, 1,-1)
glEnd()

glBindTexture(GL_TEXTURE_2D, Texture6)
glBegin(GL_QUADS)
glTexCoord2f(0,1)
glVertex3f(-1,-1, 1)
glTexCoord2f(1,1)
glVertex3f( 1,-1, 1)
glTexCoord2f(1,0)
glVertex3f( 1,-1,-1)
glTexCoord2f(0,0)
glVertex3f(-1,-1,-1)
glEnd()

Yangle#=Yangle#+.05
Xangle#=Xangle#+.025
SwapBuffers ()
loop

Ok, cool, that effect is a very easy way to increase the realism of your environment. Also, there are a
couple of other methods of fog that you COULD use.

Where you see the GL_EXP in our fog routine, this could be any of these:
• GL_EXP
• GL_EXP2
• GL_LINEAR

They are just different modes of fog that can be used. They mostly affect things like the rate of cutoff
(how the distance is releative to the density). Feel free to experiment with these, who knows, maybe
you are a LINEAR kind of person and just dont know it...

Well, in a nutshell, that is fog. Simple... Effective. Also, a cheap illusion of lighting can be achieved by
completely black fog... This is especially a nice effect in FPS style games.

<--- Back to part 2


Or

Vous aimerez peut-être aussi