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
EndIf
It works well here.
the fog will always be at 0, 0, 0 and nowhere else.