void PeriodicExploration::update_mesh() { const size_t dim = m_vertices.cols(); const size_t num_vertices = m_vertices.rows(); const size_t num_faces = m_faces.rows(); const size_t vertex_per_face = m_faces.cols(); const size_t num_voxels = m_voxels.rows(); const size_t vertex_per_voxel = m_voxels.cols(); VectorF flattened_vertices = Eigen::Map<VectorF>(m_vertices.data(), num_vertices * dim); VectorI flattened_faces = Eigen::Map<VectorI>(m_faces.data(), num_faces * vertex_per_face); VectorI flattened_voxels = Eigen::Map<VectorI>(m_voxels.data(), num_voxels * vertex_per_voxel); VectorF face_sources = m_face_sources.cast<Float>(); MeshFactory factory; factory.load_data( flattened_vertices, flattened_faces, flattened_voxels, dim, vertex_per_face, vertex_per_voxel); m_mesh = factory.create_shared(); m_mesh->add_attribute("face_source"); m_mesh->set_attribute("face_source", face_sources); }
bool MeshResource::CreateFromDesc() { if( !m_MeshDesc.pMeshGenerator ) return false; Result::Name res = m_MeshDesc.pMeshGenerator->Generate(); if( res != Result::SUCCESS ) return false; MeshFactory factory; BasicMesh *pMeshObject = factory.LoadMeshObjectFromArchive( m_MeshDesc.pMeshGenerator->MeshArchive(), m_MeshDesc.ResourcePath, m_MeshDesc.LoadOptionFlags, m_MeshDesc.MeshType ); if( pMeshObject ) { m_pMeshObject = shared_ptr<BasicMesh>( pMeshObject ); SetState( GraphicsResourceState::LOADED ); return true; } else return false; }
bool Fluids3DWindow::CreateNestedBoxes() { std::string path = mEnvironment.GetPath("VolumeRender.hlsl"); std::shared_ptr<VisualProgram> program = mProgramFactory.CreateFromFiles(path, path, ""); if (!program) { return false; } mPVWMatrixBuffer = std::make_shared<ConstantBuffer>( sizeof(Matrix4x4<float>), true); *mPVWMatrixBuffer->Get<Matrix4x4<float>>() = Matrix4x4<float>::Identity(); mTrilinearClampSampler = std::make_shared<SamplerState>(); mTrilinearClampSampler->filter = SamplerState::MIN_L_MAG_L_MIP_P; mTrilinearClampSampler->mode[0] = SamplerState::CLAMP; mTrilinearClampSampler->mode[1] = SamplerState::CLAMP; mTrilinearClampSampler->mode[2] = SamplerState::CLAMP; program->GetVShader()->Set("PVWMatrix", mPVWMatrixBuffer); program->GetPShader()->Set("volumeTexture", mFluid.GetState()); program->GetPShader()->Set("trilinearClampSampler", mTrilinearClampSampler); std::shared_ptr<VisualEffect> effect = std::make_shared<VisualEffect>(program); struct Vertex { Vector3<float> position, tcoord; }; VertexFormat vformat; vformat.Bind(VA_POSITION, DF_R32G32B32_FLOAT, 0); vformat.Bind(VA_TEXCOORD, DF_R32G32B32_FLOAT, 0); MeshFactory mf; mf.SetVertexFormat(vformat); int const numBoxes = 128; for (int i = 1; i <= numBoxes; ++i) { float extent = 0.5f*i/(numBoxes - 1.0f); std::shared_ptr<Visual> visual(mf.CreateBox(extent, extent, extent)); VertexBuffer* vbuffer = visual->GetVertexBuffer().get(); Vertex* vertex = vbuffer->Get<Vertex>(); for (unsigned int j = 0; j < vbuffer->GetNumElements(); ++j, ++vertex) { Vector3<float>& tcd = vertex->tcoord; Vector3<float> pos = vertex->position; Vector4<float> tmp{ pos[0] + 0.5f, pos[1] + 0.5f, pos[2] + 0.5f, 0.0f }; for (int k = 0; k < 3; ++k) { tcd[k] = 0.5f*(tmp[k] + 1.0f); } } visual->SetEffect(effect); mVisible.push_back(visual); } return true; }
bool MeshResource::Create() { /* LPD3DXMESH pMesh; HRESULT hr; hr = D3DXCreateMesh( m_MeshDesc.NumIndices / 3, // DWORD NumFaces, m_MeshDesc.NumVertices, // DWORD NumVertices, 0, // DWORD Options, &(m_MeshDesc.vecVertElement[0]), // CONST LPD3DVERTEXELEMENT9 * pDeclaration, DIRECT3D9.GetDevice(), // LPDIRECT3DDEVICE9 pD3DDevice, &pMesh ); */ MeshFactory factory; m_pMeshObject = factory.CreateMesh( m_MeshDesc.MeshType ); if( !m_pMeshObject ) return false; if( m_MeshDesc.NumVertices == 0 || m_MeshDesc.NumIndices == 0 ) { // Assume that the user requested to make an empty mesh // Also, don't set the state as loaded, since it may be loaded later // from the archive. return true; } bool mesh_created = false; // if( 0 < m_MeshDesc.vecVertElement.size() ) if( false ) { // mesh_created = m_pMeshObject->CreateMesh( // m_MeshDesc.NumVertices, // m_MeshDesc.NumIndices, // 0, // m_MeshDesc.vecVertElement // ); } else { mesh_created = m_pMeshObject->CreateMesh( m_MeshDesc.NumVertices, m_MeshDesc.NumIndices, 0, m_MeshDesc.VertexFormatFlags ); } if( mesh_created ) { SetState( GraphicsResourceState::LOADED ); return true; } else return false; }
void MinimumVolumeSphere3DWindow::CreateScene() { // Create the points. std::mt19937 mte; std::uniform_real_distribution<float> rnd(-1.0f, 1.0f); for (auto& v : mVertices) { v = { rnd(mte), rnd(mte), rnd(mte) }; } VertexFormat vformat; vformat.Bind(VA_POSITION, DF_R32G32B32_FLOAT, 0); MeshFactory mf; mf.SetVertexFormat(vformat); std::shared_ptr<ConstantColorEffect> effect; for (int i = 0; i < NUM_POINTS; ++i) { mPoints[i] = mf.CreateSphere(6, 6, 0.01f); effect = std::make_shared<ConstantColorEffect>(mProgramFactory, Vector4<float>({ 0.5f, 0.5f, 0.5f, 1.0f })); mPoints[i]->SetEffect(effect); mCameraRig.Subscribe(mPoints[i]->worldTransform, effect->GetPVWMatrixConstant()); std::shared_ptr<VertexBuffer> vbuffer = mPoints[i]->GetVertexBuffer(); Vector3<float>* vertex = vbuffer->Get<Vector3<float>>(); Vector3<float> offset = mVertices[i]; for (unsigned int j = 0; j < vbuffer->GetNumElements(); ++j) { vertex[j] += offset; } } // Create the segments. std::shared_ptr<VertexBuffer> vbuffer(new VertexBuffer(vformat, 12)); vbuffer->SetUsage(Resource::DYNAMIC_UPDATE); std::shared_ptr<IndexBuffer> ibuffer(new IndexBuffer( IP_POLYSEGMENT_DISJOINT, 6)); effect = std::make_shared<ConstantColorEffect>(mProgramFactory, Vector4<float>({ 0.5f, 0.0f, 0.0f, 1.0f })); mSegments = std::make_shared<Visual>(vbuffer, ibuffer, effect); mCameraRig.Subscribe(mSegments->worldTransform, effect->GetPVWMatrixConstant()); mSegments->Update(); // Create the sphere. mSphere = mf.CreateSphere(16, 16, 1.0f); effect = std::make_shared<ConstantColorEffect>(mProgramFactory, Vector4<float>({ 0.0f, 0.0f, 0.5f, 1.0f })); mSphere->SetEffect(effect); mCameraRig.Subscribe(mSphere->worldTransform, effect->GetPVWMatrixConstant()); }
//---------------------------------------------------------------------------- bool WireMeshWindow::CreateScene() { std::string path = mEnvironment.GetPath("WireMesh.hlsl"); std::shared_ptr<VertexShader> vshader(ShaderFactory::CreateVertex(path)); if (!vshader) { return false; } std::shared_ptr<GeometryShader> gshader(ShaderFactory::CreateGeometry( path)); if (!gshader) { return false; } std::shared_ptr<PixelShader> pshader(ShaderFactory::CreatePixel(path)); if (!pshader) { return false; } std::shared_ptr<ConstantBuffer> parameters( new ConstantBuffer(3 * sizeof(Vector4<float>), false)); Vector4<float>* data = parameters->Get<Vector4<float>>(); data[0] = Vector4<float>(0.0f, 0.0f, 1.0f, 1.0f); // mesh color data[1] = Vector4<float>(0.0f, 0.0f, 0.0f, 1.0f); // edge color data[2] = Vector4<float>((float)mXSize, (float)mYSize, 0.0f, 0.0f); vshader->Set("WireParameters", parameters); gshader->Set("WireParameters", parameters); pshader->Set("WireParameters", parameters); std::shared_ptr<ConstantBuffer> cbuffer( new ConstantBuffer(sizeof(Matrix4x4<float>), true)); vshader->Set("PVWMatrix", cbuffer); std::shared_ptr<VisualEffect> effect(new VisualEffect(vshader, pshader, gshader)); VertexFormat vformat; vformat.Bind(VA_POSITION, DF_R32G32B32_FLOAT, 0); MeshFactory mf; mf.SetVertexFormat(vformat); mMesh = mf.CreateSphere(16, 16, 1.0f); mMesh->SetEffect(effect); mMesh->Update(); SubscribeCW(mMesh, cbuffer); return true; }
Boundary::Ptr Boundary::extract_surface_boundary_raw( MatrixFr& vertices, MatrixIr& faces) { VectorF flattened_vertices = Eigen::Map<VectorF>(vertices.data(), vertices.rows() * vertices.cols()); VectorI flattened_faces = Eigen::Map<VectorI>(faces.data(), faces.rows() * faces.cols()); VectorI voxels = VectorI::Zero(0); MeshFactory factory; Mesh::Ptr mesh = factory.load_data(flattened_vertices, flattened_faces, voxels, vertices.cols(), faces.cols(), 0).create(); return extract_surface_boundary(*mesh); }
void BlendedAnimationsWindow::CreateScene() { mWireState = std::make_shared<RasterizerState>(); mWireState->fillMode = RasterizerState::FILL_WIREFRAME; mScene = std::make_shared<Node>(); mScene->AttachChild(mManager->GetRoot()); // Create a floor to walk on. VertexFormat vformat; vformat.Bind(VA_POSITION, DF_R32G32B32_FLOAT, 0); vformat.Bind(VA_NORMAL, DF_R32G32B32_FLOAT, 0); vformat.Bind(VA_TEXCOORD, DF_R32G32_FLOAT, 0); MeshFactory mf; mf.SetVertexFormat(vformat); mFloor = mf.CreateRectangle(2, 2, 1024.0f, 2048.0f); mFloor->name = "Floor"; mScene->AttachChild(mFloor); std::shared_ptr<VertexBuffer> vbuffer = mFloor->GetVertexBuffer(); vbuffer->SetUsage(Resource::DYNAMIC_UPDATE); unsigned int numVertices = vbuffer->GetNumElements(); Vertex* vertex = vbuffer->Get<Vertex>(); for (unsigned int i = 0; i < numVertices; ++i) { vertex[i].tcoord[0] *= 64.0f; vertex[i].tcoord[1] *= 256.0f; } std::string textureName = mEnvironment.GetPath("Grating.png"); std::shared_ptr<Texture2> texture(WICFileIO::Load(textureName, true)); texture->AutogenerateMipmaps(); std::shared_ptr<Texture2Effect> effect = std::make_shared<Texture2Effect>( mProgramFactory, texture, SamplerState::MIN_L_MAG_L_MIP_L, SamplerState::WRAP, SamplerState::WRAP); mFloor->SetEffect(effect); mCameraRig.Subscribe(mFloor->worldTransform, effect->GetPVWMatrixConstant()); for (auto const& subscriber : mManager->GetSubscribers()) { mCameraRig.Subscribe(subscriber.first->worldTransform, subscriber.second); } GetMeshes(mScene); mTrackball.Attach(mScene); mTrackball.Update(mApplicationTime); }
bool MeshResource::LoadFromDB( CBinaryDatabase<std::string>& db, const std::string& keyname ) { m_pMeshObject.reset(); string mesh_archive_key = keyname; // retrieve mesh archive from db C3DMeshModelArchive mesh_archive; db.GetData( mesh_archive_key, mesh_archive ); MeshFactory factory; BasicMesh *pMesh = factory.LoadMeshObjectFromArchive( mesh_archive, keyname, m_MeshDesc.LoadOptionFlags, m_MeshDesc.MeshType ); m_pMeshObject = boost::shared_ptr<BasicMesh>( pMesh ); return ( m_pMeshObject ? true : false ); }
void AssetManager::Init() { MeshFactory* meshFactory = MeshFactory::getInstance(); /* Add the box */ Mesh* box = meshFactory->CreateBoxMesh(); Mesh* quad = meshFactory->CreateNewQuadMesh(); Mesh* debugMesh = meshFactory->CreateDebugMesh(); Mesh* particleMesh = meshFactory->CreateParticleMesh(MAXPARTICLEBUFFER); Mesh* textMesh = meshFactory->CreateTextMesh(5000); Mesh* ppsQuad = meshFactory->CreatePPSQuad(); //box->SetAutoUnload(false); quad->SetAutoUnload(false); debugMesh->SetAutoUnload(false); particleMesh->SetAutoUnload(false); textMesh->SetAutoUnload(false); ppsQuad->SetAutoUnload(false); //InsertAssetInMap(box); InsertAssetInMap(quad); InsertAssetInMap(debugMesh); InsertAssetInMap(particleMesh); InsertAssetInMap(textMesh); InsertAssetInMap(ppsQuad); assChecker = 0; std::cout << "Asset Manager Initialized" << std::endl; }
bool MeshResource::LoadFromFile( const std::string& filepath ) { m_pMeshObject.reset(); MeshFactory factory; BasicMesh *pMeshObject = factory.LoadMeshObjectFromFile( filepath, m_MeshDesc.LoadOptionFlags, m_MeshDesc.MeshType ); if( pMeshObject ) { m_pMeshObject = shared_ptr<BasicMesh>( pMeshObject ); return true; } else { LOG_PRINT_WARNING( "Failed to load the mesh: " + filepath ); return false; } }
void Delaunay3DWindow::CreateSphere() { VertexFormat vformat; vformat.Bind(VA_POSITION, DF_R32G32B32_FLOAT, 0); vformat.Bind(VA_COLOR, DF_R32G32B32_FLOAT, 0); MeshFactory mf; mf.SetVertexFormat(vformat); mSphere = mf.CreateSphere(8, 8, 0.025f); std::shared_ptr<VertexColorEffect> effect = std::make_shared<VertexColorEffect>(mProgramFactory); mSphere->SetEffect(effect); // Move the sphere offscreen initially. mSphere->localTransform.SetTranslation(0.0f, 0.0f, -1000.0f); mSphere->Update(); mCameraRig.Subscribe(mSphere->worldTransform, effect->GetPVWMatrixConstant()); mScene->AttachChild(mSphere); }
//---------------------------------------------------------------------------- bool PlaneMeshIntersectionWindow::CreateScene() { std::string path = mEnvironment.GetPath("PlaneMeshIntersection.hlsl"); std::shared_ptr<VertexShader> vshader(ShaderFactory::CreateVertex(path)); if (!vshader) { return false; } std::shared_ptr<PixelShader> pshader(ShaderFactory::CreatePixel(path)); if (!pshader) { return false; } path = mEnvironment.GetPath("DrawIntersections.hlsl"); mDrawIntersections.reset(ShaderFactory::CreateCompute(path)); float planeDelta = 0.125f; mPMIParameters.reset(new ConstantBuffer(sizeof(PMIParameters), true)); PMIParameters& p = *mPMIParameters->Get<PMIParameters>(); p.pvMatrix = mCamera.GetProjectionViewMatrix(); p.wMatrix = Matrix4x4<float>::Identity(); p.planeVector0 = Vector4<float>(1.0f, 0.0f, 0.0f, 0.0f) / planeDelta; p.planeVector1 = Vector4<float>(0.0f, 1.0f, 0.0f, 0.0f) / planeDelta; vshader->Set("PMIParameters", mPMIParameters); std::shared_ptr<VisualEffect> effect(new VisualEffect(vshader, pshader)); VertexFormat vformat; vformat.Bind(VA_POSITION, DF_R32G32B32_FLOAT, 0); MeshFactory mf; mf.SetVertexFormat(vformat); mMesh = mf.CreateSphere(16, 16, 1.0f); mMesh->SetEffect(effect); mMesh->Update(); return true; }
Boundary::Ptr Boundary::extract_volume_boundary_raw( MatrixFr& vertices, MatrixIr& voxels) { VectorF flattened_vertices = Eigen::Map<VectorF>(vertices.data(), vertices.rows() * vertices.cols()); VectorI faces = VectorI::Zero(0); VectorI flattened_voxels = Eigen::Map<VectorI>(voxels.data(), voxels.rows() * voxels.cols()); size_t vertex_per_voxel = voxels.cols(); size_t vertex_per_face=0; if (vertex_per_voxel == 4) vertex_per_face = 3; else if (vertex_per_voxel == 8) vertex_per_face = 4; else { throw RuntimeError("Unknown voxel type."); } MeshFactory factory; Mesh::Ptr mesh = factory.load_data(flattened_vertices, faces, flattened_voxels, vertices.cols(), vertex_per_face, vertex_per_voxel).create(); return extract_volume_boundary(*mesh); }
/*! * \brief Generates a Mesh representation/approximation of a cylinder. * * The mesh is created by \ref MeshFactory::createCylinder */ void Cylinder::generateMesh() { MeshFactory meshFactory; mMesh = meshFactory.createCylinder(mRadius,mHeight,mAverageEdgeLength); mMesh->setProxy(mProxy); }
void BSplineSurfaceFitterWindow::CreateScene() { // Begin with a flat 64x64 height field. int const numSamples = 64; float const extent = 8.0f; VertexFormat hfformat; hfformat.Bind(VA_POSITION, DF_R32G32B32_FLOAT, 0); hfformat.Bind(VA_TEXCOORD, DF_R32G32_FLOAT, 0); MeshFactory mf; mf.SetVertexFormat(hfformat); mHeightField = mf.CreateRectangle(numSamples, numSamples, extent, extent); int numVertices = numSamples * numSamples; VertexPT* hfvertices = mHeightField->GetVertexBuffer()->Get<VertexPT>(); // Set the heights based on a precomputed height field. Also create a // texture image to go with the height field. std::string path = mEnvironment.GetPath("BTHeightField.png"); std::shared_ptr<Texture2> texture(WICFileIO::Load(path, false)); std::shared_ptr<Texture2Effect> txeffect = std::make_shared<Texture2Effect>(mProgramFactory, texture, SamplerState::MIN_L_MAG_L_MIP_P, SamplerState::CLAMP, SamplerState::CLAMP); mHeightField->SetEffect(txeffect); std::mt19937 mte; std::uniform_real_distribution<float> symmr(-0.05f, 0.05f); std::uniform_real_distribution<float> intvr(32.0f, 64.0f); unsigned char* data = (unsigned char*)texture->Get<unsigned char>(); std::vector<Vector3<float>> samplePoints(numVertices); for (int i = 0; i < numVertices; ++i) { unsigned char value = *data; float height = 3.0f*((float)value) / 255.0f + symmr(mte); *data++ = (unsigned char)intvr(mte); *data++ = 3 * (128 - value / 2) / 4; *data++ = 0; data++; hfvertices[i].position[2] = height; samplePoints[i] = hfvertices[i].position; } // Compute a B-Spline surface with NxN control points, where N < 64. // This surface will be sampled to 64x64 and displayed together with the // original height field for comparison. int const numControls = 32; int const degree = 3; BSplineSurfaceFit<float> fitter(degree, numControls, numSamples, degree, numControls, numSamples, &samplePoints[0]); VertexFormat ffformat; ffformat.Bind(VA_POSITION, DF_R32G32B32_FLOAT, 0); ffformat.Bind(VA_COLOR, DF_R32G32B32A32_FLOAT, 0); mf.SetVertexFormat(ffformat); mFittedField = mf.CreateRectangle(numSamples, numSamples, extent, extent); VertexPC* ffvertices = mFittedField->GetVertexBuffer()->Get<VertexPC>(); Vector4<float> translucent{ 1.0f, 1.0f, 1.0f, 0.5f }; for (int i = 0; i < numVertices; ++i) { float u = 0.5f*(ffvertices[i].position[0] / extent + 1.0f); float v = 0.5f*(ffvertices[i].position[1] / extent + 1.0f); ffvertices[i].position = fitter.GetPosition(u, v); ffvertices[i].color = translucent; } std::shared_ptr<VertexColorEffect> vceffect = std::make_shared<VertexColorEffect>(mProgramFactory); mFittedField->SetEffect(vceffect); mCameraRig.Subscribe(mHeightField->worldTransform, txeffect->GetPVWMatrixConstant()); mCameraRig.Subscribe(mFittedField->worldTransform, vceffect->GetPVWMatrixConstant()); mTrackball.Attach(mHeightField); mTrackball.Attach(mFittedField); mTrackball.Update(); }
void MinimumVolumeBox3DWindow::CreateScene() { mScene = std::make_shared<Node>(); std::mt19937 mte; std::uniform_real_distribution<float> rnd(-1.0f, 1.0f); Vector3<float> center{ 0.0f, 0.0f, 0.0f }; Vector3<float> extent{ 1.0f, 0.25f, 0.125f }; Vector3<float> axis[3] = { { 1.0f, 1.0f, 0.0f }, { -1.0f, 1.0f, 0.0f }, { 0.0f, 0.0f, 1.0f } }; Normalize(axis[0]); Normalize(axis[1]); Normalize(axis[2]); for (auto& v : mVertices) { float theta = rnd(mte) * (float)GTE_C_TWO_PI; float phi = rnd(mte) * (float)GTE_C_PI; float radius = 0.5f * (rnd(mte) + 1.0f); float x = extent[0] * cos(theta) * sin(phi); float y = extent[1] * sin(theta) * sin(phi); float z = extent[2] * cos(phi); v = center + radius * (x * axis[0] + y * axis[1] + z * axis[2]); } struct Vertex { Vector3<float> position; Vector4<float> color; }; VertexFormat vformat; vformat.Bind(VA_POSITION, DF_R32G32B32_FLOAT, 0); vformat.Bind(VA_COLOR, DF_R32G32B32A32_FLOAT, 0); std::shared_ptr<VertexBuffer> vbuffer(new VertexBuffer(vformat, NUM_POINTS)); Vertex* vertex = vbuffer->Get<Vertex>(); for (int i = 0; i < NUM_POINTS; ++i) { vertex[i].position[0] = (float)mVertices[i][0]; vertex[i].position[1] = (float)mVertices[i][1]; vertex[i].position[2] = (float)mVertices[i][2]; vertex[i].color[0] = 0.5f * (rnd(mte) + 1.0f); vertex[i].color[1] = 0.5f * (rnd(mte) + 1.0f); vertex[i].color[2] = 0.5f * (rnd(mte) + 1.0f); vertex[i].color[3] = 1.0f; } std::shared_ptr<IndexBuffer> ibuffer(new IndexBuffer(IP_POLYPOINT, NUM_POINTS)); std::shared_ptr<VertexColorEffect> effect = std::make_shared<VertexColorEffect>(mProgramFactory); mPoints = std::make_shared<Visual>(vbuffer, ibuffer, effect); mCameraRig.Subscribe(mPoints->worldTransform, effect->GetPVWMatrixConstant()); mScene->AttachChild(mPoints); // Choose the number of threads to use. The default constructor for // MinimumVolumeBox3 uses a default of 1, in which case all computations // are on the main thread. The timings below are for a 64-bit release // build (no debugger attached) on Intel Core i7-3930K CPUs running at // 3.20 GHz. unsigned int numThreads = 1; #if 0 // Compute the convex hull internally using arbitrary precision // arithmetic. This is slower than computing the hull explicitly using // the maximum fixed precision; see the other conditional block of code. Timer timer; typedef BSRational<UIntegerAP32> MVBRational; MinimumVolumeBox3<float, MVBRational> mvb3(numThreads); OrientedBox3<float> minBox = mvb3(NUM_POINTS, &mVertices[0]); std::cout << "mvb3 seconds = " << timer.GetSeconds() << std::endl; // numThreads = 1, seconds = 7.09 // numThreads = 2, seconds = 6.22 #else // If mVertices were to use 'double', you would need the template type // UIntegerFP32<167> to compute the convex hull. Timer timer; typedef BSNumber<UIntegerFP32<27>> CHRational; ConvexHull3<float, CHRational> ch3(numThreads); ch3(NUM_POINTS, &mVertices[0], 0.0f); std::vector<TriangleKey<true>> const& triangles = ch3.GetHullUnordered(); int const numIndices = static_cast<int>(3 * triangles.size()); int const* indices = static_cast<int const*>(&triangles[0].V[0]); typedef BSRational<UIntegerAP32> MVBRational; MinimumVolumeBox3<float, MVBRational> mvb3(numThreads); OrientedBox3<float> minBox = mvb3(NUM_POINTS, &mVertices[0], numIndices, indices); std::cout << "mvb3 seconds = " << timer.GetSeconds() << std::endl; // numThreads = 1, seconds = 2.69 // numThreads = 2, seconds = 2.01 #endif std::vector<int> const& hull = mvb3.GetHull(); ibuffer = std::make_shared<IndexBuffer>(IP_TRIMESH, static_cast<int>(hull.size() / 3), sizeof(int)); Memcpy(ibuffer->GetData(), &hull[0], ibuffer->GetNumBytes()); mPolytope = std::make_shared<Visual>(vbuffer, ibuffer, effect); mScene->AttachChild(mPolytope); MeshFactory mf; mf.SetVertexFormat(vformat); mBoxMesh = mf.CreateBox(1.0f, 1.0f, 1.0f); vbuffer = mBoxMesh->GetVertexBuffer(); vertex = vbuffer->Get<Vertex>(); std::array<Vector3<float>, 8> corner; minBox.GetVertices(corner); for (int i = 0; i < 8; ++i) { vertex[i].position[0] = corner[i][0]; vertex[i].position[1] = corner[i][1]; vertex[i].position[2] = corner[i][2]; vertex[i].color[0] = 0.5f * (rnd(mte) + 1.0f); vertex[i].color[1] = 0.5f * (rnd(mte) + 1.0f); vertex[i].color[2] = 0.5f * (rnd(mte) + 1.0f); vertex[i].color[3] = 1.0f; } mBoxMesh->SetEffect(effect); mScene->AttachChild(mBoxMesh); mTrackball.Attach(mScene); mTrackball.Update(); }
void PickingWindow::CreateScene() { std::string path = mEnvironment.GetPath("Checkerboard.png"); std::shared_ptr<Texture2> texture(WICFileIO::Load(path, false)); mScene = std::make_shared<Node>(); VertexFormat vformat0; vformat0.Bind(VA_POSITION, DF_R32G32B32_FLOAT, 0); vformat0.Bind(VA_TEXCOORD, DF_R32G32_FLOAT, 0); // The torus and dodecahedron are created by the mesh factory in which // the 'visual' model bounds are computed. The points and segments // primitives are created explicitly here, so we need to compute their // model bounds to be used by the picking system. MeshFactory mf; mf.SetVertexFormat(vformat0); mTorus = mf.CreateTorus(16, 16, 4.0f, 1.0f); std::shared_ptr<Texture2Effect> effect = std::make_shared<Texture2Effect>( mProgramFactory, texture, SamplerState::MIN_L_MAG_L_MIP_P, SamplerState::CLAMP, SamplerState::CLAMP); mTorus->SetEffect(effect); mCameraRig.Subscribe(mTorus->worldTransform, effect->GetPVWMatrixConstant()); mScene->AttachChild(mTorus); mDodecahedron = mf.CreateDodecahedron(); effect = std::make_shared<Texture2Effect>(mProgramFactory, texture, SamplerState::MIN_L_MAG_L_MIP_P, SamplerState::CLAMP, SamplerState::CLAMP); mDodecahedron->SetEffect(effect); mCameraRig.Subscribe(mDodecahedron->worldTransform, effect->GetPVWMatrixConstant()); mScene->AttachChild(mDodecahedron); VertexFormat vformat1; vformat1.Bind(VA_POSITION, DF_R32G32B32_FLOAT, 0); std::shared_ptr<VertexBuffer> vbuffer(new VertexBuffer(vformat1, 4)); Vector3<float>* vertices = vbuffer->Get<Vector3<float>>(); vertices[0] = { 1.0f, 1.0f, 4.0f }; vertices[1] = { 1.0f, 2.0f, 5.0f }; vertices[2] = { 2.0f, 2.0f, 6.0f }; vertices[3] = { 2.0f, 1.0f, 7.0f }; std::shared_ptr<IndexBuffer> ibuffer(new IndexBuffer(IP_POLYPOINT, 4)); std::shared_ptr<ConstantColorEffect> cceffect = std::make_shared<ConstantColorEffect>(mProgramFactory, Vector4<float>({ 0.5f, 0.0f, 0.0f, 1.0f })); mPoints = std::make_shared<Visual>(vbuffer, ibuffer, cceffect); mPoints->UpdateModelBound(); mCameraRig.Subscribe(mPoints->worldTransform, cceffect->GetPVWMatrixConstant()); mScene->AttachChild(mPoints); vbuffer = std::make_shared<VertexBuffer>(vformat1, 4); vertices = vbuffer->Get<Vector3<float>>(); vertices[0] = { -1.0f, -1.0f, 4.0f }; vertices[1] = { -1.0f, -2.0f, 5.0f }; vertices[2] = { -2.0f, -1.0f, 6.0f }; vertices[3] = { -2.0f, -2.0f, 7.0f }; ibuffer = std::make_shared<IndexBuffer>(IP_POLYSEGMENT_CONTIGUOUS, 3, sizeof(int)); ibuffer->SetSegment(0, 0, 1); ibuffer->SetSegment(1, 1, 2); ibuffer->SetSegment(2, 2, 3); cceffect = std::make_shared<ConstantColorEffect>(mProgramFactory, Vector4<float>({ 0.0f, 0.0f, 0.5f, 1.0f })); mSegments = std::make_shared<Visual>(vbuffer, ibuffer, cceffect); mSegments->UpdateModelBound(); mCameraRig.Subscribe(mSegments->worldTransform, cceffect->GetPVWMatrixConstant()); mScene->AttachChild(mSegments); for (int i = 0; i < SPHERE_BUDGET; ++i) { mSphere[i] = mf.CreateSphere(8, 8, 0.125f); cceffect = std::make_shared<ConstantColorEffect>(mProgramFactory, Vector4<float>({ 0.0f, 0.0f, 0.0f, 1.0f })); mSphere[i]->SetEffect(cceffect); mCameraRig.Subscribe(mSphere[i]->worldTransform, cceffect->GetPVWMatrixConstant()); mScene->AttachChild(mSphere[i]); } mTrackball.Attach(mScene); mTrackball.Update(); }
void LightsWindow::CreateScene() { // Copper color for the planes. Vector4<float> planeAmbient{ 0.2295f, 0.08825f, 0.0275f, 1.0f }; Vector4<float> planeDiffuse{ 0.5508f, 0.2118f, 0.066f, 1.0f }; Vector4<float> planeSpecular{ 0.580594f, 0.223257f, 0.0695701f, 51.2f }; // Gold color for the spheres. Vector4<float> sphereAmbient{ 0.24725f, 0.2245f, 0.0645f, 1.0f }; Vector4<float> sphereDiffuse{ 0.34615f, 0.3143f, 0.0903f, 1.0f }; Vector4<float> sphereSpecular{ 0.797357f, 0.723991f, 0.208006f, 83.2f }; // Various parameters shared by the lighting constants. The geometric // parameters are dynamic, modified by UpdateConstants() whenever the // camera or scene moves. These include camera model position, light // model position, light model direction, and model-to-world matrix. Vector4<float> darkGray{ 0.1f, 0.1f, 0.1f, 1.0f }; Vector4<float> lightGray{ 0.75f, 0.75f, 0.75f, 1.0f }; float angle = 0.125f*(float)GTE_C_PI; Vector4<float> lightSpotCutoff{ angle, cos(angle), sin(angle), 1.0f }; mLightWorldPosition[SVTX] = { 4.0f, 4.0f - 8.0f, 8.0f, 1.0f }; mLightWorldPosition[SPXL] = { 4.0f, 4.0f + 8.0f, 8.0f, 1.0f }; mLightWorldDirection = { -1.0f, -1.0f, -1.0f, 0.0f }; Normalize(mLightWorldDirection); std::shared_ptr<Material> material[LNUM][GNUM]; std::shared_ptr<Lighting> lighting[LNUM][GNUM]; std::shared_ptr<LightCameraGeometry> geometry[LNUM][GNUM]; for (int lt = 0; lt < LNUM; ++lt) { for (int gt = 0; gt < GNUM; ++gt) { material[lt][gt] = std::make_shared<Material>(); lighting[lt][gt] = std::make_shared<Lighting>(); geometry[lt][gt] = std::make_shared<LightCameraGeometry>(); } } // Initialize the directional lighting constants. material[LDIR][GPLN]->ambient = planeAmbient; material[LDIR][GPLN]->diffuse = planeDiffuse; material[LDIR][GPLN]->specular = planeSpecular; lighting[LDIR][GPLN]->ambient = lightGray; material[LDIR][GSPH]->ambient = sphereAmbient; material[LDIR][GSPH]->diffuse = sphereDiffuse; material[LDIR][GSPH]->specular = sphereSpecular; lighting[LDIR][GSPH]->ambient = lightGray; // Initialize the point lighting constants. material[LPNT][GPLN]->ambient = planeAmbient; material[LPNT][GPLN]->diffuse = planeDiffuse; material[LPNT][GPLN]->specular = planeSpecular; lighting[LPNT][GPLN]->ambient = darkGray; material[LPNT][GSPH]->ambient = sphereAmbient; material[LPNT][GSPH]->diffuse = sphereDiffuse; material[LPNT][GSPH]->specular = sphereSpecular; lighting[LPNT][GSPH]->ambient = darkGray; // Initialize the spot lighting constants. material[LSPT][GPLN]->ambient = planeAmbient; material[LSPT][GPLN]->diffuse = planeDiffuse; material[LSPT][GPLN]->specular = planeSpecular; lighting[LSPT][GPLN]->ambient = darkGray; lighting[LSPT][GPLN]->spotCutoff = lightSpotCutoff; material[LSPT][GSPH]->ambient = sphereAmbient; material[LSPT][GSPH]->diffuse = sphereDiffuse; material[LSPT][GSPH]->specular = sphereSpecular; lighting[LSPT][GSPH]->ambient = darkGray; lighting[LSPT][GSPH]->spotCutoff = lightSpotCutoff; // Create the effects. for (int gt = 0; gt < GNUM; ++gt) { for (int st = 0; st < SNUM; ++st) { mEffect[LDIR][gt][st] = std::make_shared<DirectionalLightEffect>( mProgramFactory, mUpdater, st, material[LDIR][gt], lighting[LDIR][gt], geometry[LDIR][gt]); mEffect[LPNT][gt][st] = std::make_shared<PointLightEffect>( mProgramFactory, mUpdater, st, material[LPNT][gt], lighting[LPNT][gt], geometry[LPNT][gt]); mEffect[LSPT][gt][st] = std::make_shared<SpotLightEffect>( mProgramFactory, mUpdater, st, material[LSPT][gt], lighting[LSPT][gt], geometry[LSPT][gt]); } } // Create the planes and spheres. VertexFormat vformat; vformat.Bind(VA_POSITION, DF_R32G32B32_FLOAT, 0); vformat.Bind(VA_NORMAL, DF_R32G32B32_FLOAT, 0); MeshFactory mf; mf.SetVertexFormat(vformat); mPlane[SVTX] = mf.CreateRectangle(128, 128, 8.0f, 8.0f); mPlane[SVTX]->localTransform.SetTranslation(0.0f, -8.0f, 0.0f); mTrackball.Attach(mPlane[SVTX]); mPlane[SPXL] = mf.CreateRectangle(128, 128, 8.0f, 8.0f); mPlane[SPXL]->localTransform.SetTranslation(0.0f, +8.0f, 0.0f); mTrackball.Attach(mPlane[SPXL]); mSphere[SVTX] = mf.CreateSphere(64, 64, 2.0f); mSphere[SVTX]->localTransform.SetTranslation(0.0f, -8.0f, 2.0f); mTrackball.Attach(mSphere[SVTX]); mSphere[SPXL] = mf.CreateSphere(64, 64, 2.0f); mSphere[SPXL]->localTransform.SetTranslation(0.0f, +8.0f, 2.0f); mTrackball.Attach(mSphere[SPXL]); mTrackball.Update(); mCaption[LDIR] = "Directional Light (left per vertex, right per pixel)"; mCaption[LPNT] = "Point Light (left per vertex, right per pixel)"; mCaption[LSPT] = "Spot Light (left per vertex, right per pixel)"; UseLightType(LDIR); }