Hi
Thought to individualise my projects to seperate threads as suggested before now, which makes total sense
So uploading the 3D modeller, which was insprired with Autodesk Inventor (of which i work for now) - just give you all some inspirations things are put together via algorythms
// Project: AGK 3D object creation system
// Created: 2020-01-11
// show all errors
SetErrorMode(2)
#constant screenwidth=1024
#constant screenheight=768
// set window properties
SetWindowTitle( "AGK 3D object creation system - Welcome" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
global modelpoints as integer[]
Type _Objects
id
r,g,b
endtype
Type Triangle
v1 as integer
v2 as integer
v3 as integer
endtype
Type Vertex
x as float
y as float
z as float
oldx as float
oldy as float
oldz as float
nx as float
ny as float
nz as float
u as float
v as float
endtype
Type Mesh
VertexList as Vertex[]
TriangleList as Triangle[]
endtype
global lander as Mesh
global angx#, angy#, startx#, starty#,camx#,camy#,camz#
Txt = CreateText("First create or Load an Object")
SetTextSize(txt,16)
Txt2 = CreateText("")
SetTextSize(txt2,16)
fps = CreateText("FPS")
SetTextSize(fps,16)
SetTextColor(fps,0,0,0,255)
type _drawpoints
x#
y#
vx#
vy#
vz#
endtype
type _SketchPoint
x#
y#
z#
nx#
ny#
nz#
endtype
global sketch3dpoints as _SketchPoint[]
global draw2dpoints as _drawpoints[]
draw2dpoint as _drawpoints
global torus
startx#=screenwidth/2
starty#=screenheight/2
SetCameraRange(1,0.1,1000)
objects as integer[]
global rendermode, objred,objgreen,objblue
objred=255 : objgreen = 255 : objblue=255
rendermode=-1
lines=-1
freehand=-1
circle=-1 : circleedges=12 : innerdistance#=1 : outerdistance#=2
arc=-1 : arcangle#=360
extruded=-1
smooth=-1
Fillet=-1 : rotateY=-1 : rotateZ=-1
torus=-1
modeller=-1 : vectormoveid=-1
orbit=-1 : zoom=-1 : pan=-1 : selec=1 : camera=-1
palette = CreateSprite(create_palette())
empty_sketch(obj)
plane = CreateObjectPlane(100,100)
SetObjectRotation(plane,90,45,0)
SetObjectPosition(plane,0,-10,0)
SetObjectColor(plane,200,200,200,255)
// TOP object buttons)
AddVirtualButton(13,50,40,64) : SetVirtualButtonText(13,"New")
AddVirtualButton(1,120,40,64) : SetVirtualButtonText(1,"Tube")
AddVirtualButton(8,190,40,64) : SetVirtualButtonText(8,"Plane")
AddVirtualButton(2,260,40,64) : SetVirtualButtonText(2,"Circle/Arc")
AddVirtualButton(4,330,40,64) : SetVirtualButtonText(4,"Torus")
AddVirtualButton(9,400,40,64) : SetVirtualButtonText(9,"Pipe Bend")
AddVirtualButton(10,470,40,64) : SetVirtualButtonText(10,"Cube")
AddVirtualButton(11,540,40,64) : SetVirtualButtonText(11,"Fillet")
AddVirtualButton(12,610,40,64) : SetVirtualButtonText(12,"Cone")
AddVirtualButton(14,680,40,64) : SetVirtualButtonText(14,"Cylinder")
AddVirtualButton(16,680,40,64) : SetVirtualButtonText(16,"Spring")
AddVirtualButton(15,950,40,64) : SetVirtualButtonText(15,"Settings")
// Bottom function buttons
AddVirtualButton(50,50,screenheight-70,64) : SetVirtualButtonText(50,"Fillet")
AddVirtualButton(51,150,screenheight-70,64) : SetVirtualButtonText(51,"Rotate (Y)")
AddVirtualButton(52,250,screenheight-70,64) : SetVirtualButtonText(52,"Rotate (Z)")
AddVirtualButton(5 ,350,screenheight-70,64) : SetVirtualButtonText(5,"Smooth")
AddVirtualButton(6 ,450,screenheight-70,64) : SetVirtualButtonText(6,"Extrude (5)") : SetVirtualButtonSize(6,90,64)
AddVirtualButton(57,550,screenheight-70,64) : SetVirtualButtonText(57,"Modeller") : SetVirtualButtonSize(57,90,64)
AddVirtualButton(56,650,screenheight-70,64) : SetVirtualButtonText(56,"Centre Pivot") : SetVirtualButtonSize(56,90,64)
AddVirtualButton(53,750,screenheight-70,64) : SetVirtualButtonText(53,"Import OBJ") : SetVirtualButtonSize(53,90,64)
AddVirtualButton(54,850,screenheight-70,64) : SetVirtualButtonText(54,"Export OBJ") : SetVirtualButtonSize(54,90,64)
AddVirtualButton(55,950,screenheight-70,64) : SetVirtualButtonText(55,"Exit")
// Right camera functions
AddVirtualButton(63,950,150,64) : SetVirtualButtonText(63,"Home")
AddVirtualButton(64,950,220,64) : SetVirtualButtonText(64,"Select")
AddVirtualButton(65,950,290,64) : SetVirtualButtonText(65,"Camera")
AddVirtualButton(60,950,360,64) : SetVirtualButtonText(60,"Oribt")
AddVirtualButton(61,950,430,64) : SetVirtualButtonText(61,"Pan")
AddVirtualButton(62,950,500,64) : SetVirtualButtonText(62,"Zoom")
camx#=0: camy#=10: camz#=-20
clicks=0
SetRawMousePosition(screenwidth/2,screenheight/2)
do
SetCameraPosition(1,camx#,camy#,camz#)
SetTextPosition(txt,screenwidth/2-GetTextTotalWidth(txt)/2,80)
SetTextPosition(txt2,screenwidth/2-GetTextTotalWidth(txt2)/2,96)
SetTextPosition(fps,screenwidth/2-GetTextTotalWidth(txt2)/2,screenheight-20)
SetSpritePosition(palette,10,480)
mx#=GetPointerX()
my#=getpointery()
if GetObjectExists(obj)
if GetRawKeyState(38) then RotateObjectLocalX(obj,1)
if GetRawKeyState(40) then RotateObjectLocalX(obj,-1)
endif
if GetRawKeyPressed(82) or GetVirtualButtonPressed(50) // Fillet
Fillet=Fillet*-1
endif
if GetRawKeyPressed(82) or GetVirtualButtonPressed(51) // R - Rotate Y
rotateY=rotateY*-1
endif
if GetRawKeyPressed(82) or GetVirtualButtonPressed(52) // R - Rotate Z
rotateZ=rotateZ*-1
endif
if GetVirtualButtonPressed(1) // Tube
SetTextString(txt,"A Tube")
SetTextString(txt2,"Created with the 'sketch_arc' function, click settings to change paremeters")
empty_sketch(obj)
sketch_arc(0,0,0,1,2,30,360)
Extrude(5)
render_sketch(smooth)
obj=CreateObjectFromMesh(lander)
endif
if GetRawKeyPressed(67) or GetVirtualButtonPressed(2) // C - Circle
circle=circle*-1
endif
if GetRawKeyPressed(84) or GetVirtualButtonPressed(4) // T - Torus
torus=torus*-1
endif
if GetRawKeyPressed(69) or GetVirtualButtonPressed(6) // E - Extrude
if GetObjectExists(obj)
extruded=extruded*-1
else
SetTextString(txt,"Please create / select an Object first")
SetTextString(txt2,"")
endif
endif
if GetRawKeyPressed(83) or GetVirtualButtonPressed(5) // S - Smooth
if GetObjectExists(obj)
smooth=smooth*-1
DeleteObject(obj)
render_sketch(smooth)
obj=CreateObjectFromMesh(lander)
else
SetTextString(txt,"Please create / select an Object first")
SetTextString(txt2,"")
endif
endif
if GetRawKeyPressed(27) or GetVirtualButtonPressed(55) then exit
if GetVirtualButtonPressed(8) // Rectangle
SetTextString(txt,"A plane")
SetTextString(txt2, "Try the Extrude function to create a cube from the plane")
sketch_rectangle(-2,-2,2,2)
render_sketch(smooth)
obj=CreateObjectFromMesh(lander)
endif
if GetVirtualButtonPressed(9) // Pipe Bend
SetTextString(txt,"A Pipe Bend")
SetTextString(txt2,"Change the parameters in the 'sketch_torus' function to change segments widths and amounts and circumferance, to make different things with pipes etc")
empty_sketch(obj)
sketch_torus(0,0,0,2,1,5,90)
render_sketch(smooth)
obj=CreateObjectFromMesh(lander)
endif
if GetVirtualButtonPressed(10) // Cube
sketch_rectangle(-1,-1,1,1)
extrude(2)
render_sketch(smooth)
obj=CreateObjectFromMesh(lander)
endif
if GetVirtualButtonPressed(11) // Supershapes
SetTextString(txt,"Fillet")
SetTextString(txt2,"Coming Soon")
sketch_rectangle(-1,-1,1,1)
sketch_rectangle(-1,-1,1,1)
// fillet(edge1,edge2)
render_sketch(smooth)
obj=CreateObjectFromMesh(lander)
endif
if GetVirtualButtonPressed(13) // New
empty_sketch(obj)
endif
if GetVirtualButtonPressed(16) // Spring
SetTextString(txt,"A Spring")
SetTextString(txt2,"Change the parameters in the 'sketch_torus' function to change segments widths and amounts and circumferance, to make different things with pipes etc")
empty_sketch(obj)
sketch_Spring(0,0,0,2,1,10,360)
render_sketch(smooth)
obj=CreateObjectFromMesh(lander)
endif
// dont do any of these unless not in modeller mode
if modeller=-1
if GetVirtualButtonPressed(60) // Orbit
camera=-1
selec=-1
orbit = 1
pan=-1
zoom=-1
endif
if GetVirtualButtonPressed(61) // Pan
camera=-1
selec=-1
orbit = -1
pan = 1
zoom=-1
endif
if GetVirtualButtonPressed(62) // Zoom
// SetTextString(txt,"Coming Soon")
// SetTextString(txt2,"")
camera=-1
selec=-1
orbit = -1
pan = -1
zoom = 1
endif
if GetVirtualButtonPressed(64) // Select
camera=-1
selec=1
orbit = -1
pan = -1
zoom = -1
endif
if GetVirtualButtonPressed(65) // Camera
camera=1
selec=-1
orbit = -1
pan = -1
zoom = -1
endif
// Rotate the object - Left MB
if manualrotate=1 and GetRawMouseLeftState() and GetObjectExists(obj) and orbit=1
ry#=Get3DVectorXFromScreen(mx#,my#+300)*800
rx#=Get3DVectorYFromScreen(mx#,my#)*800
r#=Get3DVectorZFromScreen(mx#,my#)*800
SetObjectRotation(obj,rx#,r#,0)
SetObjectColor(obj,255,0,0,255)
endif
// pan the camera
if pan=1 and GetRawMouseLeftState()
camx#=-Get3DVectorXFromScreen(mx#,my#+screenheight) * 30
camy#=-Get3DVectorYFromScreen(mx#,my#+screenheight) * 10
camz#=-Get3DVectorZFromScreen(mx#,my#+screenheight) * 50
//
endif
// zoom
if zoom=1 and GetRawMouseLeftState()
camy#=-Get3DVectorYFromScreen(mx#,my#+screenheight) * 10
camz#=-Get3DVectorZFromScreen(mx#,my#+screenheight) * 50
endif
endif
if GetVirtualButtonPressed(56) // Centre Pivot
SetTextString(txt,"Coming Soon")
SetTextString(txt2,"")
endif
if GetVirtualButtonPressed(57) // Modeller#
SetTextString(txt,"Modeller - In Progress")
SetTextString(txt2,"")
modeller=modeller*-1
endif
if modeller=1
// display the model points
for i= 0 to sketch3dpoints.length
x#=sketch3dpoints[i].x#
y#=sketch3dpoints[i].y#
z#=sketch3dpoints[i].z#
// if we already have created the model points then dont need to to anymore for each frame - just once will do
if modelpoints.length<sketch3dpoints.length
//using a box cause lot less than another primative - less object vertexes the better for performance
//could use a "ball" / "Sphere" but the vertexes for this is horrendous for each vertexes of this object to load
modelpoints.insert(CreateObjectBox(.1,.1,.1))
SetObjectColor(modelpoints[i],255,0,0,0)
setobjectposition(modelpoints[i],x#,y#,z#)
FixObjectToObject(modelpoints[i],obj)
endif
next
else
// we are out of model mode, so delete those vertex pointa
for i=0 to modelpoints.length
DeleteObject(modelpoints[i])
next
modelpoints.length=-1
endif
if GetVirtualButtonPressed(53) // Impport
SetTextString(txt,"Coming Soon")
SetTextString(txt2,"")
endif
if GetVirtualButtonPressed(54) // Expport
SetTextString(txt,"Coming Soon")
SetTextString(txt2,"")
endif
if GetVirtualButtonPressed(63) // Home
camx#=0: camy#=10: camz#=-20
endif
if Fillet=1
SetVirtualButtonColor(50,100,0,0)
SetVirtualButtonText(50,"Fillet On")
else
SetVirtualButtonColor(50,255,0,0)
SetVirtualButtonText(50,"Fillet Off")
endif
if modeller=1
SetVirtualButtonColor(57,100,0,0)
else
SetVirtualButtonColor(57,255,0,0)
endif
if GetObjectExists(obj) and rotateY=1
SetVirtualButtonColor(51,100,0,0)
RotateObjectLocalY(obj,1)
else
SetVirtualButtonColor(51,255,0,0)
endif
if GetObjectExists(obj) and rotateZ=1
SetVirtualButtonColor(52,100,0,0)
RotateObjectLocalZ(obj,1)
else
SetVirtualButtonColor(52,255,0,0)
endif
if orbit=1
SetVirtualButtonColor(60,0,0,150)
else
SetVirtualButtonColor(60,0,0,255)
endif
if orbit=1
SetVirtualButtonColor(60,0,0,100)
else
SetVirtualButtonColor(60,0,0,255)
endif
if pan=1
SetVirtualButtonColor(61,0,0,100)
else
SetVirtualButtonColor(61,0,0,255)
endif
if zoom=1
SetVirtualButtonColor(62,0,0,100)
else
SetVirtualButtonColor(62,0,0,255)
endif
if selec=1
SetVirtualButtonColor(64,0,0,100)
else
SetVirtualButtonColor(64,0,0,255)
endif
if camera=1
SetVirtualButtonColor(65,0,0,100)
else
SetVirtualButtonColor(65,0,0,255)
endif
if smooth=1
SetVirtualButtonText(5,"Flat")
SetVirtualButtonColor(5,100,100,0)
else
SetVirtualButtonText(5,"Smooth")
SetVirtualButtonColor(5,255,255,0)
endif
if extruded=1
DeleteObject(obj)
extrude(5)
render_sketch(smooth)
obj=CreateObjectFromMesh(lander)
extruded=-1
arc=-1
circle=-1
endif
if torus=1
SetTextString(txt,"A Torus")
SetTextString(txt2,"Change the parameters in the 'sketch_torus' function to change segments widths and amounts and circumferance etc")
empty_sketch(obj)
sketch_torus(0,0,0,2,1,12,360)
render_sketch(smooth)
obj=CreateObjectFromMesh(lander)
torus=-1
endif
if circle=1
empty_sketch(obj)
SetTextString(txt,"Create a circle, Press Cursor up and down to change the segment widths, press Cursor left and right to create an Arc")
SetTextString(txt2,"Hold Shift and Cursors Left and right to increase/decrease segments. Left Mouse Buton to create")
lx#=screenwidth/2
ly#=screenheight/2
if GetRawKeyState(38) then inc innerdistance#,.01
if GetRawKeyState(40) then dec innerdistance#,.01
if GetRawKeyState(37) then dec arcangle#,1
if GetRawKeyState(39) then inc arcangle#,1
if GetRawKeyPressed(37) and GetRawKeyState(16) then dec circleedges,1
if GetRawKeyPressed(39) and GetRawKeyState(16) then inc circleedges,1
if GetRawMouseLeftPressed() and clicks=0
clicks=1
else
if GetRawMouseLeftPressed() and clicks=1
empty_sketch(obj)
sketch_arc(0,0,0,innerdistance#,outerdistance#,circleedges,arcangle#)
render_sketch(smooth)
obj=CreateObjectFromMesh(lander)
circle=circle*-1
endif
endif
edgedistance# = arcangle#/circleedges
for a#=0 to arcangle# step edgedistance#
ix1#= lx#+ (innerdistance#*100) * sin(a#)
iy1#= ly#+ (innerdistance#*100) * cos(a#)
ox1#= lx#+ (outerdistance#*100) * sin(a#)
oy1#= ly#+ (outerdistance#*100) * cos(a#)
DrawLine(ix1#,iy1#,ox1#,oy1#,MakeColor(255,255,255),MakeColor(255,255,255))
ix2#= lx#+ (innerdistance#*100) * sin(a#+edgedistance#)
iy2#= ly#+ (innerdistance#*100) * cos(a#+edgedistance#)
ox2#= lx#+ (outerdistance#*100) * sin(a#+edgedistance#)
oy2#= ly#+ (outerdistance#*100) * cos(a#+edgedistance#)
DrawLine(ix2#,iy2#,ox2#,oy2#,MakeColor(255,255,255),MakeColor(255,255,255))
DrawLine(ix1#,iy1#,ix2#,iy2#,MakeColor(255,255,255),MakeColor(255,255,255))
DrawLine(ox1#,oy1#,ox2#,oy2#,MakeColor(255,255,255),MakeColor(255,255,255))
next
endif
if freehand=1 // Free hand
SetTextString(txt,"Free Hand")
SetTextString(txt2,"LMB to create a few points, create 4 points to create a QUAD... This needs work")
if GetRawMouseLeftPressed()
smx#=Get3DVectorXFromScreen(mx#,my#)*10
smy#=Get3DVectorYFromScreen(mx#,my#)*10
smz#=Get3DVectorZFromScreen(mx#,my#)*10
draw2dpoint.x#=mx#
draw2dpoint.y#=my#
draw2dpoint.vx#=smx#
draw2dpoint.vy#=smy#
draw2dpoint.vx#=smx#
draw2dpoints.insert(draw2dpoint)
inc clicks
if rendermode=1 // Triangles
if clicks>=3
render_sketch(smooth)
DeleteObject(obj)
obj=CreateObjectFromMesh(lander)
SetObjectPosition(obj,0,0,0)
clicks=0
endif
else // quads
if clicks>=4
x1#=draw2dpoints[draw2dpoints.length-3].vx#
y1#=draw2dpoints[draw2dpoints.length-3].vy#
x2#=draw2dpoints[draw2dpoints.length-2].vx#
y2#=draw2dpoints[draw2dpoints.length-2].vy#
x3#=draw2dpoints[draw2dpoints.length-1].vx#
y3#=draw2dpoints[draw2dpoints.length-1].vy#
x4#=draw2dpoints[draw2dpoints.length].vx#
y4#=draw2dpoints[draw2dpoints.length].vy#
convert_quad2tris(x1#,y1#,x2#,y2#,x3#,y3#,x4#,y4#)
render_sketch(smooth)
DeleteObject(obj)
obj=CreateObjectFromMesh(lander)
SetObjectPosition(obj,0,0,0)
clicks=0
endif
endif
endif
endif
for points=0 to draw2dpoints.length
c=MakeColor(255,255,255)
DrawBox(draw2dpoints[points].x#,draw2dpoints[points].y#,draw2dpoints[points].x#+1,draw2dpoints[points].y#+1,c,c,c,c,1)
next
// keep mouse in window
if mx#>screenwidth-10 then SetRawMousePosition(10,my#)
if mx#<10 then SetRawMousePosition(screenwidth-10,my#)
if my#<10 then SetRawMousePosition(mx#,screenheight-10)
if my#>screenheight-10 then SetRawMousePosition(mx#,10)
// Rotate objects with the mouse when over the object
// Hold Left mouse button
if GetObjectExists(obj)
// get the x, y and z unit vectors based on the pointer position
unit_x# = Get3DVectorXFromScreen(mx#,my#)
unit_y# = Get3DVectorYFromScreen(mx#,my#)
unit_z# = Get3DVectorZFromScreen(mx#,my#)
// calculate the start of the ray cast, which is the unit vector + the camera position
start_x# = unit_x# + camx#
start_y# = unit_y# + camy#
start_z# = unit_z# + camz#
// calculate the end of the vector, which is the unit vector multiplied by the length of the ray cast and then add the camera position to it
end_x# = 800*unit_x# + camx#
end_y# = 800*unit_y# + camy#
end_z# = 800*unit_z# + camz#
if modeller=-1
objecthit=ObjectRayCast(obj,start_x#,start_y#,start_z#,end_x#,end_y#,end_z#)
// if objectselected=obj
// SetObjectColor(obj,255,0,0,255)
// else
SetObjectColor(obj,objred,objgreen,objblue,255)
// endif
if objecthit<>0
SetObjectColor(obj,255,0,0,255)
manualrotate=1
endif
else
for vectors=0 to modelpoints.length
objecthit=ObjectRayCast(modelpoints[vectors],start_x#,start_y#,start_z#,end_x#,end_y#,end_z#)
SetObjectColor(modelpoints[vectors],255,0,0,255)
if objecthit<>0
SetObjectColor(modelpoints[vectors],0,255,0,255)
if GetRawMouseLeftPressed()
vectormoveid = vectors
else
// vectormoveid=-1
endif
endif
next
endif
endif
// have we selected a vertex point so move it until we release LMB
if vectormoveid <> -1
rx#=GetObjectRayCastX(0)
ry#=GetObjectRayCastY(0)
rz#=GetObjectRayCastZ(0)
SetObjectPosition(modelpoints[vectormoveid],rx#,ry#,getobjectz(modelpoints[vectormoveid]))
if GetRawMouseLeftReleased() then vectormoveid=-1
endif
// select a colour from the palette
if mx#>10 and mx#<10+128 and my#>480 and my#<480+128
set_object_palette(obj,mx#,my#)
else
// this tries to find all the edges of the object - NEEDS WORK
if selec=1 and GetRawMouseLeftState()
//SetObjectColor(obj,255,0,0,255)
// objectselected=obj
endif
endif
// rotate
if camera=1 and GetRawMouseLeftState() then move_camera()
sync()
SetWindowTitle( "AGK 3D object creation system - Welcome - fps ( " + str(ScreenFPS()) + ")" )
SetTextString(fps,str(ScreenFPS()))
loop
function set_object_palette(obj,x,y)
// Thanks Fubarpk
clearScreen()
render()
img = getImage(X,Y,X+1,Y+1)
rem create memblock
mem = createMemblockfromImage(img)
rem get memblock data
r = getMemblockbyte(mem,12):rem gets red channel data
g = getMemblockbyte(mem,13):rem gets green channel data
b = getMemblockbyte(mem,14):rem gets blue channel data
if GetRawMouseLeftPressed() // if click on the color then change it for the object
objred=r
objgreen=g
objblue = b
endif
rem tidy up
deletememblock(mem)
deleteimage(img)
clearScreen()
color = makeColor(r,g,b)
DrawBox(10,480+138,10+128,480+148,color,color,color,color,1)
if GetObjectExists(obj) then SetObjectColor(obj,r,g,b,255)
endfunction
function empty_sketch(obj)
sketch3dpoints.length=-1
emptymesh(lander)
DeleteObject(obj)
endfunction
function convert_quad2tris(x1#,y1#,x2#,y2#,x3#,y3#,x4#,y4#)
Add3DSketchPoint(x1#,y1#,0)
Add3DSketchPoint(x2#,y2#,0)
Add3DSketchPoint(x3#,y3#,0)
Add3DSketchPoint(x1#,y1#,0)
Add3DSketchPoint(x3#,y3#,0)
Add3DSketchPoint(x4#,y4#,0)
endfunction
function sketch_rectangle(x1#,y1#,x2#,y2#)
convert_quad2tris(x1#,y1#,x2#,y1#,x2#,y2#,x1#,y2#)
endfunction
function RadiansToDegrees(angle#)
d#= angle# * 180.0 / 3.14159
endfunction d#
function DegreesToRadians(angle#)
r#= (angle# * 3.14159) / 180.0
endfunction r#
function sketch_torus(x#, y#, z#,radius#, segmentradius#,edges, endangle#)
edgedistance = endangle#/edges
edgedistance=endangle#/edges
for j=0 to 360 step edgedistance
currentradius# = radius# + (segmentradius# * cos(j))
zval# = segmentradius# * sin(j)
currentradiusnxt# = radius# + (segmentradius# * cos(j+edgedistance))
zvalnxt# = segmentradius# * sin(j+edgedistance)
/* iterate sides: outer ring */
for i=0 to endangle# step edgedistance
x1# = currentradius# * cos(i)
y1# = currentradius# * sin(i)
z1# = zval#
x2# = currentradius# * cos(i+edgedistance)
y2# = currentradius# * sin(i+edgedistance)
z2# = zval#
x3# = currentradiusnxt# * cos(i+edgedistance)
y3# = currentradiusnxt# * sin(i+edgedistance)
z3# = zvalnxt#
x4# = currentradiusnxt# * cos(i)
y4# = currentradiusnxt# * sin(i)
z4# = zvalnxt#
// 2 triangles to make a sqausre plane
// triangle one
Add3DSketchPoint(x1#,y1#,z1#) // This ring point
Add3DSketchPoint(x2#,y2#,z2#) // Next ring Point
Add3DSketchPoint(x3#,y3#,z3#) // This ring next point
// triangle two
Add3DSketchPoint(x3#,y3#,z3#) // ring point
Add3DSketchPoint(x4#,y4#,z4#) // This ring next point
Add3DSketchPoint(x1#,y1#,z1#) // Next ring Point
next
next
endfunction
function sketch_Spring(x#, y#, z#,radius#, segmentradius#,edges, endangle#)
edgedistance = endangle#/edges
edgedistance=endangle#/edges
k#=.01
// for k=0 to -5
for j=0 to 360 step edgedistance
currentradius# = radius# + (segmentradius# * cos(j+k#))+k#
zval# = segmentradius# * sin(j+k#)
currentradiusnxt# = radius# + (segmentradius# * cos(j+k#+edgedistance))+k#
zvalnxt# = segmentradius# * sin(j+k#+edgedistance)
inc k#,.1
/* iterate sides: outer ring */
for i=0 to 180 step edgedistance
x1# = currentradius# * cos(i)
y1# = currentradius# * sin(i) + k#
z1# = zval#
x2# = currentradius# * cos(i+edgedistance)
y2# = currentradius# * sin(i+edgedistance) + (k#)
z2# = zval#
x3# = currentradiusnxt# * cos(i+edgedistance)
y3# = currentradiusnxt# * sin(i+edgedistance) + (k#*2)
z3# = zvalnxt#
x4# = currentradiusnxt# * cos(i)
y4# = currentradiusnxt# * sin(i) + (k#)
z4# = zvalnxt#
// 2 triangles to make a sqausre plane
// triangle one
Add3DSketchPoint(x1#,y1#,z1#) // This ring point
Add3DSketchPoint(x2#,y2#,z2#) // Next ring Point
Add3DSketchPoint(x3#,y3#,z3#) // This ring next point
// triangle two
Add3DSketchPoint(x3#,y3#,z3#) // ring point
Add3DSketchPoint(x4#,y4#,z4#) // This ring next point
Add3DSketchPoint(x1#,y1#,z1#) // Next ring Point
next
inc k#,1
next
//next
endfunction
function sketch_arc(x#, y#, z#, innerradius#,outerradius#, edges,endangle#)
edgedistance = endangle#/edges
for a=0 to endangle# step edgedistance
innerx#=x#+innerradius# * sin(a)
innery#=z#+innerradius# * cos(a)
Add3DSketchPoint(innerx#,innery#,0)
innerx#=x#+innerradius# * sin(a+edgedistance)
innery#=z#+innerradius# * cos(a+edgedistance)
Add3DSketchPoint(innerx#,innery#,0)
outerx#=x#+outerradius# * sin(a)
outery#=z#+outerradius# * cos(a)
Add3DSketchPoint(outerx#,outery#,0)
innerx#=x#+innerradius# * sin(a+edgedistance)
innery#=z#+innerradius# * cos(a+edgedistance)
Add3DSketchPoint(innerx#,innery#,0)
outerx#=x#+outerradius# * sin(a+edgedistance)
outery#=z#+outerradius# * cos(a+edgedistance)
Add3DSketchPoint(outerx#,outery#,0)
outerx#=x#+outerradius# * sin(a)
outery#=z#+outerradius# * cos(a)
Add3DSketchPoint(outerx#,outery#,0)
next
endfunction
function Extrude(depth#)
currentlength=sketch3dpoints.length
sketch as _SketchPoint
for points=0 to currentlength
// duplicate points to the depth end
sketch.x# = sketch3dpoints[points].x#
sketch.y# = sketch3dpoints[points].y#
sketch.z# = depth#
sketch3dpoints.insert(sketch)
next
for points=0 to currentlength
// we need to "join" up the current x,y,z to the extruded depth
sketch.x# = sketch3dpoints[points].x#
sketch.y# = sketch3dpoints[points].y#
sketch.z# = 0
sketch3dpoints.insert(sketch)
sketch.x# = sketch3dpoints[points+1].x#
sketch.y# = sketch3dpoints[points+1].y#
sketch.z# = depth#
sketch3dpoints.insert(sketch)
sketch.x# = sketch3dpoints[points].x#
sketch.y# = sketch3dpoints[points].y#
sketch.z# = depth#
sketch3dpoints.insert(sketch)
sketch.x# = sketch3dpoints[points].x#
sketch.y# = sketch3dpoints[points].y#
sketch.z# = 0
sketch3dpoints.insert(sketch)
sketch.x# = sketch3dpoints[points+1].x#
sketch.y# = sketch3dpoints[points+1].y#
sketch.z# = 0
sketch3dpoints.insert(sketch)
sketch.x# = sketch3dpoints[points+1].x#
sketch.y# = sketch3dpoints[points+1].y#
sketch.z# = depth#
sketch3dpoints.insert(sketch)
next
endfunction
function render_sketch(smoothmode)
emptymesh(lander)
if rendermode=1
rendermodestep=3
else
rendermodestep=3
endif
for points = 0 to sketch3dpoints.length step rendermodestep
line1x#=sketch3dpoints[points+1].x# - sketch3dpoints[points].x#
line1y#=sketch3dpoints[points+1].y# - sketch3dpoints[points].y#
line1z#=sketch3dpoints[points+1].z# - sketch3dpoints[points].z#
line2x#=sketch3dpoints[points+2].x# - sketch3dpoints[points].x#
line2y#=sketch3dpoints[points+2].y# - sketch3dpoints[points].y#
line2z#=sketch3dpoints[points+2].z# - sketch3dpoints[points].z#
lx#=line1y# * line2z# - line1z# * line2y#
ly#=line1z# * line2x# - line1x# * line2z#
lz#=line1x# * line2y# - line1y# * line2x#
len#=sqrt ( lx# * lx# + ly# * ly# + lz#*lz#)
nx#=lx#/len#
ny#=ly#/len#
nz#=lz#/len#
xx#=sketch3dpoints[points].x#
yy#=sketch3dpoints[points].y#
zz#=sketch3dpoints[points].z#
if smoothmode=-1
AddVertex(lander,xx#,yy#,zz#,nx#,ny#,nz#,0,0)
else
AddVertex(lander,xx#,yy#,zz#,xx#,yy#,zz#,0,0)
endif
xx#=sketch3dpoints[points+2].x#
yy#=sketch3dpoints[points+2].y#
zz#=sketch3dpoints[points+2].z#
if smoothmode=-1
AddVertex(lander,xx#,yy#,zz#,nx#,ny#,nz#,0,0)
else
AddVertex(lander,xx#,yy#,zz#,xx#,yy#,zz#,0,0)
endif
xx#=sketch3dpoints[points+1].x#
yy#=sketch3dpoints[points+1].y#
zz#=sketch3dpoints[points+1].z#
if smoothmode=-1
AddVertex(lander,xx#,yy#,zz#,nx#,ny#,nz#,0,0)
else
AddVertex(lander,xx#,yy#,zz#,xx#,yy#,zz#,0,0)
endif
// double sides
xx#=sketch3dpoints[points].x#
yy#=sketch3dpoints[points].y#
zz#=sketch3dpoints[points].z#
if smoothmode=-1
AddVertex(lander,xx#,yy#,zz#,nx#,ny#,nz#,0,0)
else
AddVertex(lander,xx#,yy#,zz#,xx#,yy#,zz#,0,0)
endif
xx#=sketch3dpoints[points+1].x#
yy#=sketch3dpoints[points+1].y#
zz#=sketch3dpoints[points+1].z#
if smoothmode=-1
AddVertex(lander,xx#,yy#,zz#,nx#,ny#,nz#,0,0)
else
AddVertex(lander,xx#,yy#,zz#,xx#,yy#,zz#,0,0)
endif
xx#=sketch3dpoints[points+2].x#
yy#=sketch3dpoints[points+2].y#
zz#=sketch3dpoints[points+2].z#
if smoothmode=-1
AddVertex(lander,xx#,yy#,zz#,nx#,ny#,nz#,0,0)
else
AddVertex(lander,xx#,yy#,zz#,xx#,yy#,zz#,0,0)
endif
next
endfunction
function Add3DSketchPoint(x#,y#,z#)
sketch as _SketchPoint
sketch.x# = x#
sketch.y# = y#
sketch.z# = z#
sketch3dpoints.insert(sketch)
endfunction
function normaliseX(x#,y#,z#,x2#,y2#,z2#)
nx#=(y#*z2#) - (z#*y2#)
ny#=(z#*x2#) - (x#*z2#)
nz#=(x#*y2#) - (y#*x2#)
len#=sqrt((nx#*nx#) + (ny#*ny#) + (nz#*nz#))
nx#=nx#/len#
endfunction nx#
function normaliseY(x#,y#,z#,x2#,y2#,z2#)
nx#=(y#*z2#) - (z#*y2#)
ny#=(z#*x2#) - (x#*z2#)
nz#=(x#*y2#) - (y#*x2#)
len#=sqrt((nx#*nx#) + (ny#*ny#) + (nz#*nz#))
ny#=ny#/len#
endfunction ny#
function normaliseZ(x#,y#,z#,x2#,y2#,z2#)
nx#=(y#*z2#) - (z#*y2#)
ny#=(z#*x2#) - (x#*z2#)
nz#=(x#*y2#) - (y#*x2#)
len#=sqrt((nx#*nx#) + (ny#*ny#) + (nz#*nz#))
nz#=nz#/len#
endfunction nz#
Function AddVertex(m ref as Mesh, x as float, y as float, z as float, nx as float, ny as float,nz as float, u as float, v as float)
vert as vertex
vert.x = x
vert.y = y
vert.z = z
vert.oldx = x
vert.oldy = y
vert.oldz = z
vert.nx = nx
vert.ny = ny
vert.nz = nz
vert.u = u
vert.v = v
m.VertexList.Insert(vert)
endfunction
Function AddTriangle(m ref as Mesh, v1 as integer, v2 as integer, v3 as integer)
t as Triangle
t.v1 = v1
t.v2 = v2
t.v3 = v3
m.TriangleList.Insert(t)
endfunction
Function CreateObjectFromMesh(m ref as mesh)
s=60
l=32
VertexCount = m.VertexList.Length + 1
IndexCount = (m.TriangleList.Length + 1) * 3
IndexOffset = s + VertexCount*l
//VertexCount = m.VertexList.Length + 1
//IndexCount=0
//IndexOffset = 72 + VertexCount*36
memblock = CreateMemblock(IndexOffset+IndexCount*4)
SetMemblockInt(memblock,0,VertexCount)
SetMemblockInt(memblock,4,IndexCount)
SetMemblockInt(Memblock,8,3)
SetMemblockInt(memblock,12,l)
SetmemblockInt(memblock,16,s)
SetMemblockInt(memblock,20,IndexOffset)
SetMemblockInt(memblock,24,0x0c000300)
SetMemblockString(Memblock,28,"position")
SetMemblockInt(memblock,40,0x08000300)
SetMemblockString(memblock,44,"normal")
SetMemblockInt(memblock,52,0x04000200)
SetMemblockString(memblock,56,"uv")
// SetMemblockInt(memblock,60,0x08010401)
//SetMemblockString(memblock,64,"color")
for i = 0 to m.VertexList.Length
SetMemblockFloat(memblock,s+i*l,m.VertexList[i].x)
SetMemblockFloat(memblock,s+4+i*l,m.VertexList[i].y)
SetMemblockFloat(memblock,s+8+i*l,m.VertexList[i].z)
SetMemblockFloat(memblock,s+12+i*l,m.VertexList[i].nx)
SetMemblockFloat(memblock,s+16+i*l,m.VertexList[i].ny)
SetMemblockFloat(memblock,s+20+i*l,m.VertexList[i].nz)
SetMemblockFloat(memblock,s+24+i*l,m.VertexList[i].u)
SetMemblockFloat(memblock,s+28+i*l,m.VertexList[i].v)
// SetMemblockInt(memblock,104+i*36,m.VertexList[i].color)
next
for i = 0 to m.TriangleList.Length
SetMemblockInt(memblock,IndexOffset+i*12,m.TriangleList[i].v1)
SetMemblockInt(memblock,IndexOffset+i*12+4,m.TriangleList[i].v2)
SetMemblockInt(memblock,IndexOffset+i*12+8,m.TriangleList[i].v3)
next
id = CreateObjectFromMeshMemblock(memblock)
DeleteMemblock(memblock)
endfunction id
function emptymesh(c ref as Mesh)
c.VertexList.length=-1
c.TriangleList.length=-1
endfunction
function move_camera()
SetCameraPosition(1,camx#,camy#,camz#)
//move cam with mouse
fDiffX# = (GetPointerX() - startx#)/6.0
fDiffY# = (GetPointerY() - starty#)/6.0
newX# = angx# + fDiffY#
if ( newX# > 89 ) then newX# = 89
if ( newX# < -89 ) then newX# = -89
SetCameraRotation( 1, newX#+45, angy# + fDiffX#, 0 )
endfunction
function create_palette()
/*
255,0,0
0,255,0
0,0,255
0,0,0
255,255,255
255,255,0
0,255,255
255,0,255
*/
swap()
DrawBox(0 ,0,64,64 ,MakeColor(255,0,0),MakeColor(255,255,0),MakeColor(255,255,255),MakeColor(0,0,0),1)
DrawBox(64,0,128,64 ,MakeColor(255,255,0),MakeColor(0,0,255),MakeColor(0,0,0),MakeColor(0,255,255),1)
DrawBox(0 ,64,64,128 ,MakeColor(255,255,255),MakeColor(0,0,0),MakeColor(0,255,0),MakeColor(255,255,0),1)
DrawBox(64,64,128,128,MakeColor(0,0,0),MakeColor(0,255,255),MakeColor(255,255,0),MakeColor(255,0,255),1)
for a=0 to 2
DrawBox(a,a,128-a,128-a,MakeColor(0,0,0),MakeColor(0,0,0),MakeColor(0,0,0),MakeColor(0,0,0),0)
next
render()
img = GetImage(0,0,129,129)
endfunction img
This is no way finished, far from it,
If anyone like to see differnt things on it, then shout. but this is my way of building a 3D Object Modelling system - similar to Blender
Code is free for you to self destruct as you see fit for your programming ways.
Will send constructive updates periodically.
Enjoy