Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

How to render a teapot in C++?

$
0
0
Hi. I've been working on this for a while now and have tried almost everything that comes to mind when drawing a simple teapot in C++ Directx9.. Only issue is it appears I'm missing something (most likely minor) but it is preventing my teapot from showing at all. Any help would be appreciated, Maybe it has to do with the identity matrix? I you need to see more of my code, just ask I didn't want to clutter the post with the whole file so I only put the parts I thought mattered.

Constructor & Destructor:
Lab2::Lab2(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP)
: D3DApp(hInstance, winCaption, devType, requestedVP)
{
	if (!checkDeviceCaps())
	{
		MessageBox(0, "checkDeviceCaps() Failed", 0, 0);
		PostQuitMessage(0);
	}

	InitAllVertexDeclarations();

	mGfxStats = new GfxStats();

	mCameraRadius    = 15.0f;
	mCameraRotationY = 1.4f * D3DX_PI;
	mCameraHeight    = 5.0f;

	mLightVecW     = D3DXVECTOR3(-0.577f, 0.577f, -0.577f);

	mDiffuseLight  = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
	mAmbientLight  = D3DXCOLOR(0.6f, 0.6f, 0.6f, 1.0f);
	mSpecularLight = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);

	mWhiteMtrl.ambient       = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
	mWhiteMtrl.diffuse       = D3DXCOLOR(1.0f, 1.0f, 1.0f, 0.83f);
	mWhiteMtrl.specular      = D3DXCOLOR(0.8f, 0.8f, 0.8f, 1.0f);
	mWhiteMtrl.specularPower = 16.0f;

	D3DXMatrixIdentity(&mRoomWorld);
	D3DXMatrixTranslation(&mTeapotWorld, 0.0f, 3.0f, -6.0f);

	HR(D3DXCreateTextureFromFile(gd3dDevice, "checkboard.dds", &mFloorTex));
	HR(D3DXCreateTextureFromFile(gd3dDevice, "brick1.dds", &mTeapotTex));
	HR(D3DXCreateTextureFromFile(gd3dDevice, "brick2.dds", &mWallTex));
	HR(D3DXCreateTextureFromFile(gd3dDevice, "ice.dds", &mMirrorTex));
	
	D3DXCreateTeapot(gd3dDevice, &teapotMesh, 0);

	genSphericalTexCoords(teapotMesh);

	DWORD numVertices = teapotMesh->GetNumVertices();
	DWORD numIndices  = teapotMesh->GetNumFaces();

	mGfxStats->addVertices(numVertices);
	mGfxStats->addTriangles(numIndices);

	D3DXMatrixTranslation(&mTeapotWorld, 0.0f, 3.0f, -6.0f);

	// Room geometry count.
	mGfxStats->addVertices(24);
	mGfxStats->addTriangles(8);

	buildRoomGeometry();

	buildFX();

	onresetDevice();
}

Lab2::~Lab2()
{
	delete mGfxStats;
	ReleaseCOM(mRoomVB);
	ReleaseCOM(mFloorTex);
	ReleaseCOM(mWallTex);
	ReleaseCOM(mMirrorTex);
	ReleaseCOM(mTeapotTex);
	ReleaseCOM(teapotMesh);
	ReleaseCOM(mFX);

	DestroyAllVertexDeclarations();
}



Draw Teapot Function:
void Lab2::drawTeapot()
{
	D3DXMATRIX worldViewProj;
	worldViewProj = mTeapotWorld * mView * mProj;
	HR(mFX->SetMatrix(mhWVP, &worldViewProj));

	HR(mFX->SetTexture(mhTex, mTeapotTex));

	// Cylindrically interpolate texture coordinates.
	HR(gd3dDevice->SetRenderState(D3DRS_WRAP0, D3DWRAPCOORD_0));

	D3DXMATRIX worldInvTrans;
	D3DXMatrixInverse(&worldInvTrans, 0, &mTeapotWorld);
	D3DXMatrixTranspose(&worldInvTrans, &worldInvTrans);
	
	HR(mFX->SetMatrix(mhWorldInvTrans, &worldInvTrans));
	HR(mFX->SetMatrix(mhWorld, &mTeapotWorld));

	HR(gd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE));
	HR(gd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA));
	HR(gd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA));

	//mhWorld = mTeapotWorld;

	HR(gd3dDevice->SetVertexDeclaration(VertexPNT::Decl));
	HR(gd3dDevice->SetStreamSource(0, mRoomVB, 0, sizeof(VertexPNT)));

	DWORD numVertices = teapotMesh->GetNumVertices();
	DWORD numIndices  = teapotMesh->GetNumFaces();

	// Begin passes.
	UINT numPasses = 0;
	HR(mFX->Begin(&numPasses, 0));
		
		HR(gd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, numVertices, numIndices));
		teapotMesh->DrawSubset(0);
	HR(mFX->End());

	// Disable wrap.
	HR(gd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE));
	HR(gd3dDevice->SetRenderState(D3DRS_WRAP0, 0));
}

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>