https://wiki.preterhuman.net/index.php?title=Three_Dimensional_Shading_In_Computer_Graphics&feed=atom&action=historyThree Dimensional Shading In Computer Graphics - Revision history2024-03-28T14:15:00ZRevision history for this page on the wikiMediaWiki 1.35.0https://wiki.preterhuman.net/index.php?title=Three_Dimensional_Shading_In_Computer_Graphics&diff=47313&oldid=prevNetfreak at 23:21, 13 January 20222022-01-13T23:21:52Z<p></p>
<table class="diff diff-contentalign-left diff-editfont-monospace" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 23:21, 13 January 2022</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l299" >Line 299:</td>
<td colspan="2" class="diff-lineno">Line 299:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del style="font-weight: bold; text-decoration: none;">[[Category:Computing]]</del></div></td><td colspan="2"> </td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>[[Category:Graphics]]</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>[[Category:Graphics]]</div></td></tr>
</table>Netfreakhttps://wiki.preterhuman.net/index.php?title=Three_Dimensional_Shading_In_Computer_Graphics&diff=11144&oldid=prevNetfreak at 01:04, 20 August 20192019-08-20T01:04:02Z<p></p>
<table class="diff diff-contentalign-left diff-editfont-monospace" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 01:04, 20 August 2019</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l300" >Line 300:</td>
<td colspan="2" class="diff-lineno">Line 300:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>[[Category:Computing]]</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>[[Category:Computing]]</div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">[[Category:Graphics]]</ins></div></td></tr>
</table>Netfreakhttps://wiki.preterhuman.net/index.php?title=Three_Dimensional_Shading_In_Computer_Graphics&diff=10401&oldid=prevNetfreak: Created page with "<pre> By Lithium /VLA Hopefully you have read the companion document 3DROTATE.DOC, as this one will build upon the concepts presented in my attempt to teach some of the..."2019-07-23T08:09:17Z<p>Created page with "<pre> By Lithium /VLA Hopefully you have read the companion document 3DROTATE.DOC, as this one will build upon the concepts presented in my attempt to teach some of the..."</p>
<p><b>New page</b></p><div><pre><br />
By Lithium /VLA<br />
<br />
<br />
Hopefully you have read the companion document 3DROTATE.DOC, as this one<br />
will build upon the concepts presented in my attempt to teach some of the<br />
math need to make 3D graphics a reality. This file will cover such important<br />
topics as the Dot Product and how routines are best constructed for real-time <br />
3D rotations and planar shading.<br />
<br />
<br />
<br />
Our Friend, The Dot Product<br />
<br />
The Dot Product is a neat relation that will allow you to quickly find<br />
the angle between any two vectors. It's easiest to explain graphically, so<br />
I will exercise my extended-ASCII keys.<br />
<br />
<br />
Two Vectors A & B<br />
<br />
A (Xa, Ya, Za) ³A³ = û( (Xa)ý + (Ya)ý + (Za)ý )<br />
<br />
B (Xb, Yb, Zb) ³B³ = û( (Xb)ý + (Yb)ý + (Zb)ý )<br />
<br />
<br />
Where Xa, and the others correspond to some value on their respective Axis's<br />
<br />
<br />
¿A<br />
/<br />
/<br />
/<br />
/ <br />
\ é <-- Angle Theta between vector A and B<br />
\<br />
\<br />
\<br />
ÙB<br />
<br />
<br />
Cos(é) = Xa * Xb + Ya * Yb + Za * Zb<br />
ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ<br />
³A³*³B³<br />
<br />
<br />
<br />
Example:<br />
<br />
A (1,2,3) ³A³ = û( 1ý + 2ý + 3ý) = û(14) = 3.7417<br />
<br />
B (4,5,6) ³b³ = û( 4ý + 5ý + 6ý) = û(77) = 8.7750<br />
<br />
<br />
Cos(é) = 1 * 4 + 2 * 5 + 3 * 6 = 4 + 10 + 18 = 32 = 0.9746<br />
ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ ÄÄÄÄÄ<br />
(3.7417)*(8.7750) 32.8334 32.8334<br />
<br />
<br />
ArcCos (.9746) = 12.9ø <br />
<br />
<br />
So, your wondering how this revolutionizes you code, huh? Well, remember<br />
our other friend, the Normal vector? You use Normal vectors that define<br />
the directions of everything in our 3D world. Let's say that vector A was<br />
the Normal vector from my plane, and B is a vector that shows the direction<br />
that the light in my scene is pointing. If I do the Dot Product of them,<br />
you will get the angle between them, if that angle is >= 90ø and <= 270ø<br />
then no light falls on the visible surface and it doesn't need to be <br />
displayed.<br />
<br />
<br />
Also notice, the way the values of the Cosine orient themselves<br />
<br />
<br />
<br />
90ø Cos 000ø = 1<br />
Cos 090ø = 0<br />
³ Cos 180ø = -1 <br />
Negative ³ Positive Cos 270ø = 0<br />
³<br />
³<br />
180ø ÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄ 0ø An angle between a light and a plane that<br />
³ is less than 90ø or greater than 270ø will<br />
³ be visible, so you can check if the Cos(é)<br />
Negative ³ Positive is greater than 0 to see if it is visible.<br />
³<br />
³<br />
<br />
270ø<br />
<br />
<br />
How Do You Implement The Code? Easy As ã.<br />
<br />
Examples in ASM structures<br />
<br />
We will define our points like this<br />
<br />
STRUC XYZs<br />
Xpos dd ?<br />
Ypos dd ?<br />
Zpos dd ?<br />
Dist dd ? <br />
ENDS XYZs ;size is 16 bytes<br />
<br />
<br />
The X,Y,Zpos define a point in 3D space, Dist is the distance from the origin<br />
<br />
Dist = û( Xý + Yý + Zý )<br />
<br />
Precalculate these values and have them handy in your data area<br />
<br />
<br />
Our planes should look something like this<br />
<br />
STRUC PlaneSt<br />
NumPts db ? ;3 or 4<br />
NormIndex dw ?<br />
PtsIndex dw ?<br />
dw ?<br />
dw ?<br />
dw ?<br />
ENDS PlaneSt<br />
<br />
The number of points that in the plane depends on the number your fill<br />
routines can handle you must have at least 3 and more than 6 is not suggested<br />
<br />
<br />
Then we set up our data like this<br />
<br />
MaxPoints = 100<br />
MaxPlanes = 100<br />
<br />
PointList XYZs MaxPoints DUP()<br />
PlaneList PlaneSt MaxPlanes DUP()<br />
NormalList XYZs <0,0,0, 10000h> , MaxPlanes DUP()<br />
<br />
Non-ASM User Note: <br />
<br />
I set up points in a structure that had an X,Y,Z and Distance<br />
value. I set up a plane structure that had the number of points<br />
the index number of the normal vector for that plane and the index<br />
numbers for the points in the plane.<br />
<br />
The next lines set up arrays of these points in PointList, and<br />
the number of points was defined as MaxPoints. An array of planes<br />
was created as PlaneList with MaxPlanes as the total number of <br />
plane structures in the array. NormalList is an array of the vectors<br />
that are normal to the planes, one is set up initally (I'll explain <br />
that next) and then one for each possible plane is allocated.<br />
<br />
<br />
You'll notice that I defined the first Normal and then created space for <br />
the rest of the possible normals. I'll call this first normal, the <br />
Zero Normal. It will have special properties for planes that don't shade <br />
and are never hidden.<br />
<br />
<br />
<br />
Well, before I start telling all the tricks to the writing code, let me<br />
make sure a couple of points are clear.<br />
<br />
- In the 3DROTATE.DOC I said that you could set your view point on the <br />
Z-Axis and then figure out if planes were visible by the post-rotation<br />
Normal vectors, if their Z was > 0 then display, if not, don't<br />
That is an easy way to set up the data, and I didn't feel like going<br />
into the Dot Product at the time, so I generalized. So, what if you<br />
don't view your plane from the Z-Axis, the answer is you use the...<br />
<br />
Dot Product! <br />
<br />
that's right. The angle will be used now to figure whether or not to<br />
display the plane.<br />
<br />
- I have been mentioning lights and view points as vectors that I can<br />
use with the Normal vector from my plane. To work correctly, these <br />
vectors for the lights and view should point in the direction that you<br />
are looking or the direction that the light is pointing, *NOT* a vector <br />
drawn from the origin to the viewer position or light position.<br />
<br />
- True Normal vectors only state a direction, and should therefore have<br />
a unit distance of 1. This will have the advantage of simplifying the<br />
math involved to figure you values. Also, for God's sake, pre-compute<br />
your normal, don't do this everytime. Just rotate them when you do your<br />
points and that will update their direction.<br />
<br />
If the Normal's have a length of 1 then ³A³*³B³ = 1 * 1 = 1<br />
<br />
So:<br />
Cos(é) = Xa * Xb + Ya * Yb + Za * Zb<br />
ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ<br />
³A³*³B³<br />
<br />
Is Reduced To: <br />
<br />
Cos(é) = Xa * Xb + Ya * Yb + Za * Zb<br />
<br />
<br />
We eliminated a multiply and a divide! Pat yourself on the back.<br />
<br />
- You ASM users might be wondering why I defined my Zero Normal as:<br />
<0,0,0,10000h> How does 10000h = a length of 1 ?<br />
<br />
Well, this is a trick you can do in ASM, instead of using floating point<br />
values that will be slow on computers without math co-processors, we can<br />
use a double word to hold our value. The high word holds the integer<br />
value, and the low word is our decimal. You do all of your computations<br />
with the whole register, but only pull the high word when you go to <br />
display the point. So, with that under consideration, 10000h = 1.00000<br />
Not bad for integers.<br />
<br />
<br />
- How does the Zero Normal work? Since the X,Y,and Z are all 0, the<br />
Cos(é) = 0, so if you always display when Cos(é) = 0, then that plane<br />
will always be seen.<br />
<br />
<br />
So, Beyond The Babble... How To Set Up Your Code <br />
<br />
<br />
Define Data Points, Normals, and Planes<br />
Pre-Calculate as many values as possible<br />
<br />
Rotate Points and Normals<br />
<br />
Determin Visible Planes With Dot Product<br />
(Save this value if you want to shade)<br />
<br />
Sort Visible Planes Back to Front<br />
<br />
(Determin Shade From Dot Product)<br />
<br />
Clip Plane to fit scene<br />
<br />
Draw to the screen<br />
<br />
Change Angles<br />
<br />
Goto Rotation<br />
<br />
<br />
<br />
A quick way to figure out which color to shade your plane if you are<br />
using the double word values like I described before is to take the<br />
Dot Product result, it will lie between 10000h - 0h if you would like<br />
say 16 shades over the angles, then take that value and shr ,12 that will<br />
give you a value from 0h - 10h (0-16, or 17 colors) if you make 10h into<br />
0fh, add that offset to a gradient in your palette, then you will have<br />
the color to fill your polygon with.<br />
<br />
Note also that the Cosine function is weighted toward the extremes.<br />
If you want a smooth palette change as the angles change, your palette<br />
should weight the gradient accordingly.<br />
<br />
<br />
A useful little relation for depth sorting is to be able to find the<br />
center of a triangle.<br />
<br />
E The center C = (D + E + F)/3<br />
^<br />
/ \ Divide each cooridinate by (Xd + Xe + Xf)/3 = Xc<br />
/ C \ and do the same for the Y's and Z's if you <br />
/ \ choose to sort with this method. Then rotate<br />
DÄÄÄÄÄÄÄÄÄF that point and use it to depth sort the planes<br />
<br />
<br />
Phong and Goraud Shading<br />
<br />
Recently, someone asked me about the practiblity of real-time phong and<br />
goraud shading. The technique is common to ray-tracers and requires a great<br />
deal of calculation when working with individual rays cast from each pixel,<br />
but when only using this for each plane, it is possible. This type of shading<br />
involves taking into account the reduced luminousity of light as distance<br />
increases. For each light, you define a falloff value. This value should be<br />
the distance a which the light will be at full intensity. Then at 2*FallOff <br />
you will have 1/2 intensity, 3*FallOff will yeild 1/3 and so on. To implement<br />
this type of shading, you will need to determin the distance from the light<br />
to the center of the plane. If distance < FallOff, then use the normal<br />
intensity. If it is greater, divide the FallOff value by the distance. This<br />
will give you a scalar value that you can multiple by the shading color that<br />
the plane should have. Use that offset and it will be darker since it is<br />
further away from the light source.<br />
However, to determine the distance form the light to each plane, you must<br />
use a Square Root function, these are inherently slow unless you don't care<br />
about accuracy. Also, it would be difficult to notice the use of this <br />
technique unless you have a relatively small FallOff value and your objects<br />
move about in the low intesity boundries.<br />
<br />
<br />
<br />
<br />
Well, that's all that I feel like doing tonight, and besides, Star Trek is on!<br />
So, see VLA.NFO for information about contacting myself or any of the other <br />
members of VLA.<br />
<br />
Happy Coding!<br />
<br />
</pre><br />
<br />
<br />
[[Category:Computing]]</div>Netfreak