Sign in to follow this  

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 
        6 * sizeof(Vertex), 

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


//(2) set the material of the rectangle
    BkGndMtrl = d3d::WHITE_MTRL;

//(3) create the leaf's texture 

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

//(5)render the rectangle with texture
    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.

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