Advertisement
Guest User

basic4gl render bug

a guest
Apr 1st, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.35 KB | None | 0 0
  1. ' Wavefront .obj viewer
  2.  
  3. ' Data structures
  4. '
  5. ' Data is stored as a list of vertices, containing
  6.  
  7. struc SOBJ_polyVertex
  8. dim vertexIndex
  9. dim normIndex
  10. endstruc
  11.  
  12. struc SOBJ_poly
  13. dim firstVertex, vertexCount ' Indicates a section of the polyVertex array that makes up the current polygon
  14. endstruc
  15.  
  16. struc SOBJ_model
  17. dim vertexCount, &vertex# ()() ' Actual X, Y, Z vertices
  18. dim normCount, &norm#()() ' Actual X, Y, Z normals
  19. dim polyVertexCount, SOBJ_polyVertex &polyVertex () ' Indices of vertices arranged into polygons
  20. dim polyCount, SOBJ_poly &poly () ' Polygon data
  21. endstruc
  22.  
  23. ' Variables.
  24. ' Passed as parameters to subroutines
  25. dim SOBJ_model &theModel, SOBJ_poly &thePoly, theFile, theDir$, theFileName$, SOBJ_polyVertex &thePolyVertex
  26. dim &theVec#()
  27.  
  28. ' Working variables
  29. dim i, i2, tempToken$, tempChar$, y
  30.  
  31. ' Subroutines
  32. goto Start
  33.  
  34. LoadChar:
  35. tempChar$ = ReadChar (theFile) ' Read character
  36. if tempChar$ >= "A" and tempChar$ <= "Z" then ' Convert to lowercase
  37. tempChar$ = Chr$ (Asc(tempChar$) - Asc("A") + Asc ("a"))
  38. endif
  39. return
  40.  
  41. InternalLoadToken:
  42. ' Load a text token from file "theFile"
  43. ' Same as LoadToken below, but doesn't strip comments
  44. tempToken$ = ""
  45. if tempChar$ = "" then return endif
  46.  
  47. ' Skip whitespace and format characters (except for chr$(13))
  48. while tempChar$ <> "" and tempChar$ <> chr$(13) and tempChar$ <= " "
  49. gosub LoadChar
  50. wend
  51. if tempChar$ = "" then
  52. return
  53. endif
  54.  
  55. ' Determine token type from first character
  56. tempToken$ = tempChar$
  57. if tempChar$ >= "a" and tempChar$ <= "z" then ' Word
  58.  
  59. ' Read character string
  60. gosub LoadChar
  61. while tempChar$ >= "a" and tempChar$ <= "z"
  62. tempToken$ = tempToken$ + tempChar$
  63. gosub LoadChar
  64. wend
  65. return
  66. endif
  67.  
  68. if tempChar$ >= "0" and tempChar$ <= "9" or tempChar$ = "-" or tempChar$ = "+" or tempChar$ = "." then ' Number
  69.  
  70. ' Read number
  71. gosub LoadChar
  72. while tempChar$ >= "0" and tempChar$ <= "9" or tempChar$ = "-" or tempChar$ = "+" or tempChar$ = "." or tempChar$ = "e"
  73. tempToken$ = tempToken$ + tempChar$
  74. gosub LoadChar
  75. wend
  76. return
  77. endif
  78.  
  79. ' Treat everything else as a single character symbol
  80. gosub LoadChar
  81. return
  82.  
  83. LoadToken:
  84. ' Load a text token from file "theFile"
  85.  
  86. ' Expects: theFile = An open file handle
  87. ' Returns: tempToken$ = "" If end of file reached
  88. ' tempToken$ = chr$(13) If end of line reached
  89. ' tempToken$ =
  90.  
  91. ' Load a token
  92. gosub InternalLoadToken
  93.  
  94. ' Skip comments
  95. if tempToken$ = "#" then
  96. gosub InternalLoadToken
  97. while tempToken$ <> chr$(13) and tempToken$ <> ""
  98. gosub InternalLoadToken
  99. wend
  100. endif
  101. return
  102.  
  103. OpenObjFile:
  104. theFile = OpenFileRead (theDir$ + theFileName$)
  105. tempChar$ = " "
  106. if FileError () <> "" then
  107. printr "Error opening " + theDir$ + theFileName$
  108. printr FileError ()
  109. end
  110. endif
  111. return
  112.  
  113. LoadObj:
  114.  
  115. ' Expects: theDir$ = Directory containing file(s)
  116. ' theFileName$ = Obj file to load
  117.  
  118. ' File is scanned in 2 passes.
  119. ' First pass counts the elements of each type.
  120. ' Second loads them
  121.  
  122. ' Open file
  123. gosub OpenObjFile
  124.  
  125. ' Allocate model
  126. alloc theModel
  127. dim nonorm = 0
  128.  
  129. ' First pass: Count
  130. printr "Count vertices, polys, normals..."
  131. ' for i2 = 0 to thePoly.vertexCount - 1
  132. ' &thePolyVertex = &theModel.polyVertex (thePoly.firstVertex + i2)
  133. '
  134. ' theModel.vertex# (thePolyVertex.vertexIndex) + 1
  135. '
  136. ' next
  137. gosub LoadToken
  138. while tempToken$ <> ""
  139. y = 1: gosub DisplayCounters
  140. if tempToken$ = "v" then
  141.  
  142. ' Count vertex
  143. theModel.vertexCount = theModel.vertexCount + 1
  144. if nonorm = 1 then
  145. theModel.normCount = theModel.normCount + 1
  146. endif
  147. endif
  148.  
  149. if tempToken$ = "vn" then
  150.  
  151. ' Count normal
  152. theModel.normCount = theModel.normCount + 1
  153. endif
  154.  
  155. if themodel.normCount = 0 then
  156. themodel.normcount = themodel.vertexcount
  157. nonorm = 1
  158. endif
  159. if tempToken$ = "f" then
  160.  
  161. ' Count polygon
  162. theModel.polyCount = theModel.vertexCount + 1
  163.  
  164. ' Count polygon corners
  165. gosub LoadToken
  166. while tempToken$ <> "" and tempToken$ <> chr$(13)
  167. theModel.polyVertexCount = theModel.polyVertexCount + 1
  168. while tempChar$ <> "" and tempChar$ > " " ' Skip to whitespace
  169. gosub LoadChar
  170. wend
  171. gosub LoadToken
  172. wend
  173. endif
  174.  
  175. ' Skip to end of line
  176. while tempToken$ <> chr$(13)
  177. gosub LoadToken
  178. wend
  179.  
  180. ' Next token
  181. gosub LoadToken
  182. wend
  183. gosub DisplayCounters
  184.  
  185. ' Allocate vertices and polygons
  186. alloc theModel.vertex#, theModel.vertexCount - 1, 2
  187. alloc theModel.norm#, theModel.normCount - 1, 2
  188. alloc theModel.poly, theModel.polyCount - 1
  189. alloc theModel.polyVertex, theModel.polyVertexCount - 1
  190.  
  191. ' Reset counters
  192. theModel.vertexCount = 0
  193. theModel.normCount = 0
  194. theModel.polyCount = 0
  195. theModel.polyVertexCount = 0
  196.  
  197. ' Second pass. Load data
  198. printr "Load model..."
  199. CloseFile (theFile)
  200. gosub OpenObjFile
  201.  
  202. gosub LoadToken
  203. while tempToken$ <> ""
  204. y = 3: gosub DisplayCounters
  205. if tempToken$ = "v" then ' Vector found
  206.  
  207. ' Load in each coordinate
  208. y = 1: gosub LoadToken
  209. for i = 0 to 2
  210. if tempToken$ <> "" and tempToken$ <> chr$(13) then
  211. if nonorm = 0 then
  212. theModel.vertex# (theModel.vertexCount) (i) = val (tempToken$)
  213. else
  214. theModel.vertex# (theModel.vertexCount) (i) = val (tempToken$)
  215. theModel.norm# (theModel.normCount) (i) = val (tempToken$)
  216. endif
  217. gosub LoadToken
  218. endif
  219.  
  220. next
  221. theModel.vertexCount = theModel.vertexCount + 1
  222. else
  223. if tempToken$ = "vn" then ' Normal found
  224.  
  225. ' Load in coordinates
  226. gosub LoadToken
  227. for i = 0 to 2
  228. if tempToken$ <> "" and tempToken$ <> chr$(13) then
  229. theModel.norm# (theModel.normCount) (i) = val (tempToken$)
  230. gosub LoadToken
  231. endif
  232. next
  233. theModel.normCount = theModel.normCount + 1
  234. else
  235.  
  236. if tempToken$ = "f" then ' Face found
  237.  
  238. ' Record start of polygon vertices
  239. &thePoly = &theModel.poly (theModel.polyCount)
  240. theModel.polyCount = theModel.polyCount + 1
  241. thePoly.firstVertex = theModel.polyVertexCount
  242.  
  243. ' Read vertices
  244. gosub LoadToken
  245. while tempToken$ <> "" and tempToken$ <> chr$(13)
  246. theModel.polyVertex (theModel.polyVertexCount).vertexIndex = val (tempToken$) - 1 ' Note: In the file the index is 1 origin. We are using 0 origin, so must subtract 1
  247.  
  248. ' Vertex data stored in 3 parts, separated by slashes
  249. ' Look for second part
  250. gosub LoadToken
  251. if tempToken$ = "/" then
  252. gosub LoadToken
  253.  
  254. ' Second part is texture coordinates which we haven't implemented yet
  255. ' So skip it
  256. if tempToken$ <> "" and tempToken$ <> chr$(13) and tempToken$ <> "/" then
  257. gosub LoadToken
  258. endif
  259.  
  260. ' Third part is normal
  261. if tempToken$ = "/" then
  262. gosub LoadToken
  263. if tempToken$ <> "" and tempToken$ <> chr$(13) then
  264. theModel.polyVertex (theModel.polyVertexCount).normIndex = val (tempToken$) - 1
  265. endif
  266. endif
  267. endif
  268.  
  269. while tempChar$ <> "" and tempChar$ > " " ' Skip to whitespace
  270. gosub LoadChar
  271. wend
  272. gosub LoadToken
  273.  
  274. theModel.polyVertexCount = theModel.polyVertexCount + 1
  275. wend
  276.  
  277. ' Complete polygon definition
  278. thePoly.vertexCount = theModel.polyVertexCount - thePoly.firstVertex
  279. endif
  280. endif
  281. endif
  282.  
  283. ' Skip to end of line
  284. while tempToken$ <> chr$(13)
  285. gosub LoadToken
  286. wend
  287.  
  288. ' Next token
  289. gosub LoadToken
  290. wend
  291. gosub DisplayCounters
  292.  
  293. CloseFile (theFile)
  294. return
  295.  
  296. DisplayCounters:
  297. ' Output vertex and poly counts. (To show we're actually doing something...!)
  298. locate 0, y: printr theModel.vertexCount + ", " + theModel.normCount + ", " + theModel.polyCount + ", " + theModel.polyVertexCount
  299. return
  300.  
  301. DrawObj:
  302.  
  303. ' Render obj model
  304. ' Expects: theModel = Points to model to render
  305. 'glpushmatrix()
  306. for i = 0 to theModel.polyCount - 1
  307. &thePoly = &theModel.poly (i)
  308. glpushmatrix()
  309. glBegin (GL_TRIANGLE_FAN)
  310. for i2 = 0 to thePoly.vertexCount - 1
  311. &thePolyVertex = &theModel.polyVertex (thePoly.firstVertex + i2)
  312. ' printr "drawing"
  313. 'if nonorm = 0 then 'create norms (if none are present then it will attempt to create one)
  314. 'glNormal3fv (theModel.norm# (thePolyVertex.normIndex)) 'commented it out because it kept on crashing
  315. 'else
  316. 'printr "nonorm = 1"
  317. 'glnormal3fv (theModel.vertex# (thePolyVertex.vertexIndex))
  318. 'endif
  319. glVertex3fv (theModel.vertex# (thePolyVertex.vertexIndex))
  320.  
  321. next
  322. glEnd ()
  323.  
  324. next
  325. glpopmatrix ()
  326. return
  327.  
  328. ' Main program
  329. Start:
  330.  
  331. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  332. ' Test by loading a sample model
  333.  
  334. dim SOBJ_model &model, xang#, yang#, zoom#, list, key$, saveFileName$
  335.  
  336. 'ResizeText (60, 30)
  337.  
  338. theDir$ = "Files\"
  339. theFileName$ = "level001.obj"
  340. gosub LoadObj
  341.  
  342.  
  343. 'Models can have several thousand vertices and polygons, which can cause our
  344. 'virtual machine based language to chug a little bit.
  345. ' Therefore we render the frame once into a display list, which will usually speed things up dramatically
  346. printr "Create display list..."
  347. list = glGenLists (1)
  348. glNewList (list, gl_compile)
  349. gosub DrawObj
  350. glEndList ()
  351. &model = &theModel
  352.  
  353. ' Lighting
  354. glEnable (GL_LIGHTING)
  355. glEnable (GL_LIGHT0)
  356. glEnable (GL_COLOR_MATERIAL)
  357.  
  358.  
  359.  
  360.  
  361. dim camX#, camY#, camZ#, camAng# ' Camera position and direction
  362. dim keydowne=0
  363. dim camAngY#, camY2#
  364. dim camAngX#, camX2#
  365. dim pausemenu = 0
  366. dim mouseX, mouseY, mouseXPrev, mouseYPrev
  367. Dim Angle#(2)
  368. dim isrunning=1
  369. dim jumplimit = 5 'max hight reached while jumping
  370. dim camheight = camy2#
  371. 'dim BGM_Model& model = LoadBGM("Files\", "lvl4.bgm")
  372. dim isjumping = 0
  373. dim jumpup =0
  374. dim jumpdown=0
  375. dim heightbeforechange = camy2#
  376. 'dim i
  377.  
  378. 'camstartcord
  379. camX# = 0
  380. camY# = 3
  381. camZ# = 0
  382. '`1gluPerspective (60, (1.0*WindowWidth()) / WindowHeight(), 0.1, 100)
  383. 'glMatrixMode (GL_MODELVIEW)
  384.  
  385. while isrunning=1
  386. camheight = camy#
  387. ' Draw scene
  388.  
  389. ' Clear the screen
  390. glClear (GL_DEPTH_BUFFER_BIT or GL_COLOR_BUFFER_BIT)
  391. ' Clear the depth buffer (Z buffer)
  392. ' and the colour buffer (pixels)
  393. 'Position camera
  394.  
  395. glLoadIdentity ()
  396. glRotatef(Angle#(2), 0, 0, 1)
  397. glRotatef(Angle#(1), 1, 0, 0)
  398. glRotatef(Angle#(0), 0, 1, 0)
  399. glTranslatef (-camX#, -camY#, -camZ#)
  400. glCallList (list)
  401.  
  402. 'glTranslatef(0, -1, -5)
  403. 'glCallList (list)'DrawBGM(model)
  404.  
  405. SwapBuffers ()
  406.  
  407. ' Move camera
  408. while SyncTimer (10)
  409.  
  410.  
  411. Angle#(0) = Angle#(0) + Mouse_XD()*60.0
  412. Angle#(1) = Angle#(1) + Mouse_YD()*60.0
  413. if keydown ("p") then
  414.  
  415. endif
  416. 'endif
  417. if KeyDown ("W") then
  418. if pausemenu = 0 then
  419. camX# = camX# + Cosd(Angle#(0)-90) * .2
  420. camZ# = camZ# + Sind(Angle#(0)-90) * .2
  421. endif
  422. endif
  423. if KeyDown ("S") then
  424. if pausemenu = 0 then
  425. camX# = camX# - Cosd(Angle#(0)-90) * .2
  426. camZ# = camZ# - Sind(Angle#(0)-90) * .2
  427. endif
  428. endif
  429. if KeyDown("A") Then
  430. if pausemenu = 0 then
  431. camX# = camX# - Cosd(Angle#(0)) * .2
  432. camZ# = camZ# - Sind(Angle#(0)) * .2
  433. Endif
  434. endif
  435. if KeyDown("D") Then
  436. if pausemenu = 0 then
  437. camX# = camX# + Cosd(Angle#(0)) * .2
  438. camZ# = camZ# + Sind(Angle#(0)) * .2
  439. Endif
  440. endif
  441. if isjumping = 0then
  442. if KeyDown("E") then
  443. camY# = camY# + 0.105
  444. keydowne = 1
  445. isjumping = 1
  446. jumpup = 1
  447. endif
  448. endif
  449. if isjumping =1 then
  450. if camheight < jumplimit then
  451. if jumpup =1 then
  452. camy# = camy# + 0.0575
  453. endif
  454. endif
  455. endif
  456. if isjumping = 1 then
  457. if camheight >= jumplimit then
  458. jumpup = 0
  459. jumpdown=1
  460. endif
  461. endif
  462. if isjumping = 1 then
  463. if jumpdown = 1 then
  464. if heightbeforechange = camy# then
  465. jumpdown = 0
  466. isjumping = 0
  467. endif
  468. endif
  469. endif
  470. if camy#<=3 then
  471. isjumping = 0
  472. jumpdown = 0
  473. endif
  474. if isjumping = 1 then
  475. if jumpdown = 1 then
  476. camy# = camy# - 0.075
  477. endif
  478. endif
  479.  
  480. heightbeforechange = camy#
  481. wend
  482. wend
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement