DirectX/DirectX 3D_(구)

12_Normal [1/2]

컴맹학자 2021. 7. 3. 20:48
728x90

11_Terrain 만든 지형에다가 법선 벡터라는걸 찍어 볼려고 함

 

Terrain.cpp, h (수정)

더보기
헤더에 함수선언과 구조체 수정
cpp에 CreateNormalData 몸체 구현 
마무리로 생성자에 해당 함수 추가

 

Render에 박스 영역만큼 추가

DebugLine.cpp, h

더보기
#include "Framework.h"
#include "DebugLine.h"

DebugLine* DebugLine::instance = NULL;

void DebugLine::Create()
{
	assert(instance == NULL); //이미 만들어지면 터짐
	instance = new DebugLine();
}

void DebugLine::Delete()
{
	SafeDelete(instance);
}

DebugLine * DebugLine::Get()
{
	assert(instance != NULL);
	return instance;
}

void DebugLine::RenderLine(Vector3 & start, Vector3 & end)
{
	RenderLine(start, end, Color(0, 1, 0, 1));
}

void DebugLine::RenderLine(Vector3 & start, Vector3 & end, float r, float g, float b)
{
	RenderLine(start, end, Color(r, g, b, 1));
}

void DebugLine::RenderLine(float x, float y, float z, float x2, float y2, float z2)
{
	RenderLine(Vector3(x, y, z), Vector3(x2, y2, z2), Color(0, 1, 0, 1));
}

void DebugLine::RenderLine(float x, float y, float z, float x2, float y2, float z2, float r, float g, float b)
{
	RenderLine(Vector3(x, y, z), Vector3(x2, y2, z2), Color(r, g, b, 1));
}

void DebugLine::RenderLine(float x, float y, float z, float x2, float y2, float z2, Color & color)
{
	RenderLine(Vector3(x, y, z), Vector3(x2, y2, z2), color);
}

//최종적으로 값이 들어오는곳
void DebugLine::RenderLine(Vector3 & start, Vector3 & end, Color & color)
{
	vertices[drawCount].Position = start;
	vertices[drawCount++].Color = color;

	vertices[drawCount].Position = end;
	vertices[drawCount++].Color = color;
}

void DebugLine::Update()
{
	Matrix world;
	D3DXMatrixIdentity(&world);

	shader->AsMatrix("World")->SetMatrix(world);
	shader->AsMatrix("View")->SetMatrix(Context::Get()->View());
	shader->AsMatrix("Projection")->SetMatrix(Context::Get()->Projection());
}

void DebugLine::Render()
{
	if (drawCount < 1) return; // 한번도 호출안했을때 빠져나옴

	//정점의 위치가 바뀌면 UpdateSubresoruce 수정한 내용까지만 조절
	D3D::GetDC()->UpdateSubresource(vertexBuffer, 0, NULL, vertices, sizeof(VertexColor)* drawCount, 0);
	
	//c++ 에서 수정해서 사용할떈 Map 사용하는데 D3D11_BUFFER_DESC -> CPUAccessFlags 수정

	UINT stride = sizeof(VertexColor);
	UINT offset = 0;

	D3D::GetDC()->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINELIST);
	D3D::GetDC()->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);

	shader->Draw(0, 0, drawCount);

	drawCount = 0;
	ZeroMemory(vertices, sizeof(VertexColor) * MAX_DEBUG_LINE);
}

DebugLine::DebugLine()
{
	shader = new Shader(L"12_DebugLine.fxo");
	vertices = new VertexColor[MAX_DEBUG_LINE];
	ZeroMemory(vertices, sizeof(VertexColor), MAX_DEBUG_LINE);

	//Create VertexBuffer
	{
		D3D11_BUFFER_DESC desc;
		ZeroMemory(&desc, sizeof(D3D11_BUFFER_DESC));
		desc.ByteWidth = sizeof(VertexColor) * MAX_DEBUG_LINE;
		desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;

		D3D11_SUBRESOURCE_DATA subResouce = { 0 };
		subResouce.pSysMem = vertices;

		Check(D3D::GetDevice()->CreateBuffer(&desc, &subResouce, &vertexBuffer));
	}
}

DebugLine::~DebugLine()
{
	SafeDelete(shader);

	SafeDeleteArray(vertices);
	SafeRelease(vertexBuffer);
}
#pragma once
//눈에 보이기 위한 Line 클래스
#define MAX_DEBUG_LINE 150000

class DebugLine 
{
public:
	friend class Window; //윈도우만 접근

private:
	static void Create();
	static void Delete();

public:
	static DebugLine* Get();

public:
	void RenderLine(Vector3& start, Vector3& end);
	void RenderLine(Vector3& start, Vector3& end, float r, float g, float b);
	void RenderLine(Vector3& start, Vector3& end, Color& color);

	void RenderLine(float x, float y, float z, float x2, float y2, float z2);
	void RenderLine(float x, float y, float z, float x2, float y2, float z2, float r, float g, float b);
	void RenderLine(float x, float y, float z, float x2, float y2, float z2, Color& color);

private:
	DebugLine();
	~DebugLine();

private:
	void Update();
	void Render();

private:
	struct VertexColor
	{
		VertexColor()
			:Position(0, 0, 0)
			, Color(0, 0, 0, 1) {}

		VertexColor(float x, float y, float z, float r, float g, float b)
		{
			Position.x = x;
			Position.y = x;
			Position.z = x;

			Color.r = r;
			Color.g = g;
			Color.b = b;
			Color.a = 1.0f;
		}

		Vector3 Position;
		Color Color;
	};

private:
	static DebugLine* instance;

private:
	Shader* shader;
	ID3D11Buffer* vertexBuffer;
	VertexColor* vertices;

	UINT drawCount = 0;

};

12_DebugLine.fx

더보기
matrix World, View, Projection;

struct VertexInput
{
	float4 Position : Position;
    float4 Color : Color;
};

struct VertexOutput
{
	float4 Position : SV_Position;
    float4 Color : Color;
};

VertexOutput VS(VertexInput input)
{
	VertexOutput output;
    output.Position = mul(input.Position, World);
    output.Position = mul(input.Position, View);
    output.Position = mul(input.Position, Projection);
    
    output.Color = input.Color;
	
	return output;
}

float4 PS(VertexOutput input) :SV_Target
{
    return input.Color;
}

technique11 T0
{
	pass P0
	{
		SetVertexShader(CompileShader(vs_5_0, VS()));
		SetPixelShader(CompileShader(ps_5_0, PS()));
	}

}

실행

 


추가내용

위로 향하는 공식을 사용해서 선을 그림

 

DebugLine 클래스는 싱글톤 방식으로 따로 선언 방식을 안썻지만 Window 클래스에서 생성, 삭제, 출력, 업데이트 등

해줘야함 

'DirectX > DirectX 3D_(구)' 카테고리의 다른 글

15_Mesh  (0) 2021.07.06
13_BaseMap  (0) 2021.07.04
11_Terrain  (0) 2021.07.02
10_Sampler  (0) 2021.07.02
09_Texture  (0) 2021.06.30