Difference between revisions of "HowTo:Use constructive solid geometry"
(18 intermediate revisions by 2 users not shown) | |||
Line 6: | Line 6: | ||
To declare a union object: | To declare a union object: | ||
− | + | <source lang="pov">#include "colors.inc" | |
− | + | camera{ | |
− | + | location <4, 4, -10> | |
− | + | look_at 0 | |
− | + | angle 36 | |
− | + | } | |
− | + | light_source{ <500, 500, -1000> White } | |
− | + | plane{ y, -1.5 | |
− | + | pigment{ checker Green White } | |
− | + | } | |
− | + | union{ | |
− | + | box{ -1, 1 pigment{ Blue } } | |
− | + | sphere{ 0, 1.375 pigment{ Red } } | |
− | + | }</source> | |
Since union creates a single object from multiple ones, we can apply textures and other transformations to all objects. | Since union creates a single object from multiple ones, we can apply textures and other transformations to all objects. | ||
− | + | <source lang="pov">#include "colors.inc" | |
− | + | camera{ | |
− | + | location <4, 4, -10> | |
− | + | look_at 0 | |
− | + | angle 36 | |
− | + | } | |
− | + | light_source{ <500, 500, -1000> White } | |
− | + | plane{ y, -1.5 | |
− | + | pigment{ checker Green White } | |
− | + | } | |
− | + | union{ | |
− | + | box{ -1, 1 } | |
− | + | sphere{ 0, 1.375 } | |
− | + | pigment{ Red } | |
− | + | }</source> | |
− | The above code will give the resulting object a pigment of Red, without us having to change the Blue box to Red and then others if we decide to change the color of the whole object. | + | The above code will give the resulting object a pigment of Red, without us having to change the Blue box to Red and then others if we decide to change the color of the whole object. The resultant image, rendered with anti-aliasing, is shown below: |
+ | [[Image:Csg_union_example.png|320px|thumb|center|Union Example]] | ||
== Intersections == | == Intersections == | ||
Line 46: | Line 47: | ||
So if we take our previous code and change it around a small bit: | So if we take our previous code and change it around a small bit: | ||
− | + | <source lang="pov">#include "colors.inc" | |
− | + | camera{ | |
− | + | location <4, 4, -10> | |
− | + | look_at 0 | |
− | + | angle 36 | |
− | + | } | |
− | + | light_source{ <500, 500, -1000> White } | |
− | + | plane{ y, -1.5 | |
− | + | pigment{ checker Green White } | |
− | + | } | |
− | + | intersection{ | |
− | + | box{ -1, 1 } | |
− | + | sphere{ 0, 1.375 } | |
− | + | pigment{ Red } | |
− | + | }</source> | |
− | What you'll see is a sphere with it's sides cut off because they are outside the intersection of the box and sphere. The corners are rounded because the corners of the box are outside the sphere | + | What you'll see is a sphere with it's sides cut off because they are outside the intersection of the box and sphere. The corners are rounded because the corners of the box are outside the sphere. |
+ | |||
+ | [[Image:Csg_intersection_example.png|320px|thumb|center|Intersection Example]] | ||
== Difference == | == Difference == | ||
− | Again, as with | + | Again, as with intersection and union, difference creates a single object out of many. The difference object is created by taking the initial object in the list and cutting away the other listed objects from it. A thing to note is that any of the CSG operations can be embedded with each other: |
+ | |||
+ | <source lang="pov">#include "colors.inc" | ||
+ | camera{ | ||
+ | location <4, 4, -10> | ||
+ | look_at 0 | ||
+ | angle 36 | ||
+ | } | ||
+ | light_source{ <500, 500, -1000> White } | ||
+ | plane{ y, -1.5 | ||
+ | pigment{ checker Green White } | ||
+ | } | ||
+ | difference{ | ||
+ | intersection{ | ||
+ | box{ -1, 1 } | ||
+ | sphere{ 0, 1.375 } | ||
+ | pigment{ Red } | ||
+ | } | ||
+ | union{ | ||
+ | cylinder{ -2*y, 2*y, 0.5 } | ||
+ | cylinder{ -2*x, 2*x, 0.5 } | ||
+ | cylinder{ -2*z, 2*z, 0.5 } | ||
+ | pigment{ Blue } | ||
+ | } | ||
+ | }</source> | ||
− | + | [[Image:Csg_difference_example.png|320px|thumb|center|Difference Example]] | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
== Merge == | == Merge == | ||
Line 96: | Line 101: | ||
For example, take the following code: | For example, take the following code: | ||
− | + | <source lang="pov">#include "colors.inc" | |
− | + | #include "textures.inc" | |
− | + | camera{ | |
− | + | location <0, 2, -10> | |
− | + | look_at 1*y | |
− | + | } | |
− | + | light_source{ 1000 White } | |
− | + | plane{ y, -1 pigment{ White } } | |
− | + | union{ | |
− | + | sphere{ 0, 1 translate 0.5*x } | |
− | + | sphere{ 0, 1 translate -0.5*x } | |
− | + | texture{ Glass2 } | |
− | + | } | |
− | + | merge{ | |
− | + | sphere{ 0, 1 translate 0.5*x } | |
− | + | sphere{ 0, 1 translate -0.5*x } | |
− | + | texture{ Glass2 } | |
− | + | translate 2*y | |
− | + | }</source> | |
On the bottom pair of spheres we can still see the edges of the spheres on the inside. On the top pair we don't. That's the difference between union and merge. | On the bottom pair of spheres we can still see the edges of the spheres on the inside. On the top pair we don't. That's the difference between union and merge. | ||
+ | [[Image:Csg_merge_example.png|320px|thumb|center|Merge Example]] |
Latest revision as of 21:41, 19 July 2009
Constructive Solid Geometry
Constructive Solid Geometry (CSG) is a powerful tool to combine primitive objects to create more complex objects.
Union
The union object is a way to apply a universal group to a family of objects. Their appearance does not change at all while using this. To declare a union object:
#include "colors.inc"
camera{
location <4, 4, -10>
look_at 0
angle 36
}
light_source{ <500, 500, -1000> White }
plane{ y, -1.5
pigment{ checker Green White }
}
union{
box{ -1, 1 pigment{ Blue } }
sphere{ 0, 1.375 pigment{ Red } }
}
Since union creates a single object from multiple ones, we can apply textures and other transformations to all objects.
#include "colors.inc"
camera{
location <4, 4, -10>
look_at 0
angle 36
}
light_source{ <500, 500, -1000> White }
plane{ y, -1.5
pigment{ checker Green White }
}
union{
box{ -1, 1 }
sphere{ 0, 1.375 }
pigment{ Red }
}
The above code will give the resulting object a pigment of Red, without us having to change the Blue box to Red and then others if we decide to change the color of the whole object. The resultant image, rendered with anti-aliasing, is shown below:
Intersections
Like union, intersect creates a single object out of many, however it applies a calculation that will only result in what parts of the listed objects that intersect with each other. Intersect inherits from union so anything you can do to a union object you can do in a intersect object.
So if we take our previous code and change it around a small bit:
#include "colors.inc"
camera{
location <4, 4, -10>
look_at 0
angle 36
}
light_source{ <500, 500, -1000> White }
plane{ y, -1.5
pigment{ checker Green White }
}
intersection{
box{ -1, 1 }
sphere{ 0, 1.375 }
pigment{ Red }
}
What you'll see is a sphere with it's sides cut off because they are outside the intersection of the box and sphere. The corners are rounded because the corners of the box are outside the sphere.
Difference
Again, as with intersection and union, difference creates a single object out of many. The difference object is created by taking the initial object in the list and cutting away the other listed objects from it. A thing to note is that any of the CSG operations can be embedded with each other:
#include "colors.inc"
camera{
location <4, 4, -10>
look_at 0
angle 36
}
light_source{ <500, 500, -1000> White }
plane{ y, -1.5
pigment{ checker Green White }
}
difference{
intersection{
box{ -1, 1 }
sphere{ 0, 1.375 }
pigment{ Red }
}
union{
cylinder{ -2*y, 2*y, 0.5 }
cylinder{ -2*x, 2*x, 0.5 }
cylinder{ -2*z, 2*z, 0.5 }
pigment{ Blue }
}
}
Merge
Merge is much like union, with the main difference of it actually treats all objects as one instead of just a list of objects in one family. This is more visible when using semi-clear or fully clear objects, such as water or glass-like objects.
For example, take the following code:
#include "colors.inc"
#include "textures.inc"
camera{
location <0, 2, -10>
look_at 1*y
}
light_source{ 1000 White }
plane{ y, -1 pigment{ White } }
union{
sphere{ 0, 1 translate 0.5*x }
sphere{ 0, 1 translate -0.5*x }
texture{ Glass2 }
}
merge{
sphere{ 0, 1 translate 0.5*x }
sphere{ 0, 1 translate -0.5*x }
texture{ Glass2 }
translate 2*y
}
On the bottom pair of spheres we can still see the edges of the spheres on the inside. On the top pair we don't. That's the difference between union and merge.