void shape::setShape(float* pc, const polygen& p) { _polygen.clear(); polygen& py = const_cast<polygen&>(p); int n = py.size(); float x_max=py[0][point::X]; float x_min=py[0][point::X]; float y_max=py[0][point::Y]; float y_min=py[0][point::Y]; for (int i = 0; i < n; ++i) { _polygen.push_back(py[i]); _polygen[i][point::P_C] = pc[i]; if (x_min > _polygen[i][point::X]) x_min = _polygen[i][point::X]; if (x_max < _polygen[i][point::X]) x_max = _polygen[i][point::X]; if (y_min > _polygen[i][point::Y]) y_min = _polygen[i][point::Y]; if (y_max < _polygen[i][point::Y]) y_max = _polygen[i][point::Y]; } _center = point((x_min + x_max)/2, (y_min + y_max)/2); _center[point::P_C] = _attri[C_C]; float xlen = x_max - x_min, ylen = y_max - y_min; _attri[D_D] = xlen < ylen ? xlen : ylen; rasterization(); }
shape& shape::operator * (const float scale) { int n = _polygen.size(); for (int i = 0; i < n; ++i) { _polygen[i][point::X] *= scale; _polygen[i][point::Y] *= scale; _polygen[i][point::P_C] /= scale; } rasterization(); return *this; }
void Renderer3DRasterization::render() { setMaxDepth(1.0); TransformMatrix3D lookat = camera_->getLookatMatrix(); TransformMatrix3D proj = camera_->getProjectionMatrix(); TransformMatrix3D screen = camera_->getScreenMatrix(); TransformMatrix3D transform = screen * proj * lookat; std::vector<Surface3D*> list = scene_->getTriangleList(); std::cout << "Renderer3DRasterization: Found " << list.size() << " triangles" << std::endl; for (Surface3D* triangle : list) { Color color = triangle->getColor(); // color shading according to Lambert // normally we need a light source, but we use camera this time Vector3D normal = triangle->getNormal(); Vector3D center = triangle->getCenter(); // get direction to camera Vector3D d = camera_->getCenter() - center; d.normalize(); double lambert = Mathtools::dot(d, normal); color = color * (lambert < 0.0 ? 0.0 : lambert); Vector3D p0 = transform * *(triangle->getP0()); Vector3D p1 = transform * *(triangle->getP1()); Vector3D p2 = transform * *(triangle->getP2()); // in case we used perspective projection: divide by homogeneous coordinate "w" p0.homogeneousDivide(); p1.homogeneousDivide(); p2.homogeneousDivide(); Surface3D transformedTriangle(p0, p1, p2, &color, triangle->getTexture()); transformedTriangle.setTextureAnchorPoints(triangle->getT0(), triangle->getT1(), triangle->getT2()); rasterization(&transformedTriangle); } }