Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "fbgfx.bi"
- #include "gl/gl.bi"
- #include "gl/glext.bi"
- #include "gl/glu.bi"
- type vec3f
- as single x, y, z
- end type
- type vec4f
- as single x, y, z, w
- end type
- type Matrix4x4
- as vec4f r
- as vec4f u
- as vec4f f
- as vec4f p
- end type
- const as integer SCR_W = 1024, SCR_H = 768, BPP = 32, BUFFERS = 1
- dim shared as vec4f white = type(1,1,1,1), gray = type(.2,.2,.2,.2), black = type(0,0,0,0)
- declare function init() as integer
- declare function cleanup() as integer
- declare function drawscene( byref angle as double ) as integer
- declare sub fbImageTexture( byref texture as gluint, byref textptr as any ptr )
- dim shared as Matrix4x4 lightProjectionMatrix, lightViewMatrix, cameraProjectionMatrix, cameraViewMatrix
- dim shared as Matrix4x4 biasMatrix = ( .5, 0, 0, 0, 0,.5,0,0, 0,0,.5,0, .5,.5,.5,1 )
- dim shared as Matrix4x4 mapMatrix = ( 1, 0, 0, 0, 0,1,0,0, 0,0,1,0, 0,0,0,1 )
- dim shared as integer shadowMapSize = 512
- dim shared as vec4f cameraPosition = (0, 15, 15, 1)
- dim shared as vec4f lightPosition = (3, 30, 3, 1)
- dim shared as vec3f cube = (0,4,0)
- dim shared as vec3f sphere = (6,1.5,-2)
- dim shared as gluint shadowMapTexture, frameBuffer, texture(2)
- Dim shared as GLUquadricObj ptr bModel
- dim shared glGenFramebuffersEXT as PFNglGenFramebuffersEXTPROC
- dim shared glDeleteFramebuffersEXT as PFNglDeleteFramebuffersEXTPROC
- dim shared glBindFramebufferEXT as PFNglBindFramebufferEXTPROC
- dim shared glFramebufferTexture2DEXT as PFNglFramebufferTexture2DEXTPROC
- dim shared glFramebufferRenderbufferEXT as PFNglFramebufferRenderbufferEXTPROC
- dim shared glGenRenderbuffersEXT as PFNglGenRenderbuffersEXTPROC
- dim shared glBindRenderbufferEXT as PFNglBindRenderbufferEXTPROC
- dim shared glRenderbufferStorageEXT as PFNglRenderbufferStorageEXTPROC
- dim shared glDeleteRenderbuffersEXT as PFNglDeleteRenderbuffersEXTPROC
- dim shared glCheckFramebufferStatusEXT as PFNglCheckFramebufferStatusEXTPROC
- dim shared glGenerateMipmapEXT as PFNglGenerateMipmapEXTPROC
- dim shared glActiveTextureARB as PFNglActiveTextureARBPROC
- init()
- do
- dim as double angle = timer*25
- dim as integer shiftDown = false
- if multikey(FB.SC_LSHIFT) then
- shiftDown = true
- end if
- if multikey(FB.SC_LEFT) then
- cube.x-=.1
- end if
- if multikey(FB.SC_RIGHT) then
- cube.x+=.1
- end if
- if multikey(FB.SC_UP) then
- if not shiftDown then
- cube.z-=.1
- else
- cube.y+=.1
- end if
- end if
- if multikey(FB.SC_DOWN) then
- if not shiftDown then
- cube.z+=.1
- else
- cube.y-=.1
- end if
- end if
- if multikey(FB.SC_A) then
- sphere.x-=.1
- end if
- if multikey(FB.SC_D) then
- sphere.x+=.1
- end if
- if multikey(FB.SC_W) then
- if not shiftDown then
- sphere.z-=.1
- else
- sphere.y+=.1
- end if
- end if
- if multikey(FB.SC_S) then
- if not shiftDown then
- sphere.z+=.1
- else
- sphere.y-=.1
- end if
- end if
- '1st pass__________________________________________________________
- glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, frameBuffer)
- glClear( GL_DEPTH_BUFFER_BIT )
- glMatrixMode(GL_PROJECTION)
- glLoadMatrixf(@lightProjectionMatrix.r.x)
- glMatrixMode(GL_MODELVIEW)
- glLoadMatrixf(@lightViewMatrix.r.x)
- glViewport(0, 0, shadowMapSize, shadowMapSize)
- glEnable(GL_CULL_FACE)
- glCullFace(GL_FRONT)
- glShadeModel(GL_FLAT)
- glColorMask(0, 0, 0, 0)
- glEnable(GL_POLYGON_OFFSET_FILL)
- glPolygonOffset(2, 16)
- drawScene(angle)
- glDisable(GL_POLYGON_OFFSET_FILL)
- glCullFace(GL_BACK)
- glShadeModel(GL_SMOOTH)
- glColorMask(1, 1, 1, 1)
- glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0)
- '2nd pass__________________________________________________________
- glClear(GL_DEPTH_BUFFER_BIT or GL_COLOR_BUFFER_BIT)
- glMatrixMode(GL_PROJECTION)
- glLoadMatrixf(@cameraProjectionMatrix.r.x)
- glMatrixMode(GL_MODELVIEW)
- glLoadMatrixf(@cameraViewMatrix.r.x)
- glViewport(0, 0, SCR_W, SCR_H)
- glLightfv(GL_LIGHT1, GL_POSITION, @lightPosition.x)
- glLightfv(GL_LIGHT1, GL_AMBIENT, @gray.x)
- glLightfv(GL_LIGHT1, GL_DIFFUSE, @gray.x)
- glLightfv(GL_LIGHT1, GL_SPECULAR, @black.x)
- glEnable(GL_LIGHT1)
- glEnable(GL_LIGHTING)
- drawscene(angle)
- '3rd pass__________________________________________________________
- glLightfv(GL_LIGHT1, GL_DIFFUSE, @white.x)
- glLightfv(GL_LIGHT1, GL_SPECULAR, @white.x)
- glActiveTextureARB(GL_TEXTURE0)
- glEnable(GL_TEXTURE_2D)
- glActiveTextureARB(GL_TEXTURE1)
- glEnable(GL_TEXTURE_2D)
- glBindTexture(GL_TEXTURE_2D, shadowMapTexture)
- glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR)
- glTexGenfv(GL_S, GL_EYE_PLANE, @mapMatrix.r.x)
- glEnable(GL_TEXTURE_GEN_S)
- glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR)
- glTexGenfv(GL_T, GL_EYE_PLANE, @mapMatrix.u.x)
- glEnable(GL_TEXTURE_GEN_T)
- glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR)
- glTexGenfv(GL_R, GL_EYE_PLANE, @mapMatrix.f.x)
- glEnable(GL_TEXTURE_GEN_R)
- glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR)
- glTexGenfv(GL_Q, GL_EYE_PLANE, @mapMatrix.p.x)
- glEnable(GL_TEXTURE_GEN_Q)
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE)
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL)
- glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)
- glAlphaFunc( GL_GEQUAL, .99 )
- glEnable( GL_ALPHA_TEST )
- glMatrixMode( GL_TEXTURE )
- glLoadIdentity()
- glLoadMatrixf( @biasMatrix.r.x )
- glMultMatrixf( @LightProjectionMatrix.r.x )
- glMultMatrixf( @LightViewMatrix.r.x )
- glMatrixMode( GL_MODELVIEW )
- drawscene( angle )
- glActiveTextureARB(GL_TEXTURE1)
- glDisable(GL_TEXTURE_2D)
- glDisable( GL_TEXTURE_GEN_S )
- glDisable( GL_TEXTURE_GEN_T )
- glDisable( GL_TEXTURE_GEN_R )
- glDisable( GL_TEXTURE_GEN_Q )
- glDisable( GL_LIGHTING )
- glDisable( GL_LIGHT1 )
- glDisable( GL_ALPHA_TEST )
- sleep( 3, 1 )
- flip
- loop until multikey(FB.SC_ESCAPE)
- cleanup()
- function cleanup() as integer
- gluDeleteQuadric( bModel )
- glDeleteTextures(1, @shadowMapTexture )
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0)
- glDeleteFramebuffersEXT(1, @frameBuffer)
- for i as integer = 0 to ubound(texture)
- glDeleteTextures(1, @texture(i) )
- next
- print #1, "OK!"
- sleep 1000,1
- close #1
- return 1
- end function
- function drawscene( byref angle as double ) as integer
- glColor3f(1, 1, 1)
- glActiveTextureARB(GL_TEXTURE0)
- glBindTexture( GL_TEXTURE_2D, texture(1) )
- glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SINGLE_COLOR)
- glBegin( GL_QUADS )
- glNormal3f(0,1,0)
- glTexcoord2f( 1, 1 )
- glVertex3f( 10, 0, 10 )
- glTexcoord2f( 1, 0 )
- glVertex3f( 10, 0, -10 )
- glTexcoord2f( 0, 0 )
- glVertex3f( -10, 0, -10 )
- glTexcoord2f( 0, 1 )
- glVertex3f( -10, 0, 10 )
- glEnd()
- glPushMatrix()
- glBindTexture( GL_TEXTURE_2D, texture(0) )
- glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR)
- glTranslatef(cube.x, cube.y, cube.z)
- glRotatef( angle, 1,0,0 )
- glRotatef( angle*2, 0,1,0 )
- glRotatef( angle*3, 0,0,1 )
- for i as integer = 0 to 3
- glPushMatrix()
- glRotatef(i*90,0,1,0)
- glTranslatef(0,0,.5)
- glBegin( GL_QUADS )
- glNormal3f(0,0,1)
- glTexCoord2f(-1,1)
- glVertex3f( -.5, .5, 0 )
- glTexCoord2f(-1,-1)
- glVertex3f( -.5, -.5, 0 )
- glTexCoord2f(1,-1)
- glVertex3f( .5, -.5, 0 )
- glTexCoord2f(1,1)
- glVertex3f( .5, .5, 0 )
- glEnd()
- glPopMatrix()
- next
- for i as integer = 0 to 1
- glPushMatrix()
- glRotatef(i*180,0,0,1)
- glTranslatef(0,-.5,0)
- glBegin( GL_QUADS )
- glNormal3f(0,-1,0)
- glTexcoord2f(-1,1)
- glVertex3f( -.5, 0, .5 )
- glTexcoord2f(-1,-1)
- glVertex3f( -.5, 0, -.5 )
- glTexcoord2f(1,-1)
- glVertex3f( .5, 0, -.5 )
- glTexcoord2f(1,1)
- glVertex3f( .5, 0, .5 )
- glEnd()
- glPopMatrix()
- next
- glPopMatrix()
- glPushMatrix()
- glTranslatef(sphere.x, sphere.y, sphere.z)
- glRotatef( -angle, 1,0,0 )
- glRotatef( angle*2, 0,1,0 )
- glRotatef( -angle*3, 0,0,1 )
- gluSphere( bModel, 1, 16, 16 )
- glPopMatrix()
- return true
- end function
- function init() as integer
- screencontrol FB.SET_GL_DEPTH_BITS, 24
- screencontrol FB.SET_GL_COLOR_BITS, BPP
- dim Flags as integer = FB.GFX_OPENGL or FB.GFX_HIGH_PRIORITY
- screenres SCR_W, SCR_H, BPP, BUFFERS, flags
- open cons for output as #1
- glGenFramebuffersEXT = screenglproc("glGenFramebuffersEXT")
- glDeleteFramebuffersEXT = screenglproc("glDeleteFramebuffersEXT")
- glBindFramebufferEXT = screenglproc("glBindFramebufferEXT")
- glFramebufferTexture2DEXT = screenglproc("glFramebufferTexture2DEXT")
- glFramebufferRenderbufferEXT = screenglproc("glFramebufferRenderbufferEXT")
- glGenRenderbuffersEXT = screenglproc("glGenRenderbuffersEXT")
- glBindRenderbufferEXT = screenglproc("glBindRenderbufferEXT")
- glRenderbufferStorageEXT = screenglproc("glRenderbufferStorageEXT")
- glDeleteRenderbuffersEXT = screenglproc("glDeleteRenderbuffersEXT")
- glCheckFramebufferStatusEXT = screenglproc("glCheckFramebufferStatusEXT")
- glGenerateMipmapEXT = screenglproc("glGenerateMipmapEXT")
- glActiveTextureARB = screenglproc("glActiveTextureARB")
- glShadeModel(GL_SMOOTH)
- glClearColor(0, 0, 0, 0)
- glColor4f(1, 1, 1, 1)
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
- glClearDepth(1.0)
- glDepthFunc(GL_LEQUAL)
- glEnable(GL_DEPTH_TEST)
- glEnable(GL_CULL_FACE)
- glEnable(GL_NORMALIZE)
- glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE)
- glEnable(GL_COLOR_MATERIAL)
- glMaterialfv(GL_FRONT, GL_SPECULAR, @white.x)
- glMaterialf(GL_FRONT, GL_SHININESS, 16)
- glMatrixMode(GL_MODELVIEW)
- glLoadIdentity()
- gluPerspective(60, SCR_W/SCR_H, .5, 100)
- glGetFloatv(GL_MODELVIEW_MATRIX, @cameraProjectionMatrix.r.x)
- glLoadIdentity()
- gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z, 0, 0, 0, 0, 1, 0)
- glGetFloatv(GL_MODELVIEW_MATRIX, @cameraViewMatrix.r.x)
- glLoadIdentity()
- gluPerspective(45, 1, 2, 100)
- glGetFloatv(GL_MODELVIEW_MATRIX, @lightProjectionMatrix.r.x)
- glLoadIdentity()
- gluLookAt( lightPosition.x, lightPosition.y, lightPosition.z, 0, 0, 0, 0, 1, 0)
- glGetFloatv(GL_MODELVIEW_MATRIX, @lightViewMatrix.r.x)
- glPopMatrix()
- glActiveTextureARB(GL_TEXTURE1)
- glGenTextures(1, @shadowmapTexture )
- glBindTexture(GL_TEXTURE_2D, shadowmapTexture )
- glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, ShadowMapSize, ShadowMapSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0)
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
- glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE)
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL)
- glGenFramebuffersEXT ( 1, @frameBuffer )
- glBindFramebufferEXT ( GL_FRAMEBUFFER_EXT, frameBuffer )
- glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, shadowmapTexture, 0 )
- glDrawBuffer( GL_NONE )
- glReadBuffer( GL_NONE )
- glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0)
- bModel = gluNewQuadric()
- gluQuadricDrawStyle( bModel, GLU_FILL )
- gluQuadricOrientation( bModel, GLU_OUTSIDE )
- gluQuadricNormals( bModel, GLU_FLAT )
- gluQuadricTexture( bModel, GL_TRUE )
- dim as any ptr Pic = imagecreate(128,128)
- for x as integer = 0 to 127
- for y as integer = 0 to 127
- dim as integer col = x xor y
- pset pic,(x,y),rgb(col,col,col)
- next
- next
- fbImageTexture( texture(0), pic )
- for x as integer = 0 to 127
- for y as integer = 0 to 127
- dim as integer col = 100+int(rnd*50)
- pset pic,(x,y),rgb(0,col,0)
- next
- next
- fbImageTexture( texture(1), pic )
- imagedestroy(pic)
- return true
- end function
- sub fbImageTexture( byref texture as gluint, byref textptr as any ptr )
- dim as fb.image ptr timage = cast( fb.image ptr, textptr )
- glGenTextures(1, @texture)
- glActiveTextureARB(GL_TEXTURE0)
- glBindTexture( GL_TEXTURE_2D, texture )
- glEnable( GL_TEXTURE_2D )
- glTexImage2d( GL_TEXTURE_2D, 0, GL_RGBA, timage->width, timage->height, 0, GL_BGRA, GL_UNSIGNED_BYTE, textptr+sizeof(fb.image) )
- glubuild2dmipmaps( GL_TEXTURE_2D, GL_RGBA, timage->width, timage->height, GL_BGRA, GL_UNSIGNED_BYTE, textptr+sizeof(fb.image) )
- glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT )
- glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT )
- glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR )
- glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR )
- glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
- end sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement