Sign in to follow this  
sunrisefe

how to render a tree's leaf in DX?

Recommended Posts

I try to use rectangle to denote a leaf. why my following code doesn't work correctly: the surround of the leaf is green(as follows),how to make the rectangle looks like the leaf's texture? the generated leaf see the last line of the post. sorry,I dont know how to create one link of the url.
//(1)  create rectangle 
Device->CreateVertexBuffer(
        6 * sizeof(Vertex), 
        D3DUSAGE_WRITEONLY,
        Vertex::FVF,
        D3DPOOL_MANAGED,
        &Quad,
        0);

    Vertex* v;
    Quad->Lock(0, 0, (void**)&v, 0);

    // quad built from two triangles, note texture coordinates:
    v[0] = Vertex(-1.0f, -1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
    v[1] = Vertex(-1.0f,  1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
    v[2] = Vertex( 1.0f,  1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);

    v[3] = Vertex(-1.0f, -1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
    v[4] = Vertex( 1.0f,  1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
    v[5] = Vertex( 1.0f, -1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f);

    Quad->Unlock();

//(2) set the material of the rectangle
    BkGndMtrl = d3d::WHITE_MTRL;
    BkGndMtrl.Diffuse.a=0;

//(3) create the leaf's texture 
        D3DXCreateTextureFromFile(Device,"leaf.png",&Tex);

    Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
    Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);
    Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);

 //(4) Set alpha blending states.use alpha in material's diffuse component for alpha
    Device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE);
    Device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);

    // set blending factors so that alpha component determines transparency
    Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
    Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

//(5)render the rectangle with texture
        Device->SetMaterial(&BkGndMtrl);
    Device->SetTexture(0, Tex);

    Device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);//turn on alpha
    Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);//render rectangle
    Device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);//disable alpha

the generated leaf: the leaf one in the pic is what i rendered and the left is the orignal texture. how to rend the rectanle just look like the texture. http://s19.photo.store.qq.com/http_imgload.cgi?/rurl4_b=1b718b3be555acf251d8a74b64ff766e790d9f6d7beb4b0410164e12e6eb95574ce13077280ad6486edff0a947bb01159ab41c5ae783b2cf62f7abb0cf771e7ffac49230cb3a30b0c44dbffcf698cff35d5f73e0

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