Sign in to follow this  
Hseptic

Billboarding trees are not visible

Recommended Posts

I'm trying to billboard four trees in the vertex shader, but they are not visible when running the program.
Here is some source code:

[CODE]
TreeDemo::TreeDemo(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP)
: D3DApp(hInstance, winCaption, devType, requestedVP)
{
if(!checkDeviceCaps())
{
MessageBox(0, "checkDeviceCaps() Failed", 0, 0);
PostQuitMessage(0);
}
mGfxStats = new GfxStats();

mCameraRadius = 6.0f;
mCameraRotationY = 1.2 * D3DX_PI;
mCameraHeight = 3.0f;
mLightVecW = D3DXVECTOR3(0.0, 0.707f, -0.707f);
mDiffuseLight = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
mAmbientLight = D3DXCOLOR(0.6f, 0.6f, 0.6f, 1.0f);
mSpecularLight = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
mGroundMtrl.ambient = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
mGroundMtrl.diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
mGroundMtrl.spec = D3DXCOLOR(0.4f, 0.4f, 0.4f, 1.0f);
mGroundMtrl.specPower = 8.0f;
mGateMtrl.ambient = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
mGateMtrl.diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
mGateMtrl.spec = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
mGateMtrl.specPower = 8.0f;
mTreeMtrl.ambient = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
mTreeMtrl.diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
mTreeMtrl.spec = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
mTreeMtrl.specPower = 8.0f;
D3DXMatrixIdentity(&mGroundWorld);
D3DXMatrixIdentity(&mGateWorld);
D3DXMatrixIdentity(&mTreeWorld);
HR(D3DXCreateTextureFromFile(gd3dDevice, "tree.dds", &mTreeTex));
HR(D3DXCreateTextureFromFile(gd3dDevice, "ground0.dds", &mGroundTex));
HR(D3DXCreateTextureFromFile(gd3dDevice, "gatea.dds", &mGateTex));
buildGridGeometry();
buildGateGeometry();
buildTreeGeometry();
mGfxStats->addVertices(mNumGridVertices);
mGfxStats->addTriangles(mNumGridTriangles);
// Add gate quad vertices.
mGfxStats->addVertices(4);
mGfxStats->addTriangles(2);
buildFX();
onResetDevice();
InitAllVertexDeclarations();
}
void TreeDemo::drawScene()
{
// Clear the backbuffer and depth buffer.
HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffeeeeee, 1.0f, 0));
HR(gd3dDevice->BeginScene());
// Setup the rendering FX
HR(mFX->SetTechnique(mhTech));
HR(mFX->SetValue(mhLightVecW, &mLightVecW, sizeof(D3DXVECTOR3)));
HR(mFX->SetValue(mhDiffuseLight, &mDiffuseLight, sizeof(D3DXCOLOR)));
HR(mFX->SetValue(mhAmbientLight, &mAmbientLight, sizeof(D3DXCOLOR)));
HR(mFX->SetValue(mhSpecularLight, &mSpecularLight, sizeof(D3DXCOLOR)));

drawGround();

drawTrees();

mGfxStats->display();
HR(gd3dDevice->EndScene());
// Present the backbuffer.
HR(gd3dDevice->Present(0, 0, 0, 0));
}
void TreeDemo::buildGridGeometry()
{
std::vector<D3DXVECTOR3> verts;
std::vector<DWORD> indices;
GenTriGrid(100, 100, 1.0f, 1.0f,
D3DXVECTOR3(0.0f, 0.0f, 0.0f), verts, indices);
// Save vertex count and triangle count for DrawIndexedPrimitive arguments.
mNumGridVertices = 100*100;
mNumGridTriangles = 99*99*2;
// Obtain a pointer to a new vertex buffer.
HR(gd3dDevice->CreateVertexBuffer(mNumGridVertices * sizeof(VertexPNT),
D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &mGridVB, 0));
// Now lock it to obtain a pointer to its internal data, and write the
// grid's vertex data.
VertexPNT* v = 0;
HR(mGridVB->Lock(0, 0, (void**)&v, 0));
float texScale = 0.2f;
for(int i = 0; i < 100; ++i)
{
for(int j = 0; j < 100; ++j)
{
DWORD index = i * 100 + j;
v[index].pos = verts[index];
v[index].normal = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
v[index].tex0 = D3DXVECTOR2((float)j, (float)i) * texScale;
}
}
HR(mGridVB->Unlock());

// Obtain a pointer to a new index buffer.
HR(gd3dDevice->CreateIndexBuffer(mNumGridTriangles*3*sizeof(WORD), D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16, D3DPOOL_MANAGED, &mGridIB, 0));
// Now lock it to obtain a pointer to its internal data, and write the
// grid's index data.
WORD* k = 0;
HR(mGridIB->Lock(0, 0, (void**)&k, 0));
for(DWORD i = 0; i < mNumGridTriangles*3; ++i)
k[i] = (WORD)indices[i];
HR(mGridIB->Unlock());
}
void TreeDemo::buildTreeGeometry()
{

HR(gd3dDevice->CreateVertexBuffer(16* sizeof(VertexPNT),
D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &mTreeVB, 0));
VertexPNT* v = 0;
HR(mTreeVB->Lock(0, 0, (void**)&v, 0));
v[0] = VertexPNT(-0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
v[1] = VertexPNT(-0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
v[2] = VertexPNT( 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
v[3] = VertexPNT( 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f);
v[4] = VertexPNT(-0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f);
v[5] = VertexPNT(-0.5f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f);
v[6] = VertexPNT( 0.5f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f);
v[7] = VertexPNT( 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f);
v[8] = VertexPNT(-0.5f, 0.0f, 0.0f, 2.0f, 0.0f, 2.0f, 0.0f, 1.0f);
v[9] = VertexPNT(-0.5f, 1.0f, 0.0f, 2.0f, 0.0f, 2.0f, 0.0f, 0.0f);
v[10] = VertexPNT( 0.5f, 1.0f, 0.0f, 2.0f, 0.0f, 2.0f, 1.0f, 0.0f);
v[11] = VertexPNT( 0.5f, 0.0f, 0.0f, 2.0f, 0.0f, 2.0f, 1.0f, 1.0f);
v[12] = VertexPNT(-0.5f, 0.0f, 0.0f, -2.0f, 0.0f, -2.0f, 0.0f, 1.0f);
v[13] = VertexPNT(-0.5f, 1.0f, 0.0f, -2.0f, 0.0f, -2.0f, 0.0f, 0.0f);
v[14] = VertexPNT( 0.5f, 1.0f, 0.0f, -2.0f, 0.0f, -2.0f, 1.0f, 0.0f);
v[15] = VertexPNT( 0.5f, 0.0f, 0.0f, -2.0f, 0.0f, -2.0f, 1.0f, 1.0f);
HR(mTreeVB->Unlock());

HR(gd3dDevice->CreateIndexBuffer(24*sizeof(WORD), D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16, D3DPOOL_MANAGED, &mTreeIB, 0));

WORD* k = 0;
HR(mTreeIB->Lock(0, 0, (void**)&k, 0));
k[0] = 0; k[1] = 1; k[2] = 2; // Triangle 0
k[3] = 0; k[4] = 2; k[5] = 3; // Triangle 1
k[6] = 4; k[7] = 5; k[8] = 6; // Triangle 2
k[9] = 4; k[10] = 5; k[11] = 7; // Triangle 3
k[12] = 8; k[13] = 9; k[14] = 10; // Triangle 4
k[15] = 8; k[16] = 9; k[17] = 11; // Triangle 5
k[18] = 12; k[19] = 13; k[20] = 14; // Triangle 6
k[21] = 12; k[22] = 13; k[23] = 15; // Triangle 7
HR(mTreeIB->Unlock());
}
void TreeDemo::buildFX()
{
// Create the FX from a .fx file.
ID3DXBuffer* errors = 0;
HR(D3DXCreateEffectFromFile(gd3dDevice, "DirLightTex.fx",
0, 0, D3DXSHADER_DEBUG, 0, &mFX, &errors));
if( errors )
MessageBox(0, (char*)errors->GetBufferPointer(), 0, 0);
// Obtain handles.
mhTech = mFX->GetTechniqueByName("DirLightTexTech");
mhWVP = mFX->GetParameterByName(0, "gWVP");
mhWorldInvTrans = mFX->GetParameterByName(0, "gWorldInvTrans");
mhLightVecW = mFX->GetParameterByName(0, "gLightVecW");
mhDiffuseMtrl = mFX->GetParameterByName(0, "gDiffuseMtrl");
mhDiffuseLight = mFX->GetParameterByName(0, "gDiffuseLight");
mhAmbientMtrl = mFX->GetParameterByName(0, "gAmbientMtrl");
mhAmbientLight = mFX->GetParameterByName(0, "gAmbientLight");
mhSpecularMtrl = mFX->GetParameterByName(0, "gSpecularMtrl");
mhSpecularLight = mFX->GetParameterByName(0, "gSpecularLight");
mhSpecularPower = mFX->GetParameterByName(0, "gSpecularPower");
mhEyePos = mFX->GetParameterByName(0, "gEyePosW");
mhWorld = mFX->GetParameterByName(0, "gWorld");
mhTex = mFX->GetParameterByName(0, "gTex");
}
void TreeDemo::drawGround()
{
HR(mFX->SetValue(mhAmbientMtrl, &mGroundMtrl.ambient, sizeof(D3DXCOLOR)));
HR(mFX->SetValue(mhDiffuseMtrl, &mGroundMtrl.diffuse, sizeof(D3DXCOLOR)));
HR(mFX->SetValue(mhSpecularMtrl, &mGroundMtrl.spec, sizeof(D3DXCOLOR)));
HR(mFX->SetFloat(mhSpecularPower, mGroundMtrl.specPower));
HR(mFX->SetMatrix(mhWVP, &(mGroundWorld*mView*mProj)));
D3DXMATRIX worldInvTrans;
D3DXMatrixInverse(&worldInvTrans, 0, &mGroundWorld);
D3DXMatrixTranspose(&worldInvTrans, &worldInvTrans);
HR(mFX->SetMatrix(mhWorldInvTrans, &worldInvTrans));
HR(mFX->SetMatrix(mhWorld, &mGroundWorld));
HR(mFX->SetTexture(mhTex, mGroundTex));
HR(gd3dDevice->SetVertexDeclaration(VertexPNT::Decl));
HR(gd3dDevice->SetStreamSource(0, mGridVB, 0, sizeof(VertexPNT)));
HR(gd3dDevice->SetIndices(mGridIB));
// Begin passes.
UINT numPasses = 0;
HR(mFX->Begin(&numPasses, 0));
HR(mFX->BeginPass(0));
HR(gd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, mNumGridVertices, 0, mNumGridTriangles));
HR(mFX->EndPass());

HR(mFX->End());
}
void TreeDemo::drawTrees()
{
// Enable alpha test.
HR(gd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, true));
//HR(gd3dDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL));
//HR(gd3dDevice->SetRenderState(D3DRS_ALPHAREF, 100));
// Turn off backface culling so you can see both sides of the gate.
//HR(gd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE));
//HR(mFX->SetValue(mhAmbientMtrl, &mTreeMtrl.ambient, sizeof(D3DXCOLOR)));
//HR(mFX->SetValue(mhDiffuseMtrl, &mTreeMtrl.diffuse, sizeof(D3DXCOLOR)));
//HR(mFX->SetValue(mhSpecularMtrl, &mTreeMtrl.spec, sizeof(D3DXCOLOR)));
//HR(mFX->SetFloat(mhSpecularPower, mTreeMtrl.specPower));
HR(mFX->SetMatrix(mhWVP, &(mTreeWorld*mView*mProj)));
//D3DXMATRIX worldInvTrans;
//D3DXMatrixInverse(&worldInvTrans, 0, &mGateWorld);
//D3DXMatrixTranspose(&worldInvTrans, &worldInvTrans);
//HR(mFX->SetMatrix(mhWorldInvTrans, &worldInvTrans));
//HR(mFX->SetMatrix(mhWorld, &mGateWorld));
HR(mFX->SetTexture(mhTex, mTreeTex));
HR(gd3dDevice->SetVertexDeclaration(VertexPNT::Decl));
HR(gd3dDevice->SetStreamSource(0, mTreeVB, 0, sizeof(VertexPNT)));
HR(gd3dDevice->SetIndices(mTreeIB));
// Begin passes.
UINT numPasses = 0;
HR(mFX->Begin(&numPasses, 0));
HR(mFX->BeginPass(1));
HR(gd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 16, 0, 8));
HR(mFX->EndPass());

HR(mFX->End());
// Disable alpha test.
HR(gd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, false));
// Turn culling back on.
// HR(gd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW));
}
[/CODE]

...and the shader:

[CODE]
uniform extern float4x4 gWorld;
uniform extern float4x4 gWorldInvTrans;
uniform extern float4x4 gWVP;
uniform extern float4 gAmbientMtrl;
uniform extern float4 gAmbientLight;
uniform extern float4 gDiffuseMtrl;
uniform extern float4 gDiffuseLight;
uniform extern float4 gSpecularMtrl;
uniform extern float4 gSpecularLight;
uniform extern float gSpecularPower;
uniform extern float3 gLightVecW;
uniform extern float3 gEyePosW;
uniform extern texture gTex;
sampler TexS = sampler_state
{
Texture = <gTex>;
MinFilter = Anisotropic;
MagFilter = LINEAR;
MipFilter = LINEAR;
MaxAnisotropy = 8;
AddressU = WRAP;
AddressV = WRAP;
};

struct OutputVS
{
float4 posH : POSITION0;
float4 diffuse : COLOR0;
float4 spec : COLOR1;
float2 tex0 : TEXCOORD0;
};
OutputVS DirLightTexVS(float3 posL : POSITION0, float3 normalL : NORMAL0, float2 tex0: TEXCOORD0)
{
// Zero out our output.
OutputVS outVS = (OutputVS)0;

// Transform normal to world space.
float3 normalW = mul(float4(normalL, 0.0f), gWorldInvTrans).xyz;
normalW = normalize(normalW);

// Transform vertex position to world space.
float3 posW = mul(float4(posL, 1.0f), gWorld).xyz;

//=======================================================
// Compute the color: Equation 10.3.

// Compute the vector from the vertex to the eye position.
float3 toEye = normalize(gEyePosW - posW);

// Compute the reflection vector.
float3 r = reflect(-gLightVecW, normalW);

// Determine how much (if any) specular light makes it into the eye.
float t = pow(max(dot(r, toEye), 0.0f), gSpecularPower);

// Determine the diffuse light intensity that strikes the vertex.
float s = max(dot(gLightVecW, normalW), 0.0f);

// Compute the ambient, diffuse and specular terms separatly.
float3 spec = t*(gSpecularMtrl*gSpecularLight).rgb;
float3 diffuse = s*(gDiffuseMtrl*gDiffuseLight).rgb;
float3 ambient = gAmbientMtrl*gAmbientLight;

// Sum all the terms together and copy over the diffuse alpha.
outVS.diffuse.rgb = ambient + diffuse;
outVS.diffuse.a = gDiffuseMtrl.a;
outVS.spec = float4(spec, 0.0f);
//=======================================================

// Transform to homogeneous clip space.
outVS.posH = mul(float4(posL, 1.0f), gWVP);

// Pass on texture coordinates to be interpolated in rasterization.
outVS.tex0 = tex0;

// Done--return the output.
return outVS;
}
OutputVS AABillBoardVS(float3 posL : POSITION0, float3 bbOffset : TEXCOORD0, float2 tex0: TEXCOORD1)
{
OutputVS outVS = (OutputVS)0;
float3 tempPosW = posL + bbOffset;
float3 look = gEyePosW - tempPosW;
look.y = 0.0f;
look = normalize(look);
float3 up = float3(0.0f, 1.0f, 0.0f);
float3 right = cross(up, look);
float3x3 R;
R[0] = right;
R[1] = up;
R[2] = look;
float3 posW = mul(posL, R) + bbOffset;
outVS.posH = mul(float4(posW, 1.0f), gWVP);
outVS.tex0 = tex0;
return outVS;
}
float4 DirLightTexPS(float4 c : COLOR0, float4 spec : COLOR1, float2 tex0 : TEXCOORD0) : COLOR
{
float4 texColor = tex2D(TexS, tex0);
float3 diffuse = c.rgb * texColor.rgb;
return float4(diffuse + spec.rgb, texColor.a*c.a);
}
float4 AABillBoardPS(float2 tex0 : TEXCOORD0) : COLOR
{
float4 texColor = tex2D(TexS, tex0);
return (texColor.rgb, texColor.a);
}
technique DirLightTexTech
{
pass P0
{
// Specify the vertex and pixel shader associated with this pass.
vertexShader = compile vs_2_0 DirLightTexVS();
pixelShader = compile ps_2_0 DirLightTexPS();
}
pass P1
{
vertexShader = compile vs_2_0 AABillBoardVS();
pixelShader = compile ps_2_0 AABillBoardPS();
}
}
[/CODE]

What am I missing?

Share this post


Link to post
Share on other sites
You start a new experiment and select the application to run. When you have ended the experiment by closing the application, you can browse the information about performance bottlenecks, content of buffers...

Share this post


Link to post
Share on other sites
I don't understand. "mTreeWorld" is the identity matrix, and the billboard vertex shader doesn't ever use "gWorld" in its calculations. There must be something I'm doing wrong though.

Share this post


Link to post
Share on other sites
yeah in the shader you posted it here
[color="#880000"]// Transform vertex position to world space.[/color]
[color="#000000"]float3 posW [/color][color="#666600"]=[/color][color="#000000"] mul[/color][color="#666600"]([/color][color="#000000"]float4[/color][color="#666600"]([/color][color="#000000"]posL[/color][color="#666600"],[/color] [color="#006666"]1.0f[/color][color="#666600"]),[/color][color="#000000"] gWorld[/color][color="#666600"]).[/color][color="#000000"]xyz[/color][color="#666600"];[/color]

Share this post


Link to post
Share on other sites
in here CreateVertexBuffer()
these here
v[0] = VertexPNT(-0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
v[1] = VertexPNT(-0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
v[2] = VertexPNT( 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
v[3] = VertexPNT( 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f);


For each vertex, we need to specify the translation offset that positions the billboard the vertex is part of in world space, so that we can compute the rotation part and the translation part of the world transformation.
With this set up, we can render all the billboards in one draw primitive call, and the correct billboard world matrix is generated in the vertex shader for each vertex.

Share this post


Link to post
Share on other sites
I still don't understand what it is I am supposed to do. It appears you are quoting from the same book I am reading, and therefore I am not seeing anything new about the matter.

Share this post


Link to post
Share on other sites
Also note, [i]all[/i] of the source code fragments that I have posted under my username are from the book "Introduction to 3D Game Programming With DirectX 9.0c A Shader Approach" by Frank D. Luna. I do not claim to have written [i]any[/i] of it. I have merely made minor tweaks that are a part of the book's exercises. Again, I do not claim to have written ANY of the source code that I have previsouly posted under my username. All of the credit goes to the author Frank D. Luna.

Share this post


Link to post
Share on other sites
Its funny I have the book as well but when I fire up my old pc with that code on it its not the same as yours in part it is but the vertex you have its for some reason is not the same as mine and its been a long time ago for that sample for me, I think I changed it to suite my mingy code back then. you really need to get pix running. its not that hard I remember when I first used pix(I still dont understand how you do bottle neck checks in it but)but .
What you need do is to compile and build the exe .
You will then need to copy your exe to where your program files are for this project,
Then start pix
click on file tab->new experiment.
On the experiment panel Program path select your exe.
Then under what infomation do you want to gather ??? select A single-frame capture on F12 key press
then press the start button.
The exe will load up and you should see your code in action. move your camera to where you think your trees are then press F12 key this takes a shot of the whole dx code thing and when you close the app it will display a list of things.But what you may be intrested in is the panel at the bottom left.
See Fame + sign click it that displays your devices state at the time you pressed F12. find your draw call by the looks of your code you have a few(if you using dxfont class that to has lots of draw calls dont confuse them with your draw calls). Then when you click on the draw call to the right there is a details area the render tab is your screen you should see stuff in there. the Mesh tab is how you find your trees look for the vertex that matches your trees, try it. it will all be revealed.

Share this post


Link to post
Share on other sites
I'm still confused as to what might be wrong.
Here is PreVS:
[attachment=10033:pix1.PNG]
Hers is PostVS:
[attachment=10034:pix2.PNG]
My only guess is here...
[CODE]
struct VertexPNT
{
VertexPNT()
:pos(0.0f, 0.0f, 0.0f),
normal(0.0f, 0.0f, 0.0f),
tex0(0.0f, 0.0f){}
VertexPNT(float x, float y, float z,
float nx, float ny, float nz,
float u, float v):pos(x,y,z), normal(nx,ny,nz), tex0(u,v){}
VertexPNT(const D3DXVECTOR3& v, const D3DXVECTOR3& n, const D3DXVECTOR2& uv)
:pos(v),normal(n), tex0(uv){}
D3DXVECTOR3 pos;
D3DXVECTOR3 normal;
D3DXVECTOR2 tex0;
static IDirect3DVertexDeclaration9* Decl;
};
[/CODE]


Basically I'm placing the billboard translation offset into the "normal" member of VertexPNT, and feeding it into the bboffset parameter of AABillBoardVS. Maybe that's the problem.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this