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
| #include"Triangle.hpp" #include<algorithm> #include<array>
//构造函数的实现 Triangle::Triangle() { v[0] << 0, 0, 0, 1; v[1] << 0, 0, 0, 1; v[2] << 0, 0, 0, 1; color[0] << 0.0, 0.0, 0.0; color[1] << 0.0, 0.0, 0.0; color[2] << 0.0, 0.0, 0.0; tex_coords[0] << 0.0, 0.0; tex_coords[1] << 0.0, 0.0; tex_coords[2] << 0.0, 0.0; }
//设置指定索引的纹理 void Triangle::setTexCoord(int ind, Vector2f uv) { tex_coords[ind] = uv; } //设置顶点 void Triangle::setVertex(int ind, Vector4f ver) { v[ind] = ver; } //设置法线 void Triangle::setNormal(int ind, Vector3f n) { normal[ind] = n; } //设置颜色 void Triangle::setColor(int ind, float r, float g, float b) { if ((r < 0.0) || (r > 255.) || (g < 0.0) || (g > 255.) || (b < 0.0) || (b > 255.)) { fprintf(stderr, "ERROR! Invalid color values"); fflush(stderr); exit(-1); }
color[ind] = Vector3f((float)r / 255., (float)g / 255., (float)b / 255.); return; } //将顶点坐标转换为标准的四维齐次坐标 std::array<Vector4f, 3> Triangle::toVector4() const { std::array<Vector4f, 3> res; std::transform(std::begin(v), std::end(v), res.begin(), [](auto& vec) { return Vector4f(vec.x(), vec.y(), vec.z(), 1.f); }); return res; } void Triangle::setNormals(const std::array<Vector3f, 3>& normals) { // 设置法向量 normal[0] = normals[0]; normal[1] = normals[1]; normal[2] = normals[2]; }
void Triangle::setColors(const std::array<Vector3f, 3>& colors) { // 设置颜色 auto first_color = colors[0]; setColor(0, colors[0][0], colors[0][1], colors[0][2]); setColor(1, colors[1][0], colors[1][1], colors[1][2]); setColor(2, colors[2][0], colors[2][1], colors[2][2]); }
|