CodeBase - Memblock Grid and Triangle functions
Return to the CodeBase listing
Category: Mesh
Version: 1.0
Information
Uploaded: 29th Jun 2007 03:46
Modified: 29th Jun 2007 03:46
Author: Visigoth
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,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
Full Description
REM Make_Memblock_Grid<br /> REM Created by Mike Shihrer aka Visigoth<br /> REM 06/28/2007<br /> REM Free to use and modify, have fun!<br /> REM Demonstrates creating memblock meshes<br /> <br /> <br /> <br /> set display mode 1024,768,32<br /> autocam off<br /> sync on<br /> sync rate 0<br /> <br /> Global mem_pos<br /> <br /> position camera 0,20,0<br /> <br /> rem return value is size of memblock in bytes, params are object ID,x width, z width, x units, z units<br /> mem_pos = make_memblock_grid(1,100,100,4,4)<br /> <br /> do<br /> display_memblock_data()<br /> move_camera1()<br /> sync<br /> <br /> loop<br /> <br /> <br /> function make_memblock_grid(id, x_width#, z_width#, x_segments, z_segments)<br /> <br /> unitwidth# = x_width# / x_segments<br /> unitheight# = z_width# / z_segments<br /> xpos# = 0<br /> ypos# = 0<br /> zpos# = 0<br /> J = 1<br /> d_color as dword<br /> d_color = RGB(255,255,255)<br /> <br /> rem calculate the size of the memblock we are going to need<br /> total_polys = (x_segments * z_segments) * 2<br /> total_verts = total_polys * 3<br /> memsize = (total_verts * 36) + 12<br /> <br /> rem make a memblock of a size = memsize<br /> make memblock 1,memsize<br /> rem create the memblock header, which defines the FVF being used, in this case 338(mesh data,normals data, diffuse color, uv coords)<br /> last_mem_pos = make_memblock_meshheader(1,total_verts)<br /> <br /> rem call the make_memblock_triangle function and place the triangles<br /> rem first, it creates one triangle with the verts drawn from lower left, upper left, and lower right<br /> rem second, it creates the next triangle with the verts from upper left, upper right, and lower right<br /> rem then it just loops through drawing triangles until it reachs the x unit count, resets the I incrementer to startover<br /> rem and increments the zpos to start a new row of triangles<br /> rem change the RGB values above for different colors<br /> rem I assumed creating a horizontal matrix, and set the normals as such<br /> <br /> for I = 1 to x_segments<br /> 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)<br /> 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)<br /> inc xpos#, unitwidth#<br /> If J < z_segments<br /> If I = x_segments<br /> Inc J,1<br /> inc zpos#,unitheight#<br /> xpos# = 0<br /> I = 0<br /> endif<br /> endif<br /> next I<br /> <br /> make mesh from memblock 1,1<br /> delete memblock 1<br /> make object id,1,0<br /> <br /> 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<br /> rem save mesh "grid_" + str$(id) + ".x",1<br /> delete mesh 1<br /> <br /> endfunction last_mem_pos<br /> <br /> 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)<br /> a = mem_start<br /> <br /> rem mesh data format, each block is one vertice, three blocks is a triangle<br /> rem we could probably get fancy here and do some loops, but I think this is more readable<br /> rem we are just writing the values needed for each vertice in a triangle, into the memblock<br /> rem location. Every memblock position for this format takes up 4 bytes, so after every write, we just<br /> rem increment the memblock position by 4<br /> <br /> rem vertice 1<br /> write memblock float id,a,xpos1# : inc a,4 rem x pos of triangle<br /> write memblock float id,a,ypos1# : inc a,4 rem y pos of triangle<br /> write memblock float id,a,zpos1# : inc a,4 rem z pos of triangle<br /> write memblock float id,a,0 : inc a,4 rem x normals<br /> write memblock float id,a,1 : inc a,4 rem y normals<br /> write memblock float id,a,0 : inc a,4 rem z normals<br /> write memblock dword id,a,dif_clr : inc a,4 rem diffuse color<br /> write memblock float id,a,u1 : inc a,4 rem u<br /> write memblock float id,a,v1 : inc a,4 rem v<br /> rem vertice 2<br /> write memblock float id,a,xpos2# : inc a,4 rem x pos of triangle<br /> write memblock float id,a,ypos2# : inc a,4 rem y pos of triangle<br /> write memblock float id,a,zpos2# : inc a,4 rem z pos of triangle<br /> write memblock float id,a,0 : inc a,4 rem x normals<br /> write memblock float id,a,1 : inc a,4 rem y normals<br /> write memblock float id,a,0 : inc a,4 rem z normals<br /> write memblock dword id,a,dif_clr : inc a,4 rem diffuse color<br /> write memblock float id,a,u2 : inc a,4 rem u<br /> write memblock float id,a,v2 : inc a,4 rem v<br /> rem vertice 3<br /> write memblock float id,a,xpos3# : inc a,4 rem x pos of triangle<br /> write memblock float id,a,ypos3# : inc a,4 rem y pos of triangle<br /> write memblock float id,a,zpos3# : inc a,4 rem z pos of triangle<br /> write memblock float id,a,0 : inc a,4 rem x normals<br /> write memblock float id,a,1 : inc a,4 rem y normals<br /> write memblock float id,a,0 : inc a,4 rem z normals<br /> write memblock dword id,a,dif_clr : inc a,4 rem diffuse color<br /> write memblock float id,a,u3 : inc a,4 rem u<br /> write memblock float id,a,v3 : inc a,4 rem v<br /> <br /> rem a just returns the last memblock position, which we can use to get the overall size in bytes of the mesh<br /> endfunction a<br /> <br /> function make_memblock_meshheader(id,numVerts)<br /> rem header for mesh format<br /> rem sets up header for standard FVF mesh 338<br /> rem we would change this to write a different FVF format if we wanted to<br /> <br /> write memblock dword id,0,338<br /> write memblock dword id,4,36<br /> write memblock dword id,8,numVerts<br /> <br /> endfunction 12<br /> <br /> function move_camera1()<br /> <br /> if upkey() then move camera 1<br /> if downkey()then move camera -1<br /> if leftkey() then turn camera left 1<br /> if rightkey() then turn camera right 1<br /> <br /> endfunction<br /> <br /> function display_memblock_data()<br /> text 0,0,"Use Arrow Keys to Move"<br /> text 0,10,"Total Polys on screen: " + str$(statistic(1))<br /> text 0,20,"Grid Size : " + str$(mem_pos) + " bytes"<br /> text 0,30,"FPS: " + str$(screen fps())<br /> text 0,40,"Cam X Pos: " + str$(camera position x())<br /> text 0,50,"Cam Y Pos: " + str$(camera position y())<br /> text 0,60,"Cam Z Pos: " + str$(camera position z())<br /> endfunction
Comments
No comments yet.