Download presentation
Presentation is loading. Please wait.
Published byMabel Davidson Modified over 9 years ago
1
COM 366 Reading Data
2
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 data 12 1,2 2,3 3,4 4,1 1,5 2,6 3,7 4,8 5,6 6,7 7,8 8,5 #Now the node data 8 1,-1,1 -1,-1,1 -1,1,1 1,1,1 1,-1,-1 -1,-1,-1 -1,1,-1 1,1,-1 #Data for a cube # First the link data 12 1,2 2,3 3,4 4,1 1,5 2,6 3,7 4,8 5,6 6,7 7,8 8,5 #Now the node data 8 1,-1,1 -1,-1,1 -1,1,1 1,1,1 1,-1,-1 -1,-1,-1 -1,1,-1 1,1,-1
3
COM 366 Open filename For Input As #1 Input #1, temp_line While Left$(temp_line, 1)= "#" Input #1, temp_line Wend 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
4
COM 366 Topic 6 Data structures revisited
5
COM 366 (a) (b) (c) (d) Figure 6.1 Representing Objects with polygons
6
COM 366 OBJECT Node # Co-ordinates V1V1 V2V2 V3V3 VmVm x 1,y 1,z 1 x 2,y 2,z 2 x 3,y 3,z 3 x m,y m,z m Figure 6.2 : Object Data Structure FACE LIST V 1,V 2,V 3,.... V 2,V 3,V 12,.... V 6,V 3,V 9,.... V 11,V 21,V 3,.... Face # F1F1 F2F2 F3F3 F4F4 Properties Colour, texture Appearance etc. “ “ “ Data structure for an object
7
COM 366 X Y Z CO X X Y Y Z Z H1OH1O H2OH2O GO X Y Z Figure 6.3 Local & global co-ordinates
8
COM 366 Scene Object 1Object 2 Object 3 Lighting Camera view Figure 6.4 Data structure for a scene
9
COM 366 Figure 6.5 Sweep generation of an object
10
COM 366 N = 6 N = 30 Axis of symmetry
11
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;
12
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
13
COM 366 A BX Figure 6.3 Constructive solid geometry - 1
14
COM 366 Constructive solid geometry - 2 A B X Figure 6.4
15
COM 366 Constructive solid geometry - 3 A B A - B Figure 6.5 B - A
16
COM 366 [X,Y,Z] = cylinder [X,Y,Z] = cylinder(r) [X,Y,Z] = cylinder(r,n) Three forms of the cylinder function
17
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 cone if nargin < 3, L = 1; end if nargin < 2, h = L; end if nargin == 0, R = 1; end stepsize = h/10; t = 0:stepsize:h; [X,Y,Z] = cylinder((R*(1-t/L))); When t = 0 then r = R When t = L then r = 0 Generating a cone
18
COM 366 h = L h < L Examples of cone
19
COM 366 Superquadratics Example the super ellipse m = 1 gives a normal ellipse m >1 gives a cusped figure m < 1 gives a ‘fatter figure m = 0 gives a rectangle
20
COM 366 Superquadratics in 3D (superquad)
21
COM 366 Using Fractals
22
COM 366 The Koch Curve N=0 N=1 N=2 N=5
23
COM 366 Length of the Koch Curve N =123 ……………m
24
COM 366 Dimension of the Koch Curve Definition of space dimension If 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 = Nr D 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
25
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 BB BB A A A A A A AAA AAA
26
COM 366 Change the angle between turns and introduce () to mean branch right and we start to Get more interesting shapes
27
COM 366 Use curved segments and colour Finally add some random variation in angles and lengths Making ‘graftals’ realistic
28
COM 366 Modelling mountains 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 12 In three dimensions the concept is exactly the same but using a triangle or pyramid as a starting point
29
COM 366 Add some colour and and shading !
30
COM 366 Topic 7 Hidden Surface Removal
32
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
34
N L Towards the viewer
35
COM 366 Criteria of Visibility The polygon surface is visible if 0 90 o or 270 o 360 o Or in terms of the angle cosine Cos( ) 0
36
COM 366 L = (L x,L y,L z ) Numerical evaluation of the visibility condition Viewing direction N = (N x,N y,N z ) Surface normal If these two vectors are of unit length then |L| = |N| = 1 When viewing along the z axis L reduces to (0,0,-1)
37
COM 366 Programming the visibility criterion My recommendation is that you add all the normals to your data structure (N X,N Y,N Z ) and then rotate them with the nodes before applying the visibility criterion
38
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.
42
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 ?
43
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.
45
COM 366 For all pixels Set pixel_colour = background Set pixel_depth = maximum_value End 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
46
COM 366
47
Type Vector x as single y as single z as single End type Type Vector x as single y as single z as single End type Type Polygon N as vector CR as integer CG as integer CB as integer V() as integer End Type Type Polygon N as vector CR as integer CG as integer CB as integer V() 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.