Sign in to follow this  
DragonGeo2

Rendering a scene

Recommended Posts

Hey guys, I'm having problems rendering a supposedly simple scene. I just want to draw three quads to the screen using DirectX9 in C++ and all I'm seeing is a black screen (which means that the Clear call is working). Can someone take a look at the code and tell me why I'm not seeing anything (maybe the vertices are being drawn or transformed off the screen, maybe I'm not drawing the vertex streams right, who knows).
#define IDC_MYICON                      2
#define IDD_DIRECTX_DIALOG              102
#define IDD_ABOUTBOX                    103
#define IDS_APP_TITLE                   103
#define IDM_ABOUT                       104
#define IDM_EXIT                        105
#define IDS_HELLO                       106
#define IDI_DIRECTX_ICON                107
#define IDC_DIRECTX_MENU                108
#define IDC_DIRECTX_ACCELERATOR         109
#define IDC_DIRECTX                     110
#define IDR_MAINFRAME                   128
#define IDC_STATIC                      -1

#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        129
#define _APS_NEXT_COMMAND_VALUE         32771
#define _APS_NEXT_CONTROL_VALUE         1000
#define _APS_NEXT_SYMED_VALUE           110
#endif
#endif

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <mmsystem.h>
#include <d3d9.h>
#include <d3dx9.h>

HWND              g_hWnd       = NULL;
LPDIRECT3D9       g_pD3D       = NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;

LPDIRECT3DVERTEXBUFFER9 g_pTriangleStrip_VB = NULL;
LPDIRECT3DVERTEXBUFFER9 g_pTriangleStrip2_VB = NULL;
LPDIRECT3DVERTEXBUFFER9 g_pTriangleStrip3_VB = NULL;

#define D3DFVF_MY_VERTEX ( D3DFVF_XYZ | D3DFVF_DIFFUSE )

struct Vertex
{
	float x, y, z;
    DWORD color;
};

// Ball
Vertex g_triangleStrip[] = 
{
	{ 1.0f / 635.0f, 1.0f / 507.0f, 10.0f, D3DCOLOR_COLORVALUE(1.0, 1.0, 1.0, 1.0) },
	{ 1.0f / 635.0f, 1.0f / 517.0f, 10.0f, D3DCOLOR_COLORVALUE(1.0, 1.0, 1.0, 1.0) },
	{ 1.0f / 645.0f, 1.0f / 507.0f, 10.0f, D3DCOLOR_COLORVALUE(1.0, 1.0, 1.0, 1.0) },
	{ 1.0f / 645.0f, 1.0f / 517.0f, 10.0f, D3DCOLOR_COLORVALUE(1.0, 1.0, 1.0, 1.0) }
};
// Pad1
Vertex g_triangleStrip2[] = 
{
	{ 1.0f / 1.0f / 5.0f, 300.0f, 10.0f, D3DCOLOR_COLORVALUE(1.0, 1.0, 1.0, 1.0) },
	{ 1.0f / 1.0f / 5.0f, 340.0f, 10.0f, D3DCOLOR_COLORVALUE(1.0, 1.0, 1.0, 1.0) },
	{ 1.0f / 1.0f / 10.0f, 300.0f, 10.0f, D3DCOLOR_COLORVALUE(1.0, 1.0, 1.0, 1.0) },
	{ 1.0f / 1.0f / 10.0f, 340.0f, 10.0f, D3DCOLOR_COLORVALUE(1.0, 1.0, 1.0, 1.0) }
};
// Pad2
Vertex g_triangleStrip3[] = 
{
	{ 1.0f / 1.0f / 5.0f, 300.0f, 10.0f, D3DCOLOR_COLORVALUE(1.0, 1.0, 1.0, 1.0) },
	{ 1.0f / 1.0f / 5.0f, 340.0f, 10.0f, D3DCOLOR_COLORVALUE(1.0, 1.0, 1.0, 1.0) },
	{ 1.0f / 1.0f / 10.0f, 300.0f, 10.0f, D3DCOLOR_COLORVALUE(1.0, 1.0, 1.0, 1.0) },
	{ 1.0f / 1.0f / 10.0f, 340.0f, 10.0f, D3DCOLOR_COLORVALUE(1.0, 1.0, 1.0, 1.0) }
};

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
				   LPSTR lpCmdLine, int nCmdShow);
LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
void init(void);
void shutDown(void);
void render(void);

int WINAPI WinMain(	HINSTANCE hInstance,
					HINSTANCE hPrevInstance,
					LPSTR     lpCmdLine,
					int       nCmdShow )
{
	WNDCLASSEX winClass; 
	MSG        uMsg;

    memset(&uMsg,0,sizeof(uMsg));
    
	winClass.lpszClassName = "MY_WINDOWS_CLASS";
	winClass.cbSize        = sizeof(WNDCLASSEX);
	winClass.style         = CS_HREDRAW | CS_VREDRAW;
	winClass.lpfnWndProc   = WindowProc;
	winClass.hInstance     = hInstance;
	winClass.hIcon	       = LoadIcon(hInstance, (LPCTSTR)IDI_DIRECTX_ICON);
    winClass.hIconSm	   = LoadIcon(hInstance, (LPCTSTR)IDI_DIRECTX_ICON);
	winClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
	winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	winClass.lpszMenuName  = NULL;
	winClass.cbClsExtra    = 0;
	winClass.cbWndExtra    = 0;

	if( !RegisterClassEx(&winClass) )
		return E_FAIL;

	g_hWnd = CreateWindowEx( NULL, "MY_WINDOWS_CLASS", 
                             "Window1",
						     WS_OVERLAPPEDWINDOW | WS_VISIBLE,
					         0, 0, 1280, 1024, NULL, NULL, hInstance, NULL );

	if( g_hWnd == NULL )
		return E_FAIL;

    ShowWindow( g_hWnd, nCmdShow );
    UpdateWindow( g_hWnd );

	init();

	while( uMsg.message != WM_QUIT )
	{
		if( PeekMessage( &uMsg, NULL, 0, 0, PM_REMOVE ) )
		{ 
			TranslateMessage( &uMsg );
			DispatchMessage( &uMsg );
		}
        else
		    render();
	}

	shutDown();

    UnregisterClass( "MY_WINDOWS_CLASS", winClass.hInstance );

	return uMsg.wParam;
}

LRESULT CALLBACK WindowProc( HWND   hWnd, 
							 UINT   msg, 
							 WPARAM wParam, 
							 LPARAM lParam )
{
    switch( msg )
	{
        case WM_KEYDOWN:
		{
			switch( wParam )
			{
				case VK_ESCAPE:
					PostQuitMessage(0);
				break;
			}
		}
        break;

		case WM_CLOSE:
		{
			PostQuitMessage(0);	
		}
		
        case WM_DESTROY:
		{
            PostQuitMessage(0);
		}
        break;

		default:
		{
			return DefWindowProc( hWnd, msg, wParam, lParam );
		}
		break;
	}

	return 0;
}

void init( void )
{
    g_pD3D = Direct3DCreate9( D3D_SDK_VERSION );

    D3DDISPLAYMODE d3ddm;

    g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm );

    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( &d3dpp, sizeof(d3dpp) );

    d3dpp.Windowed               = TRUE;
    d3dpp.SwapEffect             = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat       = d3ddm.Format;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
    d3dpp.PresentationInterval   = D3DPRESENT_INTERVAL_IMMEDIATE;

    g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd,
                          D3DCREATE_HARDWARE_VERTEXPROCESSING,
                          &d3dpp, &g_pd3dDevice );

	g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
	g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

	D3DXMATRIX mProjection;
    D3DXMatrixPerspectiveFovLH( &mProjection, D3DXToRadian( 110.0f ), 1280.0f / 1024.0f, 1.0f, 100.0f );
    g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &mProjection );

	Vertex *pVertices = NULL;

	// Ball
	g_pd3dDevice->CreateVertexBuffer( 4*sizeof(Vertex), 0, D3DFVF_MY_VERTEX,
									  D3DPOOL_DEFAULT, &g_pTriangleStrip_VB,
									  NULL );

	pVertices = NULL;
	g_pTriangleStrip_VB->Lock( 0, sizeof(g_triangleStrip), (void**)&pVertices, 0 );
    memcpy( pVertices, g_triangleStrip, sizeof(g_triangleStrip) );
    g_pTriangleStrip_VB->Unlock();

	// Pad1
	g_pd3dDevice->CreateVertexBuffer( 4*sizeof(Vertex), 0, D3DFVF_MY_VERTEX,
									  D3DPOOL_DEFAULT, &g_pTriangleStrip2_VB,
									  NULL );

	pVertices = NULL;
	g_pTriangleStrip2_VB->Lock( 0, sizeof(g_triangleStrip2), (void**)&pVertices, 0 );
    memcpy( pVertices, g_triangleStrip2, sizeof(g_triangleStrip2) );
    g_pTriangleStrip2_VB->Unlock();

	// Pad2
	g_pd3dDevice->CreateVertexBuffer( 4*sizeof(Vertex), 0, D3DFVF_MY_VERTEX,
									  D3DPOOL_DEFAULT, &g_pTriangleStrip3_VB,
									  NULL );

	pVertices = NULL;
	g_pTriangleStrip3_VB->Lock( 0, sizeof(g_triangleStrip3), (void**)&pVertices, 0 );
    memcpy( pVertices, g_triangleStrip3, sizeof(g_triangleStrip3) );
    g_pTriangleStrip3_VB->Unlock();
}

void shutDown( void )
{
	if( g_pTriangleStrip_VB != NULL ) 
        g_pTriangleStrip_VB->Release(); 

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

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

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

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

void render( void )
{
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
                         D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,1.0f), 1.0f, 0 );

	g_pd3dDevice->BeginScene();

	D3DXMATRIX mWorld;
	D3DXMatrixTranslation( &mWorld, 0.0f, 0.0f, 0.0f );
	g_pd3dDevice->SetTransform( D3DTS_WORLD, &mWorld );

	// Ball
	g_pd3dDevice->SetStreamSource( 0, g_pTriangleStrip_VB, 0, sizeof(Vertex) );
	g_pd3dDevice->SetFVF( D3DFVF_MY_VERTEX );
	g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2 );

	// Pad1
	g_pd3dDevice->SetStreamSource( 0, g_pTriangleStrip2_VB, 0, sizeof(Vertex) );
	g_pd3dDevice->SetFVF( D3DFVF_MY_VERTEX );
	g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2 );

	// Pad2
	g_pd3dDevice->SetStreamSource( 0, g_pTriangleStrip3_VB, 0, sizeof(Vertex) );
	g_pd3dDevice->SetFVF( D3DFVF_MY_VERTEX );
	g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2 );

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

Share this post


Link to post
Share on other sites
I don't see a view matrix created anywhere. You need to make one using D3DXMatrixLookAtLH(&view,&eye,&at,&up); Then once you've done that, you need to make your viewProj matrix using D3DXMatrixMultiply(&viewProj,&view,&proj); Then set the correct matrices (I know you need to set D3DTS_WORLD like you do, but I don't remember about view and projection matrices. I usually use shaders so I'm not used to this). See if that helps.

Share this post


Link to post
Share on other sites

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