Hi !
I've troubles using Fog with Opengl...
I'm setting all up like in Nehe's tutorials, but unfortunately the fog doesn't show up...
BTW, I'm wondering, how do OpenGL know where the fog need to start and to end ? How can it know the camera position ?
OpenGL & Fog
Re: OpenGL & Fog
What's your exact problem?
If your code looks something like the following, it should work.
BTW, even if it's not necessary in order to make fogging work, of course
OpenGL keeps track of all matrices. How else would e.g. glRotatef_() work
at all?
If your code looks something like the following, it should work.
Code: Select all
glFogi_(#GL_FOG_MODE, #GL_LINEAR)
glFogf_(#GL_FOG_START, 0.0)
glFogf_(#GL_FOG_END, 7.0)
glEnable_(#GL_FOG)
Well, it doesn't. OpenGL fog is just blending, no magic involved.Polo wrote:BTW, I'm wondering, how do OpenGL know where the fog need to start and to end ? How can it know the camera position ?
BTW, even if it's not necessary in order to make fogging work, of course
OpenGL keeps track of all matrices. How else would e.g. glRotatef_() work
at all?
Good programmers don't comment their code. It was hard to write, should be hard to read.
-
DarkDragon
- Addict

- Posts: 2347
- Joined: Mon Jun 02, 2003 9:16 am
- Location: Germany
- Contact:
Re: OpenGL & Fog
Are we talking about normal fog with no extension or are we talking about the volumetric fog?
bye,
Daniel
Daniel
Well, normal fog.
My code is like Traumatic's, but unfortunately I got a... strange result
http://gtnsoft.free.fr/fogtest.jpg
Hum... Well....
I must do something wrong somewhere...
I've put a color, without the color it looks the same, but the things you see grey are black...
My code is like Traumatic's, but unfortunately I got a... strange result
http://gtnsoft.free.fr/fogtest.jpg
Hum... Well....
I must do something wrong somewhere...
Code: Select all
c=AllocateMemory(16)
PokeF(c,0.5)
PokeF(c+4,0.5)
PokeF(c+8,0.5)
PokeF(c+12,0.5)
glFogfv_(#GL_FOG_COLOR, c)
FreeMemory(c)
glFogi_(#GL_FOG_MODE, #GL_EXP)
glFogf_(#GL_FOG_START, 0.0);;
glFogf_(#GL_FOG_END, 7.0)
glEnable_(#GL_FOG)-
DarkDragon
- Addict

- Posts: 2347
- Joined: Mon Jun 02, 2003 9:16 am
- Location: Germany
- Contact:
Yes, it's right the picture shows the fog right yeah, like here too.Polo wrote:Well, normal fog.
My code is like Traumatic's, but unfortunately I got a... strange result![]()
http://gtnsoft.free.fr/fogtest.jpg
Use volumetric fog and you get results like that what you want:
Code: Select all
XIncludeFile "C:\Dokumente und Einstellungen\Daniel\Eigene Dateien\Eigene Programme\GL\GL.pbi"
#WindowWidth = 500
#WindowHeight = 400
#WindowFlags = #PB_Window_TitleBar | #PB_Window_MaximizeGadget | #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered
Version.f = 1.0
#GL_FOG_COORDINATE_SOURCE_EXT = $8450
#GL_FOG_COORDINATE_EXT = $8451
Global hWnd.l, Event
Procedure MyWindowCallback(WindowID, Message, wParam, lParam)
Result = #PB_ProcessPureBasicEvents
If Message = #WM_SIZE
glViewport_(0, 0, WindowWidth(), WindowHeight())
Result = 1
EndIf
ProcedureReturn Result
EndProcedure
Procedure InitExtension()
Shared glFogCoordfEXT
If FindString(PeekS(glGetString_(#GL_EXTENSIONS)), "GL_EXT_fog_coord", 0) > 0
glFogCoordfEXT = wglGetProcAddress_("glFogCoordfEXT")
ProcedureReturn 1
Else
ProcedureReturn 0
EndIf
EndProcedure
Procedure glFogCoordf_(Val.f)
Shared glFogCoordfEXT
ProcedureReturn CallFunctionFast(glFogCoordfEXT, Val.f)
EndProcedure
Procedure SetCamera(FOV.f, X.f, Y.f, Z.f, AngleX.f, AngleY.f, AngleZ.f, Near.f, Far.f) ;Sets the cameraposition and the FOV
glMatrixMode_(5889)
glLoadIdentity_()
gluPerspective__(FOV, WindowWidth()/WindowHeight(), Near.f, Far.f)
glRotatef_(AngleX, 1.0, 0.0, 0.0)
glRotatef_(AngleY, 0.0, 1.0, 0.0)
glRotatef_(AngleZ, 0.0, 0.0, 1.0)
glTranslatef_(X, Y, Z)
glMatrixMode_(5888)
glDepthRange__(Near.f, Far.f)
EndProcedure
Procedure LoadTexture_(Filename.s, TexFilter.l) ; Returns the texture for an OpenGL application
img = LoadImage(#PB_Any, Filename.s)
Width.l=ImageWidth()
Height.l=ImageHeight()
Size.l=Width * Height
Dim ImageData.b(Size*3)
StartDrawing(ImageOutput())
For Y=0 To Height-1
For X=0 To Width-1
Color = Point(X,Y)
ImageData(i)=Red(Color)
i+1
ImageData(i)=Green(Color)
i+1
ImageData(i)=Blue(Color)
i+1
Next
Next
StopDrawing()
glGenTextures_(1, @Tex) ;Textur speichern
If TexFilter = 0 ;versch. Textur-Filter
glBindTexture_($0DE1, Tex)
glTexParameteri_($0DE1, $2800, $2600)
glTexParameteri_($0DE1, $2801, $2600)
glTexImage2D_($0DE1, 0, 6407, Width, Height, 0, 6407, $1401, @ImageData())
ElseIf TexFilter = 1
glBindTexture_($0DE1, Tex)
glTexParameteri_($0DE1, $2800, $2601)
glTexParameteri_($0DE1, $2801, $2601)
glTexImage2D_($0DE1, 0, 6407, Width, Height, 0, 6407, $1401, @ImageData())
ElseIf TexFilter = 2
glBindTexture_($0DE1, Tex)
glTexParameteri_($0DE1, $2800, $2601)
glTexParameteri_($0DE1, $2801, $2701)
gluBuild2DMipmaps_($0DE1, 3, Width, Height, 6407, $1401, @ImageData())
EndIf
FreeImage(img)
ProcedureReturn Tex
EndProcedure
Procedure DrawObject(Steps, Stacks)
Shared X.f, Y.f, Z.f
X.f = X * -1
Y.f = Y * -1
Z.f = Z * -1
glBegin_(#GL_TRIANGLES)
a.f : b.f
While a <= #PI*2
b = 0
While b <= #PI*2
mx.f = Sin(b)*Cos(a)
my.f = Sin(a)
mz.f = Cos(b)*Cos(a)
glFogCoordf_(Sqr(Pow(X-mx, 2)+Pow(Y-my, 2)+Pow(Z-mz, 2)))
glVertex3f_(mx, my, mz)
mx.f = Sin(b)*Cos(a + (#PI*2)/Steps)
my.f = Sin(a + (#PI*2)/Steps)
mz.f = Cos(b)*Cos(a + (#PI*2)/Steps)
glFogCoordf_(Sqr(Pow(X-mx, 2)+Pow(Y-my, 2)+Pow(Z-mz, 2)))
glVertex3f_(mx, my, mz)
mx.f = Sin(b + (#PI*2)/Stacks)*Cos(a + (#PI*2)/Steps)
my.f = Sin(a + (#PI*2)/Steps )
mz.f = Cos(b + (#PI*2)/Stacks)*Cos(a + (#PI*2)/Steps)
glFogCoordf_(Sqr(Pow(X-mx, 2)+Pow(Y-my, 2)+Pow(Z-mz, 2)))
glVertex3f_(mx, my, mz)
mx.f = Sin(b + (#PI*2)/Stacks)*Cos(a + (#PI*2)/Steps)
my.f = Sin(a + (#PI*2)/Steps )
mz.f = Cos(b + (#PI*2)/Stacks)*Cos(a + (#PI*2)/Steps)
glFogCoordf_(Sqr(Pow(X-mx, 2)+Pow(Y-my, 2)+Pow(Z-mz, 2)))
glVertex3f_(mx, my, mz)
mx.f = Sin(b + (#PI*2)/Stacks)*Cos(a)
my.f = Sin(a)
mz.f = Cos(b + (#PI*2)/Stacks)*Cos(a)
glFogCoordf_(Sqr(Pow(X-mx, 2)+Pow(Y-my, 2)+Pow(Z-mz, 2)))
glVertex3f_(mx, my, mz)
mx.f = Sin(b)*Cos(a)
my.f = Sin(a)
mz.f = Cos(b)*Cos(a)
glFogCoordf_(Sqr(Pow(X-mx, 2)+Pow(Y-my, 2)+Pow(Z-mz, 2)))
glVertex3f_(mx, my, mz)
b + (#PI*2)/Stacks
Wend
a + (#PI*2)/Steps
Wend
glEnd_()
X.f = X * -1
Y.f = Y * -1
Z.f = Z * -1
EndProcedure
If OpenWindow(0, 0, 0, #WindowWidth, #WindowHeight, #WindowFlags, "OpenGL Scene "+StrF(Version, 1))
SetWindowCallback(@MyWindowCallback())
hWnd = WindowID(0)
hDC = GetDC_(hWnd)
;Initialize OpenGL
pfd.PIXELFORMATDESCRIPTOR
pfd\nSize = SizeOf(PIXELFORMATDESCRIPTOR)
pfd\nVersion = 1
pfd\dwFlags = #PFD_SUPPORT_OPENGL | #PFD_DOUBLEBUFFER | #PFD_DRAW_TO_WINDOW
pfd\iLayerType = #PFD_MAIN_PLANE
pfd\iPixelType = #PFD_TYPE_RGBA
pfd\cColorBits = 24
pfd\cDepthBits = 32
Pixformat = ChoosePixelFormat_(hDC, pfd)
SetPixelFormat_(hDC, Pixformat, pfd)
hrc = wglCreateContext_(hDC)
wglMakeCurrent_(hDC, hrc)
SwapBuffers_(hDC)
glEnable_(#GL_DEPTH_TEST)
If InitExtension()
Dim fogColor.f(3)
fogColor(0) = 0.25
fogColor(1) = 0.25
fogColor(2) = 0.25
fogColor(3) = 1.0
glEnable_(#GL_FOG)
glFogi_(#GL_FOG_MODE, #GL_LINEAR)
glFogfv_(#GL_FOG_COLOR, @fogColor())
glFogf_(#GL_FOG_START , -0.5)
glFogf_(#GL_FOG_END , 7.5)
glHint_(#GL_FOG_HINT , #GL_NICEST)
glFogi_(#GL_FOG_COORDINATE_SOURCE_EXT, #GL_FOG_COORDINATE_EXT)
glClearColor_(fogColor(0), fogColor(1), fogColor(2), fogColor(3))
; glPolygonMode_(#GL_FRONT_AND_BACK, #GL_LINE) ;Just wireframe mode
AngleX.f
AngleY.f
AngleZ.f
X.f
Y.f
Z.f = -5.0
Repeat
Rot.f + 0.5
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
SetCamera(45.0, X, Y, Z, AngleX, AngleY, AngleZ, 0.1, 50.0)
glLoadIdentity_()
DrawObject(20, 20)
SwapBuffers_(hDC)
If GetAsyncKeyState_(#VK_UP)
Z + Cos(AngleX/180*#PI)*Cos(AngleY/180*#PI)*0.04
Y + Sin(AngleX/180*#PI)*0.04
X - Sin(AngleY/180*#PI)*Cos(AngleX/180*#PI)*0.04
ElseIf GetAsyncKeyState_(#VK_DOWN)
Z - Cos(AngleX/180*#PI)*Cos(AngleY/180*#PI)*0.04
Y - Sin(AngleX/180*#PI)*0.04
X + Sin(AngleY/180*#PI)*Cos(AngleX/180*#PI)*0.04
EndIf
If GetAsyncKeyState_(#VK_LEFT)
AngleY - 0.5
ElseIf GetAsyncKeyState_(#VK_RIGHT)
AngleY + 0.5
EndIf
If GetAsyncKeyState_(#VK_PRIOR)
AngleX - 0.5
ElseIf GetAsyncKeyState_(#VK_NEXT)
AngleX + 0.5
EndIf
Event = WindowEvent()
Until Event = #PB_Event_CloseWindow
EndIf
ReleaseDC_(hWnd, hDC)
EndIf
Endbye,
Daniel
Daniel
Hi !
Thanks for your code, it seems it does what I want, but...
You seem to call the extension glFogCoordf with glVertex3f... It's unusable, since calling glVertex is really slow !
But it seems that with the traumatic's code it should work :
http://nehe.gamedev.net/data/lessons/le ... ?lesson=16
On this nehe tutorial, it's what I'm trying to do... I don't understand why it works for them and not for me..
Thanks for your code, it seems it does what I want, but...
You seem to call the extension glFogCoordf with glVertex3f... It's unusable, since calling glVertex is really slow !
But it seems that with the traumatic's code it should work :
http://nehe.gamedev.net/data/lessons/le ... ?lesson=16
On this nehe tutorial, it's what I'm trying to do... I don't understand why it works for them and not for me..
-
DarkDragon
- Addict

- Posts: 2347
- Joined: Mon Jun 02, 2003 9:16 am
- Location: Germany
- Contact:
Ok, I have translated a part from the NeHe tutorial:
It works well here.
But forget to manipulate the PerspectiveMatrix with glTranslate glRotate then
the fog will always be at 0, 0, 0 and nowhere else.
Code: Select all
XIncludeFile "C:\Dokumente und Einstellungen\Daniel\Eigene Dateien\Eigene Programme\GL\GL.pbi"
#WindowWidth = 500
#WindowHeight = 400
#WindowFlags = #PB_Window_TitleBar | #PB_Window_MaximizeGadget | #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered
Version.f = 1.0
Global hWnd.l, Event
Procedure MyWindowCallback(WindowID, Message, wParam, lParam)
Result = #PB_ProcessPureBasicEvents
If Message = #WM_SIZE
glViewport_(0, 0, WindowWidth(), WindowHeight())
Result = 1
EndIf
ProcedureReturn Result
EndProcedure
Procedure SetCamera(FOV.f, Near.f, Far.f)
glMatrixMode_(#GL_PROJECTION)
glLoadIdentity_()
gluPerspective__(FOV, WindowWidth()/WindowHeight(), Near.f, Far.f)
glMatrixMode_(#GL_MODELVIEW)
;glDepthRange__(Near.f, Far.f)
EndProcedure
Procedure LoadTexture_(Filename.s, TexFilter.l) ; Returns the texture for an OpenGL application
img = LoadImage(#PB_Any, Filename.s)
Width.l=ImageWidth()
Height.l=ImageHeight()
Size.l=Width * Height
Dim ImageData.b(Size*3)
StartDrawing(ImageOutput())
For Y=0 To Height-1
For X=0 To Width-1
Color = Point(X,Y)
ImageData(i)=Red(Color)
i+1
ImageData(i)=Green(Color)
i+1
ImageData(i)=Blue(Color)
i+1
Next
Next
StopDrawing()
glGenTextures_(1, @Tex) ;Textur speichern
If TexFilter = 0 ;versch. Textur-Filter
glBindTexture_($0DE1, Tex)
glTexParameteri_($0DE1, $2800, $2600)
glTexParameteri_($0DE1, $2801, $2600)
glTexImage2D_($0DE1, 0, 6407, Width, Height, 0, 6407, $1401, @ImageData())
ElseIf TexFilter = 1
glBindTexture_($0DE1, Tex)
glTexParameteri_($0DE1, $2800, $2601)
glTexParameteri_($0DE1, $2801, $2601)
glTexImage2D_($0DE1, 0, 6407, Width, Height, 0, 6407, $1401, @ImageData())
ElseIf TexFilter = 2
glBindTexture_($0DE1, Tex)
glTexParameteri_($0DE1, $2800, $2601)
glTexParameteri_($0DE1, $2801, $2701)
gluBuild2DMipmaps_($0DE1, 3, Width, Height, 6407, $1401, @ImageData())
EndIf
FreeImage(img)
ProcedureReturn Tex
EndProcedure
Procedure DrawCube()
glBegin_(#GL_QUADS)
;' Front Face
glNormal3f_( 0.0, 0.0, 1.0 )
glTexCoord2f_( 0.0, 0.0 ) : glVertex3f_( -1.0,-1.0, 1.0 )
glTexCoord2f_( 1.0, 0.0 ) : glVertex3f_( 1.0,-1.0, 1.0 )
glTexCoord2f_( 1.0, 1.0 ) : glVertex3f_( 1.0, 1.0, 1.0 )
glTexCoord2f_( 0.0, 1.0 ) : glVertex3f_( -1.0, 1.0, 1.0 )
;' Back Face
glNormal3f_( 0.0, 0.0,-1.0 )
glTexCoord2f_( 1.0, 0.0 ) : glVertex3f_( -1.0,-1.0,-1.0 )
glTexCoord2f_( 1.0, 1.0 ) : glVertex3f_( -1.0, 1.0,-1.0 )
glTexCoord2f_( 0.0, 1.0 ) : glVertex3f_( 1.0, 1.0,-1.0 )
glTexCoord2f_( 0.0, 0.0 ) : glVertex3f_( 1.0,-1.0,-1.0 )
;' Top Face
glNormal3f_( 0.0, 1.0, 0.0 )
glTexCoord2f_( 0.0, 1.0 ) : glVertex3f_( -1.0, 1.0,-1.0 )
glTexCoord2f_( 0.0, 0.0 ) : glVertex3f_( -1.0, 1.0, 1.0 )
glTexCoord2f_( 1.0, 0.0 ) : glVertex3f_( 1.0, 1.0, 1.0 )
glTexCoord2f_( 1.0, 1.0 ) : glVertex3f_( 1.0, 1.0,-1.0 )
;' Bottom Face
glNormal3f_( 0.0,-1.0, 0.0 )
glTexCoord2f_( 1.0, 1.0 ) : glVertex3f_( -1.0,-1.0,-1.0 )
glTexCoord2f_( 0.0, 1.0 ) : glVertex3f_( 1.0,-1.0,-1.0 )
glTexCoord2f_( 0.0, 0.0 ) : glVertex3f_( 1.0,-1.0, 1.0 )
glTexCoord2f_( 1.0, 0.0 ) : glVertex3f_( -1.0,-1.0, 1.0 )
;' Right Face
glNormal3f_( 1.0, 0.0, 0.0 )
glTexCoord2f_( 1.0, 0.0 ) : glVertex3f_( 1.0,-1.0,-1.0 )
glTexCoord2f_( 1.0, 1.0 ) : glVertex3f_( 1.0, 1.0,-1.0 )
glTexCoord2f_( 0.0, 1.0 ) : glVertex3f_( 1.0, 1.0, 1.0 )
glTexCoord2f_( 0.0, 0.0 ) : glVertex3f_( 1.0,-1.0, 1.0 )
;' Left Face
glNormal3f_(-1.0, 0.0, 0.0 )
glTexCoord2f_( 0.0, 0.0 ) : glVertex3f_( -1.0,-1.0,-1.0 )
glTexCoord2f_( 1.0, 0.0 ) : glVertex3f_( -1.0,-1.0, 1.0 )
glTexCoord2f_( 1.0, 1.0 ) : glVertex3f_( -1.0, 1.0, 1.0 )
glTexCoord2f_( 0.0, 1.0 ) : glVertex3f_( -1.0, 1.0,-1.0 )
glEnd_()
EndProcedure
If OpenWindow(0, 0, 0, #WindowWidth, #WindowHeight, #WindowFlags, "OpenGL Scene "+StrF(Version, 1))
SetWindowCallback(@MyWindowCallback())
hWnd = WindowID(0)
hDC = GetDC_(hWnd)
;Initialize OpenGL
pfd.PIXELFORMATDESCRIPTOR
pfd\nSize = SizeOf(PIXELFORMATDESCRIPTOR)
pfd\nVersion = 1
pfd\dwFlags = #PFD_SUPPORT_OPENGL | #PFD_DOUBLEBUFFER | #PFD_DRAW_TO_WINDOW
pfd\iLayerType = #PFD_MAIN_PLANE
pfd\iPixelType = #PFD_TYPE_RGBA
pfd\cColorBits = 24
pfd\cDepthBits = 32
pixformat = ChoosePixelFormat_(hDC, pfd)
SetPixelFormat_(hDC, pixformat, pfd)
hrc = wglCreateContext_(hDC)
wglMakeCurrent_(hDC, hrc)
Dim LightAmbient.f(3)
For k=0 To 2
LightAmbient(k) = 0.5
Next
LightAmbient(1) = 1.0
Dim LightDiffuse.f(3)
For k=0 To 3
LightDiffuse(k) = 1.0
Next
Dim LightPosition.f(3)
LightPosition(2) = 2.0
LightPosition(3) = 1.0
Dim fogMode.l(2)
fogMode(0) = #GL_EXP
fogMode(1) = #GL_EXP2
fogMode(2) = #GL_LINEAR
Dim fogColor.f(4)
For k=0 To 2
fogColor(k) = 0.5
Next
fogColor(3) = 1.0
fogfilter = 2
glEnable_(#GL_LIGHTING)
glEnable_(#GL_TEXTURE_2D); // Enable Texture Mapping
glShadeModel_(#GL_SMOOTH); // Enable Smooth Shading
glClearColor_(fogColor(0),fogColor(1),fogColor(2),fogColor(3)); // We'll Clear To The Color Of The Fog
glClearDepth__(1.0); // Depth Buffer Setup
glEnable_(#GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc_(#GL_LEQUAL); // The Type Of Depth Testing To Do
glHint_(#GL_PERSPECTIVE_CORRECTION_HINT, #GL_NICEST); // Really Nice Perspective Calculations
glLightfv_(#GL_LIGHT1, #GL_AMBIENT, @LightAmbient()); // Setup The Ambient Light
glLightfv_(#GL_LIGHT1, #GL_DIFFUSE, @LightDiffuse()); // Setup The Diffuse Light
glLightfv_(#GL_LIGHT1, #GL_POSITION,@LightPosition()); // Position The Light
glEnable_(#GL_LIGHT1); // Enable Light One
glFogi_(#GL_FOG_MODE, fogMode(fogfilter)); // Fog Mode
glFogfv_(#GL_FOG_COLOR, @fogColor()); // Set Fog Color
glFogf_(#GL_FOG_DENSITY, 0.35); // How Dense Will The Fog Be
glHint_(#GL_FOG_HINT, #GL_NICEST); // Fog Hint Value
glFogf_(#GL_FOG_START, 1.0); // Fog Start Depth
glFogf_(#GL_FOG_END, 5.0); // Fog End Depth
glEnable_(#GL_FOG); // Enables GL_FOG
AngleX.f
AngleY.f
AngleZ.f
X.f
Y.f
Z.f
Tex = LoadTexture_("C:\Downloads\Programming\OpenGL\lesson16\Lesson16\Data\Crate.bmp", 2)
Repeat
Rot.f + 0.5
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
glLoadIdentity_()
SetCamera(45.0, 0.1, 100.0)
glLoadIdentity_()
glTranslatef_(X, Y, Z)
glRotatef_(Rot, 1.0, 0.0, 0.0)
DrawCube()
SwapBuffers_(hDC)
If GetAsyncKeyState_(#VK_UP)
Z + 0.05
ElseIf GetAsyncKeyState_(#VK_DOWN)
Z - 0.05
EndIf
If GetAsyncKeyState_(#VK_LEFT)
X - 0.05
ElseIf GetAsyncKeyState_(#VK_RIGHT)
X + 0.05
EndIf
Delay(10)
Event = WindowEvent()
Until Event = #PB_Event_CloseWindow
End
EndIfBut forget to manipulate the PerspectiveMatrix with glTranslate glRotate then
bye,
Daniel
Daniel
