TGC Codebase Backup



Custom Mesh & Vertex Colours by Shadow Robert

28th Sep 2003 19:33
Summary

Creates a Quad & Assigns it Vertex Colours



Description

This snippet shows howto create a custom mesh by using the memory & pointer system along with a function allowing simple and easy access to such low-level tasks.
The snippet only has a simple Quad (Plain) shown here along with vertex colours, which can be used as coloured normals for the object.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `// Example of Vertex Colours
`// Raven | 28-09-2003

#constant TRUE    1
#constant FALSE   0
#constant DEFAULT 1

FVF_XYZ = 0x002 : FVF_DIFFUSE = 0x010
FVFS_XYZ = 12 : FVFS_DIFFUSE = 4

#constant FVFCODE (FVF_XYZ||FVF_DIFFUSE)
#constant FVFSIZE (FVFS_XYZ+FVFS_DIFFUSE)

global g_pTime as float
g_pTime = timer()
m_dwNumVertices as dword   = 4
m_Object as word           = 1
pVertexBuffer as dword
pVertexBuffer = make memory(m_dwNumVertices*FVFSIZE)
pAddress = pVertexBuffer
   pAddress = AddVertex( pAddress, -1.0,  1.0, 0.0, rgb(255,  0,  0) )
   pAddress = AddVertex( pAddress,  1.0,  1.0, 0.0, rgb(  0,255,  0) )
   pAddress = AddVertex( pAddress,  1.0, -1.0, 0.0, rgb(  0,  0,255) )
   pAddress = AddVertex( pAddress, -1.0, -1.0, 0.0, rgb(255,255,255) )
   m_Mesh = CreateMeshFromVertexBuffer( pVertexBuffer, FVFCODE, FVFSIZE, m_dwNumVertices )
   make object m_Object, m_Mesh, NULL
   set object ambient m_Object, FALSE
   set object light m_Object, FALSE
   set object cull m_Object, FALSE

do
   if escapekey() then goto quit
   y#=y#+(4.0*ElapseTime())
   yrotate object m_Object,wrapvalue(y#)
loop

function AddVertex( ptr as dword, _
                    x as float, y as float, z as float, _
                    diffuse as dword )
   *ptr = x : ptr=ptr+4
   *ptr = y : ptr=ptr+4
   *ptr = z : ptr=ptr+4
   *ptr = diffuse
endfunction (ptr+4)

function CreateMeshFromVertexBuffer( pVB as dword, fvfcode as dword, fvfsize as dword, numverts as dword )
   local l_pMemblock as dword = DEFAULT
   local m_Mesh as dword      = DEFAULT
   local pMemblock as dword   = NULL
   local bSwitch as boolean   = FALSE
   while bSwitch = FASLE
      if memblock exist(l_pMemblock) = TRUE
         l_pMemblock=l_pMemblock+1
      else
         if mesh exist(m_Mesh) = TRUE
            m_Mesh=m_Mesh+1
         else
            bSwitch = TRUE
         endif
      endif
   endwhile
   make memblock l_pMemblock,(numverts*fvfsize)+12
      write memblock dword l_pMemblock, 0, fvfcode
      write memblock dword l_pMemblock, 4, fvfsize
      write memblock dword l_pMemblock, 8, numverts
      copy memory (get memblock ptr(l_pMemblock)), pVB, (numverts*fvfsize)
      make mesh from memblock m_Mesh, l_pMemblock
   delete memblock l_pMemblock
endfunction m_Mesh

function ElapseTime()
   local l_old as float = g_pTime
   g_pTime = timer()
endfunction ((g_pTime-l_old)/1000.0)

quit:
   delete memory pVertexBuffer
   delete mesh m_Mesh
   delete object m_Object
   flush video memory
   end