OSDN Git Service

Z座標で優先制御できるようにした。
authorsfpgmr <sfpg@users.sourceforge.jp>
Sun, 29 Apr 2012 22:52:18 +0000 (07:52 +0900)
committersfpgmr <sfpg@users.sourceforge.jp>
Sun, 29 Apr 2012 22:52:18 +0000 (07:52 +0900)
ShootingGame/BasicSprites.GeometryShader.gs.hlsl
ShootingGame/BasicSprites.GeometryShader.vs.hlsl
ShootingGame/BasicSprites.Instancing.vs.hlsl
ShootingGame/BasicSprites.Replication.vs.hlsl
ShootingGame/BasicSprites.cpp
ShootingGame/BasicSprites.h
ShootingGame/SimpleSprites.cpp

index 27290f9..e38ed4a 100644 (file)
@@ -13,7 +13,7 @@ cbuffer RenderTargetInfoCB
 
 struct GeometryShaderInput
 {
-    float2 origin : TRANSFORM0;
+    float4 origin : TRANSFORM0;
     float2 offset : TRANSFORM1;
     float rotation : TRANSFORM2;
     float4 color : COLOR0;
@@ -59,7 +59,8 @@ void main(point GeometryShaderInput input[1], inout TriangleStream<PixelShaderIn
             );
         posDelta[i] /= renderTargetSize;
         PixelShaderInput streamElement;
-        streamElement.pos = float4(input[0].origin + posDelta[i], 0.5f, 1.0f);
+//        streamElement.pos = float4(input[0].origin.x + posDelta[i].x,input[0].origin.y + posDelta[i].y, input[0].origin.z, input[0].origin.w);
+        streamElement.pos = float4(input[0].origin.x + posDelta[i].x,input[0].origin.y + posDelta[i].y, input[0].origin.z, input[0].origin.w);
         streamElement.tex = texCoord[i];
         streamElement.color = input[0].color;
         spriteStream.Append(streamElement);
index 3ce1549..1fcc4af 100644 (file)
@@ -8,7 +8,7 @@
 
 struct VertexShaderInput
 {
-    float2 origin : TRANSFORM0;
+    float4 origin : TRANSFORM0;
     float2 offset : TRANSFORM1;
     float rotation : TRANSFORM2;
     float4 color : COLOR0;
@@ -16,7 +16,7 @@ struct VertexShaderInput
 
 struct GeometryShaderInput
 {
-    float2 origin : TRANSFORM0;
+    float4 origin : TRANSFORM0;
     float2 offset : TRANSFORM1;
     float rotation : TRANSFORM2;
     float4 color : COLOR0;
index 690b9f3..34a0f51 100644 (file)
@@ -13,9 +13,9 @@ cbuffer RenderTargetInfoCB
 
 struct VertexShaderInput
 {
-    float2 pos : POSITION;
+    float4 pos : POSITION;
     float2 tex : TEXCOORD0;
-    float2 origin : TRANSFORM0;
+    float4 origin : TRANSFORM0;
     float2 offset : TRANSFORM1;
     float rotation : TRANSFORM2;
     float4 color : COLOR0;
@@ -38,13 +38,13 @@ PixelShaderInput main(VertexShaderInput input)
     float sinRotation;
     float cosRotation;
     sincos(input.rotation, sinRotation, cosRotation);
-    float2 posDelta = input.pos * input.offset;
+    float2 posDelta = float2(input.pos.x * input.offset.x,input.pos.y * input.offset.y);
     posDelta = float2(
         posDelta.x * cosRotation - posDelta.y * sinRotation,
         posDelta.x * sinRotation + posDelta.y * cosRotation
         );
     posDelta /= renderTargetSize;
-    ret.pos = float4(input.origin + posDelta, 0.5f, 1.0f);
+    ret.pos = float4(input.origin.x + posDelta.x,input.origin.y + posDelta.y, input.origin.z,input.origin.w);
     ret.tex = input.tex;
     ret.color = input.color;
     return ret;
index 052f8d0..86659a8 100644 (file)
@@ -8,7 +8,7 @@
 
 struct VertexShaderInput
 {
-    float2 pos : POSITIONT;
+    float4 pos : POSITIONT;
     float2 tex : TEXCOORD0;
     float4 color : COLOR0;
 };
@@ -25,7 +25,8 @@ struct PixelShaderInput
 PixelShaderInput main(VertexShaderInput input)
 {
     PixelShaderInput ret;
-    ret.pos = float4(input.pos, 0.5f, 1.0f);
+//    ret.pos = float4(input.pos, 0.5f, 1.0f);
+    ret.pos = input.pos;
     ret.tex = input.tex;
     ret.color = input.color;
     return ret;
index 71f419c..88bb9b9 100644 (file)
@@ -65,7 +65,9 @@ void SpriteBatch::Initialize(
 
     D3D11_SAMPLER_DESC samplerDesc;
     ZeroMemory(&samplerDesc, sizeof(samplerDesc));
-    samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
+//    samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
+       samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
+
     samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
     samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
     samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
@@ -124,10 +126,10 @@ void SpriteBatch::Initialize(
     {
         D3D11_INPUT_ELEMENT_DESC layoutDesc[] = 
         {
-            { "TRANSFORM", 0, DXGI_FORMAT_R32G32_FLOAT,   0, 0,  D3D11_INPUT_PER_INSTANCE_DATA, 1 },
-            { "TRANSFORM", 1, DXGI_FORMAT_R32G32_FLOAT,   0, 8,  D3D11_INPUT_PER_INSTANCE_DATA, 1 },
-            { "TRANSFORM", 2, DXGI_FORMAT_R32_FLOAT,      0, 16, D3D11_INPUT_PER_INSTANCE_DATA, 1 },
-            { "COLOR",     0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 20, D3D11_INPUT_PER_INSTANCE_DATA, 1 }
+            { "TRANSFORM", 0, DXGI_FORMAT_R32G32B32A32_FLOAT,   0, 0,  D3D11_INPUT_PER_INSTANCE_DATA, 1 },
+            { "TRANSFORM", 1, DXGI_FORMAT_R32G32_FLOAT,   0, 16,  D3D11_INPUT_PER_INSTANCE_DATA, 1 },
+            { "TRANSFORM", 2, DXGI_FORMAT_R32_FLOAT,      0, 24, D3D11_INPUT_PER_INSTANCE_DATA, 1 },
+            { "COLOR",     0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 28, D3D11_INPUT_PER_INSTANCE_DATA, 1 }
         };
         loader->LoadShader(
             "BasicSprites.GeometryShader.vs.cso",
@@ -145,12 +147,12 @@ void SpriteBatch::Initialize(
     {
         D3D11_INPUT_ELEMENT_DESC layoutDesc[] = 
         {
-            { "POSITION",  0, DXGI_FORMAT_R32G32_FLOAT,   0, 0,  D3D11_INPUT_PER_VERTEX_DATA,   0 },
-            { "TEXCOORD",  0, DXGI_FORMAT_R32G32_FLOAT,   0, 8,  D3D11_INPUT_PER_VERTEX_DATA,   0 },
-            { "TRANSFORM", 0, DXGI_FORMAT_R32G32_FLOAT,   1, 0,  D3D11_INPUT_PER_INSTANCE_DATA, 1 },
-            { "TRANSFORM", 1, DXGI_FORMAT_R32G32_FLOAT,   1, 8,  D3D11_INPUT_PER_INSTANCE_DATA, 1 },
-            { "TRANSFORM", 2, DXGI_FORMAT_R32_FLOAT,      1, 16, D3D11_INPUT_PER_INSTANCE_DATA, 1 },
-            { "COLOR",     0, DXGI_FORMAT_R8G8B8A8_UNORM, 1, 20, D3D11_INPUT_PER_INSTANCE_DATA, 1 }
+            { "POSITION",  0, DXGI_FORMAT_R32G32B32A32_FLOAT,   0, 0,  D3D11_INPUT_PER_VERTEX_DATA,   0 },
+            { "TEXCOORD",  0, DXGI_FORMAT_R32G32_FLOAT,   0, 16,  D3D11_INPUT_PER_VERTEX_DATA,   0 },
+            { "TRANSFORM", 0, DXGI_FORMAT_R32G32B32A32_FLOAT,   1, 0,  D3D11_INPUT_PER_INSTANCE_DATA, 1 },
+            { "TRANSFORM", 1, DXGI_FORMAT_R32G32_FLOAT,   1, 16,  D3D11_INPUT_PER_INSTANCE_DATA, 1 },
+            { "TRANSFORM", 2, DXGI_FORMAT_R32_FLOAT,      1, 24, D3D11_INPUT_PER_INSTANCE_DATA, 1 },
+            { "COLOR",     0, DXGI_FORMAT_R8G8B8A8_UNORM, 1, 28, D3D11_INPUT_PER_INSTANCE_DATA, 1 }
         };
         loader->LoadShader(
             "BasicSprites.Instancing.vs.cso",
@@ -164,9 +166,9 @@ void SpriteBatch::Initialize(
     {
         D3D11_INPUT_ELEMENT_DESC layoutDesc[] = 
         {
-            { "POSITIONT", 0, DXGI_FORMAT_R32G32_FLOAT,   0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0 },
-            { "TEXCOORD",  0, DXGI_FORMAT_R32G32_FLOAT,   0, 8,  D3D11_INPUT_PER_VERTEX_DATA, 0 },
-            { "COLOR",     0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 16, D3D11_INPUT_PER_VERTEX_DATA, 0 }
+            { "POSITIONT", 0, DXGI_FORMAT_R32G32B32A32_FLOAT,   0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0 },
+            { "TEXCOORD",  0, DXGI_FORMAT_R32G32_FLOAT,   0, 16,  D3D11_INPUT_PER_VERTEX_DATA, 0 },
+            { "COLOR",     0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 }
         };
         loader->LoadShader(
             "BasicSprites.Replication.vs.cso",
@@ -209,10 +211,10 @@ void SpriteBatch::Initialize(
 
         InstancingVertex vertexBufferData[] = 
         {
-            { float2(-1.0f,  1.0f), float2(0.0f, 0.0f) },
-            { float2( 1.0f,  1.0f), float2(1.0f, 0.0f) },
-            { float2(-1.0f, -1.0f), float2(0.0f, 1.0f) },
-            { float2( 1.0f, -1.0f), float2(1.0f, 1.0f) }
+            { float4(-1.0f,  1.0f , 0.0f , 0.0f), float2(0.0f, 0.0f) },
+            { float4( 1.0f,  1.0f , 0.0f , 0.0f), float2(1.0f, 0.0f) },
+            { float4(-1.0f, -1.0f , 0.0f , 0.0f), float2(0.0f, 1.0f) },
+            { float4( 1.0f, -1.0f , 0.0f , 0.0f), float2(1.0f, 1.0f) }
         };
 
         D3D11_SUBRESOURCE_DATA vertexInitialData;
@@ -651,9 +653,13 @@ unsigned int SpriteBatch::MakeUnorm(float4 color)
         r;
 }
 
-float2 SpriteBatch::StandardOrigin(float2 position, PositionUnits positionUnits, float2 renderTargetSize, float dpi)
+float4 SpriteBatch::StandardOrigin(float4 position, PositionUnits positionUnits, float2 renderTargetSize, float dpi)
 {
-    float2 origin;
+    float4 origin;
+       
+       origin.z = position.z;
+       origin.w = position.w;
+
     if (positionUnits == PositionUnits::Pixels)
     {
         origin.x = (position.x / renderTargetSize.x) * 2.0f - 1.0f;
@@ -702,7 +708,7 @@ float2 SpriteBatch::StandardOffset(float2 size, SizeUnits sizeUnits, float2 spri
 
 void SpriteBatch::Draw(
     _In_ ID3D11Texture2D* texture,
-    _In_ float2 position,
+    _In_ float4 position,
     _In_ PositionUnits positionUnits = PositionUnits::DIPs
     )
 {
@@ -717,7 +723,7 @@ void SpriteBatch::Draw(
 
 void SpriteBatch::Draw(
     _In_ ID3D11Texture2D* texture,
-    _In_ float2 position,
+    _In_ float4 position,
     _In_ PositionUnits positionUnits,
     _In_ float2 size,
     _In_ SizeUnits sizeUnits
@@ -735,7 +741,7 @@ void SpriteBatch::Draw(
 
 void SpriteBatch::Draw(
     _In_ ID3D11Texture2D* texture,
-    _In_ float2 position,
+    _In_ float4 position,
     _In_ PositionUnits positionUnits,
     _In_ float2 size,
     _In_ SizeUnits sizeUnits,
@@ -755,7 +761,7 @@ void SpriteBatch::Draw(
 
 void SpriteBatch::Draw(
     _In_ ID3D11Texture2D* texture,
-    _In_ float2 position,
+    _In_ float4 position,
     _In_ PositionUnits positionUnits,
     _In_ float2 size,
     _In_ SizeUnits sizeUnits,
@@ -777,7 +783,7 @@ void SpriteBatch::Draw(
 
 void SpriteBatch::Draw(
     _In_ ID3D11Texture2D* texture,
-    _In_ float2 position,
+    _In_ float4 position,
     _In_ PositionUnits positionUnits,
     _In_ float2 size,
     _In_ SizeUnits sizeUnits,
@@ -824,7 +830,7 @@ void SpriteBatch::Draw(
 
     // Add the sprite to the buffer.
 
-    float2 origin = StandardOrigin(position, positionUnits, m_renderTargetSize, m_dpi);
+    float4 origin = StandardOrigin(position, positionUnits, m_renderTargetSize, m_dpi);
     float2 offset = StandardOffset(size, sizeUnits, textureSize, m_dpi);
 
     if (m_technique == RenderTechnique::GeometryShader || m_technique == RenderTechnique::Instancing)
@@ -861,10 +867,10 @@ void SpriteBatch::Draw(
 
         ReplicationVertex* singleSpriteVertices = &m_vertexData[m_numSpritesDrawn * 4];
         unsigned int colorUnorm = MakeUnorm(color);
-        singleSpriteVertices[0].pos = origin + offsets[0];
-        singleSpriteVertices[1].pos = origin + offsets[1];
-        singleSpriteVertices[2].pos = origin + offsets[2];
-        singleSpriteVertices[3].pos = origin + offsets[3];
+        singleSpriteVertices[0].pos = float4(origin.x + offsets[0].x,origin.y + offsets[0].y,origin.z,origin.w);
+        singleSpriteVertices[1].pos = float4(origin.x + offsets[1].x,origin.y + offsets[1].y,origin.z,origin.w);
+        singleSpriteVertices[2].pos = float4(origin.x + offsets[2].x,origin.y + offsets[2].y,origin.z,origin.w);
+        singleSpriteVertices[3].pos = float4(origin.x + offsets[3].x,origin.y + offsets[3].y,origin.z,origin.w);
         singleSpriteVertices[0].color = colorUnorm;
         singleSpriteVertices[1].color = colorUnorm;
         singleSpriteVertices[2].color = colorUnorm;
index ba87a8b..c5c4725 100644 (file)
@@ -52,20 +52,20 @@ namespace BasicSprites
 
     struct ReplicationVertex
     {
-        float2 pos;
+        float4 pos;
         float2 tex;
         unsigned int color;
     };
 
     struct InstancingVertex
     {
-        float2 pos;
+        float4 pos;
         float2 tex;
     };
 
     struct InstanceData
     {
-        float2 origin;
+        float4 origin;
         float2 offset;
         float rotation;
         unsigned int color;
@@ -102,19 +102,19 @@ namespace BasicSprites
         void End();
         void Draw(
             _In_ ID3D11Texture2D* texture,
-            _In_ float2 position,
+            _In_ float4 position,
             _In_ PositionUnits positionUnits
             );
         void Draw(
             _In_ ID3D11Texture2D* texture,
-            _In_ float2 position,
+            _In_ float4 position,
             _In_ PositionUnits positionUnits,
             _In_ float2 size,
             _In_ SizeUnits sizeUnits
             );
         void Draw(
             _In_ ID3D11Texture2D* texture,
-            _In_ float2 position,
+            _In_ float4 position,
             _In_ PositionUnits positionUnits,
             _In_ float2 size,
             _In_ SizeUnits sizeUnits,
@@ -122,7 +122,7 @@ namespace BasicSprites
             );
         void Draw(
             _In_ ID3D11Texture2D* texture,
-            _In_ float2 position,
+            _In_ float4 position,
             _In_ PositionUnits positionUnits,
             _In_ float2 size,
             _In_ SizeUnits sizeUnits,
@@ -131,7 +131,7 @@ namespace BasicSprites
             );
         void Draw(
             _In_ ID3D11Texture2D* texture,
-            _In_ float2 position,
+            _In_ float4 position,
             _In_ PositionUnits positionUnits,
             _In_ float2 size,
             _In_ SizeUnits sizeUnits,
@@ -142,7 +142,7 @@ namespace BasicSprites
 
     private:
         unsigned int MakeUnorm(float4 color);
-        float2 StandardOrigin(float2 position, PositionUnits positionUnits, float2 renderTargetSize, float dpi);
+        float4 StandardOrigin(float4 position, PositionUnits positionUnits, float2 renderTargetSize, float dpi);
         float2 StandardOffset(float2 size, SizeUnits sizeUnits, float2 spriteSize, float dpi);
 
         Microsoft::WRL::ComPtr<ID3D11Device1> m_d3dDevice;
index c085e08..6daee80 100644 (file)
@@ -465,7 +465,7 @@ void SimpleSprites::Render()
     m_d3dContext->OMSetRenderTargets(
         1,
         backBufferRenderTargetView_.GetAddressOf(),
-        nullptr
+        backBufferDepthStencilView_.Get()
         );
 
        m_d3dContext->RSSetViewports(1, &backBufferViewPort_);
@@ -476,6 +476,8 @@ void SimpleSprites::Render()
         reinterpret_cast<float*>(&D2D1::ColorF(D2D1::ColorF::Black))
         );
 
+       m_d3dContext->ClearDepthStencilView(backBufferDepthStencilView_.Get(),D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL,1.0f,0);
+
 
     spriteBatch_->Begin();
 
@@ -483,8 +485,18 @@ void SimpleSprites::Render()
 
     spriteBatch_->Draw(
         test_.Get(),
-        float2(0.5f, 0.5f),
-        PositionUnits::Normalized,
+        float4(160.0f, 120.0f,0.2f,1.0f),
+        PositionUnits::Pixels,
+        float2(1.0f, 1.0f),
+        SizeUnits::Normalized,
+        float4(1.0f, 1.0f, 1.0f, 1.0f),
+               0.0f
+        );
+
+       spriteBatch_->Draw(
+        test_.Get(),
+        float4(168.0f, 128.0f,0.5f,1.0f),
+        PositionUnits::Pixels,
         float2(1.0f, 1.0f),
         SizeUnits::Normalized,
         float4(1.0f, 1.0f, 1.0f, 1.0f),
@@ -528,7 +540,7 @@ void SimpleSprites::Render()
     m_d3dContext->OMSetRenderTargets(
         1,
         m_renderTargetView.GetAddressOf(),
-        nullptr
+        m_depthStencilView.Get()
         );
 
        m_d3dContext->RSSetViewports(1, &swapChainViewPort_);
@@ -538,18 +550,21 @@ void SimpleSprites::Render()
         m_renderTargetView.Get(),
         reinterpret_cast<float*>(&D2D1::ColorF(D2D1::ColorF::MidnightBlue))
         );
+
+       m_d3dContext->ClearDepthStencilView(m_depthStencilView.Get(),D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL,1.0f,0);
+
        spriteBatch_->Begin();
 
-        spriteBatch_->Draw(
-            backBuffer_.Get(),
-            float2(m_window->Bounds.Width / 2.0f,m_window->Bounds.Height / 2.0f),
-            PositionUnits::DIPs,
-            float2(1.0f, 1.0f) * scale_,
-            SizeUnits::Normalized,
-            float4(1.0f, 1.0f, 1.0f, 1.0f),
-            0.0f,
-            BlendMode::Alpha
-            );
+    spriteBatch_->Draw(
+        backBuffer_.Get(),
+        float4(m_window->Bounds.Width / 2.0f,m_window->Bounds.Height / 2.0f,0.5f,1.0f),
+        PositionUnits::DIPs,
+        float2(1.0f, 1.0f) * scale_,
+        SizeUnits::Normalized,
+        float4(1.0f, 1.0f, 1.0f, 1.0f),
+        0.0f,
+        BlendMode::Alpha
+        );
 
        spriteBatch_->End();