• Advertisement
Sign in to follow this  

Billboarding trees are not visible

This topic is 2065 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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:

TreeDemo::TreeDemo(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP)
: D3DApp(hInstance, winCaption, devType, requestedVP)
MessageBox(0, "checkDeviceCaps() Failed", 0, 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;
HR(D3DXCreateTextureFromFile(gd3dDevice, "tree.dds", &mTreeTex));
HR(D3DXCreateTextureFromFile(gd3dDevice, "ground0.dds", &mGroundTex));
HR(D3DXCreateTextureFromFile(gd3dDevice, "gatea.dds", &mGateTex));
// Add gate quad vertices.
void TreeDemo::drawScene()
// Clear the backbuffer and depth buffer.
HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffeeeeee, 1.0f, 0));
// Setup the rendering FX
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)));



// 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),
// 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;

// Obtain a pointer to a new index buffer.
HR(gd3dDevice->CreateIndexBuffer(mNumGridTriangles*3*sizeof(WORD), D3DUSAGE_WRITEONLY,
// 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 = (WORD)indices;
void TreeDemo::buildTreeGeometry()

HR(gd3dDevice->CreateVertexBuffer(16* sizeof(VertexPNT),
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(gd3dDevice->CreateIndexBuffer(24*sizeof(WORD), D3DUSAGE_WRITEONLY,

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
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->SetStreamSource(0, mGridVB, 0, sizeof(VertexPNT)));
// Begin passes.
UINT numPasses = 0;
HR(mFX->Begin(&numPasses, 0));
HR(gd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, mNumGridVertices, 0, mNumGridTriangles));

void TreeDemo::drawTrees()
// Enable alpha test.
HR(gd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, true));
//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->SetStreamSource(0, mTreeVB, 0, sizeof(VertexPNT)));
// Begin passes.
UINT numPasses = 0;
HR(mFX->Begin(&numPasses, 0));
HR(gd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 16, 0, 8));

// Disable alpha test.
HR(gd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, false));
// Turn culling back on.
// HR(gd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW));

...and the shader:

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();

What am I missing?

Share this post

Link to post
Share on other sites
Have you tried to use PIX to debug what is happening in your scene? That is the first step that I would take...

Share this post

Link to post
Share on other sites
How do I use PIX exactly? I opened it up, but I don't know what to do.

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
It looks like your not setting your trees world, but your shader is using the world pos.

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="#000000"]float3 posW [color="#666600"]=[color="#000000"] mul[color="#666600"]([color="#000000"]float4[color="#666600"]([color="#000000"]posL[color="#666600"], [color="#006666"]1.0f[color="#666600"]),[color="#000000"] gWorld[color="#666600"]).[color="#000000"]xyz[color="#666600"];

Share this post

Link to post
Share on other sites
That's the wrong shader though. I am refering to the AABillBoardVS shader which is the one that renders the trees.

Share this post

Link to post
Share on other sites
Yep your right.
Then where you set your trees verts set them to world coords.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement