January 15, 2021, 06:39:26 PM

Author Topic: [bb] Virtual GL by N [ 1+ years ago ]  (Read 1349 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Virtual GL by N [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : Virtual GL
Author : N
Posted : 1+ years ago

Description : This is a quick bit of code I wrote in about an hour and a half to simulate OpenGL at a very, very basic level.  It's just made out of boredom.  Chances are there's no good reason to use it, but I'll keep adding to it until I'm satisfied ^_^

Update: Fixed some stuff with the texturing.  Added an untested triangle fan handler, I'm pretty sure it should work, but I won't know 'til someone tries it.  Added vglEnable/Disable, only two of the enumerators are true to OpenGL though.

Update 2: Fixed the triangle fan code.  Added fake Push/PopMatrix functions.  Changed vglLoadTexture() so it returns the handle of the Surface object instead of a reference to it.


Code :
Code: BlitzBasic
  1. ;VGL Example
  2.  
  3. Graphics3d 800,600,32,2
  4.  
  5. ;Include "vgl.bb"
  6.  
  7. vglInit()
  8.  
  9. ;EntityFX vglDisplayMesh,1+2+16
  10.  
  11. ;PART_FIRE = vglLoadTexture("part_fire.tga",1)     ;;;  UNCOMMENT THIS IS YOU'VE GOT A TEXTURE TO USE.
  12.  
  13. ;vglEnable(VGL_CULL_FACE)
  14. vglEnable(VGL_AUTO_NORMAL)
  15. vglDisable(VGL_VERTEX_ALPHA)
  16.  
  17. UsePushPop = True
  18.  
  19. Repeat
  20.      vglClear(VGL_COLOR_BUFFER_BIT)
  21.  
  22.      vglBegin(VGL_TRIANGLE_FAN)
  23.           vglVertex3f(0,0,0)
  24.           vglColor4f(0,1,0,1)
  25.           For X = 0 To 32
  26.                vglVertex3f(Sin(X*11.25),Cos(X*11.25),0)
  27.                vglColor4b(Sin(X*11.25)*255,Cos(X*11.25),X*7.96875,128)
  28.           Next
  29.      vglEnd()
  30.  
  31.      vglRotatef(Float(Millisecs())/60,.5,1,.25)
  32.      vglTranslatef(0,0,4)
  33.  
  34.      If Keyhit(57) Then UsePushPop = Not UsePushPop
  35.      If UsePushPop Then vglPushMatrix()
  36.           vglBegin(VGL_TRIANGLES)
  37.                vglVertex3f(-1,-1,0)
  38.                vglColor4f(1,0,0,1)
  39.                vglVertex3f(0,1,0)
  40.                vglColor4f(0,1,0,1)
  41.                vglVertex3f(1,-1,0)
  42.                vglColor4f(0,0,1,1)
  43.           vglEnd()
  44.           vglTranslatef(0,0,4)
  45.           vglRotatef(Float(Millisecs())/60,0,1,0)
  46.           vglTranslatef(0,0,4)
  47.      If UsePushPop Then vglPopMatrix()
  48.  
  49.      For E.Error = Each Error
  50.           Debuglog EEnum
  51.      Next
  52.      vglFlip()
  53. Until Keyhit(1)
  54.  
  55. vglQuit()
  56.  
  57.  
  58. ;;;;; VirtualGL CODE
  59.  
  60.  
  61. ;VirtualGL is a GL 'simulator' (a laughable concept)
  62. ;basically, this tries to mimic the very most basic level of using OpenGL
  63.  
  64. ;no enumerators should resemble the GL enumerators or variables, if they do then it's purely coincedence.
  65. ;oh yeah, and no pushing/popping
  66.  
  67. ;currently can only draw triangles and quads, and there're a few useless banks
  68.  
  69. Type Surface
  70.      Field Texture
  71.      Field Surface[32]
  72. End Type
  73.  
  74.  
  75. Type Error
  76.      Field Enum
  77. End Type
  78.  
  79.  
  80. Type DisplayList
  81.      Field Offset
  82.      Field Length
  83. End Type
  84.  
  85.  
  86. Const VGL_INVALID_ENUM = $E3FAFF01     ;invalid enumerator error
  87. Const VGL_NULL_TEXTURE = $0038AF72     ;null texture- texture not loaded.
  88.  
  89. ;vglBegin enumerators
  90. Const VGL_TRIANGLES% = $00000001
  91. Const VGL_QUADS% = $00000002
  92. Const VGL_TRIANGLE_STRIP% = $00000003
  93. Const VGL_QUAD_STRIP% = $00000004
  94. Const VGL_TRIANGLE_FAN% = $00000005
  95. Const VGL_POINTS% = $00000006
  96. Const VGL_LINES% = $00000007
  97. Const VGL_LINE_STRIP% = $00000008
  98. Const VGL_LINE_LOOP% = $00000009
  99. Const VGL_POLYGON% = $0000000A
  100.  
  101. ;vglEnd enumerators
  102. Global VGL_DRAW_MODE% = 0
  103.  
  104. ;vglEnable / vglDisable enumerators
  105. Const VGL_CULL_FACE = $6A9F0713
  106. Const VGL_AUTO_NORMAL = $FD9F1713
  107. Const VGL_VERTEX_ALPHA = $BAD213FE
  108.  
  109. ;VirtualGL data banks, or databases, take your pick
  110. Global vglCommonData% = 0
  111. Global vglDisplayListData% = 0
  112. Global vglVertexArrayData% = 0
  113.  
  114. Global vglDisplayMesh
  115.  
  116. Global NullTexture.Surface
  117. Global vglCurrentTexture.Surface
  118.  
  119. Global vglBeginOpen = False
  120. Global vglVertexOffset
  121.  
  122. Global vglViewport
  123.  
  124. Global vglRotationYaw#,vglRotationPitch#,vglRotationRoll#
  125. Global vglTranslationX#,vglTranslationY#,vglTranslationZ#
  126.  
  127. Global vglCullFaces = False
  128. Global vglUpdateNormals = False
  129. Global vglVertexAlpha = False
  130.  
  131. Dim vglStack(0)   ;to me, this is where it gets interesting.
  132. Global vglStackOffset = 0
  133.  
  134.  
  135. Function vglInit()
  136.      vglCommonData = CreateBank(0)
  137.      vglDisplayListData = CreateBank(0)
  138.      vglVertexArrayData = CreateBank(0)
  139.      vglDisplayMesh = CreateMesh()
  140.      EntityFX vglDisplayMesh,1+2
  141.      NullTexture.Surface = New Surface
  142.      NullTextureSurface[vglStackOffset] = CreateSurface(vglDisplayMesh)
  143.      vglCurrentTexture = NullTexture
  144.      vglViewport = CreateCamera()
  145.      CameraClsMode vglViewport,0,1 ;only clear the z buffer
  146.  
  147.      Dim vglStack(32)
  148.      vglStack(0) = vglDisplayMesh
  149. End Function
  150.  
  151.  
  152. Function vglQuit()
  153.      FreeBank vglCommonData
  154.      FreeBank vglDisplayListData
  155.      FreeBank vglVertexArrayData
  156.      FreeEntity vglDisplayMesh
  157.      For S.Surface = Each Surface
  158.           If STexture <> 0 Then FreeBrush STexture
  159.           Delete S
  160.      Next
  161. End Function
  162.  
  163.  
  164. Function vglBegin(VGL_ENUM=0)
  165.      If Not vglCommonData% <> 0 Or vglDisplayListData% <> 0 Or vglVertexArrayData% <> 0 Or vglDisplayMesh <> 0 Then vglError(VGL_NOT_INITIALIZED)
  166.      vglBeginOpen = True
  167.      vglVertexOffset = CountVertices(vglCurrentTextureSurface[vglStackOffset])
  168.      VGL_DRAW_MODE = VGL_ENUM
  169. End Function
  170.  
  171.  
  172. Function vglEnd()
  173.      Select VGL_DRAW_MODE
  174.           Case VGL_TRIANGLES
  175.                N = vglVertexOffset
  176.                While N+2 <= CountVertices(vglCurrentTextureSurface[vglStackOffset])-1
  177.                     AddTriangle vglCurrentTextureSurface[vglStackOffset],N,N+1,N+2
  178.                     N = N + 3
  179.                Wend
  180.  
  181.           Case VGL_QUADS
  182.                N = vglVertexOffset
  183.                While N+3 <= CountVertices(vglCurrentTextureSurface[vglStackOffset])-1
  184.                     AddTriangle vglCurrentTextureSurface[vglStackOffset],N,N+1,N+2
  185.                     AddTriangle vglCurrentTextureSurface[vglStackOffset],N+2,N+3,N
  186.                     N = N + 4
  187.                Wend
  188.  
  189.           Case VGL_TRIANGLE_STRIP
  190.  
  191.           Case VGL_QUAD_STRIP
  192.  
  193.           Case VGL_TRIANGLE_FAN
  194.                N = vglVertexOffset
  195.                While N +2 <= CountVertices(vglCurrentTextureSurface[vglStackOffset])-1
  196.                     AddTriangle vglCurrentTextureSurface[vglStackOffset],vglVertexOffset,N+1,N+2
  197.                     N = N + 1
  198.                Wend
  199.  
  200.           Case VGL_POINTS
  201.  
  202.           Case VGL_LINES
  203.  
  204.           Case VGL_LINE_STRIP
  205.  
  206.           Case VGL_LINE_LOOP
  207.  
  208.           Case VGL_POLYGON
  209.  
  210.           Default
  211.                vglError(VGL_INVALID_ENUM)
  212.      End Select
  213.  
  214.      FreeBank vglCommonData
  215.      vglCommonData = CreateBank()
  216.      VGL_DRAW_MODE = 0
  217.      vglVertexOffset = 0
  218.      vglBeginOpen = False
  219.      vglCurrentTexture = NullTexture
  220.      Return True
  221. End Function
  222.  
  223.  
  224. Function vglVertex3f(x#,y#,z#)
  225.      If VGL_DRAW_MODE = 0 Then Return False
  226.      AddVertex(vglCurrentTextureSurface[vglStackOffset],x,y,z)
  227. End Function
  228.  
  229.  
  230. Function vglTexCoord2f(u#,v#)
  231.      If VGL_DRAW_MODE = 0 Or CountVertices(vglCurrentTextureSurface[vglStackOffset])-vglVertexOffset = 0 Then Return False
  232.      VertexTexCoords vglCurrentTextureSurface[vglStackOffset],CountVertices(vglCurrentTextureSurface[vglStackOffset])-1,u,v,0,0
  233. End Function
  234.  
  235.  
  236. Function vglNormal3f(nx#,ny#,nz#)
  237.      If VGL_DRAW_MODE = 0 Or CountVertices(vglCurrentTextureSurface[vglStackOffset])-vglVertexOffset = 0 Then Return False
  238.      VertexNormal vglCurrentTextureSurface[vglStackOffset],CountVertices(vglCurrentTextureSurface[vglStackOffset])-1,nx,ny,nz
  239. End Function
  240.  
  241.  
  242. Function vglColor4f(r#,g#,b#,a#)
  243.      If VGL_DRAW_MODE = 0 Or CountVertices(vglCurrentTextureSurface[vglStackOffset])-vglVertexOffset = 0 Then Return False
  244.      VertexColor vglCurrentTextureSurface[vglStackOffset],CountVertices(vglCurrentTextureSurface[vglStackOffset])-1,r*255,g*255,b*255,a
  245. End Function
  246.  
  247.  
  248. Function vglColor4b(r%,g%,b%,a%)
  249.      If VGL_DRAW_MODE = 0 Or CountVertices(vglCurrentTextureSurface[vglStackOffset])-vglVertexOffset = 0 Then Return False
  250.      VertexColor vglCurrentTextureSurface[vglStackOffset],CountVertices(vglCurrentTextureSurface[vglStackOffset])-1,r,g,b,Float(a)/255
  251. End Function
  252.  
  253.  
  254. Function vglFlip()
  255.      EntityFX vglDisplayMesh,1+2+(16*(Not vglCullFaces))+(32*vglVertexAlpha)
  256.      If vglUpdateNormals Then UpdateNormals vglDisplayMesh
  257.      UpdateWorld
  258.      RenderWorld
  259.      Flip False
  260.  
  261.      For S.Surface = Each Surface
  262.           For N = 0 To 32
  263.                If SSurface[N] <> 0 Then ClearSurface SSurface[N],True,True
  264.           Next
  265.      Next
  266. End Function
  267.  
  268.  
  269. Function vglError(VGL_ERROR_ENUM)
  270.      E.Error = New Error
  271.      EEnum = VGL_ERROR_ENUM
  272. End Function
  273.  
  274.  
  275. Function vglGetError%()
  276.      E.Error = Last Error
  277.      Error = EEnum
  278.      Delete E
  279. End Function
  280.  
  281.  
  282. Function vglCallList(VGL_LIST.DisplayList)
  283. End Function
  284.  
  285.  
  286. Function vglAddVertex()
  287.      ResizeBank vglCommonData,BankSize(vglCommonData)+4*8+2          ;one short, eight floats, in this order: vertex number, x, y, z, normal x, y, z, u, v
  288.      Return BankSize(vglCommonData)-(4*8)-2                           ;it said five previously... old comment.  mistake.
  289. End Function
  290.  
  291.  
  292. Function vglLoadTexture(path$,flags=9)
  293.      TEX = LoadBrush(path$,flags)
  294.      If TEX <> 0 Then
  295.           S.Surface = New Surface
  296.           STexture = TEX
  297.           SSurface[0] = CreateSurface(vglDisplayMesh)
  298.           PaintSurface SSurface[vglStackOffset],STexture
  299.           Return Handle(S)
  300.      Endif
  301.      Return 0
  302. End Function
  303.  
  304.  
  305. Function vglBindTexture(Tex)     ;unfortunately, i can't simulate the second parameter of glBindTexture 'cause i don't know how to
  306.      Texture.Surface = Object.Surface(Tex)
  307.      If VGL_DRAW_MODE = 0 And Texture <> Null Then
  308.           vglCurrentTexture = Texture.Surface
  309.           If vglCurrentTextureSurface[vglStackOffset] = 0 Then vglCurrentTextureSurface[vglStackOffset] = CreateSurface(vglStack(vglStackOffset))
  310.           ;PaintSurface vglCurrentTextureSurface,vglCurrentTextureTexture
  311.           Return True
  312.      Endif
  313.      vglError(VGL_NULL_TEXTURE)
  314.      Return False
  315. End Function
  316.  
  317.  
  318. Function vglTranslatef(x#,y#,z#)
  319.      vglTranslationX = vglTranslationX + x
  320.      vglTranslationY = vglTranslationY + y
  321.      vglTranslationZ = vglTranslationZ + z
  322.      PositionMesh vglStack(vglStackOffset),x,y,z
  323. End Function
  324.  
  325.  
  326. Function vglRotatef(angle#,x#,y#,z#)
  327.      vglRotationPitch# = vglRotationPitch# + angle*x
  328.      vglRotationYaw# = vglRotationYaw# + angle*y
  329.      vglRotationRoll# = vglRotationRoll + angle*z
  330.      RotateMesh vglStack(vglStackOffset),0,0,angle*z
  331.      RotateMesh vglStack(vglStackOffset),angle*x,0,0
  332.      RotateMesh vglStack(vglStackOffset),0,angle*y,0
  333. End Function
  334.  
  335.  
  336. Function vglClear(mask)
  337.      Select VGL_ENUM
  338.           Case VGL_COLOR_BUFFER_BIT
  339.                SetBuffer(BackBuffer())
  340.                Cls
  341.           Default
  342.                vglError(VGL_INVALID_ENUM)
  343.      End Select
  344. End Function
  345.  
  346.  
  347. Function vglLoadIdentity()
  348.      PositionMesh vglStack(vglStackOffset),-vglTranslationX,-vglTranslationY,-vglTranslationZ
  349.      RotateMesh vglStack(vglStackOffset),-vglRotationPitch,-vglRotationYaw,-vglRotationRoll
  350.      vglTranslationZ = 0 : vglTranslationY = 0 : vglTranslationX = 0
  351.      vglRotationPitch = 0 : vglRotationYaw = 0 : vglRotationRoll = 0
  352. End Function
  353.  
  354.  
  355. Function vglEnable(cap)
  356.      Select cap
  357.           Case VGL_AUTO_NORMAL     ;'fraid these are the only really plausible ones to me
  358.                vglUpdateNormals = True
  359.  
  360.           Case VGL_CULL_FACE
  361.                vglCullFaces = True
  362.  
  363.           Case VGL_VERTEX_ALPHA
  364.                vglVertexAlpha = True
  365.  
  366.           Default
  367.                vglError(VGL_INVALID_ENUM)
  368.      End Select
  369. End Function
  370.  
  371.  
  372. Function vglDisable(cap)
  373.      Select cap
  374.           Case VGL_AUTO_NORMAL     ;'fraid these are the only really plausible ones to me
  375.                vglUpdateNormals = False
  376.  
  377.           Case VGL_CULL_FACE
  378.                vglCullFaces = False
  379.  
  380.           Case VGL_VERTEX_ALPHA
  381.                vglVertexAlpha = False
  382.  
  383.           Default
  384.                vglError(VGL_INVALID_ENUM)
  385.      End Select
  386. End Function
  387.  
  388.  
  389. Function vglPushMatrix() ;a really cheap attempt at push/popping matrices.
  390.      vglStackOffset = vglStackOffset + 1
  391.      If vglStackOffset > 32 Then
  392.           vglQuit()
  393.           RunTimeError "ERROR: vglStackOffset+1 > 32"
  394.      Endif
  395.      vglStack(vglStackOffset) = CreateMesh()
  396.      For S.Surface = Each Surface
  397.           SSurface[vglStackOffset] = CreateSurface(vglStack(vglStackOffset))
  398.           If S <> NullTexture Then PaintSurface SSurface[vglStackOffset],STexture
  399.      Next
  400. End Function
  401.  
  402.  
  403. Function vglPopMatrix()
  404.      If vglStackOffset-1 < 0 Then
  405.           vglQuit()
  406.           RunTimeError "ERROR: vglStackOffset-1 < 0"
  407.      Endif
  408.      AddMesh vglStack(vglStackOffset),vglStack(vglStackOffset-1)
  409.      FreeEntity vglStack(vglStackOffset)
  410.      For S.Surface = Each Surface
  411.           SSurface[vglStackOffset] = 0
  412.      Next
  413.      vglStackOffset = vglStackOffset - 1
  414. End Function


Comments :


Pepsi(Posted 1+ years ago)

 Clever & interesting! I had to move the globals above the main loop though because vglcurrenttexture is defined below that in which errored on me. No biggy though. Cool!


Pepsi(Posted 1+ years ago)

 lol... are you testing your code before you sumbit it? In your main loop you have some gl functions that need the character ' v ' in front of them so it wont error out. :)The quad and triangle do get textured for me when I use my own texture in the "PART_FIRE.Surface = vglLoadTexture("part_fire.tga",1+2)" line.


N(Posted 1+ years ago)

 Seems I had Insert on when I commented those function calls- uncommented them when posting the example- stupid mistake >_<Thanks for pointing that out :P


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal