5
« on: September 23, 2013, 03:39:10 PM »
Static
#INCLUDE "sdl/sdl.bi"
#INCLUDE "gl/gl.bi"
#INCLUDE "gl/glu.bi"
#INCLUDE "gl/glext.bi"
#INCLUDE "al/al.bi"
#INCLUDE "al/alut.bi"
#INCLUDE "3DTypes.bas"
#INCLUDE "3DMath.bas"
Type LightType
Ambient(3) As Single
Diffuse(3) As Single
Specular(3) As Single
Position(3) As Single
Control As Single
End Type
Type Particles
Position As Vector3DType
DVector As Vector3DType
RVector As Vector3DType
UVector As Vector3DType
glMatrix(15) As Single
List_Id As Integer
Life_Span As Integer
AlphaVal(3) As Single
RGBAlpha(3) As Single
Emission(3) As Single
Scale As Single
i As Integer
Z As Single
Cull As Integer
Active As Integer
Explosion As Single
Randimation As Single
End Type
Type PlayerType
Cam As EntityType
Radius As Single
vVector As Vector3DType
Gravity As Single
ViewPoint As Vector3DType
WalkSpd As Single
StrafeSpd As Single
BulletCnt As Integer
Firing As Integer
Shot_Delay As Integer
Bullet(1 To 20) As Particles
Weapon As Integer
Health As Integer
End Type
Declare Sub LOAD_OBJ (FileName As String, Model As ModelType Ptr)
Declare Sub LoadTextures(FileName As String, Num_Txtr As GLUINT, AlphaVal As Integer)
Declare Sub CalcNormals(Model As ModelType Ptr)
Declare Sub Render_Scene(Entity As EntityType, Model As ModelType Ptr, mTextures() As Integer)
Declare Sub Gen_Lights(Light As LightType)
Declare Sub Build_DLists(Model As ModelType Ptr, mTextures() As Integer, DList As Uinteger)
Declare Sub Build_Textures(mTextures() As GLUINT, Num_Textures As Integer)
Declare Sub Build_Sounds( Wav_Bff() As ALuint, Wav_Src() As ALuint, Num_Sounds As Uinteger)
Declare Sub BuildFont(Font_Texture() As GLUINT, FBase As GLUINT)
Declare Sub glPrint(Byval x As GLint, Byval y As GLint, Byref xstring As String , Byval xset As Integer, Font_Texture As GlUint, FBase As GlUint)
Declare Sub Init_Entity(Entity As EntityType)
Declare Function Check_Terrain_Collision(Cam As EntityType, vVector As Vector3DType, Gravity As Single, Radius As Single, Model As ModelType Ptr) As Integer
Declare Function Find_Angle(X1 As Single, Y1 As Single, X2 As Single, Y2 As Single) As Integer
Declare Sub Do_Particles(Model As ModelType Ptr, mTextures() As Integer, AlphaVal() As Single)
Const SCR_WIDTH = 800, MidX = SCR_WIDTH\2
Const SCR_HEIGHT= 600, MidY = SCR_HEIGHT\2
Const BPP = 32
Dim Num_Models As Integer, _
Num_Entities As Integer, _
Num_Sounds As Integer, _
Num_Textures As Integer, _
Player_Id As Integer, _
Num_Particles As Integer
Num_Models = 4
Num_Entities = 2
Num_Sounds = 5
Num_Textures = 11
Player_Id = 1
Num_Particles = 100
Dim Particle(Num_Particles) As Particles
Dim Plr As PlayerType
Dim i As Integer, i2 As Integer, F As Integer, V As Integer, S As Integer
Dim FOVy As Double, Aspect As Double, znear As Double, zfar As Double
Dim Result As Unsigned Integer, Tscreen As SDL_Surface Ptr
Dim Light As LightType
Dim Fogcolor(3) As Single
Dim SDL_flags As Integer, Event As SDL_Event
Dim Mouse_X As Integer, Mouse_Y As Integer
Dim Wav_Bff(Num_Sounds) As ALuint
Dim Wav_Src(Num_Sounds) As ALuint
Dim DLists( 1 To Num_Models ) As GLUINT
Dim Model(1 To Num_Models) As ModelType Ptr
Dim Entity(1 To Num_Entities) As EntityType
Dim mTextures(Num_Textures) As GLUINT
Dim ListenerPos(2) As ALFloat, SourcePos(2) As ALFloat, ListenerOri(5) As ALFloat
Dim Recoil As Vector3DType
Randomize Timer
SDL_flags = SDL_HWSURFACE Or _
SDL_DOUBLEBUF Or _
SDL_OPENGL Or _
SDL_OPENGLBLIT' OR _
'SDL_FULLSCREEN
result = SDL_Init(SDL_INIT_EVERYTHING)
If result <> 0 Then End
SDL_GL_SetAttribute SDL_GL_DOUBLEBUFFER, GL_TRUE
Tscreen = SDL_SetVideoMode(SCR_WIDTH, SCR_HEIGHT, BPP, SDL_flags)
If Tscreen = 0 Then
SDL_Quit
End
Else
SDL_WM_SetCaption "FPS Camera Demo", ""
SDL_ShowCursor False
End If
glViewport 0, 0, SCR_WIDTH, SCR_HEIGHT
Glmatrixmode GL_PROJECTION
Glloadidentity
FOVy = 45.0
Aspect = SCR_WIDTH / SCR_HEIGHT
znear = .1
zfar = 100
gluPerspective FOVy, aspect, znear, zfar
Glpushmatrix
glShadeModel GL_Smooth
glClearColor 0.0, 0.0, 0.0, 0.0
glClearDepth 1.0
glEnable GL_DEPTH_TEST
glDepthFunc GL_LEQUAL
glEnable GL_COLOR_MATERIAL
glenable GL_DOUBLEBUFFER
glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST
glHint GL_LINE_SMOOTH_HINT, GL_NICEST
glenable GL_LINE_SMOOTH
glHint GL_POINT_SMOOTH_HINT, GL_NICEST
glenable GL_POINT_SMOOTH
glPolygonMode GL_FRONT, GL_FILL
glEnable GL_CULL_FACE
FogColor(0)=0.01
FogColor(1)=0.01
FogColor(2)=0.01
FogColor(3)=1.0
glFogi(GL_FOG_MODE, GL_EXP2)
glFogfv(GL_FOG_COLOR, @fogColor(0))
glFogf(GL_FOG_DENSITY, 0.05)
glHint(GL_FOG_HINT, GL_NICEST)
glFogf(GL_FOG_START, 9.0)
glFogf(GL_FOG_END, 10.0)
glEnable(GL_FOG)
glGenTextures Num_Textures, @mTextures(0)
Dim fBase As GLUINT
fBase = glGenLists(256)
LoadTextures "Textures/Font.Bmp", mTextures(1), 255
BuildFont mTextures(), FBase
Gen_Lights Light
Glmatrixmode GL_TEXTURE
Glloadidentity()
glrotatef 180, 0.0, 1.0, 0.0
glrotatef 180, 0.0, 0.0, 1.0
glPrint 0, 0, "Loading, Please Wait...", 1, MTextures(1), FBase
SDL_GL_SWAPBUFFERS
Build_Sounds Wav_Bff(), Wav_Src(), Num_Sounds
Build_Textures mTextures(), Num_Textures
For i = 1 To Num_Models
Model(i)= Allocate(Len(ModelType))
Next
Load_Obj "Models/Level_001.obj", Model(1)
CalcNormals Model(1)
Entity(1).Model_Id = 1
Entity(1).Active=True
Init_Entity Entity(1)
Load_Obj "Models/Gun.obj", Model(2)
CalcNormals Model(2)
Entity(2).Model_Id = 2
Entity(2).Active=True
Entity(2).Position.X=0
Entity(2).Position.Y=-4.65
Entity(2).Position.Z=-27
Entity(2).Angle.Y = .1
Init_Entity Entity(2)
Load_Obj "Models/Missle.obj", Model(3)
CalcNormals Model(3)
Load_Obj "Models/Explosion_Sphere.obj", Model(4)
CalcNormals Model(4)
For i2 = 1 To Num_Models
For S = 0 To Model(i2)->Num_Surfaces - 1
For i= 0 To 3
Model(i2)->Surf[S].Ambient(i)= 0.15
Model(i2)->Surf[S].RGBAlpha(i) = 1.0
Model(i2)->Surf[S].Specular(i)= 0.75
Model(i2)->Surf[S].Diffuse(i)= 0.2
Model(i2)->Surf[S].Emission(i)= 0.0
Next
Model(i2)->Surf[S].Shininess = 5
Model(i2)->Surf[S].Emission(3)= 1.0
Model(i2)->Surf[S].TransParent = False
Next
Next
Dim k As Ubyte Ptr, wTime As Integer, FPS As Integer, FPSTimer As Integer, FramesPerSecond As String
Dim tDist As Single
Dim Collided As Integer
For i = 1 To Num_Models
Build_DLists Model(i), mTextures(), DLists(i)
Next
Plr.Radius = .5
Plr.Health=100
Plr.Weapon=2
Init_Entity Plr.Cam
Plr.Cam.Position.Y = 6
Plr.Cam.Position.Z = 4
Recoil.Y = 1
For i= 1 To Num_Particles
Particle(i).Life_Span = i
Particle(i).List_Id = 3
For i2 = 0 To 2
Particle(i).AlphaVal(i2) = .25
Next
Next
Dim tLine(1 To 2) As Vector3DType, _
vTriangle(1 To 3) As Vector3DType, _
tVector As Vector3DType, _
i3 As Integer
SDL_WarpMouse(MidX, MidY)
Do
wTime = SDL_GetTicks() + 20
Glclear GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT
SDL_PumpEvents
k = SDL_GetKeyState(0)
Result = Peek(K + SDLK_ESCAPE)
Do While SDL_Pollevent(@Event)
Select Case Event.Type
Case SDL_MouseMotion
Mouse_X = Event.Motion.X
Mouse_Y = Event.Motion.Y
Case SDL_MouseButtonDown
Plr.Firing = Event.Button.Button
Case SDL_MouseButtonUp
Plr.Firing = False
End Select
Loop
Plr.Cam.Angle.X = ( MidY - Mouse_Y )/750
Plr.Cam.Angle.Y = ( MidX - Mouse_X )/750
Plr.Cam.Angle.Z= 0
If Plr.Firing=1 Then Recoil.X=0
SDL_WarpMouse(MidX, MidY)
If Plr.Cam.RVector.Y<>0 Then
Plr.Cam.Angle.Z = -Plr.Cam.RVector.Y*.5
End If
Plr.WalkSpd*=.95
If Abs(Plr.WalkSpd)<1 Then Plr.WalkSpd=0
Plr.StrafeSpd*=.95
If Abs(Plr.StrafeSpd)<1 Then Plr.StrafeSpd=0
If Peek(K + SDLK_W) Then
Plr.vVector.X+= Plr.Cam.DVector.X*.02
Plr.vVector.Z+= Plr.Cam.DVector.Z*.02
End If
If Peek(K + SDLK_S) Then
Plr.vVector.X-= Plr.Cam.DVector.X*.02
Plr.vVector.Z-= Plr.Cam.DVector.Z*.02
End If
If Peek(K + SDLK_D) Then
Plr.vVector.X+= Plr.Cam.RVector.X*.02
Plr.vVector.Z+= Plr.Cam.RVector.Z*.02
End If
If Peek(K + SDLK_A) Then
Plr.vVector.X-= Plr.Cam.RVector.X*.02
Plr.vVector.Z-= Plr.Cam.RVector.Z*.02
End If
If Peek(K + SDLK_SPACE) Then
If Collided Then
Plr.Gravity=.4
alSourcefv Wav_Src(5), AL_POSITION, @ListenerPos(0)
alSourcePlay Wav_Src(5)
End If
End If
Plr.vVector.X*=.9
If Abs(Plr.vVector.X)<.001 Then Plr.vVector.X=0
Plr.vVector.Y*=.9
If Abs(Plr.vVector.Y)<.001 Then Plr.vVector.Y=0
Plr.vVector.Z*=.9
If Abs(Plr.vVector.Z)<.001 Then Plr.vVector.Z=0
Plr.Cam.oPosition.X = Plr.Cam.Position.X
Plr.Cam.oPosition.Y = Plr.Cam.Position.Y
Plr.Cam.oPosition.Z = Plr.Cam.Position.Z
Plr.Cam.Position.X += Plr.vVector.X
Plr.Cam.Position.Z += Plr.vVector.Z
Plr.Gravity -=.03
If Plr.Gravity<-1 Then Plr.Gravity=-1
Plr.Cam.Position.Y += Plr.vVector.Y +Plr.Gravity
Rot_X Plr.Cam, Plr.Cam.Angle.X
Rot_Y Plr.Cam, Plr.Cam.Angle.Y
Rot_Z Plr.Cam, Plr.Cam.Angle.Z
Collided = Check_Terrain_Collision(Plr.Cam, Plr.vVector, Plr.Gravity, Plr.Radius, Model(Entity(1).Model_Id))
ListenerPos(0) = Plr.Cam.Position.X
ListenerPos(1) = Plr.Cam.Position.Y
ListenerPos(2) = Plr.Cam.Position.Z
ListenerOri(0) = Plr.Cam.DVector.X
ListenerOri(1) = Plr.Cam.DVector.Y
ListenerOri(2) = Plr.Cam.DVector.Z
ListenerOri(3) = Plr.Cam.UVector.X
ListenerOri(4) = Plr.Cam.UVector.Y
ListenerOri(5) = Plr.Cam.UVector.Z
alListenerfv AL_POSITION, @ListenerPos(0)
alListenerfv AL_ORIENTATION, @ListenerOri(0)
alListener3f AL_VELOCITY, 0,0,0
If Not Plr.Weapon Then
If Distance(Plr.Cam.Position, Entity(2).Position)<= Plr.Radius Then
Plr.Weapon=2
'Entity(2).Active=False
'Entity(2).Angle.X = 1
End If
End If
For i=1 To Ubound(Plr.Bullet)
If Plr.Bullet(i).Active>0 Then
If Plr.Bullet(i).Explosion = 0 Then
Plr.Bullet(i).Active+=1
If Plr.Bullet(i).Active>=500 Then Plr.Bullet(i).Active=False
If Plr.Bullet(i).DVector.Y>-1 Then Plr.Bullet(i).DVector.Y-=.005
tLine(2).X=Plr.Bullet(i).Position.X + Plr.Bullet(i).DVector.X*.4
tLine(2).Y=Plr.Bullet(i).Position.Y + Plr.Bullet(i).DVector.Y*.4
tLine(2).Z=Plr.Bullet(i).Position.Z + Plr.Bullet(i).DVector.Z*.4
tLine(1).X=Plr.Bullet(i).Position.X
tLine(1).Y=Plr.Bullet(i).Position.Y
tLine(1).Z=Plr.Bullet(i).Position.Z
For i3 = 0 To Model(1)->FaceCnt - 1
For i2 = 1 To 3
vTriangle(i2).X = Model(1)->Vec[Model(1)->Face[i3].P(i2)].X
vTriangle(i2).Y = Model(1)->Vec[Model(1)->Face[i3].P(i2)].Y
vTriangle(i2).Z = Model(1)->Vec[Model(1)->Face[i3].P(i2)].Z
Next
If Is_Polygon_Intersected(vTriangle() , tLine(), tVector) Then
tLine(2).X=tVector.X
tLine(2).Y=tVector.Y
tLine(2).z=tVector.Z
Plr.Bullet(i).Explosion=.01
SourcePos(0) = Plr.Bullet(i).Position.X
SourcePos(1) = Plr.Bullet(i).Position.Y
SourcePos(2) = Plr.Bullet(i).Position.Z
alSourcefv Wav_Src(2), AL_POSITION, @SourcePos(0)
alSource3f Wav_Src(2), AL_VELOCITY, Plr.Bullet(i).DVector.X, Plr.Bullet(i).DVector.Y, Plr.Bullet(i).DVector.Z
alSourcePlay Wav_Src(2)
Plr.Bullet(i).Emission(0) = 1
Plr.Bullet(i).Emission(3) = 1
Plr.Bullet(i).RGBAlpha(0) = 1
Plr.Bullet(i).RGBAlpha(1) = .5
Plr.Bullet(i).RGBAlpha(2) = .05
Plr.Bullet(i).RGBAlpha(3) = 1
'Bullet(i).DVector.X+=Model(1)->Face[i3].Normal.X
'Bullet(i).DVector.Y+=Model(1)->Face[i3].Normal.Y
'Bullet(i).DVector.z+=Model(1)->Face[i3].Normal.Z
'Vector_Normalize Bullet(i).DVector
End If
Next
Plr.Bullet(i).Position.X = tLine(2).X
Plr.Bullet(i).Position.Y = tLine(2).Y
Plr.Bullet(i).Position.Z = tLine(2).Z
End If
If Plr.Bullet(i).Explosion>0 Then
Plr.Bullet(i).Explosion+=.1
If Plr.Bullet(i).Explosion>=1 Then
Plr.Bullet(i).Explosion=0
Plr.Bullet(i).Active=0
End If
tDist=Distance(Plr.Cam.Position, Plr.Bullet(i).Position)
If tDist<= (Plr.Radius+Plr.Bullet(i).Explosion) Then
Plr.Gravity=0
Plr.vVector.X=((Plr.Cam.Position.X-Plr.Bullet(i).Position.X)/tDist*.4)
Plr.vVector.Y=((Plr.Cam.Position.Y-Plr.Bullet(i).Position.Y)/tDist*.4)
Plr.vVector.Z=((Plr.Cam.Position.Z-Plr.Bullet(i).Position.Z)/tDist*.4)
Plr.Health-=(((Plr.Radius+1+Plr.Bullet(i).Explosion)*1.5)-tDist)
SourcePos(0) = Plr.Cam.Position.X
SourcePos(1) = Plr.Cam.Position.Y
SourcePos(2) = Plr.Cam.Position.Z
alSourcefv Wav_Src(4), AL_POSITION, @SourcePos(0)
alSourcePlay Wav_Src(4)
End If
End If
End If
Next
If Plr.Weapon Then
Entity(Plr.Weapon).RVector.X = Plr.Cam.RVector.X
Entity(Plr.Weapon).RVector.Y = Plr.Cam.RVector.Y
Entity(Plr.Weapon).RVector.Z = Plr.Cam.RVector.Z
Entity(Plr.Weapon).UVector.X = Plr.Cam.UVector.X
Entity(Plr.Weapon).UVector.Y = Plr.Cam.UVector.Y
Entity(Plr.Weapon).UVector.Z = Plr.Cam.UVector.Z
Entity(Plr.Weapon).DVector.X = Plr.Cam.DVector.X
Entity(Plr.Weapon).DVector.Y = Plr.Cam.DVector.Y
Entity(Plr.Weapon).DVector.Z = Plr.Cam.DVector.Z
Entity(Plr.Weapon).Position.X = Plr.Cam.Position.X+(Plr.Cam.DVector.X*.2)+(Plr.Cam.RVector.X*.1)+(Plr.Cam.UVector.X*-.05)
Entity(Plr.Weapon).Position.Y = Plr.Cam.Position.Y+(Plr.Cam.DVector.Y*.2)+(Plr.Cam.RVector.Y*.1)+(Plr.Cam.UVector.Y*-.05)
Entity(Plr.Weapon).Position.Z = Plr.Cam.Position.Z+(Plr.Cam.DVector.Z*.2)+(Plr.Cam.RVector.Z*.1)+(Plr.Cam.UVector.Z*-.05)
Rot_X Entity(Plr.Weapon), Recoil.X/10
End If
Entity(Plr.Weapon).Angle.X *= .95
Recoil.X *= .95
If Plr.Firing =1 And Plr.Weapon Then
Plr.BulletCnt+=1
If Plr.BulletCnt>=Ubound(Plr.Bullet) Then Plr.BulletCnt = 1
If Plr.Bullet(Plr.BulletCnt).Active=0 Then
If SDL_GetTicks()>=Plr.Shot_Delay Then
Entity(2).Angle.X = .1
Recoil.X = 0
Plr.Shot_Delay = SDL_GetTicks() + 50
Plr.Bullet(Plr.BulletCnt).Active=1
Plr.Bullet(Plr.BulletCnt).Explosion=0
Plr.Bullet(Plr.BulletCnt).Randimation = (Plr.BulletCnt Mod 2) '* 2
Plr.Bullet(Plr.BulletCnt).Position.X=Entity(Plr.Weapon).Position.X '+ (Entity(Plr.Weapon).DVector.X)
Plr.Bullet(Plr.BulletCnt).Position.Y=Entity(Plr.Weapon).Position.Y '+ (Entity(Plr.Weapon).DVector.Y)
Plr.Bullet(Plr.BulletCnt).Position.Z=Entity(Plr.Weapon).Position.Z '+ (Entity(Plr.Weapon).DVector.Z)
Plr.Bullet(Plr.BulletCnt).DVector.X=Entity(Plr.Weapon).DVector.X
Plr.Bullet(Plr.BulletCnt).DVector.Y=Entity(Plr.Weapon).DVector.Y
Plr.Bullet(Plr.BulletCnt).DVector.Z=Entity(Plr.Weapon).DVector.Z
Plr.Bullet(Plr.BulletCnt).UVector.X=Entity(Plr.Weapon).UVector.X
Plr.Bullet(Plr.BulletCnt).UVector.Y=Entity(Plr.Weapon).UVector.Y
Plr.Bullet(Plr.BulletCnt).UVector.Z=Entity(Plr.Weapon).UVector.Z
Plr.Bullet(Plr.BulletCnt).RVector.X=Entity(Plr.Weapon).RVector.X
Plr.Bullet(Plr.BulletCnt).RVector.Y=Entity(Plr.Weapon).RVector.Y
Plr.Bullet(Plr.BulletCnt).RVector.Z=Entity(Plr.Weapon).RVector.Z
Plr.Bullet(Plr.Bulletcnt).RGBAlpha(0)=1
Plr.Bullet(Plr.Bulletcnt).RGBAlpha(1)=1
Plr.Bullet(Plr.Bulletcnt).RGBAlpha(2)=1
Plr.Bullet(Plr.Bulletcnt).RGBAlpha(3)=1
alSourcefv Wav_Src(1), AL_POSITION, @ListenerPos(0)
alSourcePlay Wav_Src(1)
End If
End If
End If