DirectX/DirectX 3D_(구)

13_BaseMap

컴맹학자 2021. 7. 4. 17:50
728x90

Terrain.cpp,h (수정)

더보기
void Terrain::Render()
{
	//Visible NormalVector
	{
		static bool bCheck = false;
		static UINT interval = 1; // 그려줄 칸수 
		ImGui::Checkbox("Visible Normal", &bCheck);
		ImGui::SliderInt("Vertex interval", (int*)&interval, 1, 5);

		//중요한 내용 여기서 노말 벡터 선이 보여줌
		if (bCheck == true)
		{
			for (UINT z = 0; z < height; z += interval)
			{
				for (UINT x = 0; x < width; x += interval)
				{
					UINT index = width * z + x; //256 * 256

					Vector3 start = vertices[index].Position;
					Vector3 end = vertices[index].Position + vertices[index].Normal;

					DebugLine::Get()->RenderLine(start, end, Color(0, 1, 0, 1));
				}
			}
		}
	}

	if(baseMap != NULL)
		shader->AsSRV("BaseMap")->SetResource(baseMap->SRV());

	UINT stride = sizeof(VertexTerrain);
	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, pass, indexCount);
}

void Terrain::BaseMap(wstring file)
{
	SafeDelete(baseMap);
	baseMap = new Texture(file);
}

void Terrain::CreateVertexData()
{
	width = heightMap->GetWidth();
	height = heightMap->GetHeight();

	//그림의 컬라값을 저장함 (SRV 비슷하게 만든거)
	vector<Color> pixels;
	heightMap->ReadPixel(&pixels);

	vertexCount = width * height;
	vertices = new VertexTerrain[vertexCount];
	//여기서 '<=' -> '<' 로 변경
	for (UINT z = 0; z < height; z++) //깊이
	{
		for (UINT x = 0; x < width; x++) //좌우
		{
			UINT index = width * z + x; //그림을 반대로
			UINT pixel = width * (height - z -1) + x; //그림을 정상적

			vertices[index].Position.x = (float)x;
			vertices[index].Position.y = pixels[pixel].r * (255.0f / 10);
			vertices[index].Position.z = (float)z;

			//UV값
			vertices[index].Uv.x = x / ((float)width - 1);
			vertices[index].Uv.y = 1.0f - (z / ((float)height - 1));
		}
	}
}

cpp 내용

헤더에 변수 선언


13_Terrain.fx

더보기
matrix World, View, Projection;
float3 LightDirection = float3(-1, -1, +1);
Texture2D BaseMap;

struct VertexInput
{
    float4 Position : Position;
    float3 Normal : Normal;
    float2 Uv : Uv;
};

struct VertexOutput
{
    float4 Position : SV_Position;
    float3 Normal : Normal;
    float2 Uv : Uv;
};

RasterizerState FillMode_WireFrame
{
    FillMode = WireFrame;
};

SamplerState PointSampler
{
    Filter = MIN_MAG_MIP_POINT;
    AddressU = Wrap;
    AddressV = Wrap;
};

SamplerState LinearSampler
{
    Filter = MIN_MAG_MIP_LINEAR;
    AddressU = Wrap;
    AddressV = Wrap;
};

VertexOutput VS(VertexInput input)
{
    VertexOutput output;

    output.Position = mul(input.Position, World);
    output.Position = mul(output.Position, View);
    output.Position = mul(output.Position, Projection);

    output.Normal = mul(input.Normal, (float3x3) World);
    output.Uv = input.Uv;

    return output;
}


uint Albedo = 0;
float4 PS(VertexOutput input) : SV_Target
{
    float3 normal = normalize(input.Normal);
    LightDirection = normalize(LightDirection);

    float NdotL = dot(normal, -LightDirection) * 0.5f + 0.5f;

    float4 diffuse = float4(1, 1, 1, 1);
    diffuse = BaseMap.Sample(LinearSampler, input.Uv);

    if (Albedo == 1)
        return diffuse;

    return diffuse * NdotL;
}


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

실행

 

 


UV 좌표계랑 NDC 좌표계 위치는 다르므로 Terrain.cpp 안에 있는 CreateVertexData() 함수 UV값 셋팅할때 y값을 반전

반전 시켜줘야할 필요성이 있다.

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

16_CubeMap [ 1 / 2 ] (수정)  (0) 2021.07.06
15_Mesh  (0) 2021.07.06
12_Normal [1/2]  (0) 2021.07.03
11_Terrain  (0) 2021.07.02
10_Sampler  (0) 2021.07.02