Skip to main content
GameDev.net gamedev.net
🔒 Locked

DrawIndexedPrimitiveUP problem

Started by nedemon Sep 18, 2005 at 11:45 AM 6 replies 1.5k views
Original Post
nedemon
nedemon
Greetings to everyone. I recently got a task to draw a sphere. So i figured that the only normal way to do it is to use DrawIndexedPrimitive(UP). So i decided to start by drawing a rectangle from 2 triangles. Here's the code. It doesn't work and i guess that's because of DIP function.

//-----------------------------------------------------------------------------
#include <Windows.h>
#include <mmsystem.h>
#include <d3dx9.h>

LPDIRECT3D9             g_pD3D       = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9       g_pd3dDevice = NULL; // Our rendering device
LPDIRECT3DVERTEXBUFFER9 g_pVB        = NULL; // Buffer to hold vertices

struct CUSTOMVERTEX
{
    FLOAT x, y, z,rhw;
    DWORD color;
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)

HRESULT InitD3D( HWND hWnd )
{
    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;
    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &g_pd3dDevice ) ) )
    {
        return E_FAIL;
    }
    return S_OK;
}

HRESULT InitGeometry()
{
	CUSTOMVERTEX g_Vertices[4];
	g_Vertices[0].x = 150.0f;
	g_Vertices[0].y = 50.0f;
	g_Vertices[0].z = 0.5f;
	g_Vertices[0].rhw = 1.0f;
	g_Vertices[0].color = 0xffff0000;
	
	g_Vertices[1].x = 250.0f;
	g_Vertices[1].y = 250.0f;
	g_Vertices[1].z = 0.5f;
	g_Vertices[1].rhw = 1.0f;
	g_Vertices[1].color = 0xff00ff00;
	
	g_Vertices[2].x = 50.0f;
	g_Vertices[2].y = 250.0f;
	g_Vertices[2].z = 0.5f;
	g_Vertices[2].rhw = 1.0f;
	g_Vertices[2].color = 0xff00ffff;
    g_Vertices[3].x = 100.0f;
	g_Vertices[3].y = 150.0f;
	g_Vertices[3].z = 0.5f;
	g_Vertices[3].rhw = 1.0f;
	g_Vertices[3].color = 0;
    if( FAILED( g_pd3dDevice->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX),
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_DEFAULT, &g_pVB, NULL)))
    {
        return E_FAIL;
    }
    VOID* pVertices;
    if( FAILED( g_pVB->Lock( 0, sizeof(g_Vertices), (void**)&pVertices, 0 )))
        return E_FAIL;
			
    memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
    g_pVB->Unlock();

    return S_OK;
}
VOID Cleanup()
{
    if( g_pVB != NULL )
        g_pVB->Release();

    if( g_pd3dDevice != NULL )
        g_pd3dDevice->Release();

    if( g_pD3D != NULL )
        g_pD3D->Release();
}

VOID Render()
{
	WORD ind[] = {0, 1, 2, 1, 2, 3};
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );

    if( SUCCEEDED( g_pd3dDevice->BeginScene()))
    {
        g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX));
        g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX );
g_pd3dDevice->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, 6, 
2, ind, D3DFMT_INDEX16, &g_pVB[0], sizeof(CUSTOMVERTEX));
 
        g_pd3dDevice->EndScene();
    }
    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
        case WM_DESTROY:
            Cleanup();
            PostQuitMessage( 0 );
            return 0;
    }

    return DefWindowProc( hWnd, msg, wParam, lParam );
}
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
                      GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                      "D3D", NULL };
    RegisterClassEx( &wc );
    HWND hWnd = CreateWindow( "D3D", "D_3_D",
                              WS_OVERLAPPEDWINDOW, 100, 100, 256, 256,
                              GetDesktopWindow(), NULL, wc.hInstance, NULL );

    if( SUCCEEDED( InitD3D( hWnd ) ) )
    {
        if( SUCCEEDED( InitGeometry() ) )
        {
            ShowWindow( hWnd, SW_SHOWDEFAULT );
            UpdateWindow( hWnd );
            MSG msg;
            ZeroMemory( &msg, sizeof(msg) );
            while( msg.message!=WM_QUIT )
            {
                if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
                {
                    TranslateMessage( &msg );
                    DispatchMessage( &msg );
                }
                else
                    Render();
            }
        }
    }

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

What else...I'm familiar with DX sdk so cut it out. Also i used google and the only way it helped me is that it ran me into this place. Sorry for the size of code, but due to my beginning nature i thought i would be able to make mistakes anywhere. Thanx.
Evil Steve
Evil Steve
DrawIndexedPrimitiveUP() doesn't use a vertex buffer. It's for rendering from a pointer to an array, which makes it slower, but sometimes more convenient (I use it a fair bit in my apps).
EDIT: Try one of these instead:
g_pd3dDevice->SetIndices(g_pIB);g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 6, 0, 2);

And create an index buffer. Alternatively:
g_pd3dDevice->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, 6, 2, ind, D3DFMT_INDEX16, &g_Vertices[0], sizeof(CUSTOMVERTEX));
nedemon
nedemon
Well, nothing special about debug because the program works and even displays the blue background. It just doesn't output the figure.

I got it about buffers. How do i specify an array with indices? Is it SetIndices?
EDIT: Thanx for the code, ill try.
nedemon
nedemon
Still, doesn't seem to work
LPDIRECT3DINDEXBUFFER9 g_pIB = NULL;g_pd3dDevice->CreateIndexBuffer(sizeof(ind), 0, D3DFMT_INDEX16			,D3DPOOL_DEFAULT, &g_pIB, NULL);g_pd3dDevice->SetIndices(g_pIB);g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 6, 0, 2);

Or did i not get it? I'm just unable to get it working.

[Edited by - nedemon on September 18, 2005 12:11:16 PM]
nedemon
nedemon
I also tried drawing a triangle with 3-size of index array but it also doesnt work.
Evil Steve
Evil Steve
Do you actually lock and fill the index buffer? I should have mentioned that, sorry.
Also, the 4th parameter should be 4, not 6 (You're only accessing 4 vertices, not 6). The number of indices read from the IB (6) is implied by the primitive count and the primitive type.
Also, it's useful to disable back face culling just for testing purposes:
g_pd3dDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
nedemon
nedemon
I figured stuff about locking it just took some time.
Thanx, your help has been useful.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.