2010-10-07 14 views
9

Tôi hiện đang cố gắng làm quen với API DirectX và tôi tự hỏi phương pháp thông thường để hiển thị một sprite trong DirectX 11 (ví dụ: đối với bản sao tetris).Pratice tốt nhất để render sprites trong DirectX 11 là gì?

Có giao diện tương tự như ID3DX10Sprite và nếu không, đó sẽ là phương pháp thông thường để vẽ họa tiết trong DirectX 11?

Edit: Đây là mã HLSL mà làm việc cho tôi (việc tính toán chiếu tọa độ có thể được thực hiện tốt hơn):

struct SpriteData 
{ 
    float2 position; 
    float2 size; 
    float4 color; 
}; 

struct VSOut 
{ 
    float4 position : SV_POSITION; 
    float4 color : COLOR; 
}; 

cbuffer ScreenSize : register(b0) 
{ 
    float2 screenSize; 
    float2 padding; // cbuffer must have at least 16 bytes 
} 

StructuredBuffer<SpriteData> spriteData : register(t0); 

float2 GetVertexPosition(uint VID) 
{ 
    [branch] switch(VID) 
    { 
     case 0: 
      return float2(0, 0); 
     case 1: 
      return float2(1, 0); 
     case 2: 
      return float2(0, 1); 
     default: 
      return float2(1, 1); 
    } 
} 

float4 ComputePosition(float2 positionInScreenSpace, float2 size, float2 vertexPosition) 
{ 
    float2 origin = float2(-1, 1); 
    float2 vertexPositionInScreenSpace = positionInScreenSpace + (size * vertexPosition); 

    return float4(origin.x + (vertexPositionInScreenSpace.x/(screenSize.x/2)), origin.y - (vertexPositionInScreenSpace.y/(screenSize.y/2)), 1, 1); 
} 

VSOut VShader(uint VID : SV_VertexID, uint SIID : SV_InstanceID) 
{ 
    VSOut output; 

    output.color = spriteData[SIID].color; 
    output.position = ComputePosition(spriteData[SIID].position, spriteData[SIID].size, GetVertexPosition(VID)); 

    return output; 
} 

float4 PShader(float4 position : SV_POSITION, float4 color : COLOR) : SV_TARGET 
{ 
    return color; 
} 

Trả lời

6

Không có không phải là một tương đương. Phương pháp thông thường sẽ là vẽ một dải tam giác tạo thành một quad.

Bạn có thể sử dụng bản sao, vì vậy bạn chỉ cần cập nhật bộ đệm với dữ liệu sprite (x, y vị trí màn hình theo pixel, id của kết cấu để tìm trong mảng kết cấu, chia tỷ lệ, xoay, lớp, v.v ...) và sử dụng trình đổ bóng để hiển thị tất cả các sprites trong một cuộc gọi vẽ duy nhất.

Dưới đây là một số mẩu tin HLSL ra khỏi đỉnh đầu của tôi:

//-------------------------------------------------------------------------------------- 
// Shader code for Sprite Rendering 
//-------------------------------------------------------------------------------------- 

struct Sprite { 
    float2 position; // x, y world position 
    float rotation; 
    float scaling; 

    float layer; // if you have multiple layers of sprites (e.g. background sprites) 
    uint textureId; 
}; 

StructuredBuffer<Sprite> SpritesRO : register(t0); 
Texture2DArray<float4> TextureSlices : register (t1); 

cbuffer cbRenderConstants : register(b0) 
{ 
    matrix g_mViewProjection; 
    // other constants 
}; 

struct VSSpriteOut 
{ 
    float3 position : SV_Position; 
    uint textureId; 
}; 

//-------------------------------------------------------------------------------------    
// Sprite Vertex Shader 
//------------------------------------------------------------------------------------- 

VSSpriteOut SpriteVS(uint VID : SV_VertexID, uint SIID : SV_InstanceID) 
{ 
    VSSpriteOut Out = (VSSpriteOut)0; 

    // VID is either 0, 1, 2 or 3 
    // We can map 0 to position (0,0), 1 to (0,1), 2 to (1,0), 3 to (1,1) 

    // We fetch the sprite instance data accord SIID 
    Sprite sdata = SpritesRO[SIID]; 

    // function f computes screen space vertex position 
    float3 pos = f (g_mViewProjection, VID, position, rotation, scaling, layer etc) 

    Out.position = pos; 
    Out.textureId = sdata.textureId; 

    return Out; 
} 

//------------------------------------------------------------------------------------- 
// Sprite Pixel Shader 
//------------------------------------------------------------------------------------- 

float4 SpritePS(VSSpriteOut In) : SV_Target 
{ 
    // use In.textureId to fetch the right texture slice in texture array 
    return color; 
} 
+0

Ông có thể cho tôi một ví dụ mã ngắn xin vui lòng? Tôi đấu tranh để bắt đầu với bộ đệm. –

+0

Kiểm tra tất cả các mẫu D3D11 từ DXSDK, và xem xét cẩn thận cách chúng tạo ra bộ đệm, gắn chúng, cập nhật chúng, v.v. – Stringer

+0

Wow, cảm ơn bạn. Điều này thực sự đã giúp tôi bắt đầu :) –

Các vấn đề liên quan