Sign in to follow this  

Black Screen

This topic is 3811 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have this scene i'm doing. It has a street. Two cars and two lamppost. The problem is. The lamppost don't render at all. One of the cars do but not the second. The street doesn't even show. I just get a black screen. I've gone over the code. I cannot figure it out. Pix shows no errors neither does VS2005. It returns native 0. I just don't know what it could be. Heres the link to the image. http://tinypic.com/fullsize.php?pic=4qehst1 // Mesh loader
#include "CFXMesh.h"

CFXMesh::CFXMesh()
{
	m_pMesh = NULL;
	m_pMaterials = NULL;
	m_pTextures = NULL;
	m_dwNumMaterials = 0;
}

CFXMesh::~CFXMesh()
{
	m_dwNumMaterials = 0;
	delete[] m_pTextures;
	delete[] m_pMaterials;
}

BOOL CFXMesh::LoadMesh(CComPtr<IDirect3DDevice9> pDevice, wstring wsFile)
{
	CComPtr<ID3DXBuffer> buffer = NULL;

	HRESULT hResult = D3DXLoadMeshFromX(wsFile.c_str(), D3DXMESH_SYSTEMMEM, pDevice, NULL,
										&buffer, NULL, &m_dwNumMaterials, &m_pMesh);
	if(FAILED(hResult))
	{
		SHOWERROR(TEXT("Failed to load mesh"));
		return FALSE;
	}

	D3DXMATERIAL *materials = (D3DXMATERIAL*)buffer->GetBufferPointer();

	m_pMaterials = new D3DMATERIAL9[m_dwNumMaterials];
	m_pTextures = new CComPtr<IDirect3DTexture9>[m_dwNumMaterials];

	for(DWORD i = 0; i < m_dwNumMaterials; i++)
	{
		m_pMaterials[i] = materials[i].MatD3D;
		m_pMaterials[i].Ambient = m_pMaterials[i].Diffuse;
		m_pTextures[i] = NULL;

		if(materials[i].pTextureFilename)
		{
			hResult = D3DXCreateTextureFromFileA(pDevice, materials[i].pTextureFilename, &m_pTextures[i]);
			if(FAILED(hResult))
				return FALSE;
		}
	}
	return TRUE;
}

void CFXMesh::Render(CComPtr<IDirect3DDevice9> pDevice, CComPtr<ID3DXEffect> pEffect, D3DXHANDLE hParameter)
{
	if(pDevice && m_pMesh)
	{
		for(DWORD i = 0; i < m_dwNumMaterials; ++i)
		{
			pDevice->SetMaterial(&m_pMaterials[i]);
			pEffect->SetTexture(hParameter, m_pTextures[i]);
			pEffect->CommitChanges();
			m_pMesh->DrawSubset(i);
		}
	}
}


// Heres my calling code
#include "CFX.h"
#include <crtdbg.h>
#include "DebugNew.h"

#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif

struct CUSTOMVERTEX
{
	float x, y, z;
	float u, v;
};
const DWORD FVF = D3DFVF_XYZ | D3DFVF_TEX0;

CUSTOMVERTEX g_tri[] =
{
	{20.0f, -10.0f, 0.0f, -0.25f, 25.0f},
	{20.0f, 100.0f, 0.0f, -0.25f, 0.0f},
	{-2.0f, -10.0f, 0.0f, 0.25f, 25.0f},
	{-2.0f, 100.0f, 0.0f, 0.25f, 0.0f},
	{-2.0f, -10.0f, 1.0f, 0.375f, 25.0f},
	{-2.0f, 100.0f, 1.0f, 0.375f, 0.0f},
	{-3.0f, -10.0f, 1.0f, 0.5f, 25.0f},
	{-3.0f, 100.0f, 1.0f, 0.5f, 0.0f},
	{-13.0f, -10.0f, 1.0f, 0.75f, 25.0f},
	{-13.0f, 100.0f, 1.0f, 0.75f, 0.0f},
	{-13.0f, -10.0f, 21.0f, 1.25f, 25.0f},
	{-13.0f, 100.0f, 21.0f, 1.25f, 0.0f}
};

CFX::CFX()
{
	m_pWindow = NULL;
	m_pDecl = NULL;
	m_pEffect = NULL;
	m_pTexture = NULL;
} // End of CFX constructor.

CFX::~CFX()
{
	if(m_pWindow != NULL)
	{
		delete m_pWindow;
		m_pWindow = NULL;
	}
} // End of CFX destructor.

void CFX::SetWindow(CFXWindow *pWindow)
{
	m_pWindow = pWindow;
} // End of SetWindow function.

BOOL CFX::Initialize()
{
	return TRUE;
} // End of Initialize function.

void CFX::OnCreate(ATL::CComPtr<IDirect3DDevice9> pDevice)
{
	CComPtr<ID3DXBuffer> buffer = NULL;
	m_fpsFont.Initialize(pDevice, 15, true, false, TEXT("Arial"));
	D3DXCreateEffectFromFile(pDevice, TEXT("sample.fx"), NULL, NULL, NULL, NULL, &m_pEffect, &buffer);
	D3DXCreateTextureFromFile(pDevice, TEXT("streettexture.jpg"), &m_pTexture);
	if(!m_lampPost.LoadMesh(pDevice, TEXT("lamppost.x")))
	{
		SHOWERROR(TEXT("Failed to load mesh."));
	}
	if(!m_car.LoadMesh(pDevice, TEXT("car.x")))
	{
		SHOWERROR(TEXT("Failed to load mesg."));
	}
} // End of OnCreate function.

void CFX::OnReset(ATL::CComPtr<IDirect3DDevice9> pDevice)
{
	m_fpsFont.OnReset();
	m_pEffect->OnResetDevice();

	D3DVERTEXELEMENT9 vertDecl[] =  
	{  
		{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},  
		{0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
		D3DDECL_END()  
	}; 

	pDevice->SetTexture(0, m_pTexture);

	pDevice->CreateVertexDeclaration(vertDecl, &m_pDecl);
	pDevice->SetVertexDeclaration(m_pDecl);

	pDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
	pDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
	pDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
	pDevice->SetRenderState(D3DRS_FILLMODE, m_pWindow->GetFillMode());
	pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

	m_effectVertex.CreateBuffer(pDevice, 12, NULL, sizeof(CUSTOMVERTEX));
	m_effectVertex.SetInfo(12, g_tri);

	D3DXMATRIXA16 world;

	D3DXMatrixIdentity(&world);
	D3DXMatrixIdentity(&view);
	D3DXMatrixIdentity(&proj);
	//pDevice->SetTransform(D3DTS_WORLD, &world);

	D3DXVECTOR3 Eye(25.0f, -18.0f, 13.0f);
	D3DXVECTOR3 Look(0.0f, 12.0f, 2.0f);
	D3DXVECTOR3 Up(0.0f, 0.0f, 1.0f);

	D3DXMatrixLookAtLH(&view, &Eye, &Look, &Up);

	float aspect = 500.0f / 500.0f;
	D3DXMatrixPerspectiveFovLH(&proj, D3DXToRadian(45), aspect, 0.3f, 200.0f);
	m_pEffect->SetMatrix("xViewWorldProjection", &(world * view * proj));
	m_pEffect->SetTexture("xColoredTexture", m_pTexture);
	m_pEffect->CommitChanges();
} // End of OnReset function.

void CFX::OnLost()
{
	m_fpsFont.OnLost();
	m_pEffect->OnLostDevice();
} // End of OnLost function.

void CFX::OnDestroy()
{
} // End of OnDestroy function.

void CFX::OnUpdate(ATL::CComPtr<IDirect3DDevice9> pDevice, float fElapsedTime)
{
	UNREFERENCED_PARAMETER(pDevice);
	UNREFERENCED_PARAMETER(fElapsedTime);
} // End of OnUpdate function.

void CFX::OnRender(ATL::CComPtr<IDirect3DDevice9> pDevice, float fElapsedTime)
{
	UNREFERENCED_PARAMETER(fElapsedTime);
	pDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
	pDevice->BeginScene();

	m_effectVertex.Render(pDevice, 10, D3DPT_TRIANGLESTRIP);

#ifdef _UNICODE
	wstringstream hud;
#else
	stringstream hud;
#endif
	hud << TEXT("FPS: ") << int(m_pWindow->GetFPS()) << TEXT("\nRunning Time: ") << int(m_pWindow->GetRunningTime());
	m_fpsFont.DrawStringText(hud.str(), 0, 0, 50, 50, D3DCOLOR_XRGB(255, 0, 0), NULL, DT_LEFT | DT_NOCLIP);

	m_pEffect->SetTechnique("Simplest");
	UINT passes;
	m_pEffect->Begin(&passes, 0);
	for(UINT i = 0; i < passes; ++i)
	{
		m_pEffect->BeginPass(i);
			pDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 10);

			D3DXMATRIXA16 world;
			D3DXMatrixIdentity(&world);

			D3DXMATRIXA16 lampScal;
			D3DXMatrixScaling(&lampScal, 0.05f, 0.05f, 0.05f);
			D3DXMatrixRotationX(&lampScal, (float)D3DX_PI / 2);
			D3DXMatrixTranslation(&lampScal, -4.0f, 5.0f, 1.0f);
			m_pEffect->SetMatrix("xViewWorldProjection", &(lampScal * view * proj));
			m_lampPost.Render(pDevice, m_pEffect, "xColoredTexture");
			m_pEffect->CommitChanges();

			D3DXMatrixScaling(&lampScal, 0.05f, 0.05f, 0.05f);
			D3DXMatrixRotationX(&lampScal, (float)D3DX_PI / 2);
			D3DXMatrixTranslation(&lampScal, -4.0f, 35.0f, 1.0f);
			m_pEffect->SetMatrix("xViewWorldProjection", &(lampScal * view * proj));
			m_lampPost.Render(pDevice, m_pEffect, "xColoredTexture");
			m_pEffect->CommitChanges();

			D3DXMATRIXA16 carScal;
			D3DXMatrixScaling(&carScal, 4.0f, 4.0f, 4.0f);
			D3DXMatrixRotationYawPitchRoll(&carScal, (float)D3DX_PI / 2, (float)D3DX_PI / 2, (float)D3DX_PI / 2);
			D3DXMatrixTranslation(&carScal, 3.0f, 15.0f, 0.0f);
			m_pEffect->SetMatrix("xViewWorldProjection", &(carScal * view * proj));
			m_car.Render(pDevice, m_pEffect, "xColoredTexture");
			m_pEffect->CommitChanges();

			D3DXMatrixScaling(&carScal, 4.0f, 4.0f, 4.0f);
			D3DXMatrixRotationYawPitchRoll(&carScal, (float)D3DX_PI / 2, (float)D3DX_PI / 8, (float)D3DX_PI / 2);
			D3DXMatrixTranslation(&carScal, 28.0f, -1.9f, 0.0f);
			m_pEffect->SetMatrix("xViewWorldProjection", &(carScal * view * proj));
			m_car.Render(pDevice, m_pEffect, "xColoredTexture");
			m_pEffect->CommitChanges();

		m_pEffect->EndPass();
	}
	m_pEffect->End();

	pDevice->EndScene();
	pDevice->Present(NULL, NULL, NULL, NULL);
} // End of OnRender function.

void CFX::OnKeyDown(WPARAM wParam)
{
	switch(wParam)
	{
	case VK_ESCAPE:
		// Exit the application.
		PostQuitMessage(0);
		break;
	case VK_F1:
		if(m_pWindow != NULL)
			m_pWindow->ToggleFullscreen();
		break;
	case VK_F2:
		if(m_pWindow != NULL)
			m_pWindow->ToogleWireframe();
		break;
	}
} // End of OnKeyDown function.

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);
	UNREFERENCED_PARAMETER(nShowCmd);

	CFX *pFX = new CFX;
	CFXWindow *pWindow = new CFXWindow(pFX);

	pFX->SetWindow(pWindow);

	// Initialize any application resources.
	if(!pFX->Initialize())
		return 1;

	if(!pWindow->Initialize(TEXT("Direct3D9 By Kiryn"), hInstance, 500, 500, TRUE))
		return 1;

	// Run the message loop.
	pWindow->Run();

	// Clean up.
	delete pFX;

	return 0;
}


Thank you for the help :)

Share this post


Link to post
Share on other sites
m_pWindow->ToogleWireframe();

Perhaps a typo in your keyboard select?

start turning off a lot of those features until you get the fundamentals working, it would be nice if you had a "worked to this point", "stopped working when I did this".

Share this post


Link to post
Share on other sites

This topic is 3811 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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