Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; /////////////////////////////////////////////////////////////////////////////
- ;
- ; ██████╗██████╗ ██╗ ██████╗ █████╗ ████████╗ ██████╗ ██████╗
- ; ██╔════╝██╔══██╗██║ ██╔══██╗██╔══██╗╚══██╔══╝██╔═══██╗██╔══██╗
- ; ██║ ██████╔╝██║ ██████╔╝███████║ ██║ ██║ ██║██████╔╝
- ; ██║ ██╔═══╝ ██║ ██╔══██╗██╔══██║ ██║ ██║ ██║██╔══██╗
- ; ╚██████╗██║ ███████╗██████╔╝██║ ██║ ██║ ╚██████╔╝██║ ██║
- ; ╚═════╝╚═╝ ╚══════╝╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝
- ;
- ;
- ; MIT License
- ;
- ; Copyright (c) 2023 , CplBator
- ;
- ; Permission is hereby granted, free of charge, To any person obtaining a copy
- ; of this software And associated documentation files (the "Software"), To deal
- ; in the Software without restriction, including without limitation the rights
- ; To use, copy, modify, merge, publish, distribute, sublicense, And/Or sell
- ; copies of the Software, And To permit persons To whom the Software is
- ; furnished To do so, subject To the following conditions:
- ;
- ; The above copyright notice And this permission notice shall be included in all
- ; copies Or substantial portions of the Software.
- ;
- ; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS Or
- ; IMPLIED, INCLUDING BUT Not LIMITED To THE WARRANTIES OF MERCHANTABILITY,
- ; FITNESS For A PARTICULAR PURPOSE And NONINFRINGEMENT. IN NO EVENT SHALL THE
- ; AUTHORS Or COPYRIGHT HOLDERS BE LIABLE For ANY CLAIM, DAMAGES Or OTHER
- ; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT Or OTHERWISE, ARISING FROM,
- ; OUT OF Or IN CONNECTION With THE SOFTWARE Or THE USE Or OTHER DEALINGS IN THE
- ; SOFTWARE.
- ;
- ; /////////////////////////////////////////////////////////////////////////////
- XIncludeFile "../Core/Maths/Math.pbi"
- XIncludeFile "../Core/Maths/Matrix4.pbi"
- XIncludeFile "../Core/Maths/Vector2.pbi"
- XIncludeFile "../Core/OpenGL/OpenGL.pbi"
- DeclareModule VertexBuffer
- Enumeration
- #UNDEFINED
- #POINTS
- #LINES
- #LINE_STRIP
- #LINE_LOOP
- #TRIANGLES
- #TRIANGLE_STRIP
- #TRIANGLE_FAN
- #QUADS
- #QUAD_STRIP
- #POLYGON
- EndEnumeration
- Structure sVertex
- x.Math::real
- y.Math::real
- u.Math::real
- v.Math::real
- r.Math::real
- g.Math::real
- b.Math::real
- a.Math::real
- EndStructure
- Interface iVertexBuffer
- Free()
- SetMode(mode.l)
- Append(*vertex)
- Clear()
- Draw()
- SetVertex(index.w, x.Math::real, y.Math::real, u.Math::real, v.Math::real , r.Math::real, g.Math::real, b.Math::real, a.Math::real)
- Count.w()
- GetVertex.i(index.w)
- Printer()
- EndInterface
- Declare.i New()
- EndDeclareModule
- Module VertexBuffer
- ;//////////////////////////////////////////////////////////////////////////////
- ;
- ;//////////////////////////////////////////////////////////////////////////////
- Structure sVertexBuffer
- *vtable
- vertices_count.w
- *buffer
- need_update.l
- vbo_id.i
- vao_id.i
- mode.l
- EndStructure
- ;//////////////////////////////////////////////////////////////////////////////
- ;
- ;//////////////////////////////////////////////////////////////////////////////
- Procedure.i New()
- *vb.sVertexBuffer = AllocateStructure(sVertexBuffer)
- If *vb <> #Null
- *vb\vtable = ?vtable
- *vb\buffer = #Null
- *vb\vertices_count = 0
- *vb\need_update = #True
- *vb\mode = #UNDEFINED
- ProcedureReturn *vb
- EndIf
- ProcedureReturn #Null
- EndProcedure
- ;//////////////////////////////////////////////////////////////////////////////
- ;
- ;//////////////////////////////////////////////////////////////////////////////
- Procedure Free(*vb.sVertexBuffer)
- If *vb\buffer <> #Null
- FreeMemory(*vb\buffer)
- EndIf
- EndProcedure
- ;//////////////////////////////////////////////////////////////////////////////
- ;
- ;//////////////////////////////////////////////////////////////////////////////
- Procedure SetMode(*vb.sVertexBuffer, mode.l)
- *vb\mode = mode
- EndProcedure
- ;//////////////////////////////////////////////////////////////////////////////
- ;
- ;//////////////////////////////////////////////////////////////////////////////
- Procedure Append(*vb.sVertexBuffer, *vertex.sVertex)
- If *vb\buffer <> #Null
- *vb\buffer = ReAllocateMemory(*vb\buffer, (*vb\vertices_count + 1) * SizeOf(sVertex))
- Else
- *vb\buffer = AllocateMemory(SizeOf(sVertex))
- EndIf
- CopyMemory(*vertex, *vb\buffer + (*vb\vertices_count * SizeOf(sVertex)), SizeOf(sVertex))
- *vb\vertices_count + 1
- *vb\need_update = #True
- EndProcedure
- ;//////////////////////////////////////////////////////////////////////////////
- ;
- ;//////////////////////////////////////////////////////////////////////////////
- Procedure Clear(*vb.sVertexBuffer)
- *vb\vertices_count = 0
- *vb\need_update = #True
- If *vb\buffer <> #Null
- FreeMemory(*vb\buffer)
- EndIf
- EndProcedure
- ;//////////////////////////////////////////////////////////////////////////////
- ;
- ;//////////////////////////////////////////////////////////////////////////////
- Procedure Update(*vb.sVertexBuffer)
- If *vb\need_update = #True
- If *vb\vertices_count > 0
- OpenGL::glGenVertexArrays(1,@*vb\vao_id)
- OpenGL::glBindVertexArray(*vb\vao_id)
- OpenGL::glEnableVertexAttribArray(0)
- If Opengl::CheckError()
- Debug "glGenVertexArrays : " + OpenGL::GetLastError()
- EndIf
- OpenGL::glGenBuffers(1,@*vb\vbo_id)
- OpenGL::glBindBuffer(OpenGL::#GL_ARRAY_BUFFER, *vb\vbo_id)
- OpenGL::glBufferData(OpenGL::#GL_ARRAY_BUFFER, SizeOf(sVertex) * *vb\vertices_count, *vb\buffer, OpenGL::#GL_STATIC_DRAW)
- If Opengl::CheckError()
- Debug "glGenBuffers : " + OpenGL::GetLastError()
- EndIf
- ; xy
- OpenGL::glVertexAttribPointer(0,2,OpenGL::#GL_FLOAT, OpenGL::#GL_FALSE, 8 * SizeOf(Float), 0)
- OpenGL::glEnableVertexAttribArray(0)
- If Opengl::CheckError()
- Debug "xy : " + OpenGL::GetLastError()
- EndIf
- ;uv
- OpenGL::glVertexAttribPointer(1,2,OpenGL::#GL_FLOAT, OpenGL::#GL_FALSE, 8 * SizeOf(Float), (2 * SizeOf(Float)))
- OpenGL::glEnableVertexAttribArray(1)
- If Opengl::CheckError()
- Debug "uv : " + OpenGL::GetLastError()
- EndIf
- ;rgba
- OpenGL::glVertexAttribPointer(2,4,OpenGL::#GL_FLOAT, OpenGL::#GL_FALSE, 8 * SizeOf(Float), (4 * SizeOf(Float)))
- OpenGL::glEnableVertexAttribArray(2)
- If Opengl::CheckError()
- Debug "rgba : " + OpenGL::GetLastError()
- EndIf
- OpenGL::glBindBuffer(OpenGL::#GL_ARRAY_BUFFER, 0)
- OpenGL::glBindVertexArray(0)
- If Opengl::CheckError()
- Debug "update : " + OpenGL::GetLastError()
- EndIf
- EndIf
- *vb\need_update = #False
- EndIf
- EndProcedure
- ;//////////////////////////////////////////////////////////////////////////////
- ;
- ;//////////////////////////////////////////////////////////////////////////////
- Procedure Draw(*vb.sVertexBuffer)
- Update(*vb)
- OpenGL::glBindVertexArray(*vb\vao_id)
- If Opengl::CheckError()
- Debug "bind : " + OpenGL::GetLastError()
- EndIf
- Select *vb\mode
- Case #POINTS
- OpenGL::glDrawArrays(OpenGL::#GL_POINTS,0,*vb\vertices_count)
- Case #LINES
- OpenGL::glDrawArrays(OpenGL::#GL_LINES,0,*vb\vertices_count)
- Case #LINE_STRIP
- OpenGL::glDrawArrays(OpenGL::#GL_LINE_STRIP,0,*vb\vertices_count)
- Case#LINE_LOOP
- OpenGL::glDrawArrays(OpenGL::#GL_LINE_LOOP,0,*vb\vertices_count)
- Case #TRIANGLES
- OpenGL::glDrawArrays(OpenGL::#GL_TRIANGLES,0,*vb\vertices_count)
- Case#TRIANGLE_STRIP
- OpenGL::glDrawArrays(OpenGL::#GL_TRIANGLE_STRIP,0,*vb\vertices_count)
- Case#TRIANGLE_FAN
- OpenGL::glDrawArrays(OpenGL::#GL_TRIANGLE_FAN,0,*vb\vertices_count)
- Case #QUADS
- OpenGL::glDrawArrays(OpenGL::#GL_QUADS,0,*vb\vertices_count)
- Case #QUAD_STRIP
- OpenGL::glDrawArrays(OpenGL::#GL_QUAD_STRIP,0,*vb\vertices_count)
- Case#POLYGON
- OpenGL::glDrawArrays(OpenGL::#GL_LINE_LOOP,0,*vb\vertices_count)
- EndSelect
- If Opengl::CheckError()
- Debug "draw : " + OpenGL::GetLastError()
- EndIf
- EndProcedure
- ;//////////////////////////////////////////////////////////////////////////////
- ;
- ;//////////////////////////////////////////////////////////////////////////////
- Procedure SetVertex(*vb.sVertexBuffer, index.w, x.Math::real, y.Math::real, u.Math::real, v.Math::real , r.Math::real, g.Math::real, b.Math::real, a.Math::real)
- *ptr.sVertex = *vb\buffer + (SizeOf(sVertex) * index)
- *ptr\x = x
- *ptr\y = y
- *ptr\u = u
- *ptr\v = v
- *ptr\r = r
- *ptr\g = g
- *ptr\b = b
- *ptr\a = a
- EndProcedure
- ;//////////////////////////////////////////////////////////////////////////////
- ;
- ;//////////////////////////////////////////////////////////////////////////////
- Procedure.w Count(*self.sVertexBuffer)
- ProcedureReturn *self\vertices_count
- EndProcedure
- ;//////////////////////////////////////////////////////////////////////////////
- ;
- ;//////////////////////////////////////////////////////////////////////////////
- Procedure.i GetVertex(*self.sVertexBuffer, index.w)
- If index >= 0 And index < *self\vertices_count
- *self\need_update = #True
- ProcedureReturn *self\buffer + (SizeOf(VertexBuffer::sVertex) * index)
- EndIf
- ProcedureReturn #Null
- EndProcedure
- ;//////////////////////////////////////////////////////////////////////////////
- ;
- ;//////////////////////////////////////////////////////////////////////////////
- Procedure Printer(*vb.sVertexBuffer)
- *ptr = *vb\buffer
- For i = 0 To *vb\vertices_count-1
- *v.sVertex = *ptr
- Debug StrF(*v\x) + " : " + StrF(*v\y)
- *ptr + SizeOf(sVertex)
- Next
- EndProcedure
- DataSection
- vtable:
- Data.i @Free()
- Data.i @SetMode()
- Data.i @Append()
- Data.i @Clear()
- Data.i @Draw()
- Data.i @SetVertex()
- Data.i @Count()
- Data.i @GetVertex()
- Data.i @Printer()
- EndDataSection
- EndModule
Advertisement
Add Comment
Please, Sign In to add comment