State::stateResult_t SkyboxState::doEnableState(FrameContext & context, Node *, const RenderParam & rp) { if (rp.getFlag(NO_GEOMETRY)) { return State::STATE_SKIPPED; } Vec3 pos = context.getCamera()->getWorldOrigin(); Geometry::Matrix4x4 matrix; matrix.translate(pos); context.getRenderingContext().pushMatrix_modelToCamera(); context.getRenderingContext().multMatrix_modelToCamera(matrix); context.getRenderingContext().pushTexture(0); context.getRenderingContext().pushAndSetDepthBuffer(Rendering::DepthBufferParameters(true, false, Rendering::Comparison::LESS)); context.getRenderingContext().pushAndSetLighting(Rendering::LightingParameters(false)); Geometry::Box box(Geometry::Vec3(0.0f, 0.0f, 0.0f), sideLength); for (uint_fast8_t side = 0; side < 6; ++side) { if (!textures[side].isNull()) { context.getRenderingContext().setTexture(0,textures[side].get()); const Geometry::corner_t * corners = Geometry::Helper::getCornerIndices(static_cast<Geometry::side_t> (side)); // Reverse the corner order because we need the inner surface. Rendering::drawQuad(context.getRenderingContext(), box.getCorner(corners[1]), box.getCorner(corners[0]), box.getCorner(corners[3]), box.getCorner(corners[2]),Util::ColorLibrary::WHITE); } } context.getRenderingContext().popLighting(); context.getRenderingContext().popDepthBuffer(); context.getRenderingContext().popTexture(0); context.getRenderingContext().popMatrix_modelToCamera(); return State::STATE_OK; }
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 drawRect(RenderingContext & rc, const Geometry::Rect & rect) { static Util::Reference<Mesh> mesh; if (mesh.isNull()) { VertexDescription vertexDescription; vertexDescription.appendPosition2D(); mesh = new Mesh(vertexDescription, 4, 6); mesh->setDrawMode(Mesh::DRAW_TRIANGLES); MeshVertexData & vd = mesh->openVertexData(); float * vertices = reinterpret_cast<float *> (vd.data()); *vertices++ = 0.0f; // Bottom left *vertices++ = 0.0f; *vertices++ = 1.0f; // Bottom right *vertices++ = 0.0f; *vertices++ = 1.0f; // Top right *vertices++ = 1.0f; *vertices++ = 0.0f; // Top left *vertices++ = 1.0f; vd.updateBoundingBox(); vd.markAsChanged(); MeshIndexData & id = mesh->openIndexData(); uint32_t * indices = id.data(); indices[0] = 0; indices[1] = 2; indices[2] = 1; indices[3] = 0; indices[4] = 3; indices[5] = 2; id.updateIndexRange(); id.markAsChanged(); } Geometry::Matrix4x4 matrix; matrix.translate(rect.getX(), rect.getY(), 0.0f); matrix.scale(rect.getWidth(), rect.getHeight(), 1.0f); rc.pushMatrix_modelToCamera(); rc.multMatrix_modelToCamera(matrix); rc.displayMesh(mesh.get()); rc.popMatrix_modelToCamera(); }
void BoxTest::testSetters() { const Geometry::Box b1(-1.0f, 1.0f, -2.0f, 2.0f, -3.0f, 3.0f); Geometry::Box b2; b2.setMinX(-1.0f); b2.setMaxX(1.0f); b2.setMinY(-2.0f); b2.setMaxY(2.0f); b2.setMinZ(-3.0f); b2.setMaxZ(3.0f); CPPUNIT_ASSERT(b2 == b1); b2.setMin(Geometry::dimension_t::X, -1.0f); b2.setMax(Geometry::dimension_t::X, 1.0f); b2.setMin(Geometry::dimension_t::Y, -2.0f); b2.setMax(Geometry::dimension_t::Y, 2.0f); b2.setMin(Geometry::dimension_t::Z, -3.0f); b2.setMax(Geometry::dimension_t::Z, 3.0f); CPPUNIT_ASSERT(b2 == b1); b2.set(-1.0f, 1.0f, -2.0f, 2.0f, -3.0f, 3.0f); CPPUNIT_ASSERT(b2 == b1); b2 = Geometry::Box(); CPPUNIT_ASSERT(b2 == Geometry::Box()); b2 = b1; CPPUNIT_ASSERT(b2 == b1); b2.set(1.0f, 2.0f, 3.0f); b2.include(-1.0, -2.0f, -3.0f); CPPUNIT_ASSERT(b2 == b1); b2.set(1.0f, 2.0f, 3.0f); b2.include(Geometry::Vec3(-1.0, -2.0f, -3.0f)); CPPUNIT_ASSERT(b2 == b1); b2 = Geometry::Box(); b2.include(b1); CPPUNIT_ASSERT(b2 == b1); b2 = Geometry::Box(); b2.setCenter(Geometry::Vec3(0.0f, 0.0f, 0.0f)); b2.include(-1.0, -2.0f, -3.0f); b2.include(1.0, 2.0f, 3.0f); CPPUNIT_ASSERT(b2 == b1); b2 = Geometry::Box(); b2.setCenter(Geometry::Vec3(0.5f, 0.5f, 0.5f)); b2.setExtentX(1.0f); b2.setExtentY(1.0f); b2.setExtentZ(1.0f); CPPUNIT_ASSERT(b2 == Geometry::Box(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f)); b2 = Geometry::Box(); b2.setCenter(Geometry::Vec3(0.5f, 0.5f, 0.5f)); b2.setExtent(1.0f); CPPUNIT_ASSERT(b2 == Geometry::Box(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f)); b2.resizeAbs(2.0f); CPPUNIT_ASSERT(b2 == Geometry::Box(-2.0f, 3.0f, -2.0f, 3.0f, -2.0f, 3.0f)); b2.resizeRel(1.5f); CPPUNIT_ASSERT(b2 == Geometry::Box(-3.25f, 4.25f, -3.25f, 4.25f, -3.25f, 4.25f)); b2.translate(Geometry::Vec3(3.25f, -4.25f, 0.0f)); CPPUNIT_ASSERT(b2 == Geometry::Box(0.0f, 7.5f, -7.5f, 0.0f, -3.25f, 4.25f)); Geometry::Matrix4x4 mat; mat.setIdentity(); b2 = Geometry::Helper::getTransformedBox(b2, mat); CPPUNIT_ASSERT(b2 == Geometry::Box(0.0f, 7.5f, -7.5f, 0.0f, -3.25f, 4.25f)); mat.scale(2.0f); mat.translate(-3.25f, 4.25f, 0.0f); b2 = Geometry::Helper::getTransformedBox(b2, mat); CPPUNIT_ASSERT(b2 == Geometry::Box(-6.5f, 8.5f, -6.5f, 8.5f, -6.5f, 8.5f)); }
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(); }