Sign in to follow this  

Why this plane disappeared?

This topic is 4859 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

In my program, I can see my red plane for alpha test if I don't let a ball in the program run (I comment the code to draw this ball), but if I let the program draw this ball, I will lose that red alpha plane. I attached the main code below (I don't know how to attach files). the ball drawing code is 'g_pBallMesh->DrawSubset (0);'.
struct D3DVERTEX
{
    D3DXVECTOR3 p;
    FLOAT       tu, tv;

    static const DWORD FVF;
};

const DWORD D3DVERTEX::FVF = D3DFVF_XYZ | D3DFVF_TEX1;

VOID InitBall ()
{
	D3DXLoadMeshFromX ("ball.x", D3DXMESH_SYSTEMMEM, g_pd3dDevice, NULL,
		               &g_pBallMeshBuffer, &g_pBallEffectInstance,
					   &g_dwBallMaterials, &g_pBallMesh);

	posBall.x = 0.0;
	posBall.y = 0.0;
	posBall.z = 0.0;

	velBall.x = 5.0;
	velBall.y = 10.0;
	velBall.z = -8.0;
}

VOID  InitRacket ()
{
	D3DVERTEX* v;

	mousePos.x = 320.0;
	mousePos.y = 240.0;

	// create the texture ( alpha channel used here )
	D3DXCreateTextureFromFile (g_pd3dDevice, "racket.dds", &g_pRacketTextures);

	// create the vertex buffer
	g_pd3dDevice->CreateVertexBuffer (4 * sizeof (D3DVERTEX),
		                              D3DUSAGE_WRITEONLY, 
									  D3DVERTEX::FVF,
                                      D3DPOOL_MANAGED, 
									  &g_pRacketVertexBuf,
									  NULL);

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

	v->p.x = -100.0;
	v->p.y = 100.0;
	v->p.z = 400.0;
	v->tu = 0.0;
	v->tv = 0.0;

	v += 1;

	v->p.x = -100.0;
	v->p.y = -100.0;
	v->p.z = 400.0;
	v->tu = 0.0;
	v->tv = 1.0;

	v += 1;

	v->p.x = 100.0;
	v->p.y = 100.0;
	v->p.z = 400.0;
	v->tu = 1.0;
	v->tv = 0.0;

	v += 1;

	v->p.x = 100.0;
	v->p.y = -100.0;
	v->p.z = 400.0;
	v->tu = 1.0;
	v->tv = 1.0;

    g_pRacketVertexBuf->Unlock ();
}

VOID InitWall ()
{
	D3DVERTEX* v;

	// load the texture
	D3DXCreateTextureFromFile (g_pd3dDevice, "wall.jpg", &g_pWallTextures);
	D3DXCreateTextureFromFile (g_pd3dDevice, "floor.jpg", &g_pFloorTextures);
	D3DXCreateTextureFromFile (g_pd3dDevice, "ball.jpg", &g_pBallTextures);

	// create the vertex buffer
	g_pd3dDevice->CreateVertexBuffer (20 * sizeof (D3DVERTEX),
		                              D3DUSAGE_WRITEONLY, 
									  D3DVERTEX::FVF,
                                      D3DPOOL_MANAGED, 
									  &g_pVertextBuf,
									  NULL);

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

	v->p.x = -300.0;
	v->p.y = 300.0;
	v->p.z = -300.0;
	v->tu  = 0.0;
	v->tv  = 0.0;

	v += 1;

	v->p.x = -300.0;
	v->p.y = -300.0;
	v->p.z = -300.0;
	v->tu  = 0.0;
	v->tv  = 1.0;

	v += 1;

	v->p.x = 300.0;
	v->p.y = 300.0;
	v->p.z = -300.0;
	v->tu  = 1.0;
	v->tv  = 0.0;

	v += 1;

	v->p.x = 300.0;
	v->p.y = -300.0;
	v->p.z = -300.0;
	v->tu  = 1.0;
	v->tv  = 1.0;

	// east wall
	v += 1;

	v->p.x = -300.0;
	v->p.y = 300.0;
	v->p.z = 300.0;
	v->tu  = 0.0;
	v->tv  = 0.0;

	v += 1;

	v->p.x = -300.0;
	v->p.y = -300.0;
	v->p.z = 300.0;
	v->tu  = 0.0;
	v->tv  = 1.0;

	v += 1;

	v->p.x = -300.0;
	v->p.y = 300.0;
	v->p.z = -300.0;
	v->tu  = 1.0;
	v->tv  = 0.0;

	v += 1;

	v->p.x = -300.0;
	v->p.y = -300.0;
	v->p.z = -300.0;
	v->tu  = 0.0;
	v->tv  = 1.0;


	// west wall
	v += 1;

	v->p.x = 300.0;
	v->p.y = 300.0;
	v->p.z = -300.0;
	v->tu  = 0.0;
	v->tv  = 0.0;

	v += 1;

	v->p.x = 300.0;
	v->p.y = -300.0;
	v->p.z = -300.0;
	v->tu  = 0.0;
	v->tv  = 1.0;

	v += 1;

	v->p.x = 300.0;
	v->p.y = 300.0;
	v->p.z = 300.0;
	v->tu  = 1.0;
	v->tv  = 0.0;

	v += 1;

	v->p.x = 300.0;
	v->p.y = -300.0;
	v->p.z = 300.0;
	v->tu  = 0.0;
	v->tv  = 1.0;

	// ceiling
	v += 1;

	v->p.x = -300.0;
	v->p.y = 300.0;
	v->p.z = -300.0;
	v->tu  = 0.0;
	v->tv  = 0.0;

	v += 1;

	v->p.x = -300.0;
	v->p.y = 300.0;
	v->p.z = 300.0;
	v->tu  = 0.0;
	v->tv  = 1.0;

	v += 1;

	v->p.x = 300.0;
	v->p.y = 300.0;
	v->p.z = -300.0;
	v->tu  = 1.0;
	v->tv  = 0.0;

	v += 1;

	v->p.x = 300.0;
	v->p.y = 300.0;
	v->p.z = 300.0;
	v->tu  = 1.0;
	v->tv  = 1.0;

	// floor
	v += 1;

	v->p.x = -300.0;
	v->p.y = -300.0;
	v->p.z = -300.0;
	v->tu  = 0.0;
	v->tv  = 0.0;

	v += 1;

	v->p.x = -300.0;
	v->p.y = -300.0;
	v->p.z = 300.0;
	v->tu  = 0.0;
	v->tv  = 1.0;

	v += 1;

	v->p.x = 300.0;
	v->p.y = -300.0;
	v->p.z = -300.0;
	v->tu  = 1.0;
	v->tv  = 0.0;

	v += 1;

	v->p.x = 300.0;
	v->p.y = -300.0;
	v->p.z = 300.0;
	v->tu  = 1.0;
	v->tv  = 1.0;

	g_pVertextBuf->Unlock();

	eastPlane.normal.x = 1.0;
	eastPlane.normal.y = 0.0;
	eastPlane.normal.z = 0.0;
	eastPlane.position.x = -300.0;
	eastPlane.position.y = 0.0;
	eastPlane.position.z = 0.0;

	westPlane.normal.x = -1.0;
	westPlane.normal.y = 0.0;
	westPlane.normal.z = 0.0;
	westPlane.position.x = 300.0;
	westPlane.position.y = 0.0;
	westPlane.position.z = 0.0;

	southPlane.normal.x = 0.0;
	southPlane.normal.y = 0.0;
	southPlane.normal.z = 1.0;
	southPlane.position.x = 0.0;
	southPlane.position.y = 0.0;
	southPlane.position.z = -300.0;

	northPlane.normal.x = 0.0;
	northPlane.normal.y = 0.0;
	northPlane.normal.z = -1.0;
	northPlane.position.x = 0.0;
	northPlane.position.y = 0.0;
	northPlane.position.z = 300.0;

	ceilingPlane.normal.x = 0.0;
	ceilingPlane.normal.y = -1.0;
	ceilingPlane.normal.z = 0.0;
	ceilingPlane.position.x = 0.0;
	ceilingPlane.position.y = 300.0;
	ceilingPlane.position.z = 0.0;

	floorPlane.normal.x = 0.0;
	floorPlane.normal.y = 1.0;
	floorPlane.normal.z = 0.0;
	floorPlane.position.x = 0.0;
	floorPlane.position.y = -300.0;
	floorPlane.position.z = 0.0;
}

VOID MoveCamera()
{
    D3DXMatrixLookAtRH( &g_hMatView, &D3DXVECTOR3( 0.0, 0.0, 1000.0),
                                  &D3DXVECTOR3( 0.0, 0.0, 0.0 ),
                                  &D3DXVECTOR3( 0.0f, 1.0f, 0.0f ) );
    g_pd3dDevice->SetTransform( D3DTS_VIEW, &g_hMatView );

    D3DXMatrixPerspectiveFovRH( &g_hMatProj, D3DX_PI/2, 480.0/640.0f, 400.0f, -400.0f );
    g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &g_hMatProj );
}


VOID Render()
{
    D3DXMATRIX matNow;
	
	if (NULL == g_pd3dDevice)
        return;

    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );
    
    // we'll render the scene now
	g_pd3dDevice->BeginScene();

	MoveCamera ();

	// vertex buffer
	g_pd3dDevice->SetStreamSource (0, g_pVertextBuf, 0, sizeof (D3DVERTEX));
	g_pd3dDevice->SetFVF (D3DVERTEX::FVF);

	// wall
	g_pd3dDevice->SetTexture( 0, g_pWallTextures );

	// matrix for the house
	D3DXMatrixIdentity (&matNow);
	g_pd3dDevice->SetTransform (D3DTS_WORLDMATRIX(0), &matNow);

	g_pd3dDevice->DrawPrimitive (D3DPT_TRIANGLESTRIP, 0, 2);
	g_pd3dDevice->DrawPrimitive (D3DPT_TRIANGLESTRIP, 4, 2);
	g_pd3dDevice->DrawPrimitive (D3DPT_TRIANGLESTRIP, 8, 2);

	// ceiling
	g_pd3dDevice->DrawPrimitive (D3DPT_TRIANGLESTRIP, 12, 2);

	// floor
	g_pd3dDevice->SetTexture( 0, g_pFloorTextures );
	g_pd3dDevice->DrawPrimitive (D3DPT_TRIANGLESTRIP, 16, 2);

	// ball
	g_pd3dDevice->SetTexture( 0, g_pBallTextures );
	D3DXMatrixTranslation (&matNow, posBall.x, posBall.y, posBall.z);
	g_pd3dDevice->SetTransform (D3DTS_WORLDMATRIX(0), &matNow);
	g_pBallMesh->DrawSubset (0);

	// racket
	g_pd3dDevice->SetRenderState (D3DRS_ALPHABLENDENABLE, TRUE);
	g_pd3dDevice->SetRenderState (D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
    g_pd3dDevice->SetRenderState (D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);


	// use alpha channel in texture for alpha
    g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
    g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);

	D3DXMatrixIdentity (&matNow);
	g_pd3dDevice->SetTransform (D3DTS_WORLDMATRIX(0), &matNow);
	g_pd3dDevice->SetTexture (0, g_pRacketTextures);
	g_pd3dDevice->SetStreamSource (0, g_pRacketVertexBuf, 0, sizeof (D3DVERTEX));

	g_pd3dDevice->DrawPrimitive (D3DPT_TRIANGLESTRIP, 0, 2);

	g_pd3dDevice->SetRenderState (D3DRS_ALPHABLENDENABLE, FALSE);

    g_pd3dDevice->EndScene();
	// end of the scene rendering
    
    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}

[Edited by - Coder on August 26, 2004 12:39:52 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Coder
Please use [source][/source] tags for formatting your code. Check GDNet Forums FAQ.


I'm just wondering why those people can put their code elegantly, now I get it. Thanks.

Share this post


Link to post
Share on other sites

This topic is 4859 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.

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