Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

belfegor

Member Since 05 Jun 2007
Offline Last Active Today, 04:41 PM
*****

Posts I've Made

In Topic: Light Shader

Today, 04:08 PM

First you draw your objects once with one shader:

 

// activates shader 1
device->SetPixelShader(PixelShader1); // one that apply diffuse texture
Object.Draw();

 

Then you draw it again with other pixel shadr but enable alpha blending, for example it could be ADD mode:

 

// activates shader 2 ( 1 one is now "disabled")
device->SetPixelShader(PixelShader2); // one that does lightning
device->SetRenderState( d3drs_alphablendenable, true)
device->SetRenderState( d3drs_srcblend, one)
device->SetRenderState( d3drs_destblend, one)
Object.Draw();

So it blends over what is rendered before it. Get it?

 

Or you could do all in one shader (diffuse + lighning), but that is your choice.

 

Have you ever worked in Photoshop with different layers and blend them?

If so, it is the similar thing.


In Topic: Drawing multiple lines with one draw call

Today, 09:19 AM

Triangle strip MSDN

 

I don't know to create the right algorithm for triangle strips (since it has a bit weird order), but in this case it was easy since there is 2 triangle strip per line, so i just follow rule for first two triangles:

 

The system uses vertices v1, v2, and v3 to draw the first triangle; v2, v4, and v3 to draw the second triangle...

 

0 1 2

1 3 2

 

Since that was original way to create each line vertices (with triangle strip in mind), and also It depends in what order vertices are fed to vertex buffer, so i changed indices to CCW order:

0 2 1

2 3 1

 

I just saw something in original code, to get default cull mode comment this in shader:

technique VolumeLine
{
    pass p0
    {
        //CullMode = none; // THIS
        AlphaBlendEnable = true;
        SrcBlend = one;
        DestBlend = one;
        alpharef = 0.9;
        alphafunc = GreaterEqual;
        ZEnable = false;
        VertexShader = compile vs_2_0 VolumeLineVS();
        PixelShader = compile ps_2_0 VolumeLinePS();
    }
}

 

and also (redundant) in cpp:

// Set up some device states.
    D3DXMatrixPerspectiveFovLH( &g_mProjection, 45.f,
        (float)d3dpp.BackBufferWidth / (float)d3dpp.BackBufferHeight,
        0.1f, 100.f );

    g_pD3DDevice->SetTransform( D3DTS_PROJECTION, &g_mProjection );
    g_pD3DDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
    //g_pD3DDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE ); // THIS

In Topic: Drawing multiple lines with one draw call

Today, 04:07 AM

Here is the modified CodeSampler demo: http://pastebin.com/VRCNdUw6


In Topic: Drawing multiple lines with one draw call

Today, 02:19 AM

Ok, i looked a bit at volume lines demo from CodeSampler, but today i am to lazy to make project and test it out with above modifications.

I guess you could do something like this:

 

 

struct Line
{
    D3DXVECTOR4 pt0;
    D3DXVECTOR4 pt1;
};
 
std::vector<Line> vLines;
...
// fill lines
...
Vertex* vrts;
vBuffer->Lock(... &vrts);
std::size_t numTriDraw = 0u;
for(std::size_t i = 0; i < vLines.size(); ++i)
{
    if( i > maxNumLines)
    {
        vBuffer->Unlock();
        d3d9device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, ..., numTriDraw);
        vBuffer->Lock( ... &vrts);
        numTriDraw = 0u;
    }
    numTriDraw += triPerLine;
 
    const D3DXVECTOR4& v0 = vLines[i].pt0;
    const D3DXVECTOR4& v1 = vLines[i].pt1;

    vrts[i*4 + 0].pos = v0;        vrts[i*4 + 0].otherPos = v1;
    vrts[i*4 + 1].pos = v1;        vrts[i*4 + 1].otherPos = v0;
    vrts[i*4 + 2].pos = v0;        vrts[i*4 + 2].otherPos = v1;
    vrts[i*4 + 3].pos = v1;        vrts[i*4 + 3].otherPos = v0;

    vrts[i*4 + 0].thickness = D3DXVECTOR3( -g_fThickness, 0.f, g_fThickness * 0.5f );
    vrts[i*4 + 1].thickness = D3DXVECTOR3(  g_fThickness, 0.f, g_fThickness * 0.5f );
    vrts[i*4 + 2].thickness = D3DXVECTOR3(  g_fThickness, 0.f, g_fThickness * 0.5f );
    vrts[i*4 + 3].thickness = D3DXVECTOR3( -g_fThickness, 0.f, g_fThickness * 0.5f );

    vrts[i*4 + 0].texOffset = D3DXVECTOR4( g_fThickness, g_fThickness, 0.f, 0.f );
    vrts[i*4 + 1].texOffset = D3DXVECTOR4( g_fThickness, g_fThickness, 0.25f, 0.f );
    vrts[i*4 + 2].texOffset = D3DXVECTOR4( g_fThickness, g_fThickness, 0.f, 0.25f );
    vrts[i*4 + 3].texOffset = D3DXVECTOR4( g_fThickness, g_fThickness, 0.25f, 0.25f );
}
vBuffer->Unlock()
 
if(numTriDraw > 0u)
{
    d3d9device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, ..., numTriDraw);
}

In Topic: Drawing multiple lines with one draw call

Today, 01:50 AM

1.

I would advice you to use DrawIndexedPrimitive instead DrawIndexedPrimitiveUP which is slow.

 

2.

Create VB, IB and vertex declaration instead FVF.

 

Given vertex input you gave in thread that you have linked

 

struct TInputVertex
{
float4 pos : POSITION; // Position of this vertex
float4 otherPos : NORMAL; // Position of the other vertex at the other end of the line.
float4 texOffset : TEXCOORD0; // Tex coord offset.
float3 thickness : TEXCOORD1; // Thickness info.
};

 

Vetex declaration becomes

 

    D3DVERTEXELEMENT9 vertexElements[] =
    {
        {0,  0,  D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION,  0},
        {0, 16,  D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL,  0},
        {0, 32,  D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD,  0},
        {0, 48,  D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD,  1},
        D3DDECL_END()
    };

    HRESULT hr = d3d9device->CreateVertexDeclaration(vertexElements, &vDeclaration);
    if(FAILED(hr))
        ...// act on error

 

vertex

 

struct Vertex
{
    D3DXVECTOR4 pos;
    D3DXVECTOR4 normal;
    D3DXVECTOR4 texCoordOff;
    D3DXVECTOR3 thickness;
};

 

VB and IB

 

 
    const std::size_t maxLinesCnt = 1000;
    const std::size_t vertsPerLine  = 4;
    const std::size_t triPerLine      = 2;
    const std::size_t numVerts      = maxLinesCnt * vertsPerLine;
 
    D3DPOOL pool = D3DPOOL_DEFAULT;
    DWORD usage  = D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC;
    UINT len     = sizeof(Vertex) * numVerts;
    hr = d3d9device->CreateVertexBuffer(len, usage, 0, pool, &vBuffer, nullptr);
    if(FAILED(hr))
        ...// act on error
 
    D3DFORMAT indFormat  = (std::numeric_limits<WORD>::max() < numVerts) ? D3DFMT_INDEX32 : D3DFMT_INDEX16;
    std::size_t indBytes = (std::numeric_limits<WORD>::max() < numVerts) ? sizeof(DWORD) : sizeof(WORD);
    len = indBytes * maxLinesCnt * triPerLine * 3;
    hr = d3d9device->CreateIndexBuffer(len, usage, indFormat, pool, &iBuffer, nullptr);
    if(FAILED(hr))
        ...// act on error

 

Fill your VB and IB and set these before drawing:

 

d3d9device->SetVertexDeclaration(vDeclaration);
d3d9device->SetIndices(iBuffer);
d3d9device->SetStreamSource(0, vBuffer, 0, sizeof(Vertex));
 
d3d9device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, ...);

PARTNERS