Here are some useful commands that maybe of interest, all written from memory so hope they work
PI
Polar coordinates
Getting a point in 2D space depending on an angle and distance from the origin 0,0
X# = originx + (distance# * sin (angle))
Y# = originy + (distance# * cos (angle))
Fract
Returns the fractional component eg
4.8754356
Returns
0.8754356
Function fract ( n# )
R# = n# - floor( n# )
Endfunction R#
Lerp
An interpolation between two numbers
Time# should be between 0 and 1 so .5 is half way for example, so 0 is the start and 1 is the end.
Function Lerp ( start#, end#, time# )
R# = ( ( end# - start# ) * time# ) + start#
Endfunction R#
Smoothstep
A smoother approach to Lerp, returns values on a curve style between two numbers.
function smoothstep(x# as float, edge0# as float, edge1# as float )
// Scale, and clamp x to 0..1 range
x# = clamp((x# - edge0#) / (edge1# - edge0#), 0.0, 1.0);
r# = x# * x# * (3.0f - 2.0f * x#);
Endfunction r#
Clamp
Keeps a value clamped to a lower limit or upper limit.
Function clamp(x# , lowerlimit#, upperlimit# )
R# = x#
if (x# < lowerlimit#) then R# = lowerlimit#
if (x# > upperlimit#) then R#= upperlimit#
Endfunction R#
Map
Remaps a number from one range to another
Like
map ( 50, 0, 100, 0, 500 )
will return 250 cause 50 in the range 0-100 is exactly half, So it must return exactly half of 0 to 500 which is 250
Function map ( num, start1, stop1, start2, stop2)
R# = (num - start1) / (stop1 - start1) * (stop2 - start2) + start2;
Endfunction R#
Normalizing
Normalize 2d or 3d vectors
Returns a type of x and y or z values into a vector type.
// Place this in your types area to enable to store
// The x y and z when the functions return them
Type vectortype
x#
y#
z#
Endtype
Function normalise2D(vx#, vy#)
Local vector as vectortype
Local Length# As Float
Length# = Sqrt( (vx# * vx# ) + ( v.y# * v.y#) )
vector.x = vx# / Length#
Vector.y = vy# / Length#
Endfunction vector
Function normalise3D(vx#, vy#, vz# )
Local vector as vectortype
Local Length# As Float
Length = Sqrt( (vx#*vx# )+(vy#*vy#)+(vz#*vz#) )
Vector.x = vx# / Length#
Vector y = vy# / Length#
Vector.z = vz# / Length#
Endfunction vector
Distance
Calculates the distance between 2 or 3 points
Function dist2d ( x1#, y1#, x2#, y2# )
dx# = x2#-x1#
dy# = y2#-y1#
D# = sqrt(dx#*dx# + dy#*dy#)
Endfunction D#
Function dist3d ( x1#, y1#, z1#, x2#, y2#, z2#)
dx# = x2#-x1#
dy# = y2#-y1#
dz# = z2#-z1#
D# = sqrt(dx#*dx# + dy#*dy# + dz#*dz#)
Endfunction D#
Min and Max
Returns the largest number in a sequence of numbers or two individual numbers.
Function max ( max1#, max2# )
If max1# > max2#
R# = max1#
Else
R# = max2#
Endfunction R#
Min
Returns the smallest of two numbers
Function min ( min1#, min2 )
If nin1# < min2#
R# = min1#
Else
R# = min2#
Endif
Endfunction R#
Mean and median averages
Get the average of all numbers added then divided by the amount of numbers in the array
Function get_mean ( numbers ref as integer[] )
R = 0
For l = 1 to numbers.length
Count = count + numbers [ l ]
Next
R = count / numbers.length
Endfunction R
Median
Get the middle number in the array
Function get_median ( numbers ref as integer[] )
// First sort the array
Numbers.sort ()
R = numbers [ numbers.length / 2 ]
Endfunction R
Get object in screen
Objects as integer[]
o = 0
For x = -5 to 5
For y= -5 to 5
For z=-5 to 5
Objects.insert(1)
Objects[ o ] = createobjectcube( 1, 1, 1)
Setobjectposition (objects[ o ], x, 0, z)
Inc o
Next
Next
Next
Do
For o = 0 to objects.length
If getobjectinscreen( objects[ o ] ) = 1
Setibjectvisible (objects[o], 1)
Else
Setibjectvisible (objects[o] , 0)
Endif
Next
Sync ()
Loop
Marching cubes
To do and coming soon
noise 2D
Frequency# = 1.0
Amplitude# = 1.0
Lacunarity# = 2.0
Persistence# = 1.0 / Lacunarity#
SetupNoise ( frequency#, amplitude#, lacunarity#, persistence# )
For x = -50 to 50
For z = -50 to 50
Noise# = floor (GetnoiseXY ( x, z ))
Obj = Createobjectcube ( 1, 1, 1 )
Setobjectposition (obj, x, noise#, z)
Next
Next
Do
Sync()
Loop
noise 3D
Noisethreshold# = 0.7
Frequency# = 1.0
Amplitude# = 1.0
Lacunarity# = 2.0
Persistence# = 1.0 / Lacunarity#
SetupNoise ( frequency#, amplitude#, lacunarity#, persistence# )
For x = -8 to 8
For y = -8 to 8
For z = -8 to 8
Noise# = GetnoiseXYZ ( x, y, z ) // returns between -1 and 1
If noise# > noisethreshold#
Obj = Createobjectcube ( 1, 1, 1 )
Setobjectposition (obj, x, noise#, z)
Endif
Next
Next
Next
Do
Sync()
Loop
Mesh memblocks
Adding vertexes to create an object.
You can have as much as you need on a mesh, say you wish to apply thousands of objects to the mesh it will only provide 1 drawcall to the GPU but if you did those thousand objects individually then it will be thousand drawcalls per sync. Just remember that concept to decide is a mesh suitable for your app. Having physics to your objects will be complicated so this may not suit that requirement, but maybe good for static 3D worlds and place removable objects as different entities of the mesh to help with FPS and good for big world apps
// meshmemblock types
Type Vertex
air as integer
x as float
y as float
z as float
nx as float
ny as float
nz as float
u as float
v as float
endtype
Type Triangle
v1 as integer
v2 as integer
v3 as integer
endtype
Type Mesh
VertexList as Vertex[]
endtype
global MeshID as Mesh
// Add a couple of triangles
addsampleToMesh ( MeshIID )
// Create it
obj=CreateObjectFromMesh(meshid,1)
// Destroy mesh data only
Emptymesh()
Do
Sync()
Loop
function addsampleToMesh(m ref as Mesh)
AddVertex(m, 0,0,0 , 0,1,0, 0 ,0)
AddVertex(m, 0,1,0 ,. 0,1,0,. 0 ,.5)
AddVertex(m, 1,0,0 , 0,1,0, .5 ,0 )
Endfunction
function emptymesh()
meshid.VertexList.length=-1
endfunction
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.nx = nx
vert.ny = ny
vert.nz = nz
vert.u = u
vert.v = v
m.VertexList.Insert(vert)
endfunction
Function CreateObjectFromMesh(m ref as mesh, MeshIndex)
VertexCount = m.VertexList.Length + 1
IndexCount = 0
IndexOffset = 60 + VertexCount*36
chunkmesh = CreateMemblock(IndexOffset+IndexCount*4)
SetMemblockInt(chunkmesh,0,VertexCount)
SetMemblockInt(chunkmesh,4,IndexCount)
SetMemblockInt(chunkmesh,8,3)
SetMemblockInt(chunkmesh,12,32) // no color - 36 if color
SetmemblockInt(chunkmesh,16,60)
SetMemblockInt(chunkmesh,20,IndexOffset)
SetMemblockInt(chunkmesh,24,0x0c000300)
SetMemblockString(chunkmesh,28,"position")
SetMemblockInt(chunkmesh,40,0x08000300)
SetMemblockString(chunkmesh,44,"normal")
SetMemblockInt(chunkmesh,52,0x04000200)
SetMemblockString(chunkmesh,56,"uv")
for i = 0 to m.VertexList.Length
SetMemblockFloat(chunkmesh,60+i*32,m.VertexList[i].x)
SetMemblockFloat(chunkmesh,64+i*32,m.VertexList[i].y)
SetMemblockFloat(chunkmesh,68+i*32,m.VertexList[i].z)
SetMemblockFloat(chunkmesh,72+i*32,m.VertexList[i].nx)
SetMemblockFloat(chunkmesh,76+i*32,m.VertexList[i].ny)
SetMemblockFloat(chunkmesh,80+i*32,m.VertexList[i].nz)
SetMemblockFloat(chunkmesh,84+i*32,m.VertexList[i].u)
SetMemblockFloat(chunkmesh,88+i*32,m.VertexList[i].v)
//SetMemblockInt(memblock,104+i*36,m.VertexList[i].color) //maybe one day or year in 2019 lol
next
DeleteObject(1)
CreateObjectFromMeshMemblock(1,chunkmesh)
endfunction 1
UVs
A UV coordinates apply for texturing a triangle.
The image to use can be as small or big as you need, but only normalized between 0 and 1.
The following applies to a texture image
X = 0, Y = 0 or 0,0 is top left corner
X = 1 , Y = 0 or 1, 0 is top right corner
X = 0, Y = 1 or 0,1 is bottom left corner
X = 0, Y = 1 or 0,1 is bottom right corner
If you wish to use multiple areas of the texture to apply to different object faces inside 1 texture.
Like 4 textures in one image then think of it as a 4x4 grid each occupying exactly half top and bottom then the cords will be
First quadrant - top keft square
0,0 to .5 , .5
Second quadrant - top right square
0, .5 to 1, .5
Third quadrant - bottom left square
0, .5 to .5, 1
Forth quadrant - bottom right square
.5, .5 to 1, 1
If that makes any sense
This will do for now, I'm working on image manipulation like merging and blurring.
Merging two images together
And
Blurring with likes of mean, median, gaussian, motion which uses a convolution and these matrices which scans through the images applying the values in them to produce the blurring.
I'll add more as and when I think if any that may help achieve our programming problems
That's all for now to keep us going for a while and hope they are useful and indeed work as I dont have AGk at moment