void Matrix4x4::ArbAxisRotation(Vertex &rotVert, int degrees) { float radians = degrees * (float(PI)/180); rotVert.Normalize(); float xy = rotVert._x * rotVert._y; float xz = rotVert._x * rotVert._z; float yz = rotVert._y * rotVert._z; float xSq = rotVert._x * rotVert._x; float ySq = rotVert._y * rotVert._y; float zSq = rotVert._z * rotVert._z; float oneMincos = (1-cos(radians)); Matrix4x4 matRotate; matRotate.Identity(); matRotate._11 = xSq * oneMincos + cos(radians); matRotate._12 = xy * oneMincos + rotVert._z * sin(radians); matRotate._13 = xz * oneMincos - rotVert._y * sin(radians); matRotate._21 = xy * oneMincos - rotVert._z * sin(radians); matRotate._22 = ySq * oneMincos + cos(radians); matRotate._23 = yz * oneMincos + rotVert._x * sin(radians); matRotate._31 = xz * oneMincos + rotVert._y * sin(radians); matRotate._32 = yz * oneMincos - rotVert._x * sin(radians); matRotate._33 = zSq * oneMincos + cos(radians); Transformation(matRotate); }
void Mesh::computeNormals(void) { if (normals.size() == vertices.size()) { std::cout << "Normals already exist! : " << normals.size() << "\n"; return; } clock_t begin, end; double elapsed_secs; begin = clock(); std::cout << "Calculating normals begin\n"; std::vector<Vertex> norms(vertices.size()); for (auto &t : triangles) { Vertex pa, pb, pc; Vertex diff1, diff2; Vertex trinorm; pa = vertices[t.v1]; pb = vertices[t.v2]; pc = vertices[t.v3]; diff1 = pb - pa; diff2 = pc - pa; trinorm = diff1.Cross(diff2); trinorm = trinorm.Normalize(); trinormals.push_back(trinorm); float theta1 = (pb - pa).Angle(pc - pa); float theta2 = (pa - pb).Angle(pc - pb); float theta3 = (pb - pc).Angle(pa - pc); norms[t.v1] = norms[t.v1] + trinorm * t.Area(vertices) * theta1; norms[t.v2] = norms[t.v2] + trinorm * t.Area(vertices) * theta2; norms[t.v3] = norms[t.v3] + trinorm * t.Area(vertices) * theta3; } for (auto &n : norms) { Vertex v = n.Normalize(); normals.push_back(v); } end = clock(); elapsed_secs = double(end - begin) / CLOCKS_PER_SEC; std::cout << "Calculating normals end : elapsed time: " << elapsed_secs << "\n"; }