-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmesh.m
More file actions
151 lines (128 loc) · 5.05 KB
/
mesh.m
File metadata and controls
151 lines (128 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
classdef mesh < handle
% Creates buffers for vertices, normals, colors, indices
% Right-handed coordinate system!
properties
name char
vertices
verts1
verts2
verts3
indices
colors
normals
material material = lambertian([0.5, 0.5, 0.5])
rotation = eye(3)
scale = ones(1,3)
translation = zeros(1,3)
end
methods (Access = public)
function obj = mesh(meshName, transform)
%mesh Construct an instance of this class
obj.name = meshName;
% read transform
if isfield(transform, 'rotation')
obj.rotation = obj.createRotationMatrix(obj, transform.rotation);
end
if isfield(transform, 'scale')
obj.scale = transform.scale;
end
if isfield(transform, 'translation')
obj.translation = transform.translation;
end
% create buffers
if (strcmp(meshName, 'Single Triangle'))
createSingleTriangle(obj);
elseif (strcmp(meshName, 'Sphere'))
createSphere(obj);
elseif (strcmp(meshName, 'Plane'))
createPlane(obj);
else
error('Invalid mesh name!');
end
% apply transform
obj.applyTransform();
obj.verts1 = obj.vertices(obj.indices(:,1),:);
obj.verts2 = obj.vertices(obj.indices(:,2),:);
obj.verts3 = obj.vertices(obj.indices(:,3),:);
end
end
methods (Access = private)
function createSingleTriangle(obj)
obj.vertices = [-0.5, 0, 0;...
0.5, 0, 0;...
0.0, 1, 0];
obj.indices = [1, 2, 3];
obj.colors = [1, 0, 0;...
0, 1, 0;...
0, 0, 1];
obj.normals = [0,0,1; 0,0,1; 0,0,1];
end
function createPlane(obj)
obj.vertices = [ 1,0, 1;...
1,0,-1;...
-1,0,-1;...
-1,0, 1];
obj.indices = [1,2,3;1,3,4];
obj.colors = zeros(4,3) + [0.5,0.5,0.5];
obj.normals = zeros(4,3) + [0,1,0];
end
function createSphere(obj)
phiRes = 16;
thetaRes = 16;
radius = 1;
nbrVert = phiRes * thetaRes;
obj.vertices = zeros(nbrVert,3);
obj.colors = rand(nbrVert,3);
obj.colors((end-phiRes+1):end, :) = obj.colors(1:phiRes,:);
obj.normals = zeros(nbrVert,3);
theta = 0.0;
dTheta = 2*pi/(thetaRes - 1);
phi = 0.0;
dPhi = pi/(phiRes - 1);
% create vertices and normals
index = 1;
for i=1:thetaRes
cos_theta = cos(theta);
sin_theta = sin(theta);
phi = 0.0;
for j=1:phiRes
cos_phi = cos(phi);
sin_phi = sin(phi);
obj.vertices(index,:) = [radius * sin_theta * sin_phi,...
-radius * cos_phi,...
radius * cos_theta * sin_phi];
obj.normals(index,:) = [sin_theta*sin_phi,...
-cos_phi,...
cos_theta*sin_phi]/(radius*radius);
phi = phi + dPhi;
index = index + 1;
end
theta = theta + dTheta;
end
% create indices
index = 1;
for i=0:(thetaRes-2)
for j=0:(phiRes-2)
obj.indices(index,:) = [phiRes*i + j,...
phiRes*i + j + 1 + phiRes,...
phiRes*i + j + 1] + 1;
index = index + 1;
obj.indices(index,:) = [phiRes*i + j,...
phiRes*i + j + phiRes...
phiRes*i + j + phiRes + 1] + 1;
index = index + 1;
end
end
obj.colors = obj.normals*0.5 + 0.5;
end
%% help functions
function rotation = createRotationMatrix(obj, rot)
rotation = rotx(rot(1))' * roty(rot(2))' * rotz(rot(3))';
end
function applyTransform(obj)
obj.vertices = obj.vertices*diag(obj.scale)*obj.rotation + obj.translation;
obj.normals = obj.normals*diag(1./obj.scale)*obj.rotation;
obj.normals = obj.normals./sqrt(sum(obj.normals.*obj.normals,2));% normalise
end
end
end