前田稔(Maeda Minoru)の超初心者のプログラム入門
struct VertexPosition { DirectX::XMFLOAT3 pos; DirectX::XMFLOAT3 norm; }; |
const D3D11_INPUT_ELEMENT_DESC vertexDesc[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, }; |
VertexPosition cubeVertices[] = { { XMFLOAT3(-0.5f, 0.5f, -0.5f), XMFLOAT3(0.0f, 1.0f, 0.0f) }, // +Y (top face) { XMFLOAT3( 0.5f, 0.5f, -0.5f), XMFLOAT3(0.0f, 1.0f, 0.0f) }, { XMFLOAT3( 0.5f, 0.5f, 0.5f), XMFLOAT3(0.0f, 1.0f, 0.0f) }, { XMFLOAT3(-0.5f, 0.5f, 0.5f), XMFLOAT3(0.0f, 1.0f, 0.0f) }, { XMFLOAT3(-0.5f, -0.5f, 0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f) }, // -Y (bottom face) { XMFLOAT3( 0.5f, -0.5f, 0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f) }, { XMFLOAT3( 0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f) }, { XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, -1.0f, 0.0f) }, { XMFLOAT3(0.5f, 0.5f, 0.5f), XMFLOAT3(1.0f, 0.0f, 0.0f) }, // +X (right face) { XMFLOAT3(0.5f, 0.5f, -0.5f), XMFLOAT3(1.0f, 0.0f, 0.0f) }, { XMFLOAT3(0.5f, -0.5f, -0.5f), XMFLOAT3(1.0f, 0.0f, 0.0f) }, { XMFLOAT3(0.5f, -0.5f, 0.5f), XMFLOAT3(1.0f, 0.0f, 0.0f) }, { XMFLOAT3(-0.5f, 0.5f, -0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f) }, // -X (left face) { XMFLOAT3(-0.5f, 0.5f, 0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f) }, { XMFLOAT3(-0.5f, -0.5f, 0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f) }, { XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(-1.0f, 0.0f, 0.0f) }, { XMFLOAT3(-0.5f, 0.5f, 0.5f), XMFLOAT3(0.0f, 0.0f, 1.0f) }, // +Z (front face) { XMFLOAT3( 0.5f, 0.5f, 0.5f), XMFLOAT3(0.0f, 0.0f, 1.0f) }, { XMFLOAT3( 0.5f, -0.5f, 0.5f), XMFLOAT3(0.0f, 0.0f, 1.0f) }, { XMFLOAT3(-0.5f, -0.5f, 0.5f), XMFLOAT3(0.0f, 0.0f, 1.0f) }, { XMFLOAT3( 0.5f, 0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f) }, // -Z (back face) { XMFLOAT3(-0.5f, 0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f) }, { XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f) }, { XMFLOAT3( 0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, -1.0f) }, }; |
unsigned short modelIndices[] = { 0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11, 12, 13, 14, 12, 14, 15, 16, 17, 18, 16, 18, 19, 20, 21, 22, 20, 22, 23 }; |
struct PixelShaderInput { float4 pos : SV_POSITION; float3 norm : NORMAL; }; float4 main(PixelShaderInput input) : SV_TARGET { float3 lightDirection = normalize(float3(1, -1, 0)); float lightMagnitude = 0.8f * saturate(dot( input.norm, -lightDirection)) + 0.2f; return float4(1,1,0.2,1) * lightMagnitude; } |
cbuffer ModelViewProjectionConstantBuffer : register(b0) { matrix model; matrix view; matrix projection; }; struct VertexShaderInput { float3 pos : POSITION; float3 norm : NORMAL; }; struct VertexShaderOutput { float4 pos : SV_POSITION; float3 norm : NORMAL; }; VertexShaderOutput main(VertexShaderInput input) { VertexShaderOutput output; float4 pos = float4(input.pos, 1.0f); // Transform the vertex position into projected space. pos = mul(pos, model); pos = mul(pos, view); pos = mul(pos, projection); output.pos = pos; float4 norm = float4(normalize(input.norm), 0.0f); norm = mul(norm, model); output.norm = normalize(norm.xyz); return output; } |
#define KAKU 7 // (pyramid の角の数+1) // モデルの頂点座標 VertexPosition modelVertices[KAKU*2]; // モデルの頂点 Index unsigned short modelIndices[KAKU*6]; |
for(int slice=0; slice<KAKU; slice++) { float v = (float)slice/(float)(KAKU-1); float theta = v * 3.14f * 2; modelVertices[2*slice+0].pos = XMFLOAT3( sinf(theta),-1.0f, cosf(theta) ); modelVertices[2*slice+0].norm = XMFLOAT3( sinf(theta), 1.0f, cosf(theta) ); modelVertices[2*slice+1].pos = XMFLOAT3( 0.0f, 1.0f, 0.0f ); modelVertices[2*slice+1].norm = XMFLOAT3( sinf(theta), 1.0f, cosf(theta) ); } for(int slice=0; slice<(KAKU-1); slice++) { modelIndices[3*slice+0] = slice*2+0; modelIndices[3*slice+1] = slice*2+1; modelIndices[3*slice+2] = slice*2+2; modelIndices[3*(KAKU+slice)+0] = slice*2+3; modelIndices[3*(KAKU+slice)+1] = slice*2+2; modelIndices[3*(KAKU+slice)+2] = slice*2+1; } |