-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working on bounding box calculation for issue #22 and briefly started…
… thinking on generating the appropriate vertex shader.
- Loading branch information
1 parent
305c311
commit ad12d56
Showing
5 changed files
with
179 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,69 @@ | ||
# Get the abstract solid model's bounding box in space (and possibly also its children's bounding boxes to get a bounding volume hierarchy) | ||
# Get the abstract solid model's bounding box in space (and possibly also its | ||
# children's bounding boxes to get a bounding volume hierarchy) | ||
# Bounding boxes are axis aligned for now for simiplicity's sake | ||
|
||
compileASMBounds = (abstractSolidModel) -> | ||
dispatch = | ||
scene: (node) -> | ||
sphere: (node) -> | ||
cylinder: (node) -> | ||
intersect: (node) -> | ||
union: (node) -> | ||
difference: (node) -> | ||
translate: (node) -> | ||
material: (node) -> | ||
# Constants (enum) | ||
INTERSECT = 0 | ||
UNION = 1 | ||
|
||
compileASMNode = (node) -> | ||
switch typeof node | ||
when 'object' | ||
if dispatch[node.type]? | ||
return dispatch[node.type] node | ||
else | ||
mecha.log "Unexpected node type '#{node.type}'." | ||
return {} | ||
else | ||
mecha.log "Unexpected node of type '#{typeof node}'." | ||
return {} | ||
if abstractSolidModel.type != 'scene' | ||
mecha.log "Expected node of type 'scene' at the root of the solid model, instead, got '#{abstractSolidModel.type}'." | ||
return | ||
preDispatch = | ||
invert: (stack, node, flags) -> | ||
flags.invert = not flags.invert | ||
union: (stack, node, flags) -> | ||
flags.composition.push UNION | ||
intersect: (stack, node, flags) -> | ||
flags.composition.push INTERSECT | ||
default: (stack, node, flags) -> | ||
return | ||
|
||
compileASMNode abstractSolidModel | ||
intersectChildren: (nodes) -> | ||
bounds = [[-Infinity, -Infinity, -Infinity], [Infinity, Infinity, Infinity]] | ||
for n in nodes | ||
bounds[0][i] = Math.max n.bounds[0][i], bounds[0][i] for i in [0..2] | ||
bounds[1][i] = Math.min n.bounds[1][i], bounds[1][i] for i in [0..2] | ||
bounds | ||
|
||
unionChildren: (nodes) -> | ||
bounds = [[Infinity, Infinity, Infinity], [-Infinity, -Infinity, -Infinity]] | ||
for n in nodes | ||
bounds[0][i] = Math.min n.bounds[0][i], bounds[0][i] for i in [0..2] | ||
bounds[1][i] = Math.max n.bounds[1][i], bounds[1][i] for i in [0..2] | ||
bounds | ||
|
||
postDispatch = | ||
invert: (stack, node, flags) -> | ||
flags.invert = not flags.invert | ||
stack[0].nodes.push node | ||
union: (stack, node, flags) -> | ||
flags.composition.pop() | ||
stack[0].nodes.push node | ||
intersect: (stack, node, flags) -> | ||
flags.composition.pop() | ||
stack[0].nodes.push node | ||
translate: (stack, node, flags) -> | ||
stack[0].nodes.push node | ||
halfspace: (stack, node, flags) -> | ||
node.bounds = [[-Infinity, -Infinity, -Infinity], [Infinity, Infinity, Infinity]] | ||
node.bounds[if flags.invert then 0 else 1][node.attr.axis] = node.attr.val | ||
stack[0].nodes.push node | ||
cylinder: (stack, node, flags) -> | ||
node.bounds = [[-node.attr.radius, -node.attr.radius, -node.attr.radius], [node.attr.radius, node.attr.radius, node.attr.radius]] | ||
node.bounds[0][node.attr.axis] = -Infinity | ||
node.bounds[1][node.attr.axis] = Infinity | ||
stack[0].nodes.push node | ||
sphere: (stack, node, flags) -> | ||
node.bounds = [[-node.attr.radius, -node.attr.radius, -node.attr.radius], [node.attr.radius, node.attr.radius, node.attr.radius]] | ||
stack[0].nodes.push node | ||
material: (stack, node, flags) -> | ||
stack[0].nodes.push node | ||
default: (stack, node, flags) -> | ||
stack[0].nodes.push node | ||
|
||
flags = | ||
invert: false | ||
composition: [UNION] | ||
result = mapASM preDispatch, postDispatch, [{nodes: []}], abstractSolidModel, flags | ||
result.flags = flags | ||
result | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.