Date post: | 19-Jan-2016 |
Category: |
Documents |
Upload: | mabel-davidson |
View: | 213 times |
Download: | 0 times |
COM 366
Reading Data
COM 366
Reading data from disc files
Organise the data in a logical fashion with comments as necessary.
#Data for a cube# First the link data121,22,33,44,11,52,63,74,85,66,77,88,5#Now the node data81,-1,1-1,-1,1-1,1,11,1,11,-1,-1-1,-1,-1-1,1,-11,1,-1
#Data for a cube# First the link data121,22,33,44,11,52,63,74,85,66,77,88,5#Now the node data81,-1,1-1,-1,1-1,1,11,1,11,-1,-1-1,-1,-1-1,1,-11,1,-1
COM 366
Open filename For Input As #1 Input #1, temp_line While Left$(temp_line, 1)= "#"
Input #1, temp_lineWend
num_lines = Clnt(temp_line) ReDim L(2, num_lines)
For i = 1 To num lines Input #1, L(1, i), L(2, i)
Next i input #1, temp_line While Left$(temp_line, 1)= "#"
Input #1, temp_line Wend
num_points = Clnt(temp_line) ReDim X(num_points), Y(num_points), Z(num_points) For i = 1 To num_points
Input #1, V(i).X, V(i).Y, V(i).Z Next i
Close #1
A simple procedure to read the text file
COM 366
Topic 6
Data structures revisited
COM 366
(a) (b) (c) (d)
Figure 6.1
Representing Objects with polygons
COM 366
OBJECTOBJECT
Node # Co-ordinates
V1
V2
V3
Vm
x1,y1,z1
x2,y2,z2
x3,y3,z3
xm,ym,zm
Figure 6.2 : Object Data Structure
FACE LIST
V1,V2,V3,....
V2,V3,V12,....
V6,V3,V9,....
V11,V21,V3,....
Face #
F1
F2
F3
F4
Properties
Colour, textureAppearance etc.
“
“
“
Data structure for an object
COM 366
X
Y
Z
CO
X
X
Y
Y
Z
Z
H1O
H2O
GO
X
Y
Z
Figure 6.3
Local & global co-ordinates
COM 366
Scene
Object 1 Object 2 Object 3
Lighting Cameraview
Figure 6.4
Data structure for a scene
COM 366
Figure 6.5
Sweep generation of an object
COM 366
N = 6
N = 30
Axis of symmetry
COM 366
function RevObject(n)
%To generate a 3D object from profile in 'goblet.txt'
%NOTE this version only works with 5 nodes in the profile
%First read in the data, set the number of nodes and faces
U = dlmread('goblet.txt');
[a,b] = size(U);
numnodes = a*n;
numpatch = (a-1)*n;
Node = zeros(numnodes,3);
PSet = zeros(numpatch,4);
%Now calculate all the node values
for k = 0:n-1
for L = 1:a
theta = 2*k*pi/n;
Node(L+a*k,1) = U(L,1)*cos(theta);
Node(L+a*k,2) = U(L,1)*sin(theta);
Node(L+a*k,3) = U(L,2);
end;
end;
COM 366
for k = 1:n
term = 5*(k-1);
for L = 1:4
Pset(4*(k-1)+L,:) = [(term+L) (term+L+1) (term+L+6) (term+L+5)];
end
end;
%Finally ensure that the last but one profile connects to the first
for k = numpatch-3:numpatch
Pset(k,3)=Pset(k,3)-numnodes;
Pset(k,4) = Pset(k,4) - numnodes;
end
%Uncomment the following line if you want to see the patch array
%Pset
patch('Vertices',Node,'Faces',Pset,'facevertexCdata',[0.2 0.2 1],'facecolor','flat')
axis square
rotate3d on
COM 366
A B X
Figure 6.3
Constructive solid geometry - 1
X A B
COM 366
Constructive solid geometry - 2
X A B
A B X
Figure 6.4
COM 366
X A B
Constructive solid geometry - 3
A B A - B
Figure 6.5
B - A
COM 366
[X,Y,Z] = cylinder
[X,Y,Z] = cylinder(r)
[X,Y,Z] = cylinder(r,n)
Three forms of the cylinder function
COM 366
function [X,Y,Z] = Cone(R,h,L)%To create X,Y,Z data to draw a truncated cone.%R = base radius of the cone.%h = height to which the cone is drawn%L = apex of the cone%Note setting h = L will draw the full coneif nargin < 3, L = 1; endif nargin < 2, h = L; endif nargin == 0, R = 1; endstepsize = h/10;t = 0:stepsize:h;[X,Y,Z] = cylinder((R*(1-t/L)));
L
tRr 1.
When t = 0 then r = R
When t = L then r = 0
Generating a cone
COM 366
h = L h < L
Examples of cone
COM 366
Superquadratics
Example the super ellipse
)(sin.
)(cos.
m
m
by
ax
m = 1 gives a normal ellipse
m >1 gives a cusped figure
m < 1 gives a ‘fatter figure
m = 0 gives a rectangle
COM 366
Superquadratics in 3D (superquad)
COM 366
Using Fractals
COM 366
The Koch Curve
N=0N=1
N=2 N=5
COM 366
Length of the Koch Curve
N = 1 2 3 …………… m
33.13
1.4 78.1
9
1.16 37.2
27
1.64
length then As m
mm
3
1.4
COM 366
Dimension of the Koch Curve
Definition of space dimensionIf N is the number of self similar copies required when an object is scaled by r then the
Dimension of the object ‘D’ is given by :
1 = NrD
If a line is scaled by 1/3 then 3 copies are required thus the
dimension of a line is 1 = 3(1/3)D. Therefore D = 1
If a square is scaled by 1/3 then 9 copies are required thus the
dimension of a square is 1 = 9(1/3)D. Therefore D = 2
For the Koch curve if we scale by 1/3 we require 4 sections and the dimension is
2857.1)3log(
)4log(D
COM 366
Using ‘graftals’The correct name is parallel graph grammars
Definitions (you can improvise your own)
1. A AA
2. B A[B]AA[B]
3. [ ] means ‘branch left
If we start with A we generate a dull sequence A AA AAAA etc. But lets start with B and
Record the first 3 generations :
(0) B (1) A[B]AA[B] (2) AA[A[B]AA[B]]AAAA[A[B]AA[B]]
B
B
B
A
A
A
B B
B B
A
A
A
A
A
A
AAA
AAA
COM 366
Change the angle between turns and introduce () to mean branch right and we start to
Get more interesting shapes
COM 366
Use curved segments and colour Finally add some random
variation in angles and lengths
Making ‘graftals’ realistic
COM 366
Modelling mountains
RxxPyyy
xxx
iiiinew
iinew
.2
12
1
11
1
P is a perturbation based on the length ‘s’ of the line e.g. 2-s and R is a random number between 0 and 1
0 1 2
In three dimensions the concept is exactly the same but using a triangle or pyramid as a starting point
COM 366
Add some colour and and shading !
COM 366
Topic 7
Hidden Surface Removal
COM 366
Convex Solids
A solid is convex if a straight line joining any two points on the surface passes entirely through the body of the solid or through its surface.
Examples of common convex solids include :
Sphere, cube, cone, cylinder, pyramid
All remaining solids are non convex
N
L Towards the viewer
COM 366
Criteria of Visibility
The polygon surface is visible if
0 90o or 270o 360o
Or in terms of the angle cosine Cos() 0
NL
NL
.cos
COM 366
L = (Lx,Ly,Lz)
Numerical evaluation of the visibility condition
Viewing direction
N = (Nx,Ny,Nz) Surface normal
If these two vectors are of unit length then
|L| = |N| = 1
L N L L L N N N L N L N L Nx y z x y z x x y y z z ( , , ) ( , , )
When viewing along the z axis L reduces to (0,0,-1)
cos( ) N z 0 0 or N z
COM 366
Programming the visibility criterion
My recommendation is that you add all the normals to your data structure (NX,NY,NZ) and then rotate them with the nodes before applying the visibility criterion
COM 366
Hidden Surface Removal for Non Convex Objects
The painters ( or z buffer) algorithm
If we can, in an unambiguous manner, sort the polygons into order of increasing z values then we can correctly render the object by drawing the polygons with the largest z values first. Remember that for a left handed co-ordinate system the polygons with the largest z value will be the furthest from the view point.
Test 1 : Do the X extents of P and Q not overlap ?
Test2 : Do the Y extents of P and Q not overlap ?
Test3 : Is P completely on the side of the plane of Q away from the viewer ?
Test4 : Is Q completely on the side of the plane of P nearer to the viewer ?
Test5 : Do the projections of the two polygons on the viewing plane not overlap ?
COM 366
The depth buffer method
This method like the z buffer algorithm begins with a list of polygons but makes no attempt to sort the polygons by z depth instead two pixel based buffers are created. For each pixel position we store the current pixel colour in one buffer. The second buffer contains a single z value at each pixel position representing the depth of the currently displayed pixel.
COM 366
For all pixels Set pixel_colour = background Set pixel_depth = maximum_valueEnd for all pixels
For each polygon For each projected_polygon_pixel If z_co_ord < pixel_depth then Set pixel_colour = ploygon_colour Set pixel_depth = z_co_ord Endif End for each projected_polygon_pixel
End for each polygon
COM 366
COM 366
Type Vector x as
single y as
single z as
singleEnd type
Type Vector x as
single y as
single z as
singleEnd type
Type PolygonN as vectorCR as integerCG as integerCB as integerV() as integer
End Type
Type PolygonN as vectorCR as integerCG as integerCB as integerV() as integer
End Type
GLOBAL Object() as integer ‘The array of polygon numbers
GLOBAL Poly() as Polygon ‘ The array of polygons
GLOBAL Node() as Vector ‘ The array of vertex co-ordinates
GLOBAL num_polygons, num_nodes as integer.
GLOBAL Object() as integer ‘The array of polygon numbers
GLOBAL Poly() as Polygon ‘ The array of polygons
GLOBAL Node() as Vector ‘ The array of vertex co-ordinates
GLOBAL num_polygons, num_nodes as integer.
Programming in VB