Compare commits
2 Commits
16f2ee8a1e
...
8bcf7d5d79
| Author | SHA1 | Date | |
|---|---|---|---|
| 8bcf7d5d79 | |||
| c2557116a8 |
49
font.fx
49
font.fx
@ -13,7 +13,12 @@ SamplerState samPoint {
|
|||||||
|
|
||||||
struct VS_INPUT
|
struct VS_INPUT
|
||||||
{
|
{
|
||||||
float2 Pos : POSITION;
|
float4 Pos : TEXCOORD;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct GS_INPUT
|
||||||
|
{
|
||||||
|
float4 Pos : SV_POSITION;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct PS_INPUT
|
struct PS_INPUT
|
||||||
@ -22,20 +27,44 @@ struct PS_INPUT
|
|||||||
float2 Tex : TEXCOORD0;
|
float2 Tex : TEXCOORD0;
|
||||||
};
|
};
|
||||||
|
|
||||||
PS_INPUT VS(VS_INPUT input)
|
|
||||||
|
GS_INPUT VS(VS_INPUT input)
|
||||||
{
|
{
|
||||||
PS_INPUT output = (PS_INPUT)0;
|
GS_INPUT output;
|
||||||
|
|
||||||
float2 Pos = input.Pos * vGlyphScale + vPosition;
|
output.Pos = input.Pos;
|
||||||
Pos = Pos * vInvScreenSize + float2(-1, 1);
|
|
||||||
output.Pos = float4(Pos.xy, 0, 1);
|
|
||||||
|
|
||||||
float2 Tex = float2(input.Pos.x, -input.Pos.y);
|
|
||||||
|
|
||||||
output.Tex = (Tex + vCharCoord) * vTexScale;
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const float2 vertices[] = {
|
||||||
|
float2( 0.0f, 0.0f), // -- top right
|
||||||
|
float2( 1.0f, 0.0f), // -- top left
|
||||||
|
float2( 0.0f, -1.0f), // -- bottom right
|
||||||
|
float2( 1.0f, -1.0f), // -- bottom left
|
||||||
|
};
|
||||||
|
|
||||||
|
[maxvertexcount(4)]
|
||||||
|
void GS (point GS_INPUT input[1], inout TriangleStream<PS_INPUT> TriStream)
|
||||||
|
{
|
||||||
|
PS_INPUT output;
|
||||||
|
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
float2 Pos;
|
||||||
|
//Pos = vertices[i] * vGlyphScale + vPosition;
|
||||||
|
Pos = vertices[i] * vGlyphScale + input[0].Pos.xy;
|
||||||
|
Pos = Pos * vInvScreenSize + float2(-1, 1);
|
||||||
|
output.Pos = float4(Pos.xy, 0, 1);
|
||||||
|
|
||||||
|
output.Tex = vertices[i] * float2(1, -1);
|
||||||
|
//output.Tex = (output.Tex + vCharCoord) * vTexScale;
|
||||||
|
output.Tex = (output.Tex + input[0].Pos.zw) * vTexScale;
|
||||||
|
|
||||||
|
TriStream.Append(output);
|
||||||
|
}
|
||||||
|
TriStream.RestartStrip();
|
||||||
|
}
|
||||||
|
|
||||||
float4 PS(PS_INPUT input) : SV_Target
|
float4 PS(PS_INPUT input) : SV_Target
|
||||||
{
|
{
|
||||||
float4 texColor = txDiffuse.Sample(samPoint, input.Tex);
|
float4 texColor = txDiffuse.Sample(samPoint, input.Tex);
|
||||||
@ -50,7 +79,7 @@ technique10 Font
|
|||||||
pass P0
|
pass P0
|
||||||
{
|
{
|
||||||
SetVertexShader(CompileShader(vs_4_0, VS()));
|
SetVertexShader(CompileShader(vs_4_0, VS()));
|
||||||
SetGeometryShader(NULL);
|
SetGeometryShader(CompileShader(gs_4_0, GS()));
|
||||||
SetPixelShader(CompileShader(ps_4_0, PS()));
|
SetPixelShader(CompileShader(ps_4_0, PS()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
70
src/main.cpp
70
src/main.cpp
@ -370,16 +370,8 @@ HRESULT LoadMesh()
|
|||||||
return S_OK;
|
return S_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct FontVertex {
|
const int g_iFontBufferLength = 512;
|
||||||
D3DXVECTOR2 Pos;
|
typedef D3DXVECTOR4 FontVertex;
|
||||||
};
|
|
||||||
|
|
||||||
const FontVertex FontVertices[] = {
|
|
||||||
D3DXVECTOR2( 0.0f, 0.0f), // -- top right
|
|
||||||
D3DXVECTOR2( 1.0f, 0.0f), // -- top left
|
|
||||||
D3DXVECTOR2( 0.0f, -1.0f), // -- bottom right
|
|
||||||
D3DXVECTOR2( 1.0f, -1.0f), // -- bottom left
|
|
||||||
};
|
|
||||||
|
|
||||||
HRESULT InitFontBuffers()
|
HRESULT InitFontBuffers()
|
||||||
{
|
{
|
||||||
@ -422,7 +414,7 @@ HRESULT InitFontBuffers()
|
|||||||
//////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
D3D10_INPUT_ELEMENT_DESC layout[] = {
|
D3D10_INPUT_ELEMENT_DESC layout[] = {
|
||||||
{"POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0 , D3D10_INPUT_PER_VERTEX_DATA, 0},
|
{"TEXCOORD", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0 , D3D10_INPUT_PER_VERTEX_DATA, 0},
|
||||||
};
|
};
|
||||||
UINT numElements = (sizeof (layout)) / (sizeof (layout[0]));
|
UINT numElements = (sizeof (layout)) / (sizeof (layout[0]));
|
||||||
|
|
||||||
@ -443,16 +435,15 @@ HRESULT InitFontBuffers()
|
|||||||
//////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
D3D10_BUFFER_DESC bd;
|
D3D10_BUFFER_DESC bd;
|
||||||
D3D10_SUBRESOURCE_DATA initData;
|
|
||||||
|
|
||||||
// position
|
// position
|
||||||
bd.Usage = D3D10_USAGE_DEFAULT;
|
bd.Usage = D3D10_USAGE_DYNAMIC;
|
||||||
bd.ByteWidth = (sizeof (FontVertices));
|
bd.ByteWidth = (sizeof (FontVertex)) * g_iFontBufferLength;
|
||||||
bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
|
bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
|
||||||
bd.CPUAccessFlags = 0;
|
bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
|
||||||
bd.MiscFlags = 0;
|
bd.MiscFlags = 0;
|
||||||
initData.pSysMem = FontVertices;
|
|
||||||
hr = g_pd3dDevice->CreateBuffer(&bd, &initData, &g_pVertexBuffersFont[0]);
|
hr = g_pd3dDevice->CreateBuffer(&bd, NULL, &g_pVertexBuffersFont[0]);
|
||||||
if (FAILED(hr)) {
|
if (FAILED(hr)) {
|
||||||
print("CreateBuffer\n");
|
print("CreateBuffer\n");
|
||||||
return hr;
|
return hr;
|
||||||
@ -899,6 +890,49 @@ void RenderModel(float t)
|
|||||||
|
|
||||||
void RenderFont()
|
void RenderFont()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
// dynamic vertex buffer
|
||||||
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
FontVertex * pData;
|
||||||
|
HRESULT hr;
|
||||||
|
hr = g_pVertexBuffersFont[0]->Map(D3D10_MAP_WRITE_DISCARD,
|
||||||
|
0,
|
||||||
|
(void **)&pData);
|
||||||
|
if (FAILED(hr)) {
|
||||||
|
print("g_pVertexBuffersFont->Map");
|
||||||
|
}
|
||||||
|
|
||||||
|
int advance = 10;
|
||||||
|
const char * s = "asdf";
|
||||||
|
int ix = 0;
|
||||||
|
const int charStride = (int)(g_FontSize.Texture.Width / g_FontSize.Glyph.Width);
|
||||||
|
while (ix < g_iFontBufferLength && *s) {
|
||||||
|
char c = *s++;
|
||||||
|
|
||||||
|
float px = (float)advance;
|
||||||
|
float py = -10.0;
|
||||||
|
|
||||||
|
float cx = 0;
|
||||||
|
float cy = 0;
|
||||||
|
if (c >= 0x20 && c <= 0x7f) {
|
||||||
|
c -= 0x20;
|
||||||
|
cx = (float)(c % charStride);
|
||||||
|
cy = (float)(c / charStride);
|
||||||
|
print("%c %f %f\n", c + 0x20, cx, cy);
|
||||||
|
}
|
||||||
|
|
||||||
|
pData[ix++] = FontVertex(px, py, cx, cy);
|
||||||
|
|
||||||
|
advance += g_FontSize.Glyph.Width;
|
||||||
|
}
|
||||||
|
g_pVertexBuffersFont[0]->Unmap();
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
// effect variables
|
||||||
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
D3DXVECTOR2 invScreenSize = D3DXVECTOR2(2.0f / (float)g_ViewportSize.Width,
|
D3DXVECTOR2 invScreenSize = D3DXVECTOR2(2.0f / (float)g_ViewportSize.Width,
|
||||||
2.0f / (float)g_ViewportSize.Height);
|
2.0f / (float)g_ViewportSize.Height);
|
||||||
|
|
||||||
@ -923,7 +957,7 @@ void RenderFont()
|
|||||||
UINT offset[] = { 0 };
|
UINT offset[] = { 0 };
|
||||||
g_pd3dDevice->IASetInputLayout(g_pVertexLayoutFont);
|
g_pd3dDevice->IASetInputLayout(g_pVertexLayoutFont);
|
||||||
g_pd3dDevice->IASetVertexBuffers(0, g_dwVertexBufferCountFont, g_pVertexBuffersFont, stride, offset);
|
g_pd3dDevice->IASetVertexBuffers(0, g_dwVertexBufferCountFont, g_pVertexBuffersFont, stride, offset);
|
||||||
g_pd3dDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
|
g_pd3dDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_POINTLIST);
|
||||||
|
|
||||||
D3D10_TECHNIQUE_DESC techDesc;
|
D3D10_TECHNIQUE_DESC techDesc;
|
||||||
g_pTechniqueFont->GetDesc(&techDesc);
|
g_pTechniqueFont->GetDesc(&techDesc);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user