Totally newb direct3D problem with invisable geometry

Started by
5 comments, last by superpig 17 years, 8 months ago
Hey all, i was wondering if you could help me out here, I'm trying to make a simple demo that will create a triangle, but I can't seem to see my geometry, In the main function I intialise Direct3D like this:

//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D9             g_pD3D       = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9       g_pd3dDevice = NULL; // Our rendering device
LPDIRECT3DVERTEXBUFFER9	g_pVertexBuffer=NULL;

Pryamid FirstCollectable; //

//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
    // Create the D3D object, which is needed to create the D3DDevice.
    if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
        return E_FAIL;
    
	D3DPRESENT_PARAMETERS d3dpp; 
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

    // Create the Direct3D device    
	if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &g_pd3dDevice ) ) )
    {
        return E_FAIL;
    }

    // Device state would normally be set here
	g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, D3DCOLOR_XRGB(255,255,255));

    // Turn off D3D lighting, since we are providing our own vertex colors
    g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );

    return S_OK;
}

With that lot done i try to draw using this

//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
    if( NULL == g_pd3dDevice )
        return;

    // Clear the backbuffer to a black color
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );
    
    // Begin the scene
    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
    {
        // Rendering of scene objects can happen here
	FirstCollectable.DrawPryamid (g_pd3dDevice);
        // End the scene
        g_pd3dDevice->EndScene();
    }

    // Present the backbuffer contents to the display
    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}

Once that lot is done my WinMain looks a bit like this:

//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
    // Register the window class
    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, 
                      GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                      "D3D Tutorial", NULL };
    RegisterClassEx( &wc );

    // Create the application's window
    HWND hWnd = CreateWindow( "D3D Tutorial", "ICA", 
                              WS_OVERLAPPEDWINDOW, 100, 100, 800, 600,
                              NULL, NULL, wc.hInstance, NULL );

    // Initialize Direct3D
    if( SUCCEEDED( InitD3D( hWnd ) ) )
    { 
        // Show the window
        // Create Some Geometry
		FirstCollectable.Initialise (g_pd3dDevice, g_pD3D, g_pVertexBuffer );
		
		// Set up a camera (or matricees)
		Camera newcamera (g_pd3dDevice); 

		ShowWindow( hWnd, SW_SHOWDEFAULT );
        UpdateWindow( hWnd );
        // Enter the message loop
        MSG msg; 
        while( GetMessage( &msg, NULL, 0, 0 ) )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
    }

    UnregisterClass( "D3D Tutorial", wc.hInstance );
    return 0;
}

I have classes to initialise the triangle that go like this:

void Pryamid::Initialise (LPDIRECT3DDEVICE9 gp_D3dDevice, LPDIRECT3D9 g_pD3D, LPDIRECT3DVERTEXBUFFER9 gp_VertexBuffer)
{
VertexBuffer = gp_VertexBuffer;
gp_D3dDevice->CreateVertexBuffer( 3*sizeof(newVertex), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_MANAGED, &VertexBuffer, NULL );

VertexBuffer->Lock (0,0,(void**)&newVertex,0);

newVertex[0].FillVertex (-1.0f, -1.0f, 0.0f, 0xffff0000);
newVertex[1].FillVertex ( 1.0f, -1.0f, 0.0f, 0xff0000ff);
newVertex[2].FillVertex ( 0.0f,  1.0f, 0.0f, 0xffffffff);

VertexBuffer->Unlock ();
}
Once that is initialised i use this to draw:

void Pryamid::DrawPryamid (LPDIRECT3DDEVICE9 gp_D3dDevice)
{
	// To draw we pass the vertices down a stream. We set the source as our vertex buffer	
	gp_D3dDevice->SetStreamSource(0,VertexBuffer,0,sizeof(newVertex));
	//gp_D3dDevice
	// We then tell Direct3D the make up of the vertices
	gp_D3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);

	// Now draw our one triangle
	gp_D3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
}
I guess you might want to see the pryamid.h file :):

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)

class Pryamid
{
public:
	Pryamid(void);
	void DrawPryamid (LPDIRECT3DDEVICE9);
	void Initialise (LPDIRECT3DDEVICE9, LPDIRECT3D9, LPDIRECT3DVERTEXBUFFER9);
	LPDIRECT3DDEVICE9 m_Direct3DDevice;
	LPDIRECT3DVERTEXBUFFER9 VertexBuffer;
	Vertex newVertex [3];

public:
	~Pryamid(void);
};
and finally i have a camera class that i use to setup all my matricees:

#include "Camera.h"


Camera::Camera(LPDIRECT3DDEVICE9 gp_D3dDevice)
{
D3DXMATRIX matProj;
	FLOAT fAspect=((FLOAT)800)/600;
	D3DXMatrixPerspectiveFovLH(&matProj,D3DX_PI/4,fAspect,1.0f,100.0f);
	gp_D3dDevice->SetTransform(D3DTS_PROJECTION,&matProj);

	// View Matrix
	// Set the camera back a bit along the Z axis and looking down Z	
	D3DXMATRIX matView;	
	D3DXVECTOR3 vEyePt(0.0f,1.0f,-3.0f );
	D3DXVECTOR3 vLookatPt(0.0f,0.0f,1.0f );
	D3DXVECTOR3 vUpVec(0.0f,1.0f,0.0f );		
	D3DXMatrixLookAtLH(&matView,&vEyePt,&vLookatPt,&vUpVec );
	gp_D3dDevice->SetTransform(D3DTS_VIEW,&matView ); 

	// World Matrix
	// Just using identity (so triangle is at origin of world)
	D3DXMATRIX matWorld;
	D3DXMatrixIdentity(&matWorld);
	gp_D3dDevice->SetTransform(D3DTS_WORLD, &matWorld ); 
}

Camera::~Camera(void)
{
}
Well thats about it, all i get is a black screen with nothing showing at all. If there was anything else that you think you need to tell me whats going on please let me know. Oh and I'm sorry to be begging for help, but I'm a total newb at doing anything much with directx and well i didn't really know what else i should do :(
Advertisement
two sugestions:
1. try moving the cam a couple of steps back , cause you might be standing infront of it.
2. turn off back face culling

g_pd3dDevice->SetRenderState(D3DRS_CULLMODE,0);
if not instructed other wise , direct3d will remove one face of a polygon , assuming you will
never see that face. for example if you have a decorative crate , the player wouldn't be
allowed to look inside such a thing , so direct3d doesn't render at all to save some cpu cycles.
see D3DRS_CULLMODE in the directx SDK documentation.
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction."
I think your problem is that your triangle points are declared in a counter-clockwise order. You are looking at what directx thinks is the backside of your triangle. Make your points in a clockwise order and it should work.
I've tried inverting the order which I initialise the verticess and it hasn't made any difference :( anyhoo, i have uploaded the code onto my webspace download it here its built for visual studio 2005 rather than 2003 (i dunno how many people still us that)

i wish i knew what was wrong with this, I think that I've got everything in there and it matches up pretty much perfectly with the demos from microsoft.
Try directtutorial.com. There are really good tutorials there.
That site looks good, I think I'm ok when it comes to the actual starting of it, its just when i split the files up into seperate classes that i really have trouble.

PLEASE CAN SOMEONE HELP ME! Is there something blinding obvious that I'm missing here? I think that I've checked every tutorial and forum i can and i still can't see whats wrong with this bloody thing!

[Edited by - -pete- on August 14, 2006 7:11:34 AM]
There's a tool in the DirectX SDK called 'PIX' that will help here (though you need the most recent version of it - August 2006). Take a single-frame PIX capture of your program and then look at what it's doing.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

This topic is closed to new replies.

Advertisement