TGC Codebase Backup



Memblock Grid and Triangle functions by Visigoth

29th Jun 2007 3:46
Summary

REM Make_Memblock_Grid REM Created by Mike Shihrer aka Visigoth REM 06/28/2007 REM Free to use and modify, have fun! REM Demonstrates creating memblock meshes set display mode 1024



Description

REM Make_Memblock_Grid
REM Created by Mike Shihrer aka Visigoth
REM 06/28/2007
REM Free to use and modify, have fun!
REM Demonstrates creating memblock meshes



set display mode 1024,768,32
autocam off
sync on
sync rate 0

Global mem_pos

position camera 0,20,0

rem return value is size of memblock in bytes, params are object ID,x width, z width, x units, z units
mem_pos = make_memblock_grid(1,100,100,4,4)

do
display_memblock_data()
move_camera1()
sync

loop


function make_memblock_grid(id, x_width#, z_width#, x_segments, z_segments)

unitwidth# = x_width# / x_segments
unitheight# = z_width# / z_segments
xpos# = 0
ypos# = 0
zpos# = 0
J = 1
d_color as dword
d_color = RGB(255,255,255)

rem calculate the size of the memblock we are going to need
total_polys = (x_segments * z_segments) * 2
total_verts = total_polys * 3
memsize = (total_verts * 36) + 12

rem make a memblock of a size = memsize
make memblock 1,memsize
rem create the memblock header, which defines the FVF being used, in this case 338(mesh data,normals data, diffuse color, uv coords)
last_mem_pos = make_memblock_meshheader(1,total_verts)

rem call the make_memblock_triangle function and place the triangles
rem first, it creates one triangle with the verts drawn from lower left, upper left, and lower right
rem second, it creates the next triangle with the verts from upper left, upper right, and lower right
rem then it just loops through drawing triangles until it reachs the x unit count, resets the I incrementer to startover
rem and increments the zpos to start a new row of triangles
rem change the RGB values above for different colors
rem I assumed creating a horizontal matrix, and set the normals as such

for I = 1 to x_segments
last_mem_pos = make_memblock_triangle(1,last_mem_pos,xpos#,ypos#,zpos#,xpos#,ypos#,zpos# + unitheight#,xpos# + unitwidth#,ypos#,zpos#,0,1,0,0,1,1,d_color)
last_mem_pos = make_memblock_triangle(1,last_mem_pos,xpos#,ypos#,zpos# + unitheight#,xpos# + unitwidth#,0,zpos# + unitheight#,xpos# + unitwidth#,ypos#,zpos#,0,0,1,0,1,1,d_color)
inc xpos#, unitwidth#
If J < z_segments
If I = x_segments
Inc J,1
inc zpos#,unitheight#
xpos# = 0
I = 0
endif
endif
next I

make mesh from memblock 1,1
delete memblock 1
make object id,1,0

rem uncomment this is you want to save the mesh, but beware, there seems to be a limit to how big a mesh you can save. Keep it under 80k polys
rem save mesh "grid_" + str$(id) + ".x",1
delete mesh 1

endfunction last_mem_pos

function make_memblock_triangle(id,mem_start,xpos1#,ypos1#,zpos1#,xpos2#,ypos2#,zpos2#,xpos3#,ypos3#,zpos3#,u1,v1,u2,v2,u3,v3,dif_clr as dword)
a = mem_start

rem mesh data format, each block is one vertice, three blocks is a triangle
rem we could probably get fancy here and do some loops, but I think this is more readable
rem we are just writing the values needed for each vertice in a triangle, into the memblock
rem location. Every memblock position for this format takes up 4 bytes, so after every write, we just
rem increment the memblock position by 4

rem vertice 1
write memblock float id,a,xpos1# : inc a,4 rem x pos of triangle
write memblock float id,a,ypos1# : inc a,4 rem y pos of triangle
write memblock float id,a,zpos1# : inc a,4 rem z pos of triangle
write memblock float id,a,0 : inc a,4 rem x normals
write memblock float id,a,1 : inc a,4 rem y normals
write memblock float id,a,0 : inc a,4 rem z normals
write memblock dword id,a,dif_clr : inc a,4 rem diffuse color
write memblock float id,a,u1 : inc a,4 rem u
write memblock float id,a,v1 : inc a,4 rem v
rem vertice 2
write memblock float id,a,xpos2# : inc a,4 rem x pos of triangle
write memblock float id,a,ypos2# : inc a,4 rem y pos of triangle
write memblock float id,a,zpos2# : inc a,4 rem z pos of triangle
write memblock float id,a,0 : inc a,4 rem x normals
write memblock float id,a,1 : inc a,4 rem y normals
write memblock float id,a,0 : inc a,4 rem z normals
write memblock dword id,a,dif_clr : inc a,4 rem diffuse color
write memblock float id,a,u2 : inc a,4 rem u
write memblock float id,a,v2 : inc a,4 rem v
rem vertice 3
write memblock float id,a,xpos3# : inc a,4 rem x pos of triangle
write memblock float id,a,ypos3# : inc a,4 rem y pos of triangle
write memblock float id,a,zpos3# : inc a,4 rem z pos of triangle
write memblock float id,a,0 : inc a,4 rem x normals
write memblock float id,a,1 : inc a,4 rem y normals
write memblock float id,a,0 : inc a,4 rem z normals
write memblock dword id,a,dif_clr : inc a,4 rem diffuse color
write memblock float id,a,u3 : inc a,4 rem u
write memblock float id,a,v3 : inc a,4 rem v

rem a just returns the last memblock position, which we can use to get the overall size in bytes of the mesh
endfunction a

function make_memblock_meshheader(id,numVerts)
rem header for mesh format
rem sets up header for standard FVF mesh 338
rem we would change this to write a different FVF format if we wanted to

write memblock dword id,0,338
write memblock dword id,4,36
write memblock dword id,8,numVerts

endfunction 12

function move_camera1()

if upkey() then move camera 1
if downkey()then move camera -1
if leftkey() then turn camera left 1
if rightkey() then turn camera right 1

endfunction

function display_memblock_data()
text 0,0,"Use Arrow Keys to Move"
text 0,10,"Total Polys on screen: " + str$(statistic(1))
text 0,20,"Grid Size : " + str$(mem_pos) + " bytes"
text 0,30,"FPS: " + str$(screen fps())
text 0,40,"Cam X Pos: " + str$(camera position x())
text 0,50,"Cam Y Pos: " + str$(camera position y())
text 0,60,"Cam Z Pos: " + str$(camera position z())
endfunction



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    REM Make_Memblock_Grid
REM Created by Mike Shihrer aka Visigoth
REM 06/28/2007
REM Free to use and modify, have fun!
REM Demonstrates creating memblock meshes



set display mode 1024,768,32
autocam off
sync on
sync rate 0

Global mem_pos

position camera 0,20,0

rem return value is size of memblock in bytes, params are object ID,x width, z width, x units, z units
mem_pos = make_memblock_grid(1,100,100,4,4)

do
display_memblock_data()
move_camera1()
sync

loop


function make_memblock_grid(id, x_width#, z_width#, x_segments, z_segments)

unitwidth# = x_width# / x_segments
unitheight# = z_width# / z_segments
xpos# = 0
ypos# = 0
zpos# = 0
J = 1
d_color as dword
d_color = RGB(255,255,255)

rem calculate the size of the memblock we are going to need
total_polys = (x_segments * z_segments) * 2
total_verts = total_polys * 3
memsize = (total_verts * 36) + 12

rem make a memblock of a size = memsize
make memblock 1,memsize
rem create the memblock header, which defines the FVF being used, in this case 338(mesh data,normals data, diffuse color, uv coords)
last_mem_pos = make_memblock_meshheader(1,total_verts)

rem call the make_memblock_triangle function and place the triangles
rem first, it creates one triangle with the verts drawn from lower left, upper left, and lower right
rem second, it creates the next triangle with the verts from upper left, upper right, and lower right
rem then it just loops through drawing triangles until it reachs the x unit count, resets the I incrementer to startover
rem and increments the zpos to start a new row of triangles
rem change the RGB values above for different colors
rem I assumed creating a horizontal matrix, and set the normals as such

for I = 1 to x_segments
   last_mem_pos = make_memblock_triangle(1,last_mem_pos,xpos#,ypos#,zpos#,xpos#,ypos#,zpos# + unitheight#,xpos# + unitwidth#,ypos#,zpos#,0,1,0,0,1,1,d_color)
   last_mem_pos = make_memblock_triangle(1,last_mem_pos,xpos#,ypos#,zpos# + unitheight#,xpos# + unitwidth#,0,zpos# + unitheight#,xpos# + unitwidth#,ypos#,zpos#,0,0,1,0,1,1,d_color)
   inc xpos#, unitwidth#
If J < z_segments
   If I = x_segments
      Inc J,1
      inc zpos#,unitheight#
      xpos# = 0
      I = 0
   endif
endif
next I

make mesh from memblock 1,1
delete memblock 1
make object id,1,0

rem uncomment this is you want to save the mesh, but beware, there seems to be a limit to how big a mesh you can save. Keep it under 80k polys
rem save mesh "grid_" + str$(id) + ".x",1
delete mesh 1

endfunction last_mem_pos

function make_memblock_triangle(id,mem_start,xpos1#,ypos1#,zpos1#,xpos2#,ypos2#,zpos2#,xpos3#,ypos3#,zpos3#,u1,v1,u2,v2,u3,v3,dif_clr as dword)
a = mem_start

rem mesh data format, each block is one vertice, three blocks is a triangle
rem we could probably get fancy here and do some loops, but I think this is more readable
rem we are just writing the values needed for each vertice in a triangle, into the memblock
rem location. Every memblock position for this format takes up 4 bytes, so after every write, we just
rem increment the memblock position by 4

rem vertice 1
write memblock float id,a,xpos1# : inc a,4  rem x pos of triangle
write memblock float id,a,ypos1# : inc a,4  rem y pos of triangle
write memblock float id,a,zpos1# : inc a,4  rem z pos of triangle
write memblock float id,a,0 : inc a,4 rem x normals
write memblock float id,a,1 : inc a,4 rem y normals
write memblock float id,a,0 : inc a,4 rem z normals
write memblock dword id,a,dif_clr : inc a,4 rem diffuse color
write memblock float id,a,u1 : inc a,4 rem u
write memblock float id,a,v1 : inc a,4 rem v
rem vertice 2
write memblock float id,a,xpos2# : inc a,4 rem x pos of triangle
write memblock float id,a,ypos2# : inc a,4 rem y pos of triangle
write memblock float id,a,zpos2# : inc a,4 rem z pos of triangle
write memblock float id,a,0 : inc a,4 rem x normals
write memblock float id,a,1 : inc a,4 rem y normals
write memblock float id,a,0 : inc a,4 rem z normals
write memblock dword id,a,dif_clr : inc a,4 rem diffuse color
write memblock float id,a,u2 : inc a,4 rem u
write memblock float id,a,v2 : inc a,4 rem v
rem vertice 3
write memblock float id,a,xpos3# : inc a,4 rem x pos of triangle
write memblock float id,a,ypos3# : inc a,4 rem y pos of triangle
write memblock float id,a,zpos3# : inc a,4 rem z pos of triangle
write memblock float id,a,0 : inc a,4 rem x normals
write memblock float id,a,1 : inc a,4 rem y normals
write memblock float id,a,0 : inc a,4 rem z normals
write memblock dword id,a,dif_clr : inc a,4 rem diffuse color
write memblock float id,a,u3 : inc a,4 rem u
write memblock float id,a,v3 : inc a,4 rem v

rem a just returns the last memblock position, which we can use to get the overall size in bytes of the mesh
endfunction a

function make_memblock_meshheader(id,numVerts)
rem header for mesh format
rem sets up header for standard FVF mesh 338
rem we would change this to write a different FVF format if we wanted to

write memblock dword id,0,338
write memblock dword id,4,36
write memblock dword id,8,numVerts

endfunction 12

function move_camera1()

if upkey() then move camera 1
if downkey()then move camera -1
if leftkey() then turn camera left 1
if rightkey() then turn camera right 1

endfunction

function display_memblock_data()
text 0,0,"Use Arrow Keys to Move"
text 0,10,"Total Polys on screen: " + str$(statistic(1))
text 0,20,"Grid Size : " + str$(mem_pos) + " bytes"
text 0,30,"FPS: " + str$(screen fps())
text 0,40,"Cam X Pos: " + str$(camera position x())
text 0,50,"Cam Y Pos: " + str$(camera position y())
text 0,60,"Cam Z Pos: " + str$(camera position z())
endfunction