I want to write a MeshDropper Program in Tier 2 / C++
but I have not written 3D code for a while so I made the first test with Tier 1
and thought this could help beginners to start such a program itself.
It's not a real MeshDropper, you can only drop a random colored box and cylinder here, it is for demonstration how it works.
If you want a full Meshdropper expand itself, do not wait if I made my Tier 2 MeshDropper ready.
I have not that much time this year, so the project may fall in a deep sleep.
// Following 3 lines only for AppGameKitStudio, uncomment one.
// #renderer "Basic" // This will choose the OpenGL renderer
// #renderer "Advanced" // This will choose Vulkan
// #renderer "Prefer Best" // This will use the default system which will first try to use Vulkan and if that's not possible on the hardware it will fall back to OpenGL
// Project: MeshDropperEasy
// A small demo how a meshdropper start could be made only with cubes and cylinder for demonstration, no texture only random colors.
// It is much to do to get a full MeshDropper / Worldbuilder
//-----------------------------------------------------------
// as example what is to do:
// Change Arrays to have dynamic Arrays (1 to .... )
// Expand to loading mesh from file and texture, shader, terrain, skybox and so on ....
// Expand to scale each axis separately and rotating each axis too.
// Expand the save and load file routine.
// And much more can be done.
// Or write the whole thing with C++ / Tier2 as I would do in future.
// But don't wait, I can not say if I have time do make it ready.
// Created: 10/August/2019 by Zuchini from https://forum.thegamecreators.com/
// show all errors
SetErrorMode(2)
// set window properties
maxH = GetMaxDeviceHeight()
maxW = GetMaxDeviceWidth()
mxH = maxH/2
mxW = maxW/2
SetWindowTitle( "MeshDropper-Easy Demo --- Public Domain" )
SetWindowSize( maxW, maxH, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
MaximizeWindow()
// set display properties
SetVirtualResolution( maxW, maxH ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 60, 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
SetPrintSize( 18 )
SetPrintColor( 0,0,0 )
SetGenerateMipmaps( 1 )
SetClearColor( 128, 140, 150 )
// position and orientate camera
SetCameraPosition(1,0,20,-10)
SetCameraLookAt(1,0,20,0,0)
SetAmbientColor( 100, 100, 100 ) // Ambient Color for Shadow color
// Start with random seed number for colors
SetRandomSeed( GetMilliseconds() )
// Not needed for the MeshDropper-Editor, only for Demo.
// Start Demo Objects --------------------------------------
// Test-Object Plane
plane1 = CreateObjectPlane( 500, 500 )
SetObjectRotation(plane1,90,0,0)
SetObjectColor( plane1, 80, 90, 120, 0 )
// Test-Object Box
box = CreateObjectBox( 20, 20, 20 )
//SetObjectTransparency(box,1)
SetObjectColor( box, 120, 80, 110, 255)
SetObjectPosition( box, 0, 10, 50 )
SetObjectCastShadow( box, 1 )
shmap = 3
SetShadowMappingMode( shmap )
//---- End Demo Objects -------------------------------
//MarkerBox --- for ObjectRayCast between the two boxes, set the mbox1 invisible it follows the camera.
// mbox2 is a child of mbox1 and rotates with mbox1 and mbox1 rotates with the camera. So mbox2 is like a child of the camera.
mBox1 = CreateObjectBox( 0.1,0.1,0.1 )
mBox2 = CreateObjectBox( 1,1,1 )
SetObjectColor( mBox1, 255, 255, 255, 255 )
SetObjectColor( mBox2, 0, 0, 255, 128 )
SetObjectPosition( mBox1, 0,0,0)
SetObjectPosition( mBox2, 0,0,0)
FixObjectToObject(mBox2, mBox1)
MoveObjectLocalZ(mBox2, 400) // how far the child mbox2 is away from the camera
SetObjectPosition( mBox1, GetCameraX( 1 ), GetCameraY( 1 ), GetCameraZ( 1 ) )
SetObjectVisible( mBox1, 0 )
// Marker bubble
marker = CreateObjectSphere( 1, 12, 12 )
SetObjectTransparency(marker,1)
SetObjectColor( marker, 255, 0, 255, 128 )
// Types
Type PlaceObj
objType as integer // 1=box 2 =cylinder
id as integer
// shadow mode
sh as integer
// position
x as float
y as float
z as float
// angle
xa as float
ya as float
za as float
// scale
xs as float
ys as float
zs as float
// color
r as integer
g as integer
b as integer
EndType
// Some Globals
Global TheObject as PlaceObj[9] // For the Demo only 10 objects (0 to 9)
Global scale as float
global actNum as integer // counts how much objects are used
actNum = -1
global actualObj as integer // the actual object id
global helpon as integer
helpon = 0
// Main-Loop
// =================================================================================
While GetRawKeyPressed(27) <> 1
Gosub Mouse // Mouse movement
Gosub Keys // Keys with the working stuff
Gosub MarkPosition // Move the Marker-Bubble around
if helpon = 1
Print( "F1 = toggle Help" )
Print("Keys W,A,S,D and mouse move = move Camera / + Shift to move faster / Mouse-Scrollwheel to move forward and backward")
Print("Press Key 1 to set a box with size 1,1,1 at actual marker position")
Print("Press Key 2 to set a cylinder with size 1 and 12 segments at actual marker position")
Print("With + and - scale the last object")
Print("With cursor Keys move the last object on floor, with Shift and up/down move the last object up and down")
Print("With PageUp and PageDown rotate the last object")
Print("With Key C give the last object a new color")
Print("With F5 save the Objects, with F6 load objects from another session, you must first close the Programm, a reset is not included.")
Print("Feel free to add new features.")
else
if actualObj > 0
Print( "F1 = toggle Help" )
Print( "Y-Rotation: " + str(GetObjectAngleY(actualObj),2) )
else
Print( "F1 = toggle Help" )
endif
endif
Sync()
EndWhile
End
// ==================================================================================
//______________ Subroutines_______________________________________
Mouse:
mx2 = GetRawMouseX()
my2 = GetRawMouseY()
mx = mx2-mx1
my = my2-my1
SetRawMousePosition( mxW, mxH )
mx1 = GetRawMouseX()
my1 = GetRawMouseY()
RotateCameraGlobalY( 1, mx/20.0 )
RotateCameraLocalX( 1, my/20.0 )
mwd = GetRawMouseWheelDelta()
if mwd <> 0
if GetRawKeyState(16) // Key Shift
MoveCameraLocalZ( 1, mwd * 25.0 )
else
MoveCameraLocalZ( 1, mwd * 5.0 )
endif
endif
return
Keys:
if GetRawKeyState(16) // Key shift fast camera movement
If GetRawKeyState(87) //Key W
MoveCameraLocalZ( 1, 10 )
endif
If GetRawKeyState(83) //Key S
MoveCameraLocalZ( 1, -10 )
endif
If GetRawKeyState(65) //Key A
MoveCameraLocalX( 1, -10 )
endif
If GetRawKeyState(68) //Key D
MoveCameraLocalX( 1, 10 )
endif
if GetRawKeyState(38) // Key up --- Move Object up
if actualObj <> 0
TheObject[actNum].y = TheObject[actNum].y + 0.05 // z position
SetObjectPosition( actualObj, TheObject[actNum].x, TheObject[actNum].y, TheObject[actNum].z) // store z position
endif
endif
if GetRawKeyState(40) // Key down --- Move Object down
if actualObj <> 0
TheObject[actNum].y = TheObject[actNum].y - 0.05 // z position
SetObjectPosition( actualObj, TheObject[actNum].x, TheObject[actNum].y, TheObject[actNum].z) // store z position
endif
endif
elseif GetRawkeyState(17) or GetRawkeyState(259) or GetRawkeyState(260) // Key CTRL
If GetRawkeyPressed(75) // Key K
shmap = shmap + 1
if shmap = 5 then shmap = 0
SetShadowMappingMode( shmap ) // change shadow mapping mode
endif
else
// normal camera movement
If GetRawKeyState(87) //Key W
MoveCameraLocalZ( 1, 1 )
endif
If GetRawKeyState(83) //Key S
MoveCameraLocalZ( 1, -1 )
endif
If GetRawKeyState(65) //Key A
MoveCameraLocalX( 1, -1 )
endif
If GetRawKeyState(68) //Key D
MoveCameraLocalX( 1, 1 )
endif
if GetRawKeyReleased(67) // Key C
// Change Color
colR = Random( 50, 255 )
colG = Random( 50, 255 )
colB = Random( 50, 255 )
if actualObj <> 0
SetObjectColor( actualObj, colR, colG, colB, 255 )
TheObject[actNum].r = colR
TheObject[actNum].g = colG
TheObject[actNum].b = colB
endif
endif
if GetRawKeyReleased(48) // Key 0
placeObj = 0 // Object nothing = marker only
actualObj = 0
endif
if GetRawKeyReleased(49) // Key 1 --- insert a box
if actNum < 9
inc actNum
TheObject[actNum].id = CreateObjectBox( 1, 1, 1 )
actualObj = TheObject[actNum].id
SetObjectPosition( actualObj, markRayX, markRayY, markRayZ)
TheObject[actNum].x = markRayX // x position
TheObject[actNum].y = markRayY // y position
TheObject[actNum].z = markRayZ // z position
TheObject[actNum].objType = 1 // Object = Box
scale = 1.0
TheObject[actNum].xs = scale // x position
TheObject[actNum].ys = scale // y position
TheObject[actNum].zs = scale // z position
TheObject[actNum].xa = 0 // x rotation
TheObject[actNum].ya = 0 // y rotation
TheObject[actNum].za = 0 // z rotation
colR = Random( 50, 255 )
colG = Random( 50, 255 )
colB = Random( 50, 255 )
SetObjectColor( actualObj, colR, colG, colB, 255 )
TheObject[actNum].r = colR
TheObject[actNum].g = colG
TheObject[actNum].b = colB
endif
endif
if GetRawKeyReleased(50) // Key 2 --- insert a cylinder
if actNum < 9
inc actNum
TheObject[actNum].id = CreateObjectCylinder( 1, 1, 12 )
actualObj = TheObject[actNum].id
SetObjectPosition( actualObj, markRayX, markRayY, markRayZ)
TheObject[actNum].x = markRayX // x position
TheObject[actNum].y = markRayY // y position
TheObject[actNum].z = markRayZ // z position
TheObject[actNum].objType = 2 // Object = Cylinder
scale = 1.0
TheObject[actNum].xs = scale // x position
TheObject[actNum].ys = scale // y position
TheObject[actNum].zs = scale // z position
TheObject[actNum].xa = 0 // x rotation
TheObject[actNum].ya = 0 // y rotation
TheObject[actNum].za = 0 // z rotation
colR = Random( 50, 255 )
colG = Random( 50, 255 )
colB = Random( 50, 255 )
SetObjectColor( actualObj, colR, colG, colB, 255 )
TheObject[actNum].r = colR
TheObject[actNum].g = colG
TheObject[actNum].b = colB
endif
endif
// scaling the whole object for demo
if GetRawKeyState(107) // Key +
scale = scale * 1.01
if actualObj <> 0
SetObjectScale( actualObj, scale, scale, scale )
TheObject[actNum].xs = scale // x position
TheObject[actNum].ys = scale // y position
TheObject[actNum].zs = scale // z position
endif
endif
if GetRawKeyState(109) // Key -
scale = scale / 1.01
if actualObj <> 0
SetObjectScale( actualObj, scale, scale, scale )
TheObject[actNum].xs = scale // x position
TheObject[actNum].ys = scale // y position
TheObject[actNum].zs = scale // z position
endif
endif
if GetRawKeyState(37) // Key Left --- Move Object Left
if actualObj <> 0
TheObject[actNum].x = TheObject[actNum].x - 0.05 // x position
SetObjectPosition( actualObj, TheObject[actNum].x, TheObject[actNum].y, TheObject[actNum].z) // store x position
endif
endif
if GetRawKeyState(39) // Key Right --- Move Object Right
if actualObj <> 0
TheObject[actNum].x = TheObject[actNum].x + 0.05 // x position
SetObjectPosition( actualObj, TheObject[actNum].x, TheObject[actNum].y, TheObject[actNum].z) // store x position
endif
endif
if GetRawKeyState(38) // Key up --- Move Object forward
if actualObj <> 0
TheObject[actNum].z = TheObject[actNum].z + 0.05 // z position
SetObjectPosition( actualObj, TheObject[actNum].x, TheObject[actNum].y, TheObject[actNum].z) // store z position
endif
endif
if GetRawKeyState(40) // Key down --- Move Object backward
if actualObj <> 0
TheObject[actNum].z = TheObject[actNum].z - 0.05 // z position
SetObjectPosition( actualObj, TheObject[actNum].x, TheObject[actNum].y, TheObject[actNum].z) // store z position
endif
endif
// Rotating y-axis is enough for demo
if GetRawKeyState(33) // Key pageup --- rotate Object y axis
if actualObj <> 0
RotateObjectLocalY( actualObj, 0.1 )
TheObject[actNum].ya = GetObjectAngleY(actualObj) // y rotation
endif
endif
if GetRawKeyState(34) // Key pageup --- rotate Object y axis
if actualObj <> 0
RotateObjectLocalY( actualObj, -0.1 )
TheObject[actNum].ya = GetObjectAngleY(actualObj) // y rotation
endif
endif
if GetRawKeyReleased(116) // Key F5 for Quick-Save
gosub SaveFile
endif
if GetRawKeyReleased(117) // Key F6 for Quick-Load
gosub LoadFile
endif
if GetRawKeyReleased(112) // F1 toggle Helpfile
if helpon = 0
helpon = 1
SetPrintColor( 200, 0, 0 )
elseif helpon = 1
helpon = 0
SetPrintColor( 0, 0, 0 )
endif
endif
endif
return
// Move the Marker-Bubble around
MarkPosition:
SetObjectPosition( mBox1, GetCameraX( 1 ), GetCameraY( 1 ), GetCameraZ( 1 ) )
camAX# = GetCameraAngleX( 1 )
camAY# = GetCameraAngleY( 1 )
camAZ# = GetCameraAngleZ( 1 )
SetObjectRotation( mBox1, camAX#, camAY#, camAZ# )
cursx = Get3DVectorXFromScreen( mx1, my1 )
cursy = Get3DVectorYFromScreen( mx1, my1 )
cursz = Get3DVectorZFromScreen( mx1, my1 )
// get camera position
camx = GetCameraX( 1 )
camy = GetCameraY( 1 )
camz = GetCameraZ( 1 )
markStartX = GetObjectWorldX( mBox1 )
markStartY = GetObjectWorldY( mBox1 )
markStartZ = GetObjectWorldZ( mBox1 )
markEndX = GetObjectWorldX( mBox2 )
markEndY = GetObjectWorldY( mBox2 )
markEndZ = GetObjectWorldZ( mBox2 )
object_hit = ObjectRayCast(0,camx,camy,camz,markEndX,markEndY,markEndZ)
n=GetObjectRayCastNumHits()
obj = GetObjectRayCastHitID(0)
If obj
markRayX = GetObjectRayCastX( 0 )
markRayY = GetObjectRayCastY( 0 )
markRayZ = GetObjectRayCastZ( 0 )
endif
If obj <> marker
SetObjectPosition( marker, markRayX, markRayY, markRayZ )
endif
return
SaveFile:
dataout = OpenToWrite( "MeshdropperDataFile.dat" )
For i = 0 to 9
WriteInteger( dataout, TheObject[i].objType )
WriteInteger( dataout, TheObject[i].id )
WriteInteger( dataout, TheObject[i].sh ) // unused
WriteFloat( dataout, TheObject[i].x )
WriteFloat( dataout, TheObject[i].y )
WriteFloat( dataout, TheObject[i].z )
WriteFloat( dataout, TheObject[i].xa )
WriteFloat( dataout, TheObject[i].ya )
WriteFloat( dataout, TheObject[i].za )
WriteFloat( dataout, TheObject[i].xs )
WriteFloat( dataout, TheObject[i].ys )
WriteFloat( dataout, TheObject[i].zs )
WriteInteger( dataout, TheObject[i].r )
WriteInteger( dataout, TheObject[i].g )
WriteInteger( dataout, TheObject[i].b )
next
CloseFile(dataout)
return
LoadFile:
datain = OpenToRead( "MeshdropperDataFile.dat" )
lasti = 0
if FileIsOpen( datain )
for i = 0 to 9
if TheObject[i].id <> 0 then DeleteObject( TheObject[i].id )
TheObject[i].objType = ReadInteger( datain )
TheObject[i].id = ReadInteger( datain )
TheObject[i].sh = ReadInteger( datain ) // unused
TheObject[i].x = ReadFloat( datain )
TheObject[i].y = ReadFloat( datain )
TheObject[i].z = ReadFloat( datain )
TheObject[i].xa = ReadFloat( datain )
TheObject[i].ya = ReadFloat( datain )
TheObject[i].za = ReadFloat( datain )
TheObject[i].xs = ReadFloat( datain )
TheObject[i].ys = ReadFloat( datain )
TheObject[i].zs = ReadFloat( datain )
TheObject[i].r = ReadInteger( datain )
TheObject[i].g = ReadInteger( datain )
TheObject[i].b = ReadInteger( datain )
if TheObject[i].objType = 1
TheObject[i].id = CreateObjectBox( TheObject[i].xs, TheObject[i].ys, TheObject[i].zs )
SetObjectPosition(TheObject[i].id,TheObject[i].x, TheObject[i].y, TheObject[i].z)
SetObjectRotation(TheObject[i].id,TheObject[i].xa, TheObject[i].ya, TheObject[i].za)
SetObjectColor( TheObject[i].id, TheObject[i].r, TheObject[i].g, TheObject[i].b, 255 )
elseif TheObject[i].objType = 2
TheObject[i].id = CreateObjectCylinder( 1, 1, 12 )
SetObjectPosition(TheObject[i].id,TheObject[i].x, TheObject[i].y, TheObject[i].z)
SetObjectScale(TheObject[i].id,TheObject[i].xs, TheObject[i].ys, TheObject[i].zs)
SetObjectRotation(TheObject[i].id,TheObject[i].xa, TheObject[i].ya, TheObject[i].za)
SetObjectColor( TheObject[i].id, TheObject[i].r, TheObject[i].g, TheObject[i].b, 255 )
else
if lasti = 0
lasti = i-1
endif
endif
next
CloseFile(datain)
endif
actNum = lasti
actualObj = TheObject[lasti].id
return