コード例 #1
0
ファイル: Mesh.hpp プロジェクト: fracture91/graphics-hw2
		// add 3 identical normal vectors to normals array using newell method
		// also add line segments to normalLines
		void addNormal(vec4 a, vec4 b, vec4 c) {
			vec4 normal(0, 0, 0, 0);
			// newell method
			vec4 verts[3] = {a, b, c};
			for(int i = 0; i < 3; i++) {
				vec4 current = verts[i];
				vec4 next = verts[(i + 1) % 3];
				normal.x += (current.y - next.y)*(current.z + next.z);
				normal.y += (current.z - next.z)*(current.x + next.x);
				normal.z += (current.x - next.x)*(current.y + next.y);
			}
			normal = normalize(normal);
			normals[pointIndex] = normals[pointIndex + 1] = normals[pointIndex + 2] = normal;
			
			// add a line to normalLines by finding center of face,
			// adding a line through center along normal extending out by maxSize
			if(maxSize == 0) {
				maxSize = box->getMaxSize();
			}
			vec4 center = (a + b + c) / 3;
			normalLines[lineIndex] = center;
			normalLines[++lineIndex] = center + (maxSize/20 * normal);
			lineIndex++;
		}