SlimDX Tutorial 7 - 메쉬


#include <Windows.h>
using namespace System;
using namespace System::Windows::Forms;
using namespace System::Drawing;
using namespace SlimDX;
using namespace SlimDX::Direct3D9;
struct TextureVertex
{
    Vector3 positon;    
    int color;
    float tu, tv;
};
public ref class WndForm : public Form
{   
    Direct3D^ direct3D;
    PresentParameters^ presentParams;   
    Device^ device; 
    Mesh^ mesh;
    array<Material ^> ^meshMaterials;
    array<Texture ^> ^meshTextures;
    String^ lastError;
    
public:
    WndForm(void)
    {   
        this->ClientSize = System::Drawing::Size(400, 300);
        this->StartPosition = FormStartPosition::CenterScreen;
        this->Text = "D3D Tutorial 6";
    }
    bool InitD3D()
    {
        try
        {
            // 환경변수 세팅
            direct3D = gcnew Direct3D();
            presentParams = gcnew PresentParameters();
            presentParams->Windowed = true;
            presentParams->SwapEffect = SwapEffect::Discard;
            presentParams->BackBufferFormat = Format::Unknown;
            presentParams->EnableAutoDepthStencil = true;
            presentParams->AutoDepthStencilFormat = Format::D16;
            
            // 디바이스 생성
            device = gcnew Device(direct3D, 0, DeviceType::Hardware, this->Handle, CreateFlags::SoftwareVertexProcessing, presentParams);
            // 디바이스 세팅
            device->SetRenderState(RenderState::CullMode, Cull::None);
            device->SetRenderState(RenderState::ZEnable, true);
            device->SetRenderState(RenderState::Ambient, 0xffffff);
            // 모델 로딩
            mesh = Mesh::FromFile(device, "tiger.x", MeshFlags::SystemMemory);
            array<ExtendedMaterial> ^materials = mesh->GetMaterials();
            meshMaterials = gcnew array<Material ^>(materials->Length);
            meshTextures = gcnew array<Texture ^>(materials->Length);           
            for (int i = 0; i < materials->Length; i++)
            {
                meshMaterials[i] = materials[i].MaterialD3D;
                meshMaterials[i]->Ambient = meshMaterials[i]->Diffuse;
                meshTextures[i] = Texture::FromFile(device, materials[i].TextureFileName);
            }           
            return true;
        }
        catch (Direct3D9Exception^ ex)
        {
            lastError = ex->ToString();
            return false;
        }
    }
    void SetupMatrix()
    {       
        device->SetTransform(TransformState::World, Matrix::RotationY(Environment::TickCount / 1000.0f));
        device->SetTransform(TransformState::View, Matrix::LookAtLH(Vector3(0.0f, 3.0f, -5.0f), Vector3(0.0f, 0.0f, 0.0f), Vector3(0.0f, 1.0f, 0.0f)));
        device->SetTransform(TransformState::Projection, Matrix::PerspectiveFovLH((float)Math::PI / 4, 1.0f, 1.0f, 100.0f));        
    }
    
    void Render()
    {
        if (!device)
            return;
        
        // 렌더링 시작
        device->Clear(ClearFlags::Target | ClearFlags::ZBuffer, System::Drawing::Color::Black, 1.0f, 0);
        device->BeginScene();
        SetupMatrix();
        // 렌더링       
        for (int i = 0; i < meshMaterials->Length; i++)
        {           
            device->Material = *meshMaterials[i];
            device->SetTexture(0, meshTextures[i]);
            mesh->DrawSubset(i);
        }
        // 렌더링 끝
        device->EndScene();
        device->Present();
    }   
protected:
    ~WndForm()
    {
    }
};

INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)
{   
    WndForm^ frm = gcnew WndForm();
    if (!frm->InitD3D())
    {
        MessageBox::Show("Initializing D3D Device Failed");
        return 0;
    }
    frm->Show();
    while (frm->Created)
    {
        frm->Render();
        Application::DoEvents();
    }
    return 0;
}

0 개의 댓글:

댓글 쓰기