Hello Community,
I am trying to build up a simple renderer for dx11. Since I am at the very start and do not want use the toolkit and its drawing functions I searched in the web and came across this:
void DrawLine(float x1, float y1, float x2, float y2, Color color)
{
if (this->dxManager->pContext == NULL)
return;
int a = color.A & 0xff;
int r = color.R & 0xff;
int g = color.G & 0xff;
int b = color.B & 0xff;
UINT viewportNumber = 1;
D3D11_VIEWPORT vp;
float blendFactor[] = { 0.0f, 0.0f, 0.0f, 0.0f };
this->dxManager->pContext->OMSetBlendState(this->transparency, blendFactor, 0xffffffff);
this->dxManager->pContext->RSGetViewports(&viewportNumber, &vp);
float xx0 = 2.0f * (x1 - 0.5f) / vp.Width - 1.0f;
float yy0 = 1.0f - 2.0f * (y1 - 0.5f) / vp.Height;
float xx1 = 2.0f * (x2 - 0.5f) / vp.Width - 1.0f;
float yy1 = 1.0f - 2.0f * (y2 - 0.5f) / vp.Height;
COLOR_VERTEX* v = NULL;
D3D11_MAPPED_SUBRESOURCE mapData;
if (FAILED(this->dxManager->pContext->Map(this->mVertexBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &mapData)))
return;
v = (COLOR_VERTEX*)mapData.pData;
v[0].Position.x = xx0;
v[0].Position.y = yy0;
v[0].Position.z = 0;
v[0].Color = D3DXCOLOR(
((float)r / 255.0f),
((float)g / 255.0f),
((float)b / 255.0f),
((float)a / 255.0f));
v[1].Position.x = xx1;
v[1].Position.y = yy1;
v[1].Position.z = 0;
v[1].Color = D3DXCOLOR(
((float)r / 255.0f),
((float)g / 255.0f),
((float)b / 255.0f),
((float)a / 255.0f));
this->dxManager->pContext->Unmap(this->mVertexBuffer, NULL);
UINT Stride = sizeof(COLOR_VERTEX);
UINT Offset = 0;
this->dxManager->pContext->IASetVertexBuffers(0, 1, &this->mVertexBuffer, &Stride, &Offset);
this->dxManager->pContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP);
this->dxManager->pContext->IASetInputLayout(this->mInputLayout);
this->dxManager->pContext->VSSetShader(this->mVS, 0, 0);
this->dxManager->pContext->PSSetShader(this->mPS, 0, 0);
this->dxManager->pContext->GSSetShader(NULL, 0, 0);
this->dxManager->pContext->Draw(2, 0);
}
It works very well , looks messy and will be cleaned up by me as soon I understand the calculations, since C/P makes no sense for me, but I still learn from existing code.
In this case its for Drawing a simple line. And it uses the "D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP" as common to me from dx9.
The only thing I do not understand is how/why the calculation from common position to the two vertext position works.
float xx0 = 2.0f * (x1 - 0.5f) / vp.Width - 1.0f;
float yy0 = 1.0f - 2.0f * (y1 - 0.5f) / vp.Height;
float xx1 = 2.0f * (x2 - 0.5f) / vp.Width - 1.0f;
float yy1 = 1.0f - 2.0f * (y2 - 0.5f) / vp.Height;
Because I would just have made the vertexes without seeing this code like:
//...
v[0].Position.x = x1;
v[0].Position.y = y1;
//...
v[1].Position.x = x2;
v[1].Position.y = y2;
//...
Like given from the function parameters.
I see that the viewport, means the size of the painting area gets calculated in somehow,
but what does the muliplication by 2.0f inside of the x coords or "1.0f - ..." in the y coords mean ? Or why is 0.5f substracted from
x and y each time ? Are these corrections and scalings like I learned with the vertexes in openGL ?
I hope someone can tell me :)
,greetings
( ps: if somebody knows , is there a way to add a line width ? )