Rotation about x, y and z-axis from xy, yz and xz plane

Thread Starter

zulfi100

Joined Jun 7, 2012
656
Hi,

Algorithms are given in another book (Computer Graphics a prograaming approach: Steven Harrington):

1. Algorithm Set-Shading ON-OFF

2. Algorithm Unity (normalizes a vector and compute its length)

3. Algorithm Display-File-Enter(OP) : Routine to enter an instruction into display file

4. Calculates the shades of Polygons

5. Algorithm make-Shade (calculates and sets the shading style for a polygon)

6. Algorithm Intensity Shade Setting (enters correct interior style for shading)

7. Algorithm set-Object-Shade(Reflectivity, Refraction, Gloss): Sets object’s shading parameters

8. Algorithm Set-Light (saves light parameters)

9. Algorithm COPY-Light (update current lighting parameters)

10. Algorithm New-View3 (create a new overall viewing transformation)

11. Algorithm : Initialize(Initialize the shading parameters)

Ideas are given in the book from which we did arbitrary rotation:
Simplest is Constant Intensity Shading. Accurate is Phong Shading & Speedy is Fast Phong Shading.
All of them of transforming from 2d (I donot see reference to Z-axis in any of their equations). Last one is using following eq:
N=Ax + By +C
All the 4 ideas are taking only 6 pages.
Zulfi.
 
Last edited:

MrAl

Joined Jun 17, 2014
11,389
Hi again,

I wonder if they are for 2d then? Maybe not for 3d surfaces.
I wrote my own algorithm a while back, quite a while actually, and it works somewhat but for small angles sometimes it shades incorrectly so i thought if i found other algorithms i could try them. But mine has to be able to understand a 3d object, with three axis x,y,z, and determine which surfaces are hidden and which are in front at the very least, and possibly depth also.
I guess i am not in too much of a hurry for this though because i have so many other things that came up recently that are taking most of my time now. But if it is only 6 pages maybe you could post the shading algorithm and i'll take a look at that one to see if it looks like ti might work. But do they say it is 3d shading in the book, or just some sort of 2d shading?
Thanks :)
 

Thread Starter

zulfi100

Joined Jun 7, 2012
656
Hi,
I think its 3d. I got following from Wikipodia:
Phong shading refers to an interpolation technique for surface shading in 3D computer graphics. It is also called Phong interpolation[1] or normal-vector interpolation shading.[2]Specifically, it interpolates surface normals across rasterized polygons and computes pixel colors based on the interpolated normals and a reflection model. Phong shading may also refer to the specific combination of Phong interpolation and the Phong reflection model.



Contents
[hide]


History[edit]
Phong shading and the Phong reflection model were developed at the University of Utah by Bui Tuong Phong, who published them in his 1973 Ph.D. dissertation.[3][4] Phong's methods were considered radical at the time of their introduction, but have since become the de facto baseline shading method for many rendering applications. Phong's methods have proven popular due to their generally efficient use of computation time per rendered pixel.

Phong interpolation[edit]

Phong shading interpolation example
Phong shading improves upon Gouraud shading and provides a better approximation of the shading of a smooth surface. Phong shading assumes a smoothly varying surface normal vector.
I would try to upload it tomorrow.

Zulfi.
 

MrAl

Joined Jun 17, 2014
11,389
Hi again,

Sounds interesting. Back when i did my program there was probably little info on the web so i had to write my own stuff completely from scratch. This looks like it should help. Thanks again.
 

panic mode

Joined Oct 10, 2011
2,715
can you not combine those pages into single PDF? it is a mess trying to follow them...

also you have bitten more than you can chew... once you understand math behind transforms, shading will be straight forward.

before you do ANYTHING, you MUST have solid grasp on basic trigonometry, vectors, matrices, dot and cross product etc. then you can lookup Euler Angles and start to piece together what you really want to know.

what you are trying to do is display some object from different perspective. this involved two operations: translation and rotation.

order of translation does not matter (because you are not showing intermediate steps), you will still get to same point even if you translate in different order (though the intermediate path is different).

order of rotations however DOES matter, if out of order end position will be different.

so lets talk a bit more about rotation...
forget your texts and math, just think. suppose you have real 3D object in space, maybe a brick or chocolate bar or whatever, supposedly object has dimensions LxWxH, but then you reach it with your hand and rotate it by SOME angle about SOME axis (does not really matter which one). what do you think will happen with shape or dimensions of the object? unless it was something made out of playdough so you could deform it, its dimensions will still be the same (LxWxH) regardless what type of displacement we perform (rotation or translation).

this sounds silly but actually it is a VERY important concept... because next step will be to understand rotation matrix.

we can use any matrix to do transforms but not every matrix is a valid rotation matrix. rotation matrix must have some special properties:
- it can be seen as set of vectors (and they MUST be unit vectors regardless if you look at rows or columns of rotation matrix)
- those unit vectors form a proper Cartesian base if they are normal to each other
- all values of rotation matrix are result of Sin/Cos products, all must have values in range [-1,1]
- this can be left or right hand base but normally convention is to use right hand


so what the heck is a unit vector? and what is a vector?

vector is usually described as something with both direction and magnitude. if we look at some point in space, we can tell its position by specifying coordinates such as X,Y,Z. in real life this is cumbersome to handle (three variables instead of one) so the other more popular way is to describe that point as a vector starting from some origin and ending at point. it has length and direction. to scale vector we can use simple process where each element of vector is multiplied by some scalar 'a'.

Suppose we have vector P=<x,y,z>
length of a vector can be computed using some 2000 year old math (Pythagorean):
Length=Magnitude=|P|=sqrt(x^2+y^2+z^2)
we can change vector length by mentioned multiplication

m*P=m * <x,y,z> = <m*x, m*y, m*z>
and new vector length will be m*L because
sqrt( (mx)^2 + (my)^2 +(mz)^2)
= sqrt( m^2(x^2 +y^2 +z^2))
=m*sqrt(x^2 +y^2 +z^2)
=m*|P|

unit vector is just a vector of length 1. this is VERY important. you can think of it as a unit (that is why it is called unit vector). reason is if you do operations using vectors (rotation matrices) that are not unit vectors (norm 1) then result will not just be translated/rotated but also DISTORTED. or example if your object was not just a single point but bunch or points forming some shape (cuboid, sphere or whatever) then we do not want after rotation to see something looking like Aztec pyramid or pancake...

to get unit vector in the same direction as P, we just need to multiply it by some scalar that will change length of P to 1.

that scalar is
n=1/|P|

basically we just divide vector by its length (something MrAll somehow forgot in reply 17). ;)

next part is translation... this is simple vector addition:
OP=<x1,y2,z1>
OQ=<x2,y2,z2>
then if we take one of vectors and connect it to end of other we get
OP' = OP + OQ = OPQ = OQP = <x1+x2, y1+y2,z1+z2>

also about rotation... this is where you must be proficient with basic trigonometry.
any rotation in space can be described as a set of three basic rotations such as
Rz(A) = rotation about Z axis by angle A
Ry(B) = rotation about Y axis by angle B
Rx(C) = rotation about X axis by angle C.

there are several choices for the axes of rotation but whichever you choose, you must stick with and be consistent!!! btw. this one is used by Kuka Robots (also called ZYX) but there are ZYZ, ZXZ, ... lookup wiki Euler Angles for more details.

in 3D we are normally referring to coordinate system with three axes (X,Y and Z). unit vectors in directions of those three axes are usually called i^, j^, k^.

this means that in XYZ coordinates tips of those unit vectors are at:
i^ = <1,0,0>
j^ = <0,1,0>
k^ = <0,0,1>

suppose we have point P = <3,4,0>
we can also write it's location as
P^ = 3*i^ + 4*j^ + 0*k^

we already know that norm of any row or column of rotation matrix is 1. so lets start by observing what happens if we try to rotate i^ about Z axis by A degrees.

because we rotate about Z (or about vector k^), regardless of rotation angle A, there will never be any Z component, rotation will keep i^ in XY plane.

i^ = <1,0,0> but after rotation it will be
i^' = <cos(A),sin(A),0>
so if A=45deg, we get i^'=<0.7071,0.7071,0>
if A=270deg, we get i^'=<0,-1,0> etc.
you can do the same for any other rotation and by combining three basic rotations, you will basically arrive at rotation matrix for YOUR system...

while this is recommended exercise, you don't have to do it since this is all on Wiki or in any decent robotics or 3D graphics book.

then you do matrix multiplication of basic three rotation matrix to get one you will always use, all you need to do is feed it angle values A,B,C. note that rotation matrices are 3x3.
to get general form which includes both translation and rotation, we need one more dimension - and we are looking at homogeneous transform:
[r11 r12 r13 x]
[r21 r22 r23 y]
[r31 r32 r33 z]
[ 0 0 0 1]

notice structure: 3x3 rotation matrix, translation vector <x,y,z> and another unit vector (part if identity matrix)

btw, back to your question - how to find values of A,B,C?
that is reverses transform, it means that you know rotation matrix coefficients:
[r11 r12 r13]
[r21 r22 r23]
[r31 r32 r33]
but now need to figure out angles A,B,C that result in those coefficient values.
first you need to know your rotation matrix (this will depend on choices you made earlier).
then you need to solve system of equations using your favourite method...

for example you have determined that rotation matrix is ZYX (check http://en.wikipedia.org/wiki/Euler_angles) then...
r11 = cos(A)cos(B)
r21 = cos(B)sin(A)
r31 = -sin(B)
r12 = cos(A)sin(B)sin(C)-cos(C)sin(A)
...
r32 = cos(B)sin(C)
r33 = cos(B)cos(C)
etc.

in this case r31 looks interesting because it involves single angle, in comparison, r12 is a terrible starting point candidate...

problem here is still that there can be two solutions for this (and their periods which occur every 360deg).

fortunately both solutions are valid but... you have to be careful, which ever solution you pick - you must be consistent. you cannot pick one solution for one term and later on pick another one elsewhere.

once you know B, should be easy to compute A from r11 or r21 etc. another good starting point would be r32/r33 because that is just tan(C).
there is a post about doing same type of computation when trying to compute base (user frame) using three points in space. you may want to check it out:
http://www.robot-forum.com/robotfor...t-how-does-algorithm-works/msg67208/#msg67208

if you are just displaying objects on a screen, then angles A,B,C are generated either by AI (if video game for example) or user interaction (drag and spin field of view by mouse) in which case you only need forward transform.
 
Last edited:

Thread Starter

zulfi100

Joined Jun 7, 2012
656
Hi my friend,
I would say: "Its a great article". I have understood some points but still trying to grasp some other things. Any way thanks for providing me this interesting information.
I highly appreciate your interest in my problem.
Zulfi.
 

panic mode

Joined Oct 10, 2011
2,715
you are welcome... I don't have time to dig deeper and teach course on this but I tried to lump together something that should resemble proper steps when learning about 3D motions.

if you google homogeneous transforms you will find tons of interesting material such as
https://www.cs.iastate.edu/~cs577/handouts/homogeneous-transform.pdf

notice that chapter 5 shows you elementary rotations...

next good step is to make your own calculator that uses mentioned transforms and converts data back and forth - basically you will need this as a library in your real program... that should include primitive functions such as computing vector length, normalizing vector, dot and cross product, matrix multiplication, inverse matrix etc.

code sample in last link is in KRL (and comments are in German :p) but should still be quite readable since it is a high level language (Pascal like) and as you know, here ZYX convention is used. note that to be strictly 'proper' Euler convention, rotations must use only two axes (ZYZ for example uses only Z and Y, although Z is used twice). example and comments I posted are sometimes referred to as Tait-Bryant.
 

MrAl

Joined Jun 17, 2014
11,389
..to get unit vector in the same direction as P, we just need to multiply it by some scalar that will change length of P to 1.

that scalar is
n=1/|P|

basically we just divide vector by its length (something MrAll somehow forgot in reply 17). ;)
Hi,

Forgot? Forgot what?
If we define the unit vector already we dont need to calculate it. We may still want to calculate the length, even though we dont really have to except for comparative reasons.
But feel free to show me what you were talking about as i may not understand your point correctly yet.
 

MrAl

Joined Jun 17, 2014
11,389
Hi,

I understand completely, as sometimes the text can be quite long and depending on the time i have i may not be able to read it all but would still like to contribute to the threads. Most of the time it's interesting stuff though so i dont mind reading a little more :)
 

Thread Starter

zulfi100

Joined Jun 7, 2012
656
Hi,
Thanks everybody specially Mr. AI. Yes,
the text can be quite long and depending on the time i have i may not be able to read it all but would still like to contribute to the threads.
Mr. AI is right. This way we can get speedy replies. This problem is solved now.

Zulfi.
 
Top