Thread: Total newb directx invisable geometry question

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    6

    Total newb directx invisable geometry question

    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:

    Code:
    //-----------------------------------------------------------------------------
    // 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
    Code:
    //-----------------------------------------------------------------------------
    // 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:

    Code:
    //-----------------------------------------------------------------------------
    // 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:

    Code:
    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:

    Code:
    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 :

    Code:
    #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:

    Code:
    #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

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Common mistakes:

    • Turning lighting on yet not specifying a light source. Since black is the absence of light, if you clear the screen to black and draw...no object.
    • Not setting the projection matrix
    • Not setting the view matrix
    • Not translating the object to the correct world space
    • Generating an incorrect camera view matrix
    • Turning lighting off, thus using pre-lit vertices, but not specifying a diffuse color for the vertices
    • Not using the correct flexible vertex format flags
    • Not using the flexible vertex format flags in the order they appear in the vertex structure.
    • Setting a 360 degree projection matrix - this results in very small objects and odd renders.
    • Using vertex buffers yet not specifying the stream source
    • Using index buffers yet not specifying the index source
    • Using vertex buffers but not specifying the flexible vertex format for them.
    • Specifying an incorrect vertex stride length in SetStreamSource()


    I'm betting it's one of those. W/o more code it's impossible to tell you what it is. Show me the setup for this scene.
    Last edited by VirtualAce; 08-12-2006 at 08:39 PM.

  3. #3
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Not to sound too stupid or anything, but where do you call Render()? I don't see it in the Main function.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    Quote Originally Posted by Frobozz
    Not to sound too stupid or anything, but where do you call Render()? I don't see it in the Main function.
    Its in my MsgProc () function, since i thought that would be best place for it.

    Anyhow, 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.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    For DX the diff between 2003 and 2005 will be next to nothing. The only thing that might flag a warning is if you use the C standard library functions which Microsoft has taken it upon themselves to declare deprecated - which it IS NOT DEPRECATED and is still part of the ISO standard.

    To remove deprecation use _CRT_SECURE_NO_DEPRECATE in your project options under pre-processor. This will remove those non-standard warnings. Again the only thing non-standard and deprecated here is MS's attempt to hijack the language.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    I don't think its really made much difference, i don't get any problems or errors (none that cause any compiler errors anyway) but i don't actually have 2003 to make a new solution.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 11-03-2008, 09:48 PM
  2. newb question about interpreting and writing functions
    By crazychile in forum C Programming
    Replies: 1
    Last Post: 10-23-2008, 07:51 PM
  3. newb question
    By C_ntua in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2008, 09:44 AM
  4. Newb question: Modifying Arrays
    By Almina in forum C++ Programming
    Replies: 1
    Last Post: 07-07-2008, 11:17 PM
  5. DirectX question, please read!
    By Crossbow in forum Game Programming
    Replies: 2
    Last Post: 12-12-2001, 07:18 PM