Ok, back to business. Sorry for the short absence. I've been a bit busy the past couple days.
So I've been working on the welding and after following your instructions jason I'm almost there! But I've having a few issues still.
Quote: "think I made an Object, weld it, then converted that to a mesh, deleted the object - then made a new mesh the way I described above with the welded mesh."
Did all that, and now the UV's are messed up big time. Here's a small screenie:
I've been tinkering around with differ commands and stuff but haven't really had a break through yet... Here's my latest source:
sync on
sync rate 0
wid#=640.0
hei#=480.0
set window on
set window title "Level Editor"
set display mode wid#,hei#,32
color backdrop rgb(80,80,250)
autocam on
position camera 0,0,0
set text font "arial"
set text size 20
ink rgb(255,255,255),0
`Variable Setup
global MapSizeX# as Integer
global MapSizeZ# as Integer
global MapSegmentsX# as Integer
global MapSegmentsZ# as Integer
global CursorSizeX# as Integer
global CursorSizeZ# as Integer
global CursorSegmentsX# as Integer
global CursorSegmentsZ# as Integer
global CamSpeed# as Integer
MapSizeX# = 32
MapSizeZ# = 32
MapSegmentsX# = MapSizeX#/1
MapSegmentsZ# = MapSizeZ#/1
CursorSizeX# = 64
CursorSizeZ# = 64
CursorSegmentsX# = 8
CursorSegmentsZ# = 8
CamSpeed# = 10
load image "grass.png", 1
`Setup World
create_matrix(1,MapSizeX#,MapSizeZ#,MapSegmentsX#,MapSegmentsZ#)
create_limbs(1)
`Texture()
`************MAIN PROGRAM LOOP START***************
do
Camera(CamSpeed#)
`threeDcursor()
Debug()
set cursor 0, 0
lock vertexdata for mesh 1
print "get vertexdata vertex count() = "; get vertexdata vertex count()
print "get vertexdata index count() = "; get vertexdata index count()
unlock vertexdata
` if mouseclick()=1 then WeldLimbVertices(1, 0)
sync
loop
`************MAIN PROGRAM LOOP END*****************
remstart
Function threedcursor()
camx#=camera position x()
camy#=camera position y()
camz#=camera position z()
pick screen mousex(), mousey(), 10
xv#=get pick vector x()
yv#=get pick vector y()
zv#=get pick vector z()
`this vector is a certain percentage of the camera height
`so if we take the inverse of that ratio and multiply the
`original vecotr coordinates by that answer,
`the vector will reach all the way to y=0
mult#=abs(camy#/yv#)
xv#=xv#*mult#
yv#=yv#*mult#
zv#=zv#*mult#
`Make array of terrain heightmap
For x=0 to MapSegmentsX#
For y=0 to MapSegmentsZ#
ter(x,y) = Get Matrix Height(2,x,y)
Next y
next x
`Apply part of array to heightmap
newxv=xv#
newyv=yv#
newyz=zv#
text 100,60, "x:"+str$(newxv)
text 100,70, "y:"+str$(yv#)
text 100,80, "y:"+str$(zv#)
`position matrix
` position matrix 1,camx#+xv#-(CursorSizeX#/2),camy#+yv#+get ground height(2,matrix position x(1), matrix position z(1)),camz#+zv#-(CursorSizeZ#/2)
EndFunction
remend
FUNCTION Camera(CamSpeed#)
cam_x# = CAMERA ANGLE X() + MOUSEMOVEY() * .2
cam_y# = CAMERA ANGLE Y() + MOUSEMOVEX() * .2
cam_z# = CAMERA ANGLE Z() + MOUSEMOVEZ() * .2
if mouseclick()=2
IF CamSpeed# = 0 THEN EXITFUNCTION
IF upkey() = 1
polarity=1
MOVE CAMERA CamSpeed#
XROTATE CAMERA cam_x#
ENDIF
IF downkey() = 1
polarity=-1
MOVE CAMERA -CamSpeed#
XROTATE CAMERA cam_x#
ENDIF
IF leftkey() = 1
ROTATE CAMERA 0, cam_y#-90, 0
MOVE CAMERA CamSpeed#
ROTATE CAMERA cam_x#, cam_y#, 0
ENDIF
IF rightkey() = 1
ROTATE CAMERA 0, cam_y#+90, 0
MOVE CAMERA CamSpeed#
ROTATE CAMERA cam_x#, cam_y#, 0
ENDIF
ROTATE CAMERA cam_x#, cam_y#, cam_z#
endif
ENDFUNCTION
Function Debug()
text 0,30,"Cam Pos X:"+str$(camera position x())
text 0,40,"Cam Pos Y:"+str$(camera position z())
text 0,50, "Cam Pos Z:"+str$(camera position y())
if object exist(1)
text 0,60,"Obj Pos X:"+str$(object position x(1))
text 0,70,"Obj Pos Y:"+str$(object position y(1))
text 0,80,"Obj Pos Z:"+str$(object position z(1))
endif
text 0,90,"FPS: "+str$(screen fps())
text 0,100, "Polygons: "+str$(statistic(1))
text 0,110, "Mem: "+str$(mem)
EndFunction
function create_matrix(mem_numb, width, depth, segx, segz)
fvf = 338
size = 36
tilesizex# = width/segx
tilesizez# = depth/segz
poly = width * depth * 2
totalsize = (poly * size *3) + 12
make memblock mem_numb, totalsize
write memblock dword mem_numb, 0, fvf
write memblock dword mem_numb, 4, size
write memblock dword mem_numb, 8, poly * 3
pos=12
for x=1 to segx
for z=1 to segz
writevectordata(mem_numb,pos,(x-1.0)*tilesizex#,0.000,(z-1.0)*tilesizez#,0.000,1.000,0.000,rgb(0,255,255),0.000,1.000)
writevectordata(mem_numb,pos+36,x*tilesizex#,0.000,z*tilesizez#,0.000,1.000,0.000,rgb(255,0,255),1.000,0.000)
writevectordata(mem_numb,pos+72,x*tilesizex#,0.000,(z-1.0)*tilesizez#,0.000,1.000,0.000,rgb(255,255,0),1.000,1.000)
writevectordata(mem_numb,pos+108,(x-1.0)*tilesizex#,0.000,(z-1.0)*tilesizez#,0.000,1.000,0.000,rgb(0,255,255),0.000,1.000)
writevectordata(mem_numb,pos+144,(x-1.0)*tilesizex#,0.000,z*tilesizez#,0.000,1.000,0.000,rgb(255,0,255),0.000,0.000)
writevectordata(mem_numb,pos+180,x*tilesizex#,0.000,z*tilesizez#,0.000,1.000,0.000,rgb(255,255,0),1.000,0.000)
pos=pos+216
next segz
next segx
make mesh from memblock 1, mem_numb
make object 1,1,0
WeldLimbVertices(1, 0)
delete mesh 1
make mesh from object 1,1
delete object 1
Make object triangle 1,1,1,1,1,1,1,1,1,1
`position object 1, camera position x()-(width/2), camera position y()-25, camera position z()-(depth/2)
`set object wireframe 1,1
global mem
mem = GET MEMBLOCK SIZE(mem_numb)
endfunction
function writevectordata(mem_numb,pos,X# as float,Y# as float,Z# as float,NX# as float,NY# as float,NZ# as float,color as dword,TU# as float,TV# as float)
write memblock float mem_numb,pos,X#
write memblock float mem_numb,pos+4,Y#
write memblock float mem_numb,pos+8,Z#
write memblock float mem_numb,pos+12,NX#
write memblock float mem_numb,pos+16,NY#
write memblock float mem_numb,pos+20,NZ#
write memblock dword mem_numb,pos+24,color
write memblock float mem_numb,pos+28,TU#
write memblock float mem_numb,pos+32,TV#
endfunction
Function create_limbs(mesh_numb)
limbnum=0
for x = 1 to 10
for z = 1 to 10
limbnum=limbnum+1
add limb 1,limbnum,mesh_numb
offset limb 1,limbnum,(x-1)*32,0,(z-1)*32
`Set Limb Normals 1,limbnum
next z
next x
` remove limb 1,1 `//Make room for object
Endfunction
Function Texture()
texture object 1,1
EndFunction
type Vertex_t
x as float
y as float
z as float
u as float
v as float
Diffuse as dword
OriginalPosition as integer
UsageCount as integer
endtype
type VertexInfo_t
Vertex as Vertex_t
OriginalPosition as integer
Matches as integer
UsageCount as integer
endtype
global dim Vertex() as VertexInfo_t
global dim Index() as integer
function WeldLimbVertices(Object as integer, Limb as integer)
local V as Vertex_t
local VI as VertexInfo_t
ConvertLimbToTriList(Object, Limb)
lock vertexdata for limb Object, Limb
` Create/recreate arrays needed for manipulation
undim Vertex()
global dim Vertex( get vertexdata vertex count() - 1 ) as VertexInfo_t
undim Index()
global dim Index( get vertexdata index count() - 1 ) as integer
` Copy all vertex data into an array
for i = 0 to array count( Vertex() )
V.x = get vertexdata position x(i)
V.y = get vertexdata position y(i)
V.z = get vertexdata position z(i)
V.u = get vertexdata u(i)
V.v = get vertexdata v(i)
V.Diffuse = get vertexdata diffuse(i)
Vertex(i).Vertex = V
Vertex(i).OriginalPosition = i
Vertex(i).Matches = i
Vertex(i).UsageCount = 0
next
` Copy all index data into an array
for i = 0 to array count( Index() )
Index(i) = get indexdata(i)
inc Vertex( Index(i) ).UsageCount
next
` Look for matching vertices
for i = 1 to array count( Vertex() )
if Vertex(i).UsageCount > 0
for j = 0 to i - 1
if CheckMatchingVertex(i, j) = 1
` Update the matching vertex
Vertex(j).UsageCount = Vertex(j).UsageCount + Vertex(i).UsageCount
` Update this duplicate vertex
Vertex(i).UsageCount = 0
Vertex(i).Matches = j
` Skip on to the next vertex
exit
endif
next
endif
next
` Sort the vertices so unused ones are at the end
repeat
Swap = 0
for i = 1 to array count( Vertex() )
if Vertex(i-1).UsageCount < Vertex(i).UsageCount
Swap = 1
VI = Vertex(i-1)
Vertex(i-1) = Vertex(i)
Vertex(i) = VI
endif
next
until Swap = 0
` Update the indices so that duplicates point at the original
for i = 0 to array count( Index() )
Index(i) = FindVertexFromIndex( Index(i) )
next
` Update the object index data
for i = 0 to array count( Index() )
set indexdata i, Index(i)
next
` Update the object vertex data
i = 0
while i <= array count( Vertex() )
if Vertex(i).UsageCount <= 0 then exit
set vertexdata position i, Vertex(i).Vertex.x, Vertex(i).Vertex.y, Vertex(i).Vertex.z
set vertexdata uv i, Vertex(i).Vertex.u, Vertex(i).Vertex.v
set vertexdata diffuse i, Vertex(i).Vertex.Diffuse
inc i
endwhile
` Remove unused vertices from the vertex data
delete mesh from vertexdata i, get vertexdata vertex count(), get vertexdata index count(), get vertexdata index count()
unlock vertexdata
GenerateNormalsForLimb(Object, Limb)
endfunction
function CheckMatchingVertex(i as integer, j as integer)
if Vertex(i).Vertex.x <> Vertex(j).Vertex.x then exitfunction 0
if Vertex(i).Vertex.y <> Vertex(j).Vertex.y then exitfunction 0
if Vertex(i).Vertex.z <> Vertex(j).Vertex.z then exitfunction 0
endfunction 1
function FindVertexFromIndex(Ind as integer)
for i = 0 to array count( Vertex() )
if Vertex(i).OriginalPosition = Ind
if Vertex(i).OriginalPosition = Vertex(i).Matches then exitfunction i
exitfunction FindVertexFromIndex( Vertex(i).Matches )
endif
next
endfunction -1
global DBProBasic3DDebug as integer
function LoadDLL_Basic3D()
for i = 1 to 256
if dll exist(i) = 0
DBProBasic3DDebug = i
load dll "DBProBasic3DDebug.dll", DBProBasic3DDebug
exitfunction
endif
next
DBProBasic3DDebug_DLL = -1
endfunction
function ConvertLimbToTriList(Object as integer, Limb as integer)
local Result as integer
local MeshAddr as dword
MeshAddr = GetMeshAddress(Object, Limb)
if MeshAddr
` If not a tri list, convert it
if PeekDword( MeshAddr + 36 ) <> 4
Result = call dll( DBProBasic3DDebug, "?ConvertLocalMeshToTriList@@YA_NPAUsMesh@@@Z", MeshAddr )
endif
` Really nasty ... if there is no index data, make some up!
if PeekDword( MeshAddr + 32 ) = 0
` Must lock the mesh before manipulating it, else changes won't happen
lock vertexdata for limb Object, Limb
` Get some memory for the index list
IndexData = make memory( get vertexdata vertex count() * 2 )
` Set the index count, and set it to point at the allocated memory
PokeDword( MeshAddr + 32, get vertexdata vertex count() )
PokeDword( MeshAddr + 24, IndexData )
` Put numbers into the index list
for i = 0 to get vertexdata vertex count()-1
set indexdata i, i
next
` Unlock to commit the changes
unlock vertexdata
endif
endif
endfunction Result
function GenerateNormalsForLimb(Object as integer, Limb as integer)
local Result as integer
local MeshAddr as dword
MeshAddr = GetMeshAddress(Object, Limb)
if MeshAddr
Result = call dll( DBProBasic3DDebug, "?GenerateNewNormalsForMesh@@YAXPAUsMesh@@@Z", MeshAddr )
endif
endfunction Result
function GetMeshAddress(Object as integer, Limb as integer)
local ObjectAddr as dword
local FrameListAddr as dword
local FrameAddr as dword
local MeshAddr as dword
if DBProBasic3DDebug = 0 then LoadDLL_Basic3D()
ObjectAddr = call dll( DBProBasic3DDebug, "?GetObjectA@@YAPAUsObject@@H@Z", Object )
if ObjectAddr
if PeekDword( ObjectAddr + 4 ) > Limb
FrameListAddr = PeekDword( ObjectAddr + 12 )
FrameAddr = PeekDword( FrameListAddr + (Limb * 4) )
MeshAddr = PeekDword( FrameAddr + 760 )
endif
endif
endfunction MeshAddr
function PeekDword(Addr as dword)
local Value as dword
Value = *Addr
endfunction Value
function PokeDword(Addr as dword, Value as dword)
*Addr = Value
endfunction
Advice?
"If I have seen a little further it is by standing on the shoulders of Giants" -Isaac Newton