목차

     

     


     

     

    Cube

     

     


    Cube.fx

     

    Cube.fx

    더보기
    matrix World;
    matrix View;
    matrix Projection;
    float4 Color;
    
    struct VertexInput
    {
        float4 Position : Position;
    };
    
    struct VertexOutput
    {
        float4 Position : SV_Position;
    };
    
    VertexOutput VS(VertexInput input)
    {
        VertexOutput output;
        output.Position = mul(input.Position, World);
        output.Position = mul(output.Position, View);
        output.Position = mul(output.Position, Projection);
        
        return output;
    }
    
    float4 PS(VertexOutput input) : SV_Target
    {
        return Color;
    }
    
    RasterizerState FillMode_Wireframe
    {
        FillMode = Wireframe;
    };
    
    technique11 T0
    {
        pass P0
        {
            SetVertexShader(CompileShader(vs_5_0, VS()));
            SetPixelShader(CompileShader(ps_5_0, PS()));
        }
    
        pass P1
        {
            SetRasterizerState(FillMode_Wireframe);
    
            SetVertexShader(CompileShader(vs_5_0, VS()));
            SetPixelShader(CompileShader(ps_5_0, PS()));
        }
    }

     

     


     

    Context

     

    Context.h

    더보기
    #pragma once
    
    class Context
    {
    public:
    	static Context* Get();
    	static void Create();
    	static void Delete();
    
    private:
    	Context();
    	~Context();
    
    public:
    	void ResizeScreen();
    
    	void Update();
    	void Render();
    
    	Matrix View();
    	Matrix Projection();
    
    	class Perspective* GetPerspective() { return perspective; }
    	class Viewport* GetViewport() { return viewport; }
    	class Camera* GetCamera() { return camera; }
    
    private:
    	static Context* instance;
    
    private:
    	class Perspective* perspective;
    	class Viewport* viewport;
    	class Camera* camera;
    };

     

     

    Context.cpp

    더보기
    #include "Framework.h"
    #include "Context.h"
    #include "Viewer/Viewport.h"
    #include "Viewer/Perspective.h"
    #include "Viewer/Freedom.h"
    
    Context* Context::instance = NULL;
    
    Context * Context::Get()
    {
    	//assert(instance != NULL);
    
    	return instance;
    }
    
    void Context::Create()
    {
    	assert(instance == NULL);
    
    	instance = new Context();
    }
    
    void Context::Delete()
    {
    	SafeDelete(instance);
    }
    
    Context::Context()
    {
    	D3DDesc desc = D3D::GetDesc();
    
    	perspective = new Perspective(desc.Width, desc.Height);
    	viewport = new Viewport(desc.Width, desc.Height);
    	camera = new Freedom();
    }
    
    Context::~Context()
    {
    	SafeDelete(perspective);
    	SafeDelete(viewport);
    	SafeDelete(camera);
    }
    
    void Context::ResizeScreen()
    {
    	perspective->Set(D3D::Width(), D3D::Height());
    	viewport->Set(D3D::Width(), D3D::Height());
    }
    
    void Context::Update()
    {
    	camera->Update();
    }
    
    void Context::Render()
    {
    	viewport->RSSetViewport();
    
    	
    	string str = string("FrameRate : ") + to_string(ImGui::GetIO().Framerate);
    	Gui::Get()->RenderText(5, 5, 1, 1, 1, str);
    
    	Vector3 R;
    	camera->RotationDegree(&R);
    
    	Vector3 P;
    	camera->Position(&P);
    
    	str = "Camera Rotation : ";
    	str += to_string((int)R.x) + ", " + to_string((int)R.y) + ", " + to_string((int)R.z);
    	Gui::Get()->RenderText(5, 20, 1, 1, 1, str);
    
    	str = "Camera Position : ";
    	str += to_string((int)P.x) + ", " + to_string((int)P.y) + ", " + to_string((int)P.z);
    	Gui::Get()->RenderText(5, 35, 1, 1, 1, str);
    }
    
    Matrix Context::View()
    {
    	Matrix view;
    	camera->GetMatrix(&view);
    
    	return view;
    }
    
    D3DXMATRIX Context::Projection()
    {
    	Matrix projection;
    	perspective->GetMatrix(&projection);
    
    	return projection;
    }

     

     


     

    Cube

     

    GridDemo.h와  GridDemo.cpp 그 전과 동일하게 사용

     

    CubeDemo.h

    더보기
    #pragma once
    #include "Systems/IExecute.h"
    
    class CubeDemo : public IExecute
    {
    public:
    	virtual void Initialize() override;
    	virtual void Ready() override {}
    	virtual void Destroy() override;
    	virtual void Update() override;
    	virtual void PreRender() override {}
    	virtual void Render() override;
    	virtual void PostRender() override {}
    	virtual void ResizeScreen() override {}
    
    private:
    	Shader* shader;
    
    	UINT vertexCount;
    	Vertex* vertices;
    	ID3D11Buffer* vertexBuffer;
    
    	UINT indexCount;
    	UINT* indices;
    	ID3D11Buffer* indexBuffer;
    
    	Vector3 position = Vector3(0, 0, 0);
    	Vector3 rotation = Vector3(0, 0, 0);
    	Matrix world;
    	Color color = Color(0, 0, 1, 1);
    };

     

     

    CubeDemo.cpp

    더보기
    #include "stdafx.h"
    #include "CubeDemo.h"
    
    void CubeDemo::Initialize()
    {
    	Context::Get()->GetCamera()->RotationDegree(13, 0, 0);
    	Context::Get()->GetCamera()->Position(0, 7, -19);
    
    
    	shader = new Shader(L"15_Cube.fx");
    	
    	
    	float w = 0.5f, h = 0.5f, d = 0.5f;
    	
    	vertexCount = 24;
    	vertices = new Vertex[vertexCount];
    
    	//Front
    	vertices[0].Position = Vector3(-w, -h, -d);
    	vertices[1].Position = Vector3(-w, +h, -d);
    	vertices[2].Position = Vector3(+w, -h, -d);
    	vertices[3].Position = Vector3(+w, +h, -d);
    
    	//Back
    	vertices[4].Position = Vector3(-w, -h, +d);
    	vertices[5].Position = Vector3(+w, -h, +d);
    	vertices[6].Position = Vector3(-w, +h, +d);
    	vertices[7].Position = Vector3(+w, +h, +d);
    
    	//Top
    	vertices[8].Position = Vector3(-w, +h, -d);
    	vertices[9].Position = Vector3(-w, +h, +d);
    	vertices[10].Position = Vector3(+w, +h, -d);
    	vertices[11].Position = Vector3(+w, +h, +d);
    
    	//Bottom
    	vertices[12].Position = Vector3(-w, -h, -d);
    	vertices[13].Position = Vector3(+w, -h, -d);
    	vertices[14].Position = Vector3(-w, -h, +d);
    	vertices[15].Position = Vector3(+w, -h, +d);
    
    	//Left
    	vertices[16].Position = Vector3(-w, -h, +d);
    	vertices[17].Position = Vector3(-w, +h, +d);
    	vertices[18].Position = Vector3(-w, -h, -d);
    	vertices[19].Position = Vector3(-w, +h, -d);
    
    	//Right
    	vertices[20].Position = Vector3(+w, -h, -d);
    	vertices[21].Position = Vector3(+w, +h, -d);
    	vertices[22].Position = Vector3(+w, -h, +d);
    	vertices[23].Position = Vector3(+w, +h, +d);
    
    
    	indexCount = 36;
    	indices = new UINT[indexCount]
    	{
    		0, 1, 2, 2, 1, 3,
    		4, 5, 6, 6, 5, 7,
    		8, 9, 10, 10, 9, 11,
    		12, 13, 14, 14, 13, 15,
    		16, 17, 18, 18, 17, 19,
    		20, 21, 22, 22, 21, 23
    	};
    	
    
    
    	//Create Vertex Buffer
    	{
    		D3D11_BUFFER_DESC desc;
    		ZeroMemory(&desc, sizeof(D3D11_BUFFER_DESC));
    		desc.ByteWidth = sizeof(Vertex) * vertexCount;
    		desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    
    		D3D11_SUBRESOURCE_DATA subResource = { 0 };
    		subResource.pSysMem = vertices;
    
    		Check(D3D::GetDevice()->CreateBuffer(&desc, &subResource, &vertexBuffer));
    	}
    	
    
    	//Create Index Buffer
    	{
    		D3D11_BUFFER_DESC desc;
    		ZeroMemory(&desc, sizeof(D3D11_BUFFER_DESC));
    		desc.ByteWidth = sizeof(UINT) * indexCount;
    		desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
    
    		D3D11_SUBRESOURCE_DATA subResource = { 0 };
    		subResource.pSysMem = indices;
    
    		Check(D3D::GetDevice()->CreateBuffer(&desc, &subResource, &indexBuffer));
    	}
    	
    	SafeDeleteArray(vertices);
    	SafeDeleteArray(indices);
    
    
    	D3DXMatrixIdentity(&world);
    }
    
    void CubeDemo::Destroy()
    {
    	SafeDelete(shader);
    
    	SafeRelease(vertexBuffer);
    	SafeRelease(indexBuffer);
    }
    
    void CubeDemo::Update()
    {
    	static float speed = 10.0f;
    	ImGui::SliderFloat("Speed", &speed, 5, 20);
    
    	static float speed2 = 2.0f;
    	ImGui::SliderFloat("Speed2", &speed2, 1, 5);
    
    	if (Keyboard::Get()->Press(VK_SHIFT))
    	{
    		if (Keyboard::Get()->Press(VK_RIGHT))
    			rotation.x += speed * Time::Delta();
    		else if (Keyboard::Get()->Press(VK_LEFT))
    			rotation.x -= speed * Time::Delta();
    
    		if (Keyboard::Get()->Press(VK_UP))
    			rotation.z += speed * Time::Delta();
    		else if (Keyboard::Get()->Press(VK_DOWN))
    			rotation.z -= speed * Time::Delta();
    	}
    	else
    	{
    		if (Keyboard::Get()->Press(VK_RIGHT))
    			position.x += speed * Time::Delta();
    		else if (Keyboard::Get()->Press(VK_LEFT))
    			position.x -= speed * Time::Delta();
    
    		if (Keyboard::Get()->Press(VK_UP))
    			position.z += speed * Time::Delta();
    		else if (Keyboard::Get()->Press(VK_DOWN))
    			position.z -= speed * Time::Delta();
    	}
    
    	Matrix R, T;
    	D3DXMatrixRotationYawPitchRoll(&R, rotation.y, rotation.x, rotation.z);
    	D3DXMatrixTranslation(&T, position.x, position.y, position.z);
    
    	world = R * T;
    }
    
    void CubeDemo::Render()
    {
    	shader->AsVector("Color")->SetFloatVector(color);
    	shader->AsMatrix("World")->SetMatrix(world);
    	shader->AsMatrix("View")->SetMatrix(Context::Get()->View());
    	shader->AsMatrix("Projection")->SetMatrix(Context::Get()->Projection());
    
    
    	UINT stride = sizeof(Vertex);
    	UINT offset = 0;
    
    	D3D::GetDC()->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
    	D3D::GetDC()->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
    	D3D::GetDC()->IASetIndexBuffer(indexBuffer, DXGI_FORMAT_R32_UINT, 0);
    	
    	shader->DrawIndexed(0, 0, indexCount);
    }

     

    Main.cpp

    더보기
    #include "stdafx.h"
    #include "Main.h"
    #include "Systems/Window.h"
    
    #include "CubeDemo.h"
    #include "GridDemo.h"
    
    void Main::Initialize()
    {
    	Push(new CubeDemo());
    	Push(new GridDemo());
    }
    
    아래코드 전과 동일

     

     

     


     

    실행화면

     

    '⭐ DirectX > DirectX11 3D' 카테고리의 다른 글

    [DirectX11] 017 Texture Load  (0) 2023.01.14
    [DirectX11] 016 Texture  (0) 2023.01.14
    [DirectX11] 013~014 Camera  (0) 2023.01.11
    [DirectX11] 012 Grid  (1) 2023.01.10
    [DirectX11] 011 Index  (0) 2023.01.10