Пример #1
0
void LoadMeshes(FbxNode* pFbxNode, packed_freelist<Mesh>& sceneMeshes)
{
    // Material
    const uint32_t materialCount = pFbxNode->GetMaterialCount();
    for (uint32_t i = 0; i < materialCount; ++i) {
        FbxSurfaceMaterial* pFbxMaterial = pFbxNode->GetMaterial(i);
        if (pFbxMaterial && !pFbxMaterial->GetUserDataPtr()) {
            FbxAutoPtr<Material> pMaterial(new Material);
            if (pMaterial->init(pFbxMaterial)) {
                pFbxMaterial->SetUserDataPtr(pMaterial.Release());
            }
        }
    }

    FbxNodeAttribute* nodeAttribute = pFbxNode->GetNodeAttribute();
    if (nodeAttribute) {
        // Mesh
        if (nodeAttribute->GetAttributeType() == FbxNodeAttribute::eMesh) {
            FbxMesh* pFbxMesh = pFbxNode->GetMesh();
            if (pFbxMesh && !pFbxMesh->GetUserDataPtr()) {
                Mesh mesh;
                if (mesh.init(pFbxMesh)) {
                    sceneMeshes.insert(mesh);
                }
                // TODO:
                FbxAutoPtr<Mesh> pMesh(new Mesh);
                if (pMesh->init(pFbxMesh)) {
                    pFbxMesh->SetUserDataPtr(pMesh.Release());
                }
            }
        }
        // Light
        else if (nodeAttribute->GetAttributeType() == FbxNodeAttribute::eLight) {
            FbxLight* pFbxLight = pFbxNode->GetLight();
            if (pFbxLight && !pFbxLight->GetUserDataPtr()) {
                FbxAutoPtr<Light> pLight(new Light);
                if (pLight->init(pFbxLight)) {
                    pFbxLight->SetUserDataPtr(pLight.Release());
                }
            }
        }
    }

    const int childCount = pFbxNode->GetChildCount();
    for (int i = 0; i < childCount; ++i) {
        LoadMeshes(pFbxNode->GetChild(i), sceneMeshes);
    }
}
Пример #2
0
	bool Model_IO::Load(const std::vector<uint8_t>& data, const achar* path)
	{
		Meshes.clear();

		BinaryReader reader;
		reader.ReadIn(data.begin(), data.end());

		// ヘッダーチェック
		uint8_t header_true [] = "MDL";
		for (int32_t i = 0; i < 4; i++)
		{
			auto h = reader.Get<uint8_t>();
			if (header_true[i] != h) return false;
		}

		// バージョン
		int32_t version = reader.Get<int32_t>();

		// ボーン
		LoadDeformer(Deformer_, reader, path, version);

		// メッシュ
		LoadMeshes(Meshes, reader, path);

		// アニメーション
		int32_t sourceCount = reader.Get<int32_t>();
		AnimationSources.resize(sourceCount);
		for (int32_t i = 0; i < sourceCount; i++)
		{
			LoadAnimationSource(AnimationSources[i], reader, path);
		}

		int32_t clipCount = reader.Get<int32_t>();
		AnimationClips.resize(clipCount);
		for (int32_t i = 0; i < clipCount; i++)
		{
			LoadAnimationClip(AnimationClips[i], reader, path);
		}

		return true;
	}
	Model::Model(const std::string& filePath, bool flipUVs)
		: mMeshes(), mMaterials()
	{
		std::ifstream inFile(filePath, ifstream::in | ifstream::binary);
		if (inFile.is_open())
		{
			char readData[sizeof(uint32_t)];

			inFile.read(readData, sizeof(uint32_t));
			uint32_t numMat;
			memcpy(&numMat, readData, sizeof(uint32_t));

			inFile.read(readData, sizeof(uint32_t));
			uint32_t numMesh;
			memcpy(&numMesh, readData, sizeof(uint32_t));

			LoadMaterials(inFile, numMat);
			LoadMeshes(inFile, numMesh);

			inFile.close();
		}
	}
Пример #4
0
ParticleTest::ParticleTest( const vec3 &o_pos ) : CObject( o_pos, vec3(), vec3(), CVec < SMaterial, void >() )
{
	D3D11_RASTERIZER_DESC rsDesc;
    rsDesc.AntialiasedLineEnable = false;
    rsDesc.CullMode = D3D11_CULL_NONE;
    rsDesc.DepthBias = 0;
    rsDesc.DepthBiasClamp = 0.f;
    rsDesc.DepthClipEnable = true;
    rsDesc.FillMode = D3D11_FILL_SOLID;
    rsDesc.FrontCounterClockwise = false;
    rsDesc.MultisampleEnable = false;
    rsDesc.ScissorEnable = false;
    rsDesc.SlopeScaledDepthBias = 0.f;

	_rs = RasterizerStatesManager::GetState( &rsDesc );

	D3D11_BLEND_DESC blendDesc = {};
    blendDesc.AlphaToCoverageEnable = false;
    blendDesc.IndependentBlendEnable = false;
    blendDesc.RenderTarget[ 0 ].BlendEnable = true;
    blendDesc.RenderTarget[ 0 ].SrcBlend = D3D11_BLEND_ONE;
    blendDesc.RenderTarget[ 0 ].DestBlend = D3D11_BLEND_ONE;
    blendDesc.RenderTarget[ 0 ].BlendOp = D3D11_BLEND_OP_ADD;
    blendDesc.RenderTarget[ 0 ].SrcBlendAlpha = D3D11_BLEND_ZERO;
    blendDesc.RenderTarget[ 0 ].DestBlendAlpha = D3D11_BLEND_ZERO;
    blendDesc.RenderTarget[ 0 ].BlendOpAlpha = D3D11_BLEND_OP_ADD;
    blendDesc.RenderTarget[ 0 ].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;

	_bs = BlendStatesManager::GetState( &blendDesc );

	D3D11_SAMPLER_DESC sampDesc;
    sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
    sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
    sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
    sampDesc.BorderColor[ 0 ] = 1.f;
    sampDesc.BorderColor[ 1 ] = 1.f;
    sampDesc.BorderColor[ 2 ] = 1.f;
    sampDesc.BorderColor[ 3 ] = 1.f;
    sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
    sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
    sampDesc.MaxAnisotropy = 1;
    sampDesc.MaxLOD = FLT_MAX;
    sampDesc.MinLOD = -FLT_MAX;
    sampDesc.MipLODBias = 0.f;

	_ss = SamplersManager::GetState( &sampDesc );

	_particlesDrawShader = ShadersManager::AcquireByName( "particleTest" );

	_meshesDrawShader = ShadersManager::AcquireByName( "meshesTest" );

	ASSUME( sizeof(SParticle) == 48 );

	static const D3D11_BUFFER_DESC vbufDesc =
	{
		ParticlesReserveSize * sizeof(SParticle),
		D3D11_USAGE_DEFAULT,
		D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS,
		0,
		D3D11_RESOURCE_MISC_BUFFER_STRUCTURED,
		sizeof(SParticle)
	};

	DXHRCHECK( RendererGlobals::i_Device->CreateBuffer( &vbufDesc, 0, _particlesVB.AddrModifiable() ) );
    DXHRCHECK( RendererGlobals::i_Device->CreateUnorderedAccessView( _particlesVB, 0, _particlesVBUAV.AddrModifiableRelease() ) );
    DXHRCHECK( RendererGlobals::i_Device->CreateShaderResourceView( _particlesVB, 0, _particlesVBSRV.AddrModifiableRelease() ) );

	static const D3D11_BUFFER_DESC vbufMeshDesc =
	{
		ParticlesReserveSize * sizeof(SParticle),
		D3D11_USAGE_DEFAULT,
		D3D11_BIND_VERTEX_BUFFER,
		0,
		0,
		sizeof(SParticle)
	};

	DXHRCHECK( RendererGlobals::i_Device->CreateBuffer( &vbufMeshDesc, 0, _meshesVB.AddrModifiable() ) );

	ASSUME( sizeof(ParticleFrame) == 80 );

	static const D3D11_BUFFER_DESC vbufFrameDataDesc =
	{
		ParticlesReserveSize * sizeof(ParticleFrame),
		D3D11_USAGE_DEFAULT,
		D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS,
		0,
		D3D11_RESOURCE_MISC_BUFFER_STRUCTURED,
		sizeof(ParticleFrame)
	};

	DXHRCHECK( RendererGlobals::i_Device->CreateBuffer( &vbufFrameDataDesc, 0, _particlesFrameDataVB.AddrModifiable() ) );
    DXHRCHECK( RendererGlobals::i_Device->CreateUnorderedAccessView( _particlesFrameDataVB, 0, _frameDataUAV.AddrModifiableRelease() ) );
    DXHRCHECK( RendererGlobals::i_Device->CreateShaderResourceView( _particlesFrameDataVB, 0, _frameDataSRV.AddrModifiableRelease() ) );

	_textureView = TextureLoader::Load( "Textures/ball.dds" );

	_circleTextureView = TextureLoader::Load( "Textures/circle.dds" );
	
	if( CompileShader( L"Shaders/eraseFrameData_cs.hlsl", &_eraseFrameDataCS ) )
	{
		SENDLOG( CLogger::Tag::important, "compiled erase frame data cs\n" );
	}

	D3D11_BUFFER_DESC uniBufDesc;
    uniBufDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
    uniBufDesc.ByteWidth = 256;
    uniBufDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
    uniBufDesc.MiscFlags = 0;
    uniBufDesc.StructureByteStride = 0;
    uniBufDesc.Usage = D3D11_USAGE_DYNAMIC;
    DXHRCHECK( RendererGlobals::i_Device->CreateBuffer( &uniBufDesc, 0, _uniBuffer.AddrModifiable() ) );

	static const VertexBufferFieldDesc MeshBufferDesc[] =
	{
		{ "POSITION", DXGI_FORMAT_R32G32B32_FLOAT, 0, 0 },
		{ "COLOR", DXGI_FORMAT_R32G32B32A32_FLOAT, 16, 0 },
		{ "TEXCOORD", DXGI_FORMAT_R32G32_FLOAT, 32, 0 }
	};

	LayoutsManager::BufferDesc_t compiledMeshBufferDesc = RendererGlobals::DefLayoutsManager.CompileBufferDesc( MakeRefVec( MeshBufferDesc ) );
	bln meshBlendResult = ShadersManager::TryToBlend( compiledMeshBufferDesc, _meshesDrawShader, &_meshInputLayout );
	ASSUME( meshBlendResult );

	//LoadPSystems();

	LoadMeshes();
}
Пример #5
0
int main(void)
{
    // Initialization
    //--------------------------------------------------------------------------------------
    const int screenWidth = 800;
    const int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "raylib example - obj viewer");

    // Define the camera to look into our 3d world
    Camera camera = { { 30.0f, 30.0f, 30.0f }, { 0.0f, 10.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };

    Model model = LoadModel("resources/models/turret.obj");                     // Load default model obj
    Texture2D texture = LoadTexture("resources/models/turret_diffuse.png");     // Load default model texture
    model.materials[0].maps[MAP_DIFFUSE].texture = texture; // Bind texture to model

    Vector3 position = { 0.0, 0.0, 0.0 };                   // Set model position
    BoundingBox bounds = MeshBoundingBox(model.meshes[0]);  // Set model bounds
    bool selected = false;                                  // Selected object flag

    SetCameraMode(camera, CAMERA_FREE);     // Set a free camera mode

    char objFilename[64] = "turret.obj";

    SetTargetFPS(60);               // Set our game to run at 60 frames-per-second
    //--------------------------------------------------------------------------------------

    // Main game loop
    while (!WindowShouldClose())    // Detect window close button or ESC key
    {
        // Update
        //----------------------------------------------------------------------------------
        if (IsFileDropped())
        {
            int count = 0;
            char **droppedFiles = GetDroppedFiles(&count);

            if (count == 1)
            {
                if (IsFileExtension(droppedFiles[0], ".obj"))
                {
                    for (int i = 0; i < model.meshCount; i++) UnloadMesh(&model.meshes[i]);
                    model.meshes = LoadMeshes(droppedFiles[0], &model.meshCount);
                    bounds = MeshBoundingBox(model.meshes[0]);
                }
                else if (IsFileExtension(droppedFiles[0], ".png"))
                {
                    UnloadTexture(texture);
                    texture = LoadTexture(droppedFiles[0]);
                    model.materials[0].maps[MAP_DIFFUSE].texture = texture;
                }

                strcpy(objFilename, GetFileName(droppedFiles[0]));
            }

            ClearDroppedFiles();    // Clear internal buffers
        }

        UpdateCamera(&camera);

        // Select model on mouse click
        if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
        {
            // Check collision between ray and box
            if (CheckCollisionRayBox(GetMouseRay(GetMousePosition(), camera), bounds)) selected = !selected;
            else selected = false;
        }
        //----------------------------------------------------------------------------------

        // Draw
        //----------------------------------------------------------------------------------
        BeginDrawing();

            ClearBackground(RAYWHITE);

            BeginMode3D(camera);

                DrawModel(model, position, 1.0f, WHITE);   // Draw 3d model with texture

                DrawGrid(20.0, 10.0);        // Draw a grid

                if (selected) DrawBoundingBox(bounds, GREEN);

            EndMode3D();

            DrawText("Free camera default controls:", 10, 20, 10, DARKGRAY);
            DrawText("- Mouse Wheel to Zoom in-out", 20, 40, 10, GRAY);
            DrawText("- Mouse Wheel Pressed to Pan", 20, 60, 10, GRAY);
            DrawText("- Alt + Mouse Wheel Pressed to Rotate", 20, 80, 10, GRAY);
            DrawText("- Alt + Ctrl + Mouse Wheel Pressed for Smooth Zoom", 20, 100, 10, GRAY);

            DrawText("Drag & drop .obj/.png to load mesh/texture.", 10, GetScreenHeight() - 20, 10, DARKGRAY);
            DrawText(FormatText("Current file: %s", objFilename), 250, GetScreenHeight() - 20, 10, GRAY);
            if (selected) DrawText("MODEL SELECTED", GetScreenWidth() - 110, 10, 10, GREEN);

            DrawText("(c) Turret 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY);

        EndDrawing();
        //----------------------------------------------------------------------------------
    }

    // De-Initialization
    //--------------------------------------------------------------------------------------
    UnloadModel(model);         // Unload model

    ClearDroppedFiles();        // Clear internal buffers

    CloseWindow();              // Close window and OpenGL context
    //--------------------------------------------------------------------------------------

    return 0;
}
Пример #6
0
void LoadMeshes(Scene* pScene, std::vector<uint32_t>* loadedMeshIDs)
{
    FbxManager* fbxManager = FbxManager::Create();

    FbxIOSettings* pFbxIOSettings = FbxIOSettings::Create(fbxManager, IOSROOT);
    fbxManager->SetIOSettings(pFbxIOSettings);

    (*(fbxManager->GetIOSettings())).SetBoolProp(IMP_FBX_MATERIAL, true);
    (*(fbxManager->GetIOSettings())).SetBoolProp(IMP_FBX_TEXTURE, true);
    (*(fbxManager->GetIOSettings())).SetBoolProp(IMP_FBX_LINK, false);
    (*(fbxManager->GetIOSettings())).SetBoolProp(IMP_FBX_SHAPE, false);
    (*(fbxManager->GetIOSettings())).SetBoolProp(IMP_FBX_GOBO, false);
    (*(fbxManager->GetIOSettings())).SetBoolProp(IMP_FBX_ANIMATION, true);
    (*(fbxManager->GetIOSettings())).SetBoolProp(IMP_FBX_GLOBAL_SETTINGS, true);

    bool bEmbedMedia = true;
    (*(fbxManager->GetIOSettings())).SetBoolProp(EXP_FBX_MATERIAL, true);
    (*(fbxManager->GetIOSettings())).SetBoolProp(EXP_FBX_TEXTURE, true);
    (*(fbxManager->GetIOSettings())).SetBoolProp(EXP_FBX_EMBEDDED, bEmbedMedia);
    (*(fbxManager->GetIOSettings())).SetBoolProp(EXP_FBX_SHAPE, true);
    (*(fbxManager->GetIOSettings())).SetBoolProp(EXP_FBX_GOBO, true);
    (*(fbxManager->GetIOSettings())).SetBoolProp(EXP_FBX_ANIMATION, true);
    (*(fbxManager->GetIOSettings())).SetBoolProp(EXP_FBX_GLOBAL_SETTINGS, true);

    FbxImporter* pFbxImporter = FbxImporter::Create(fbxManager, "");

    // Initialize the importer.
    bool result = pFbxImporter->Initialize(pScene->loadPath.c_str(), -1, fbxManager->GetIOSettings());
    if (!result) {
        printf("Get error when init FBX Importer: %s\n\n",
            pFbxImporter->GetStatus().GetErrorString());
        exit(-1);
    }

    // fbx version number
    int major, minor, revision;
    pFbxImporter->GetFileVersion(major, minor, revision);

    // import pFbxScene
    FbxScene* pFbxScene = FbxScene::Create(fbxManager, "myScene");
    pFbxImporter->Import(pFbxScene);
    pFbxImporter->Destroy();
    pFbxImporter = nullptr;

    // check axis system
    FbxAxisSystem axisSystem = pFbxScene->GetGlobalSettings().GetAxisSystem();
    FbxAxisSystem vulkanAxisSystem(FbxAxisSystem::eYAxis,
        FbxAxisSystem::eParityOdd,
        FbxAxisSystem::eRightHanded);
    if (axisSystem != vulkanAxisSystem) {
        axisSystem.ConvertScene(pFbxScene);
    }

    // check unit system
    FbxSystemUnit systemUnit = pFbxScene->GetGlobalSettings().GetSystemUnit();
    if (systemUnit.GetScaleFactor() != 1.0) {
        FbxSystemUnit::cm.ConvertScene(pFbxScene);
    }

    // Triangulate Mesh
    FbxGeometryConverter fbxGeometryConverter(fbxManager);
    fbxGeometryConverter.Triangulate(pFbxScene, true);

    // Load Texture
    int textureCount = pFbxScene->GetTextureCount();
    for (int i = 0; i < textureCount; ++i) {
        FbxTexture* pFbxTexture = pFbxScene->GetTexture(i);
        FbxFileTexture* pFbxFileTexture = FbxCast<FbxFileTexture>(pFbxTexture);
        if (pFbxTexture && pFbxFileTexture->GetUserDataPtr()) {
        }
    }
    LoadMeshes(pFbxScene->GetRootNode(), pScene->meshes);
}