void drawBox(RenderingContext & rc, const Geometry::Box & box) { static Util::Reference<Mesh> mesh; if (mesh.isNull()) { VertexDescription vertexDescription; vertexDescription.appendPosition3D(); vertexDescription.appendNormalFloat(); const Geometry::Box unitBox(Geometry::Vec3(-0.5f, -0.5f, -0.5f), Geometry::Vec3(0.5f, 0.5f, 0.5f)); mesh = MeshUtils::MeshBuilder::createBox(vertexDescription, unitBox); } Geometry::Matrix4x4 matrix; matrix.translate(box.getCenter()); matrix.scale(box.getExtentX(), box.getExtentY(), box.getExtentZ()); rc.pushMatrix_modelToCamera(); rc.multMatrix_modelToCamera(matrix); rc.displayMesh(mesh.get()); rc.popMatrix_modelToCamera(); }
void drawCamera(RenderingContext & rc, const Util::Color4f & color) { static Util::Reference<Mesh> mesh; if (mesh.isNull()) { std::deque<Mesh *> meshes; std::deque<Geometry::Matrix4x4f> transformations; { VertexDescription vertexDescription; vertexDescription.appendPosition3D(); vertexDescription.appendNormalFloat(); Geometry::Box box(Geometry::Vec3f(0.0f, 0.0f, 0.1f), 0.2f, 0.5f, 0.8f); meshes.push_back(MeshUtils::MeshBuilder::createBox(vertexDescription, box)); transformations.push_back(Geometry::Matrix4x4()); } { // Lens meshes.push_back(MeshUtils::MeshBuilder::createConicalFrustum(0.1f, 0.25f, 0.2f, 16)); Geometry::Matrix4x4f mat; mat.translate(0.0f, 0.0f, -0.3f); mat.rotate_deg(90.0f, 0.0f, 1.0f, 0.0f); transformations.push_back(mat); } { // Lens cap meshes.push_back(MeshUtils::MeshBuilder::createDiscSector(0.25f, 16)); Geometry::Matrix4x4f mat; mat.translate(0.0f, 0.0f, -0.5f); mat.rotate_deg(-90.0f, 0.0f, 1.0f, 0.0f); transformations.push_back(mat); } { // First film reel meshes.push_back(MeshUtils::MeshBuilder::createConicalFrustum(0.2f, 0.2f, 0.1f, 16)); Geometry::Matrix4x4f mat; mat.translate(-0.05f, 0.45f, -0.1f); transformations.push_back(mat); } { meshes.push_back(MeshUtils::MeshBuilder::createDiscSector(0.2f, 16)); Geometry::Matrix4x4f mat; mat.translate(-0.05f, 0.45f, -0.1f); transformations.push_back(mat); } { meshes.push_back(MeshUtils::MeshBuilder::createDiscSector(0.2f, 16)); Geometry::Matrix4x4f mat; mat.translate(0.05f, 0.45f, -0.1f); mat.rotate_deg(180.0f, 0.0f, 1.0f, 0.0f); transformations.push_back(mat); } { // Second film reel meshes.push_back(MeshUtils::MeshBuilder::createConicalFrustum(0.2f, 0.2f, 0.1f, 16)); Geometry::Matrix4x4f mat; mat.translate(-0.05f, 0.45f, 0.3f); transformations.push_back(mat); } { meshes.push_back(MeshUtils::MeshBuilder::createDiscSector(0.2f, 16)); Geometry::Matrix4x4f mat; mat.translate(-0.05f, 0.45f, 0.3f); transformations.push_back(mat); } { meshes.push_back(MeshUtils::MeshBuilder::createDiscSector(0.2f, 16)); Geometry::Matrix4x4f mat; mat.translate(0.05f, 0.45f, 0.3f); mat.rotate_deg(180.0f, 0.0f, 1.0f, 0.0f); transformations.push_back(mat); } mesh = MeshUtils::combineMeshes(meshes, transformations); } rc.pushAndSetLighting(LightingParameters(false)); rc.pushAndSetColorMaterial(Util::Color4f(color)); rc.displayMesh(mesh.get()); rc.popMaterial(); rc.popLighting(); }
void drawCoordSys(RenderingContext & rc, float scale) { static Util::Reference<Mesh> arrow; static Util::Reference<Mesh> sphere; static Util::Reference<Mesh> charX; static Util::Reference<Mesh> charY; static Util::Reference<Mesh> charZ; const float radius = 0.025f; if (arrow.isNull()) { std::deque<Mesh *> meshes; std::deque<Geometry::Matrix4x4> transformations; Geometry::Matrix4x4 transform; meshes.push_back(MeshUtils::MeshBuilder::createConicalFrustum(radius, radius, 0.7f, 16)); transformations.push_back(transform); meshes.push_back(MeshUtils::MeshBuilder::createConicalFrustum(radius, 2.0f * radius, 0.01f, 16)); transform.translate(0.7f, 0.0f, 0.0f); transformations.push_back(transform); meshes.push_back(MeshUtils::MeshBuilder::createCone(2.0f * radius, 0.29f, 16)); transform.translate(0.01f, 0.0f, 0.0f); transformations.push_back(transform); arrow = MeshUtils::combineMeshes(meshes, transformations); MeshUtils::optimizeIndices(arrow.get()); while (!meshes.empty()) { delete meshes.back(); meshes.pop_back(); } } if (sphere.isNull()) { Util::Reference<Mesh> icosahedron = MeshUtils::PlatonicSolids::createIcosahedron(); sphere = MeshUtils::PlatonicSolids::createEdgeSubdivisionSphere(icosahedron.get(), 2); Geometry::Matrix4x4 transform; transform.scale(1.1f * radius); MeshUtils::transform(sphere.get()->openVertexData(), transform); } if(charX.isNull()) { std::deque<Mesh *> meshes; std::deque<Geometry::Matrix4x4> transformations; VertexDescription vertexDescription; vertexDescription.appendPosition3D(); vertexDescription.appendNormalFloat(); const Geometry::Box box(Geometry::Vec3f(0.0f, 0.0f, 0.0f), 0.02f, 0.2f, 0.05f); { meshes.push_back(MeshUtils::MeshBuilder::createBox(vertexDescription, box)); Geometry::Matrix4x4 transform; transform.translate(1.2f, 0.0f, 0.0f); transform.rotate_deg(30.0f, 0.0f, 0.0f, -1.0f); transformations.push_back(transform); } { meshes.push_back(MeshUtils::MeshBuilder::createBox(vertexDescription, box)); Geometry::Matrix4x4 transform; transform.translate(1.2f, 0.0f, 0.0f); transform.rotate_deg(-30.0f, 0.0f, 0.0f, -1.0f); transformations.push_back(transform); } charX = MeshUtils::combineMeshes(meshes, transformations); MeshUtils::optimizeIndices(charX.get()); while(!meshes.empty()) { delete meshes.back(); meshes.pop_back(); } } if(charY.isNull()) { std::deque<Mesh *> meshes; std::deque<Geometry::Matrix4x4> transformations; VertexDescription vertexDescription; vertexDescription.appendPosition3D(); vertexDescription.appendNormalFloat(); const Geometry::Box box(Geometry::Vec3f(0.0f, 0.0f, 0.0f), 0.02f, 0.1f, 0.05f); { meshes.push_back(MeshUtils::MeshBuilder::createBox(vertexDescription, box)); Geometry::Matrix4x4 transform; transform.translate(0.025f, 0.045f, 0.0f); transform.rotate_deg(30.0f, 0.0f, 0.0f, -1.0f); transformations.push_back(transform); } { meshes.push_back(MeshUtils::MeshBuilder::createBox(vertexDescription, box)); Geometry::Matrix4x4 transform; transform.translate(-0.025f, 0.045f, 0.0f); transform.rotate_deg(-30.0f, 0.0f, 0.0f, -1.0f); transformations.push_back(transform); } { meshes.push_back(MeshUtils::MeshBuilder::createBox(vertexDescription, box)); Geometry::Matrix4x4 transform; transform.translate(0.0f, -0.045f, 0.0f); transformations.push_back(transform); } charY = MeshUtils::combineMeshes(meshes, transformations); Geometry::Matrix4x4 transform; transform.translate(1.2f, 0.0f, 0.0f); transform.rotate_deg(90.0f, 0.0f, 0.0f, -1.0f); MeshUtils::transform(charY->openVertexData(), transform); MeshUtils::optimizeIndices(charY.get()); while(!meshes.empty()) { delete meshes.back(); meshes.pop_back(); } } if(charZ.isNull()) { std::deque<Mesh *> meshes; std::deque<Geometry::Matrix4x4> transformations; VertexDescription vertexDescription; vertexDescription.appendPosition3D(); vertexDescription.appendNormalFloat(); const Geometry::Box box(Geometry::Vec3f(0.0f, 0.0f, 0.0f), 0.02f, 0.1f, 0.05f); { meshes.push_back(MeshUtils::MeshBuilder::createBox(vertexDescription, box)); Geometry::Matrix4x4 transform; transform.translate(1.2f, 0.075f, 0.0f); transform.rotate_deg(90.0f, 0.0f, 0.0f, -1.0f); transformations.push_back(transform); } { meshes.push_back(MeshUtils::MeshBuilder::createBox(vertexDescription, box)); Geometry::Matrix4x4 transform; transform.translate(1.2f, 0.0f, 0.0f); transform.rotate_deg(-30.0f, 0.0f, 0.0f, -1.0f); transform.scale(1.0f, 1.6f, 1.0f); transformations.push_back(transform); } { meshes.push_back(MeshUtils::MeshBuilder::createBox(vertexDescription, box)); Geometry::Matrix4x4 transform; transform.translate(1.2f, -0.075f, 0.0f); transform.rotate_deg(-90.0f, 0.0f, 0.0f, -1.0f); transformations.push_back(transform); } charZ = MeshUtils::combineMeshes(meshes, transformations); MeshUtils::optimizeIndices(charZ.get()); while(!meshes.empty()) { delete meshes.back(); meshes.pop_back(); } } // Origin rc.pushAndSetColorMaterial(Util::ColorLibrary::WHITE); rc.displayMesh(sphere.get()); rc.popMaterial(); // X axis Geometry::Matrix4x4 transform; transform.scale(scale, 1.0f, 1.0f); rc.pushMatrix_modelToCamera(); rc.multMatrix_modelToCamera(transform); rc.pushAndSetColorMaterial(Util::ColorLibrary::RED); rc.displayMesh(arrow.get()); rc.displayMesh(charX.get()); rc.popMaterial(); rc.popMatrix_modelToCamera(); // Y axis transform.setIdentity(); transform.scale(1.0f, scale, 1.0f); transform.rotate_deg(90.0f, 0.0f, 0.0f, 1.0f); rc.pushMatrix_modelToCamera(); rc.multMatrix_modelToCamera(transform); rc.pushAndSetColorMaterial(Util::ColorLibrary::GREEN); rc.displayMesh(arrow.get()); rc.displayMesh(charY.get()); rc.popMaterial(); rc.popMatrix_modelToCamera(); // Z axis transform.setIdentity(); transform.scale(1.0f, 1.0f, scale); transform.rotate_deg(90.0f, 0.0f, -1.0f, 0.0f); rc.pushMatrix_modelToCamera(); rc.multMatrix_modelToCamera(transform); rc.pushAndSetColorMaterial(Util::ColorLibrary::BLUE); rc.displayMesh(arrow.get()); rc.displayMesh(charZ.get()); rc.popMaterial(); rc.popMatrix_modelToCamera(); }
void drawQuad(RenderingContext & rc, const Geometry::Vec3 & lowerLeft, const Geometry::Vec3 & lowerRight, const Geometry::Vec3 & upperRight, const Geometry::Vec3 & upperLeft) { static Util::Reference<Mesh> mesh; if (mesh.isNull()) { VertexDescription vertexDescription; vertexDescription.appendPosition3D(); vertexDescription.appendNormalFloat(); vertexDescription.appendTexCoord(); mesh = new Mesh(vertexDescription, 4, 6); MeshIndexData & id = mesh->openIndexData(); uint32_t * indices = id.data(); indices[0] = 0; indices[1] = 1; indices[2] = 2; indices[3] = 0; indices[4] = 2; indices[5] = 3; id.updateIndexRange(); id.markAsChanged(); } const Geometry::Vec3 edgeA = lowerRight - lowerLeft; const Geometry::Vec3 edgeB = upperLeft - lowerLeft; Geometry::Vec3 normal = edgeA.cross(edgeB); normal.normalize(); MeshVertexData & vd = mesh->openVertexData(); float * vertices = reinterpret_cast<float *> (vd.data()); // Lower left *vertices++ = lowerLeft.getX(); *vertices++ = lowerLeft.getY(); *vertices++ = lowerLeft.getZ(); *vertices++ = normal.getX(); *vertices++ = normal.getY(); *vertices++ = normal.getZ(); *vertices++ = 0.0f; *vertices++ = 0.0f; // Lower right *vertices++ = lowerRight.getX(); *vertices++ = lowerRight.getY(); *vertices++ = lowerRight.getZ(); *vertices++ = normal.getX(); *vertices++ = normal.getY(); *vertices++ = normal.getZ(); *vertices++ = 1.0f; *vertices++ = 0.0f; // Upper right *vertices++ = upperRight.getX(); *vertices++ = upperRight.getY(); *vertices++ = upperRight.getZ(); *vertices++ = normal.getX(); *vertices++ = normal.getY(); *vertices++ = normal.getZ(); *vertices++ = 1.0f; *vertices++ = 1.0f; // Upper left *vertices++ = upperLeft.getX(); *vertices++ = upperLeft.getY(); *vertices++ = upperLeft.getZ(); *vertices++ = normal.getX(); *vertices++ = normal.getY(); *vertices++ = normal.getZ(); *vertices++ = 0.0f; *vertices++ = 1.0f; vd.updateBoundingBox(); vd.markAsChanged(); rc.displayMesh(mesh.get()); }