Post by DJLinux on Feb 25, 2008 15:00:02 GMT -5
Only for fun i rewrote the old plane shadow projection with SUB's and FUNCTION's.
Joshy
Joshy
type OBJECT3D
dim List ' OpenGL list
dim xp#,yp#,zp# ' position
dim xr#,yr#,zr# ' rotation
dim xs#,ys#,zs# ' size
dim r#,g#,b# ' color
end type
declare sub InitOpenGL()
declare Sub DrawObject(o as Object3D,red#,green#,blue#)
declare Sub EnableShadow(Matrix#()())
declare sub DisableShadow(Position#())
declare function CreateShadowMatrix#(LightPos#()) ()()
declare function CreateXZPlane()
declare function CreateBox()
dim LightPosition#(3) ' x,y,z,w
dim ShadowMatrix#(3,3) ' 4x4 OpenGL
dim ShadowColor#(2) ' r,g,b
dim Box as OBJECT3D
dim Ground as OBJECT3D
Box.List=CreateBox()
Box.yp#=3
Box.r# =1:Box.g# =1
Box.xs#=2:Box.ys#=2:Box.zs#=2
Ground.List=CreateXZPlane()
Ground.r#=0.9:Ground.g#=0.9:Ground.b#=0.9
Ground.xs#=100:Ground.zs#=100
ShadowColor# = vec3(0.4,0.4,0.4)
LightPosition# = vec4(0,100,100,1)
ShadowMatrix# = CreateShadowMatrix#(LightPosition#)
' enable Ligth0
InitOpenGL()
while True
glClear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glTransLatef (0,-3,-20) ' move the camerea
DisableShadow(LightPosition#)
DrawObject(Ground,Ground.r#,Ground.g#,Ground.b#)
DrawObject(Box,Box.r#,Box.g#,Box.b#)
EnableShadow(ShadowMatrix#)
DrawObject(Box,ShadowColor#(0),ShadowColor#(1),ShadowColor#(2))
SwapBuffers()
Box.xr#=Box.xr#+1
Box.yr#=Box.yr#+2
Box.zr#=Box.zr#+3
WaitTimer(1000/50)
wend
end
sub InitOpenGL()
glEnable (GL_LIGHTING)
glEnable (GL_COLOR_MATERIAL)
glCullFace(GL_BACK)
glEnable (GL_CULL_FACE)
glEnable (GL_LIGHT0)
glLightfv (GL_LIGHT0,GL_AMBIENT ,vec4(0.2,0.2,0.2,1.0))
glLightfv (GL_LIGHT0,GL_DIFFUSE ,vec4(0.6,0.6,0.8,1.0))
glLightfv (GL_LIGHT0,GL_SPECULAR,vec4(0.6,0.6,0.8,1.0))
end sub
' create shadow matrix from light position
function CreateShadowMatrix#(LightPos#()) ()()
dim Matrix#(3,3)
Matrix#(0,0)=LightPos#(1)' Y
Matrix#(1,0)=LightPos#(0)' X
Matrix#(1,2)=LightPos#(2)' Z
Matrix#(1,3)=1.0 ' ground Y normale points up
Matrix#(2,2)=LightPos#(1)' Y
Matrix#(3,3)=LightPos#(1)' Y
' all others are 0.0
return Matrix#
end function
' save matrix, disable light and set shadow matrix
Sub EnableShadow(Matrix#()())
glDisable(GL_DEPTH_TEST)
glDisable(GL_LIGHTING)
glPushMatrix()
glMultMatrixf(Matrix#)
end sub
' restore matrix, enable light and set light position
sub DisableShadow(Position#())
glPopMatrix()
glEnable(GL_DEPTH_TEST)
glEnable(GL_LIGHTING)
glLightfv(GL_LIGHT0,GL_POSITION,Position#)
end sub
' create 1x1x1 unit box as OpenGL list
function CreateBox()
dim List
List=glGenLists(1)
glNewList(List,GL_COMPILE)
glBegin(GL_QUADS)
glNormal3f(0,0,1)
glVertex3f(-0.5, 0.5, 0.5):glVertex3f(-0.5,-0.5, 0.5)
glVertex3f( 0.5,-0.5, 0.5):glVertex3f( 0.5, 0.5, 0.5)
glNormal3f(1,0,0)
glVertex3f( 0.5, 0.5, 0.5):glVertex3f( 0.5,-0.5, 0.5)
glVertex3f( 0.5,-0.5,-0.5):glVertex3f( 0.5, 0.5,-0.5)
glNormal3f (0,0,-1)
glVertex3f( 0.5, 0.5,-0.5):glVertex3f( 0.5,-0.5,-0.5)
glVertex3f(-0.5,-0.5,-0.5):glVertex3f(-0.5, 0.5,-0.5)
glNormal3f(-1,0,0)
glVertex3f(-0.5, 0.5,-0.5):glVertex3f(-0.5,-0.5,-0.5)
glVertex3f(-0.5,-0.5, 0.5):glVertex3f(-0.5, 0.5, 0.5)
glNormal3f(0,1,0)
glVertex3f(-0.5, 0.5,-0.5):glVertex3f(-0.5, 0.5, 0.5)
glVertex3f( 0.5, 0.5, 0.5):glVertex3f( 0.5, 0.5,-0.5)
glNormal3f(0,-1,0)
glVertex3f(-0.5,-0.5, 0.5):glVertex3f(-0.5,-0.5,-0.5)
glVertex3f( 0.5,-0.5,-0.5):glVertex3f( 0.5,-0.5, 0.5)
glEnd()
glEndList()
return List
end function
' create 1x1 unit plane as OpenGL list
function CreateXZPlane()
dim List
List=glGenLists(1)
glNewList(List,GL_COMPILE)
glBegin(GL_QUADS)
glNormal3f(0,1,0):glVertex3f(-0.5,0,-0.5)
glNormal3f(0,1,0):glVertex3f(-0.5,0, 0.5)
glNormal3f(0,1,0):glVertex3f( 0.5,0, 0.5)
glNormal3f(0,1,0):glVertex3f( 0.5,0,-0.5)
glEnd()
glEndList()
return List
end function
' draw a 3D Object use curent position,rotation and size
Sub DrawObject(o as Object3D,red#,green#,blue#)
glPushMatrix()
' move only if not 0,0
if (o.xp# or o.yp# or o.zp#) then
glTranslatef(o.xp#,o.yp#,o.zp#)
end if
' rotate only if not 0.0
if o.xr# then glRotatef(o.xr#,1,0,0):end if
if o.yr# then glRotatef(o.yr#,0,1,0):end if
if o.zr# then glRotatef(o.zr#,0,0,1):end if
' scale only if not 1.0
if (o.xs#-1.0) or (o.ys#-1.0) or (o.zs#-1.0) then
glScalef(o.xs#,o.ys#,o.zs#)
end if
glColor3f(red#,green#,blue#)
glCallList(o.List)
glPopMatrix()
end sub