Hi,
I tried to make box model out of 6 planes. The idea is to encapsulate 6 planes in parent object and make some method to change material of individual plane. So far box is created but it seams `HYP_Object.AddChild` is not enough to make a composite object.
here is test code:
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<geexlab>
<scene name="test" show_ref_grid="TRUE" display_fps="TRUE">
<window_size width="960" height="640" width_offset="30" height_offset="50" fullscreen="FALSE" />
<ref_grid_color r="0.423" g="0.423" b="1.0" />
</scene>
<camera name="myCamera" fov="60.0" navigation_mode="EXAMINE" near_plane="1.0" far_plane="40000.0" inertia="FALSE">
<lookat x="0.0" y="0.0" z="0.0" w="1.0" />
<position x="0.0" y="31.0" z="-15.0" />
<bkg_color r="0.0" g="0.0" b="0.0" />
</camera>
<light name="myLight">
<ambient r="0.2" g="0.2" b="0.2" a="1.0" />
<specular r="0.1" g="0.1" b="0.1" a="1.0" />
<orientation pitch="-0.7" yaw="-0.70" roll="-0.70" />
<position x="1.0" y="40.0" z="1.0" />
<diffuse r="2.0" g="2.0" b="2.0" a="1.0" />
</light>
<texture name="imageJPG" filename="room.jpg" pixel_format="BGR" />
<material name="constuctionPlaneMat" >
<add_texture name="imageJPG" />
</material>
<!--
<mesh name="plane1" shape_type="PLANE" render="TRUE" lighting="FALSE"
back_face_culling="FALSE" >
<plane x_size="150.0" z_size="128.0" />
<position x="0.0" y="0.0" z="0.0" />
<orientation pitch="90.0" />
<attach_material name="constuctionPlaneMat" />
</mesh>
-->
<script name="logging.lua" filename="logging.lua" run_mode="INIT" />
<script name="class_structure.lua" filename="class_structure.lua" run_mode="INIT" />
<script name="box.lua" filename="box.lua" run_mode="INIT" />
<script name="test_init.lua" filename="test_init.lua" run_mode="INIT" />
<!--
<script name="update_frame.lua" filename="mainloop.lua" run_mode="FRAME" />
-->
</geexlab>
box.lua
CustomBox = GenericClass:new()
function CustomBox.new(self, width, height, depth)
log( " Creating new box ..."..width..', '..height..', '..depth )
local new = getmetatable(self).new(self)
if not depth then depth=0 end
if not height then height=0 end
if not width then width=0 end
new.width, new.height, new.depth = width, height, depth
-- replacing this call:
-- new.id = HYP_Mesh.CreateBox(width, height, depth, 1, 1, 1)
-- creating 6 planes to represent a box
local material = HYP_Object.GetId("constuctionPlaneMat")
new.sides = {}
new.sides[0] = new:CreatePlane(height, depth, 1, 1, CustomBox.PLANE_TYPE_YZ, material)
new.sides[1] = new:CreatePlane(height, depth, 1, 1, CustomBox.PLANE_TYPE_YZ, material)
HYP_Object.SetPosition(new.sides[0], width/2, 0, 0)
HYP_Object.SetPosition(new.sides[1], -width/2, 0, 0)
new.sides[2] = new:CreatePlane(width, depth, 1, 1, CustomBox.PLANE_TYPE_XZ, material)
new.sides[3] = new:CreatePlane(width, depth, 1, 1, CustomBox.PLANE_TYPE_XZ, material)
HYP_Object.SetPosition(new.sides[2], 0, height/2, 0)
HYP_Object.SetPosition(new.sides[3], 0, -height/2, 0)
new.sides[4] = new:CreatePlane(width, height, 1, 1, CustomBox.PLANE_TYPE_XY, material)
new.sides[5] = new:CreatePlane(width, height, 1, 1, CustomBox.PLANE_TYPE_XY, material)
HYP_Object.SetPosition(new.sides[4], 0, 0, depth/2)
HYP_Object.SetPosition(new.sides[5], 0, 0, -depth/2)
log( " Making box out of planes ..." )
-- reparent sides to form a box model
-- There is no method to create empty model obj. to hold this meshes, will try with gizmo ;)
local parent = HYP_Object.CreateGizmo()
for index = 0, 6 do
HYP_Object.AddChild( parent, new.sides[index] )
end
new.model = parent
return new
end
CustomBox.PLANE_TYPE_XZ = 0
CustomBox.PLANE_TYPE_XY = 1
CustomBox.PLANE_TYPE_YZ = 2
-- somehow HYP_Mesh.CreatePlane`s fifth parameter doesnot work ... so here we make a litle zbudz
function CustomBox.CreatePlane(self, w, h, w_segs, h_segs, plane_type, material)
local ret = HYP_Mesh.CreatePlane(w, h, w_segs, h_segs, PLANE_XZ)
log("new plane: "..w..', '..h..', '..plane_type)
log("mat id: ".. tostring(material))
HYP_Object.ClearAllMaterials(ret)
if material then HYP_Object.SetMaterial(ret, material) end
if plane_type == CustomBox.PLANE_TYPE_XY then
log("rotate about X")
HYP_Object.SetPitch(ret, 90, 2)
elseif plane_type == CustomBox.PLANE_TYPE_YZ then
log("rotate about Z")
HYP_Object.SetRoll(ret, 90, 2)
end
return ret
end
test_init.lua
our_box = CustomBox:new(1, 3, 7)
HYP_Object.SetPosition(our_box.model, 10, 0, 0)
`HYP_Object.SetPosition(our_box.model, 10, 0, 0)` is intended to move the whole box to new position, but i didn`t reparent those planes properly ...
logging.lua
LOG_USE_FILE = false
if LOG_USE_FILE == true then
local f = assert(io.open("app.log", "ab"))
f:write('\n\n\n\n\n\n\n\n\n\n\n\n\n logging started ..............................\n')
f:flush()
end
function log(msg)
HYP_Debug.Trace(msg)
if LOG_USE_FILE == true then
f:write(msg .. '\n')
f:flush()
end
end
class_structure.lua
GenericClass = {
new = function(self, obj)
local obj = obj or {}
setmetatable(obj, self)
self.__index = self
return obj
end
}
-- return a copy of the table t
function Clone(t)
local new = {}
local i, v = next(t, nil)
while i do
new[i] = v
i, v = next(t, i)
end
setmetatable(new, getmetatable(t) or {})
return new
end
-- return a copy of the table t
function DeepClone(t)
local new = {} -- create a new table
local i, v = next(t, nil) -- i is an index of t, v = t[i]
while i do
if type(v) == "table" then v = DeepClone(v) end
new[i] = v
i, v = next(t, i) -- get next index
end
setmetatable(new, getmetatable(t) or {})
return new
end