Advertisement
Dr_Davenstein

glShadowMap

Nov 3rd, 2020
956
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "fbgfx.bi"
  2. #include "gl/gl.bi"
  3. #include "gl/glext.bi"
  4. #include "gl/glu.bi"
  5.  
  6.  
  7. type vec3f
  8.     as single x, y, z
  9. end type
  10.  
  11. type vec4f
  12.     as single x, y, z, w
  13. end type
  14.  
  15. type Matrix4x4
  16.     as vec4f r
  17.     as vec4f u
  18.     as vec4f f
  19.     as vec4f p
  20. end type
  21.  
  22.  
  23. const as integer SCR_W = 1024, SCR_H = 768, BPP = 32, BUFFERS = 1
  24. dim shared as vec4f white = type(1,1,1,1), gray = type(.2,.2,.2,.2), black = type(0,0,0,0)
  25.  
  26.  
  27. declare function init() as integer
  28. declare function cleanup() as integer
  29. declare function drawscene( byref angle as double ) as integer
  30. declare sub fbImageTexture( byref texture as gluint, byref textptr as any ptr )
  31.  
  32. dim shared as Matrix4x4 lightProjectionMatrix, lightViewMatrix, cameraProjectionMatrix, cameraViewMatrix
  33. dim shared as Matrix4x4 biasMatrix = ( .5, 0, 0, 0, 0,.5,0,0, 0,0,.5,0, .5,.5,.5,1 )
  34. dim shared as Matrix4x4 mapMatrix = ( 1, 0, 0, 0, 0,1,0,0, 0,0,1,0, 0,0,0,1 )
  35.  
  36. dim shared as integer shadowMapSize = 512
  37. dim shared as vec4f cameraPosition = (0, 15, 15, 1)
  38. dim shared as vec4f lightPosition = (3, 30, 3, 1)
  39. dim shared as vec3f cube = (0,4,0)
  40. dim shared as vec3f sphere = (6,1.5,-2)
  41.  
  42. dim shared as gluint shadowMapTexture, frameBuffer, texture(2)
  43. Dim shared as GLUquadricObj ptr bModel
  44.  
  45. dim shared glGenFramebuffersEXT            as PFNglGenFramebuffersEXTPROC
  46. dim shared glDeleteFramebuffersEXT         as PFNglDeleteFramebuffersEXTPROC
  47. dim shared glBindFramebufferEXT            as PFNglBindFramebufferEXTPROC
  48. dim shared glFramebufferTexture2DEXT       as PFNglFramebufferTexture2DEXTPROC
  49. dim shared glFramebufferRenderbufferEXT    as PFNglFramebufferRenderbufferEXTPROC
  50. dim shared glGenRenderbuffersEXT           as PFNglGenRenderbuffersEXTPROC
  51. dim shared glBindRenderbufferEXT           as PFNglBindRenderbufferEXTPROC
  52. dim shared glRenderbufferStorageEXT        as PFNglRenderbufferStorageEXTPROC
  53. dim shared glDeleteRenderbuffersEXT          as PFNglDeleteRenderbuffersEXTPROC
  54. dim shared glCheckFramebufferStatusEXT       as PFNglCheckFramebufferStatusEXTPROC
  55. dim shared glGenerateMipmapEXT             as PFNglGenerateMipmapEXTPROC
  56. dim shared glActiveTextureARB                    as PFNglActiveTextureARBPROC
  57.  
  58.  
  59.  
  60.  
  61. init()
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68. do
  69.  
  70.  
  71.     dim as double angle = timer*25
  72.    
  73.     dim as integer shiftDown = false
  74.    
  75.     if multikey(FB.SC_LSHIFT) then
  76.         shiftDown = true
  77.     end if
  78.    
  79.    
  80.     if multikey(FB.SC_LEFT) then
  81.         cube.x-=.1
  82.     end if
  83.    
  84.     if multikey(FB.SC_RIGHT) then
  85.         cube.x+=.1
  86.     end if
  87.    
  88.     if multikey(FB.SC_UP) then
  89.         if not shiftDown then
  90.             cube.z-=.1
  91.         else   
  92.             cube.y+=.1
  93.         end if
  94.     end if
  95.    
  96.     if multikey(FB.SC_DOWN) then
  97.         if not shiftDown then
  98.             cube.z+=.1
  99.         else   
  100.             cube.y-=.1
  101.         end if
  102.     end if
  103.    
  104.    
  105.    
  106.     if multikey(FB.SC_A) then
  107.         sphere.x-=.1
  108.     end if
  109.    
  110.     if multikey(FB.SC_D) then
  111.         sphere.x+=.1
  112.     end if
  113.    
  114.     if multikey(FB.SC_W) then
  115.         if not shiftDown then
  116.             sphere.z-=.1
  117.         else   
  118.             sphere.y+=.1
  119.         end if
  120.     end if
  121.    
  122.     if multikey(FB.SC_S) then
  123.         if not shiftDown then
  124.             sphere.z+=.1
  125.         else   
  126.             sphere.y-=.1
  127.         end if
  128.     end if
  129.    
  130.    
  131.    
  132.     '1st pass__________________________________________________________
  133.     glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, frameBuffer)
  134.    
  135.     glClear( GL_DEPTH_BUFFER_BIT )
  136.  
  137.     glMatrixMode(GL_PROJECTION)
  138.     glLoadMatrixf(@lightProjectionMatrix.r.x)
  139.  
  140.     glMatrixMode(GL_MODELVIEW)
  141.     glLoadMatrixf(@lightViewMatrix.r.x)
  142.  
  143.     glViewport(0, 0, shadowMapSize, shadowMapSize)
  144.     glEnable(GL_CULL_FACE)
  145.     glCullFace(GL_FRONT)
  146.  
  147.     glShadeModel(GL_FLAT)
  148.     glColorMask(0, 0, 0, 0)
  149.  
  150.     glEnable(GL_POLYGON_OFFSET_FILL)
  151.     glPolygonOffset(2, 16)
  152.     drawScene(angle)
  153.     glDisable(GL_POLYGON_OFFSET_FILL)
  154.    
  155.     glCullFace(GL_BACK)
  156.     glShadeModel(GL_SMOOTH)
  157.     glColorMask(1, 1, 1, 1)
  158.    
  159.     glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0)
  160.  
  161.  
  162.     '2nd pass__________________________________________________________
  163.     glClear(GL_DEPTH_BUFFER_BIT or GL_COLOR_BUFFER_BIT)
  164.  
  165.     glMatrixMode(GL_PROJECTION)
  166.     glLoadMatrixf(@cameraProjectionMatrix.r.x)
  167.  
  168.     glMatrixMode(GL_MODELVIEW)
  169.     glLoadMatrixf(@cameraViewMatrix.r.x)
  170.  
  171.     glViewport(0, 0, SCR_W, SCR_H)
  172.  
  173.     glLightfv(GL_LIGHT1, GL_POSITION, @lightPosition.x)
  174.     glLightfv(GL_LIGHT1, GL_AMBIENT, @gray.x)
  175.     glLightfv(GL_LIGHT1, GL_DIFFUSE, @gray.x)
  176.     glLightfv(GL_LIGHT1, GL_SPECULAR, @black.x)
  177.     glEnable(GL_LIGHT1)
  178.     glEnable(GL_LIGHTING)
  179.  
  180.     drawscene(angle)
  181.  
  182.  
  183.  
  184.     '3rd pass__________________________________________________________
  185.     glLightfv(GL_LIGHT1, GL_DIFFUSE, @white.x)
  186.     glLightfv(GL_LIGHT1, GL_SPECULAR, @white.x)
  187.    
  188.     glActiveTextureARB(GL_TEXTURE0)
  189.     glEnable(GL_TEXTURE_2D)
  190.     glActiveTextureARB(GL_TEXTURE1)
  191.     glEnable(GL_TEXTURE_2D)
  192.     glBindTexture(GL_TEXTURE_2D, shadowMapTexture)
  193.    
  194.     glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR)
  195.     glTexGenfv(GL_S, GL_EYE_PLANE, @mapMatrix.r.x)
  196.     glEnable(GL_TEXTURE_GEN_S)
  197.  
  198.     glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR)
  199.     glTexGenfv(GL_T, GL_EYE_PLANE, @mapMatrix.u.x)
  200.     glEnable(GL_TEXTURE_GEN_T)
  201.  
  202.     glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR)
  203.     glTexGenfv(GL_R, GL_EYE_PLANE, @mapMatrix.f.x)
  204.     glEnable(GL_TEXTURE_GEN_R)
  205.  
  206.     glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR)
  207.     glTexGenfv(GL_Q, GL_EYE_PLANE, @mapMatrix.p.x)
  208.     glEnable(GL_TEXTURE_GEN_Q)
  209.    
  210.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE)
  211.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL)
  212.     glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)
  213.    
  214.     glAlphaFunc( GL_GEQUAL, .99 )
  215.     glEnable( GL_ALPHA_TEST )
  216.  
  217.     glMatrixMode( GL_TEXTURE )
  218.     glLoadIdentity()
  219.     glLoadMatrixf( @biasMatrix.r.x )
  220.     glMultMatrixf( @LightProjectionMatrix.r.x )
  221.     glMultMatrixf( @LightViewMatrix.r.x )
  222.     glMatrixMode( GL_MODELVIEW )
  223.  
  224.     drawscene( angle )
  225.    
  226.     glActiveTextureARB(GL_TEXTURE1)
  227.     glDisable(GL_TEXTURE_2D)
  228.     glDisable( GL_TEXTURE_GEN_S )
  229.     glDisable( GL_TEXTURE_GEN_T )
  230.     glDisable( GL_TEXTURE_GEN_R )
  231.     glDisable( GL_TEXTURE_GEN_Q )
  232.    
  233.    
  234.     glDisable( GL_LIGHTING )
  235.     glDisable( GL_LIGHT1 )
  236.     glDisable( GL_ALPHA_TEST )
  237.  
  238.     sleep( 3, 1 )
  239.  
  240.     flip
  241.  
  242.  
  243. loop until multikey(FB.SC_ESCAPE)
  244.  
  245. cleanup()
  246.  
  247. function cleanup() as integer
  248.    
  249.     gluDeleteQuadric( bModel )
  250.     glDeleteTextures(1, @shadowMapTexture )
  251.     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0)
  252.     glDeleteFramebuffersEXT(1, @frameBuffer)
  253.    
  254.     for i as integer = 0 to ubound(texture)
  255.         glDeleteTextures(1, @texture(i) )
  256.     next
  257.    
  258.     print #1, "OK!"
  259.    
  260.     sleep 1000,1
  261.    
  262.     close #1
  263.    
  264.     return 1
  265.        
  266. end function
  267.  
  268.  
  269. function drawscene( byref angle as double ) as integer
  270.    
  271.     glColor3f(1, 1, 1)
  272.    
  273.     glActiveTextureARB(GL_TEXTURE0)
  274.     glBindTexture( GL_TEXTURE_2D, texture(1) )
  275.     glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SINGLE_COLOR)
  276.    
  277.     glBegin( GL_QUADS )
  278.    
  279.     glNormal3f(0,1,0)
  280.    
  281.     glTexcoord2f( 1, 1 )
  282.     glVertex3f( 10, 0, 10 )
  283.    
  284.     glTexcoord2f( 1, 0 )
  285.     glVertex3f( 10, 0, -10 )
  286.    
  287.     glTexcoord2f( 0, 0 )
  288.     glVertex3f( -10, 0, -10 )
  289.    
  290.     glTexcoord2f( 0, 1 )
  291.     glVertex3f( -10, 0, 10 )
  292.    
  293.     glEnd()
  294.  
  295.     glPushMatrix()
  296.    
  297.    
  298.     glBindTexture( GL_TEXTURE_2D, texture(0) )
  299.     glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR)
  300.     glTranslatef(cube.x, cube.y, cube.z)
  301.     glRotatef( angle, 1,0,0 )
  302.     glRotatef( angle*2, 0,1,0 )
  303.     glRotatef( angle*3, 0,0,1 )
  304.    
  305.  
  306.     for i as integer = 0 to 3
  307.  
  308.         glPushMatrix()
  309.  
  310.         glRotatef(i*90,0,1,0)
  311.         glTranslatef(0,0,.5)
  312.  
  313.         glBegin( GL_QUADS )
  314.         glNormal3f(0,0,1)
  315.        
  316.         glTexCoord2f(-1,1)
  317.         glVertex3f( -.5, .5, 0 )
  318.        
  319.         glTexCoord2f(-1,-1)
  320.         glVertex3f( -.5, -.5, 0 )
  321.        
  322.         glTexCoord2f(1,-1)
  323.         glVertex3f( .5, -.5, 0 )
  324.        
  325.         glTexCoord2f(1,1)
  326.         glVertex3f( .5, .5, 0 )
  327.         glEnd()
  328.  
  329.         glPopMatrix()
  330.  
  331.     next
  332.    
  333.  
  334.     for i as integer = 0 to 1
  335.  
  336.         glPushMatrix()
  337.  
  338.         glRotatef(i*180,0,0,1)
  339.         glTranslatef(0,-.5,0)
  340.  
  341.         glBegin( GL_QUADS )
  342.         glNormal3f(0,-1,0)
  343.        
  344.         glTexcoord2f(-1,1)
  345.         glVertex3f( -.5, 0, .5 )
  346.        
  347.         glTexcoord2f(-1,-1)
  348.         glVertex3f( -.5, 0, -.5 )
  349.        
  350.         glTexcoord2f(1,-1)
  351.         glVertex3f( .5, 0, -.5 )
  352.        
  353.         glTexcoord2f(1,1)
  354.         glVertex3f( .5, 0, .5 )
  355.         glEnd()
  356.  
  357.         glPopMatrix()
  358.  
  359.     next
  360.     glPopMatrix()
  361.        
  362.    
  363.     glPushMatrix()
  364.         glTranslatef(sphere.x, sphere.y, sphere.z)
  365.         glRotatef( -angle, 1,0,0 )
  366.         glRotatef( angle*2, 0,1,0 )
  367.         glRotatef( -angle*3, 0,0,1 )
  368.         gluSphere( bModel, 1, 16, 16 )
  369.    glPopMatrix()
  370.    
  371.     return true
  372.  
  373. end function
  374.  
  375.  
  376. function init() as integer
  377.  
  378.  
  379.     screencontrol FB.SET_GL_DEPTH_BITS, 24
  380.     screencontrol FB.SET_GL_COLOR_BITS, BPP
  381.  
  382.     dim Flags as integer = FB.GFX_OPENGL or FB.GFX_HIGH_PRIORITY
  383.  
  384.     screenres SCR_W, SCR_H, BPP, BUFFERS, flags
  385.    
  386.     open cons for output as #1
  387.    
  388.     glGenFramebuffersEXT                = screenglproc("glGenFramebuffersEXT")
  389.     glDeleteFramebuffersEXT             = screenglproc("glDeleteFramebuffersEXT")
  390.     glBindFramebufferEXT                = screenglproc("glBindFramebufferEXT")
  391.     glFramebufferTexture2DEXT           = screenglproc("glFramebufferTexture2DEXT")
  392.     glFramebufferRenderbufferEXT        = screenglproc("glFramebufferRenderbufferEXT")
  393.     glGenRenderbuffersEXT               = screenglproc("glGenRenderbuffersEXT")
  394.     glBindRenderbufferEXT               = screenglproc("glBindRenderbufferEXT")
  395.     glRenderbufferStorageEXT            = screenglproc("glRenderbufferStorageEXT")
  396.     glDeleteRenderbuffersEXT            = screenglproc("glDeleteRenderbuffersEXT")
  397.     glCheckFramebufferStatusEXT     = screenglproc("glCheckFramebufferStatusEXT")
  398.     glGenerateMipmapEXT                 = screenglproc("glGenerateMipmapEXT")
  399.     glActiveTextureARB               = screenglproc("glActiveTextureARB")
  400.    
  401.     glShadeModel(GL_SMOOTH)
  402.     glClearColor(0, 0, 0, 0)
  403.     glColor4f(1, 1, 1, 1)
  404.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
  405.    
  406.     glClearDepth(1.0)
  407.     glDepthFunc(GL_LEQUAL)
  408.     glEnable(GL_DEPTH_TEST)
  409.  
  410.     glEnable(GL_CULL_FACE)
  411.  
  412.     glEnable(GL_NORMALIZE)
  413.    
  414.     glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE)
  415.     glEnable(GL_COLOR_MATERIAL)
  416.  
  417.     glMaterialfv(GL_FRONT, GL_SPECULAR, @white.x)
  418.     glMaterialf(GL_FRONT, GL_SHININESS, 16)
  419.    
  420.    
  421.     glMatrixMode(GL_MODELVIEW)
  422.    
  423.     glLoadIdentity()
  424.     gluPerspective(60, SCR_W/SCR_H, .5, 100)
  425.     glGetFloatv(GL_MODELVIEW_MATRIX, @cameraProjectionMatrix.r.x)
  426.  
  427.     glLoadIdentity()
  428.     gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z, 0, 0, 0, 0, 1, 0)
  429.     glGetFloatv(GL_MODELVIEW_MATRIX, @cameraViewMatrix.r.x)
  430.  
  431.     glLoadIdentity()
  432.     gluPerspective(45, 1, 2, 100)
  433.     glGetFloatv(GL_MODELVIEW_MATRIX, @lightProjectionMatrix.r.x)
  434.  
  435.     glLoadIdentity()
  436.     gluLookAt(  lightPosition.x, lightPosition.y, lightPosition.z, 0, 0, 0, 0, 1, 0)
  437.     glGetFloatv(GL_MODELVIEW_MATRIX, @lightViewMatrix.r.x)
  438.  
  439.     glPopMatrix()
  440.  
  441.     glActiveTextureARB(GL_TEXTURE1)
  442.     glGenTextures(1, @shadowmapTexture )
  443.     glBindTexture(GL_TEXTURE_2D, shadowmapTexture )
  444.     glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, ShadowMapSize, ShadowMapSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0)
  445.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
  446.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
  447.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
  448.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
  449.     glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)
  450.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE)
  451.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL)
  452.  
  453.     glGenFramebuffersEXT ( 1, @frameBuffer )
  454.     glBindFramebufferEXT ( GL_FRAMEBUFFER_EXT, frameBuffer )
  455.     glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, shadowmapTexture, 0 )
  456.     glDrawBuffer( GL_NONE )
  457.     glReadBuffer( GL_NONE )
  458.     glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0)
  459.    
  460.    
  461.     bModel = gluNewQuadric()
  462.     gluQuadricDrawStyle( bModel, GLU_FILL )
  463.     gluQuadricOrientation( bModel, GLU_OUTSIDE )
  464.     gluQuadricNormals( bModel, GLU_FLAT )
  465.     gluQuadricTexture( bModel, GL_TRUE )
  466.    
  467.     dim as any ptr Pic = imagecreate(128,128)
  468.  
  469.     for x as integer = 0 to 127
  470.         for y as integer = 0 to 127
  471.             dim as integer col = x xor y
  472.             pset pic,(x,y),rgb(col,col,col)
  473.         next
  474.     next
  475.    
  476.     fbImageTexture( texture(0), pic )
  477.    
  478.     for x as integer = 0 to 127
  479.         for y as integer = 0 to 127
  480.             dim as integer col = 100+int(rnd*50)
  481.             pset pic,(x,y),rgb(0,col,0)
  482.         next
  483.     next
  484.    
  485.     fbImageTexture( texture(1), pic )
  486.    
  487.     imagedestroy(pic)
  488.    
  489.     return true
  490.  
  491. end function
  492.  
  493.  
  494. sub fbImageTexture( byref texture as gluint, byref textptr as any ptr )
  495.  
  496.     dim as fb.image ptr timage = cast( fb.image ptr, textptr )
  497.  
  498.     glGenTextures(1, @texture)
  499.     glActiveTextureARB(GL_TEXTURE0)
  500.     glBindTexture( GL_TEXTURE_2D, texture )
  501.     glEnable( GL_TEXTURE_2D )
  502.     glTexImage2d( GL_TEXTURE_2D, 0, GL_RGBA, timage->width, timage->height, 0, GL_BGRA, GL_UNSIGNED_BYTE, textptr+sizeof(fb.image) )
  503.     glubuild2dmipmaps( GL_TEXTURE_2D, GL_RGBA, timage->width, timage->height, GL_BGRA, GL_UNSIGNED_BYTE, textptr+sizeof(fb.image) )
  504.     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT )
  505.     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT )
  506.     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR )
  507.     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR )
  508.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
  509.  
  510. end sub
  511.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement