SlimDX Tutorial 5 - 조명 설정


#include <Windows.h>
using namespace System;
using namespace System::Windows::Forms;
using namespace System::Drawing;
using namespace SlimDX;
using namespace SlimDX::Direct3D9;
struct ColorVertex
{
    float x, y, z;
    int color;
};
public ref class WndForm : public Form
{   
    Direct3D^ direct3D;
    DeviceType^ deviceType;
    Device^ device;
    VertexBuffer^ vertexBuffer;
    PresentParameters^ presentParams;
    String^ lastError;
    
public:
    WndForm(void)
    {   
        this->ClientSize = System::Drawing::Size(400, 300);
        this->StartPosition = FormStartPosition::CenterScreen;
        this->Text = "D3D Tutorial 3";
    }
    bool InitD3D()
    {
        try
        {
            // 환경변수 세팅
            direct3D = gcnew Direct3D();
            presentParams = gcnew PresentParameters();
            presentParams->Windowed = true;
            presentParams->SwapEffect = SwapEffect::Discard;
            presentParams->BackBufferFormat = Format::Unknown;
            // 디바이스 생성
            device = gcnew Device(direct3D, 0, DeviceType::Hardware, this->Handle, CreateFlags::HardwareVertexProcessing, presentParams);
            // 디바이스 세팅
            device->SetRenderState(RenderState::CullMode, Cull::None);
            device->SetRenderState(RenderState::Lighting, false);
            // 버퍼 생성            
            vertexBuffer = gcnew VertexBuffer(device, 3 * sizeof(ColorVertex), Usage::None, VertexFormat::Position | VertexFormat::Diffuse, Pool::Default);
            
            ColorVertex vertx[] =
            {
                { 1.0f,-1.0f, 0.0f, 0xffff0000, },
                { -1.0f,-1.0f, 0.0f, 0xff0000ff, },
                {  0.0f, 1.0f, 0.0f, 0xffffffff, },
            };
                        
            DataStream^ stream = vertexBuffer->Lock(0, 0, LockFlags::None);
            IntPtr vptr(vertx);
            stream->WriteRange(vptr, 3 * sizeof(ColorVertex));
            vertexBuffer->Unlock();
            return true;
        }
        catch (Direct3D9Exception^ ex)
        {
            lastError = ex->ToString();
            return false;
        }
    }
    void SetupMatrix()
    {
        int iTime = Environment::TickCount % 1000;
        float fAngle = iTime * (2.0f * static_cast<float>(Math::PI)) / 1000.0f;     
        
        device->SetTransform(TransformState::World, Matrix::RotationY(fAngle));
        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(static_cast<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();
        // 렌더링       
        device->SetStreamSource(0, vertexBuffer, 0, sizeof(ColorVertex));       
        device->VertexFormat = VertexFormat::Position | VertexFormat::Diffuse;
        device->DrawPrimitives(PrimitiveType::TriangleList, 0, 1);
        // 렌더링 끝
        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 개의 댓글:

댓글 쓰기