Sigh, Why can't I draw a 2D quad on top of my 3d scene?

Started by
3 comments, last by oxygen_728 20 years ago
I've tried for so long to get this to work... I put the code to create & display a quad in my main render() function. Of course this will be hidden away in some other functions, but to figure out how to get it to work I put it in my render function. Basically.... Begin Scene 1. Setup Matrices 2. Process Input 3. Draw 3d Scene (building and bouncing balls) 4. "HOPEFULLY" overlay a quad (2 triangles with triangle strip) 5. Draw text End Scene Perhaps from seeing those steps you can see something wrong... I'm out of ideas. I've looked at so many tutorials that my eyes hurt... my code LOOKS right... I just don't see any results no matter how I flip around my vertices. Here's my code, Thank you VERY much for your help & time.

VOID Render()
{
	// Process Keyboard Input & Update Objects accordingly

	ProcessKBInput();
	if(!UpdateObjects())
		MessageBox(NULL,"Failed to Update Objects", "Update Objects (main)", MB_OK);

    // Clear the backbuffer and the zbuffer

    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 
                         D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
    
    // Begin the scene

    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
    {
		RECT destRect;
		SetRect( &destRect, 1, 1, 0, 0 );

        // Setup the world, view, and projection matrices

        SetupMatrices();

		// Render our 3D stuff ... Building + Bouncing Balls

		Building1->Render(g_pd3dDevice);	
		//CP->Render(g_pd3dDevice);


//Code to Draw a Square////////////////////////////////


	LPDIRECT3DVERTEXBUFFER9 g_square;
	CPVertex g_square_vertices[] ={
		{ 250.0f,  200.0f, 1.0f, 0.5f, 0xFFFFFFFF }, // x, y, z, rhw, color

		{ 250.0f,  50.0f,  1.0f, 0.5f, 0xFFFFFFFF },
		{ 400.0f,  200.0f, 1.0f, 0.5f, 0xFFFFFFFF },
		{ 400.0f,  50.0f,  1.0f, 0.5f, 0xFFFFFFFF } };

	if( FAILED( g_pd3dDevice->CreateVertexBuffer( 4*sizeof(CPVertex),
													0, D3DFVF_CUSTOMVERTEX,
													D3DPOOL_DEFAULT, &g_square, NULL ) ) )
	{
		// handle case

	}


	VOID* pVertices;
	if( FAILED( g_square->Lock( 0, sizeof(g_square_vertices), (void**)&pVertices, 0 ) ) )
		return;
	memcpy( pVertices, g_square_vertices, sizeof(g_square_vertices) );
	g_square->Unlock();


	g_pd3dDevice->SetStreamSource(0,g_square,0, sizeof(CPVertex));
	g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
	g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2);

//END code to draw a square//////////////////////////////////


	// Begin Font Stuff

	// Display frames per second.

	char buffer[20];
	long debugFPS = timeGetTime();
	if( debugFPS - FPSTIME2 > 250 ){
		DEBUGNUM = 1000.0f / (debugFPS - FPSTIME);
		FPSTIME2 = debugFPS;
	}
	FPSTIME = debugFPS;
	gcvt(DEBUGNUM,3,buffer);

	g_pd3dxFont->DrawText( NULL,
		strcat(buffer, " fps"), 
		-1, 
		&destRect, 
		DT_NOCLIP, 
		D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f) );
	// End Font Stuff



        // End the scene

        g_pd3dDevice->EndScene();
    }

    // Present the backbuffer contents to the display

    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}

[edited by - oxygen_728 on March 29, 2004 2:13:32 AM] [edited by - oxygen_728 on March 29, 2004 2:15:00 AM]
Advertisement
Again, I just want to say I highly appreciate the time and effort you experts are willing to give to help people like myself.
I''ll answer my question now... I found a great tutorial by Andy Pike that explains this.

First off, you render your 3d stuff & end your scene.

Second, you setup a new set of matrices for your 2D stuff. (World Matrix etc)

THEN you can render your 2d stuff!

Here''s a link

http://www.andypike.com/tutorials/DirectX8/011.asp
Okay, I'm not sure about the world, view, and projection matrices. They seem to be the same as when you render the normal scene. Since you're using transformed vertices I think that you're going to want to reset the World, View, and Projection matrices back to NULL. (and as a side note: Creating a vertex buffer each frame!? You should create it as little as possible, and since it appears to be the same from frame to frame you should create it at load time and then use when rendering. Now if you used a varying amount of vertices on each frame I could see a justification for creating one each frame, but yours is static so you should create it ONCE.)

Edit: Or you could answer your own question while I'm answering it.

[edited by - Erzengeldeslichtes on March 29, 2004 2:38:48 AM]
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
hehe, Ya i was just doing everything each from to made sure it got done. I just had some ordering issues.

Thanks for the reply

This topic is closed to new replies.

Advertisement