glblurr.bmx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. Rem
  2. NEHE OpenGL Lesson 36: Radial Blur & Rendering To A Texture
  3. converted to blitzmax by David Bird
  4. EndRem
  5. Strict
  6. 'User Defined Variables
  7. Global C_WIDTH = 640
  8. Global C_HEIGHT= 480
  9. Global angle# 'Used To Rotate The Helix
  10. Global vertexes#[4,3] 'Holds Float Info For 4 Sets Of Vertices
  11. Global normal#[3] 'An Array To Store The Normal Data
  12. Global BlurTexture 'An Unsigned Int To Store The Texture Number
  13. Global tSize=256
  14. Const RAD_TO_DEG! = 57.2957795130823208767981548141052
  15. Function Cos_R!(rads!)
  16. Return Cos(rads * RAD_TO_DEG)
  17. EndFunction
  18. Function Sin_R!(rads!)
  19. Return Sin(rads * RAD_TO_DEG)
  20. EndFunction
  21. Function EmptyTexture() ' Create An Empty Texture
  22. Local txtnumber ' Texture ID
  23. Local data[tSize*tSize*4]
  24. glGenTextures(1, Varptr txtnumber ) ' Create 1 Texture
  25. glBindTexture(GL_TEXTURE_2D, txtnumber) ' Bind The Texture
  26. glTexImage2D(GL_TEXTURE_2D, 0, 4, 128, 128, 0,..
  27. GL_RGBA, GL_UNSIGNED_BYTE, data) ' Build Texture Using Information In data
  28. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR)
  29. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR)
  30. Return txtnumber ' Return The Texture ID
  31. EndFunction
  32. Function ReduceToUnit(vector:Float Ptr) ' Reduces A Normal Vector (3 Coordinates)
  33. Local length#
  34. ' Calculates The Length Of The Vector
  35. length = Sqr((vector[0]*vector[0]) + (vector[1]*vector[1]) + (vector[2]*vector[2]))
  36. If length = 0.0 length=1.0 ' Prevents Divide By 0 Error By Providing
  37. vector[0]:/ length ' Dividing Each Element By
  38. vector[1]:/ length ' The Length Results In A
  39. vector[2]:/ length ' Unit Normal Vector.
  40. End Function
  41. Function ProcessHelix() ' Draws A Helix
  42. Local a
  43. Local x# ' Helix x Coordinate
  44. Local y# ' Helix y Coordinate
  45. Local z# ' Helix z Coordinate
  46. Local phi# ' Angle
  47. Local theta# ' Angle
  48. Local v#,u# ' Angles
  49. Local r# ' Radius Of Twist
  50. Local twists = 5 ' 5 Twists
  51. Local glfMaterialColor#[]=[0.4#,0.2#,0.8#,1.0#] ' Set The Material Color
  52. Local specular#[]=[1.0#,1.0#,1.0#,1.0#] ' Sets Up Specular Lighting
  53. Local tv1#[3],tv2#[3]
  54. glLoadIdentity() ' Reset The Modelview Matrix
  55. gluLookAt(0, 5, 50, 0, 0, 0, 0, 1, 0) ' Eye Position (0,5,50) Center Of Scene (0,0,0), Up On Y Axis
  56. glPushMatrix() ' Push The Modelview Matrix
  57. glTranslatef(0,0,-50) ' Translate 50 Units Into The Screen
  58. glRotatef(angle/2.0,1,0,0) ' Rotate By angle/2 On The X-Axis
  59. glRotatef(angle/3.0,0,1,0) ' Rotate By angle/3 On The Y-Axis
  60. glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,glfMaterialColor)
  61. glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,specular)
  62. r=1.5 ' Radius
  63. glBegin(GL_QUADS) ' Begin Drawing Quads
  64. For phi=0 Until 360 Step 20.0 ' 360 Degrees In Steps Of 20
  65. For theta=0 Until (360*twists) Step 20.0 ' 360 Degrees * Number Of Twists In Steps Of 20
  66. v=(phi/180.0*3.142) ' Calculate Angle Of First Point ( 0 )
  67. u=(theta/180.0*3.142) ' Calculate Angle Of First Point ( 0 )
  68. x=(cos_r(u)*(2.0+cos_r(v) ))*r ' Calculate x Position (1st Point)
  69. y=(sin_r(u)*(2.0+cos_r(v) ))*r ' Calculate y Position (1st Point)
  70. z=((( u-(2.0*3.142)) + sin_r(v) )* r) ' Calculate z Position (1st Point)
  71. vertexes[0,0]=x ' Set x Value Of First Vertex
  72. vertexes[0,1]=y ' Set y Value Of First Vertex
  73. vertexes[0,2]=z ' Set z Value Of First Vertex
  74. v=(phi/180.0*3.142) ' Calculate Angle Of Second Point ( 0 )
  75. u=((theta+20)/180.0*3.142) ' Calculate Angle Of Second Point ( 20 )
  76. x=(cos_r(u)*(2.0+cos_r(v) ))*r ' Calculate x Position (2nd Point)
  77. y=(sin_r(u)*(2.0+cos_r(v) ))*r ' Calculate y Position (2nd Point)
  78. z=((( u-(2.0*3.142)) + sin_r(v) ) * r) ' Calculate z Position (2nd Point)
  79. vertexes[1,0]=x ' Set x Value Of Second Vertex
  80. vertexes[1,1]=y ' Set y Value Of Second Vertex
  81. vertexes[1,2]=z ' Set z Value Of Second Vertex
  82. v=((phi+20)/180.0*3.142) ' Calculate Angle Of Third Point ( 20 )
  83. u=((theta+20)/180.0*3.142) ' Calculate Angle Of Third Point ( 20 )
  84. x=(cos_r(u)*(2.0+cos_r(v) ))*r ' Calculate x Position (3rd Point)
  85. y=(sin_r(u)*(2.0+cos_r(v) ))*r ' Calculate y Position (3rd Point)
  86. z=((( u-(2.0*3.142)) + sin_r(v) ) * r) ' Calculate z Position (3rd Point)
  87. vertexes[2,0]=x ' Set x Value Of Third Vertex
  88. vertexes[2,1]=y ' Set y Value Of Third Vertex
  89. vertexes[2,2]=z ' Set z Value Of Third Vertex
  90. v=((phi+20)/180.0*3.142) ' Calculate Angle Of Fourth Point ( 20 )
  91. u=((theta)/180.0*3.142) ' Calculate Angle Of Fourth Point ( 0 )
  92. x=Float(cos_r(u)*(2.0+cos_r(v) ))*r ' Calculate x Position (4th Point)
  93. y=Float(sin_r(u)*(2.0+cos_r(v) ))*r ' Calculate y Position (4th Point)
  94. z=Float((( u-(2.0*3.142)) + sin_r(v) ) * r) ' Calculate z Position (4th Point)
  95. vertexes[3,0]=x ' Set x Value Of Fourth Vertex
  96. vertexes[3,1]=y ' Set y Value Of Fourth Vertex
  97. vertexes[3,2]=z ' Set z Value Of Fourth Vertex
  98. For a=0 Until 3
  99. ' Calculate The Vector From Point 1 To Point 0
  100. tv1[a] = vertexes[0,a] - vertexes[1,a] ' Vector 1.x=Vertex[0].x-Vertex[1].x
  101. tv2[a] = vertexes[1,a] - vertexes[2,a] ' Vector 2.x=Vertex[0].x-Vertex[1].x
  102. ' Compute The Cross Product To Give Us A Surface Normal
  103. Next
  104. normal[0] = tv1[1]*tv2[2] - tv1[2]*tv2[1]
  105. normal[1] = tv1[2]*tv2[0] - tv1[0]*tv2[2]
  106. normal[2] = tv1[0]*tv2[1] - tv1[1]*tv2[0]
  107. ReduceToUnit(normal) ' Normalize The Vectors
  108. glNormal3f(normal[0],normal[1],normal[2]) ' Set The Normal
  109. ' Render The Quad
  110. glVertex3f(vertexes[0,0],vertexes[0,1],vertexes[0,2])
  111. glVertex3f(vertexes[1,0],vertexes[1,1],vertexes[1,2])
  112. glVertex3f(vertexes[2,0],vertexes[2,1],vertexes[2,2])
  113. glVertex3f(vertexes[3,0],vertexes[3,1],vertexes[3,2])
  114. Next
  115. Next
  116. glEnd() ' Done Rendering Quads
  117. glPopMatrix() ' Pop The Matrix
  118. EndFunction
  119. Function ViewOrtho() ' Set Up An Ortho View
  120. glMatrixMode(GL_PROJECTION) ' Select Projection
  121. glPushMatrix() ' Push The Matrix
  122. glLoadIdentity() ' Reset The Matrix
  123. glOrtho( 0, C_WIDTH ,C_HEIGHT , 0, -10, 10 ) ' Select Ortho Mode (640x480)
  124. glMatrixMode(GL_MODELVIEW) ' Select Modelview Matrix
  125. glPushMatrix() ' Push The Matrix
  126. glLoadIdentity() ' Reset The Matrix
  127. EndFunction
  128. Function ViewPerspective() ' Set Up A Perspective View
  129. glMatrixMode( GL_PROJECTION ) ' Select Projection
  130. glPopMatrix() ' Pop The Matrix
  131. glMatrixMode( GL_MODELVIEW ) ' Select Modelview
  132. glPopMatrix() ' Pop The Matrix
  133. EndFunction
  134. Function RenderToTexture() ' Renders To A Texture
  135. glViewport(0,0,tSize,tSize) ' Set Our Viewport (Match Texture Size)
  136. ProcessHelix() ' Render The Helix
  137. glBindTexture(GL_TEXTURE_2D,BlurTexture) ' Bind To The Blur Texture
  138. ' Copy Our ViewPort To The Blur Texture (From 0,0 To 128,128... No Border)
  139. glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 0, 0, tSize, tSize, 0)
  140. glClearColor(0.0, 0.0, 0.25, 0.5) ' Set The Clear Color To Medium Blue
  141. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ' Clear The Screen And Depth Buffer
  142. glViewport(0 , 0,C_WIDTH ,C_HEIGHT) ' Set Viewport (0,0 to 640x480)
  143. EndFunction
  144. Function DrawBlur(times, inc#) ' Draw The Blurred Image
  145. Local num
  146. Local spost# = 0.0 ' Starting Texture Coordinate Offset
  147. Local alphainc# = 0.9 / Float(times) ' Fade Speed For Alpha Blending
  148. Local alpha# = 0.1 ' Starting Alpha Value
  149. ' Disable AutoTexture Coordinates
  150. glDisable(GL_TEXTURE_GEN_S)
  151. glDisable(GL_TEXTURE_GEN_T)
  152. glEnable(GL_TEXTURE_2D) ' Enable 2D Texture Mapping
  153. glDisable(GL_DEPTH_TEST) ' Disable Depth Testing
  154. glBlendFunc(GL_SRC_ALPHA,GL_ONE) ' Set Blending Mode
  155. glEnable(GL_BLEND) ' Enable Blending
  156. glBindTexture(GL_TEXTURE_2D,BlurTexture) ' Bind To The Blur Texture
  157. ViewOrtho() ' Switch To An Ortho View
  158. alphainc = alpha / times ' alphainc=0.2f / Times To Render Blur
  159. glBegin(GL_QUADS) ' Begin Drawing Quads
  160. For num = 0 Until times ' Number Of Times To Render Blur
  161. glColor4f(1.0, 1.0, 1.0, alpha) ' Set The Alpha Value (Starts At 0.2)
  162. glTexCoord2f(0+spost,1-spost) ' Texture Coordinate ( 0, 1 )
  163. glVertex2f(0,0) ' First Vertex ( 0, 0 )
  164. glTexCoord2f(0+spost,0+spost) ' Texture Coordinate ( 0, 0 )
  165. glVertex2f(0,C_HEIGHT) ' Second Vertex ( 0, 480 )
  166. glTexCoord2f(1-spost,0+spost) ' Texture Coordinate ( 1, 0 )
  167. glVertex2f(C_WIDTH,C_HEIGHT) ' Third Vertex ( C_WIDTH, 480 )
  168. glTexCoord2f(1-spost,1-spost) ' Texture Coordinate ( 1, 1 )
  169. glVertex2f(C_WIDTH,0) ' Fourth Vertex ( C_WIDTH, 0 )
  170. spost:+ inc ' Gradually Increase spost (Zooming Closer To Texture Center)
  171. alpha = alpha - alphainc ' Gradually Decrease alpha (Gradually Fading Image Out)
  172. Next
  173. glEnd() ' Done Drawing Quads
  174. ViewPerspective() ' Switch To A Perspective View
  175. glEnable(GL_DEPTH_TEST) ' Enable Depth Testing
  176. glDisable(GL_TEXTURE_2D) ' Disable 2D Texture Mapping
  177. glDisable(GL_BLEND) ' Disable Blending
  178. glBindTexture(GL_TEXTURE_2D,0) ' Unbind The Blur Texture
  179. EndFunction
  180. Function Initialize ()
  181. ' Start Of User Initialization
  182. angle = 0.0 ' Set Starting Angle To Zero
  183. BlurTexture = EmptyTexture() ' Create Our Empty Texture
  184. glViewport(0 , 0,C_WIDTH ,C_HEIGHT) ' Set Up A Viewport
  185. glMatrixMode(GL_PROJECTION) ' Select The Projection Matrix
  186. glLoadIdentity() ' Reset The Projection Matrix
  187. gluPerspective(50, Float(C_WIDTH)/Float(C_HEIGHT), 5, 2000) ' Set Our Perspective
  188. glMatrixMode(GL_MODELVIEW) ' Select The Modelview Matrix
  189. glLoadIdentity() ' Reset The Modelview Matrix
  190. glEnable(GL_DEPTH_TEST) ' Enable Depth Testing
  191. Local global_ambient#[]=[0.2#, 0.2#, 0.2#, 1.0#] ' Set Ambient Lighting To Fairly Dark Light (No Color)
  192. Local light0pos#[]= [0.0#, 5.0#, 10.0#, 1.0#] ' Set The Light Position
  193. Local light0ambient#[]= [0.2#, 0.2#, 0.2#, 1.0#] ' More Ambient Light
  194. Local light0diffuse#[]= [0.3#, 0.3#, 0.3#, 1.0#] ' Set The Diffuse Light A Bit Brighter
  195. Local light0specular#[]=[0.8#, 0.8#, 0.8#, 1.0#] ' Fairly Bright Specular Lighting
  196. Local lmodel_ambient#[]=[ 0.2#,0.2#,0.2#,1.0#] ' And More Ambient Light
  197. glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lmodel_ambient) ' Set The Ambient Light Model
  198. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient) ' Set The Global Ambient Light Model
  199. glLightfv(GL_LIGHT0, GL_POSITION, light0pos) ' Set The Lights Position
  200. glLightfv(GL_LIGHT0, GL_AMBIENT, light0ambient) ' Set The Ambient Light
  201. glLightfv(GL_LIGHT0, GL_DIFFUSE, light0diffuse) ' Set The Diffuse Light
  202. glLightfv(GL_LIGHT0, GL_SPECULAR, light0specular) ' Set Up Specular Lighting
  203. glEnable(GL_LIGHTING) ' Enable Lighting
  204. glEnable(GL_LIGHT0) ' Enable Light0
  205. glShadeModel(GL_SMOOTH) ' Select Smooth Shading
  206. glMateriali(GL_FRONT, GL_SHININESS, 128)
  207. glClearColor(0.0, 0.0, 0.0, 0.5) ' Set The Clear Color To Black
  208. Return 1 ' Return TRUE (Initialization Successful)
  209. EndFunction
  210. Function Update(milliseconds#) ' Perform Motion Updates Here
  211. If KeyDown(KEY_ESCAPE) End
  212. angle:+(milliseconds/5.0) ' Update angle Based On The Clock
  213. EndFunction
  214. Function Draw() ' Draw The Scene
  215. glClearColor(0.0, 0.0, 0.0, 0.25) ' Set The Clear Color To Black
  216. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ' Clear Screen And Depth Buffer
  217. glLoadIdentity() ' Reset The View
  218. RenderToTexture() ' Render To A Texture
  219. ProcessHelix() ' Draw Our Helix
  220. DrawBlur(25,.02) ' Draw The Blur Effect
  221. glFlush () ' Flush The GL Rendering Pipeline
  222. EndFunction
  223. GLGraphics C_WIDTH,C_HEIGHT,32
  224. Initialize()
  225. Repeat
  226. Update(25)
  227. Draw()
  228. Flip
  229. Forever