Jump to content
  • Advertisement
Sign in to follow this  
Sync Views

image not drawing

This topic is 3741 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

Ok this is the third day Ive looked at the same bit on non working code, and I really don't see the problem, and Direct3d isn't reporting any fail errors either :( I really have no idea where to start debugging code that seems to be correct when there no errors to at least give me a start...
	bool LibCore::D3dCreate()
	{
		if(!Wnd)
		{
			return false;
		}
		if(D3d)
		{
			return false;
		}

		D3d = Direct3DCreate9(D3D_SDK_VERSION);
		if(!D3d)
		{
			return false;
		}

	    ZeroMemory(&D3dCaps, sizeof(D3dCaps));
		if(FAILED(D3d->GetDeviceCaps
			(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &D3dCaps)))
		{
			D3d->Release();
			D3d = 0;
			return false;
		}

		//Setup present parameters
		D3DPRESENT_PARAMETERS D3dParams;
		ZeroMemory(&D3dParams, sizeof(D3dParams));
		D3dParams.SwapEffect      = D3DSWAPEFFECT_DISCARD;
		D3dParams.hDeviceWindow   = Wnd;
		D3dParams.BackBufferCount = 1;
		D3dParams.EnableAutoDepthStencil = true;
		D3dParams.AutoDepthStencilFormat = D3DFMT_D16;


		D3DDISPLAYMODE d3ddm;
		RECT rWindow;
		if(!GetClientRect (Wnd, &rWindow))
		{
			D3d->Release();
			D3d = 0;
			return false;
		}

		//Get display mode
		D3d->GetAdapterDisplayMode (D3DADAPTER_DEFAULT, &d3ddm);

		//Setup backbuffer
		D3dParams.Windowed = true;
		D3dParams.BackBufferFormat = D3DFMT_A8R8G8B8;
		D3dParams.BackBufferWidth  = rWindow.right  - rWindow.left;
		D3dParams.BackBufferHeight = rWindow.bottom - rWindow.top;

		//Check if hardware vertex processing is available
		if (D3dCaps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
		{    
			//Create device with hardware vertex processing
			if(FAILED(D3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL, Wnd,
				D3DCREATE_HARDWARE_VERTEXPROCESSING, &D3dParams, &D3dDevice)))  
				{
					D3d->Release();
					D3d = 0;
					return false;
				}
		}
		else
		{
			//Create device with software vertex processing
			if(FAILED(D3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL, Wnd,
				D3DCREATE_SOFTWARE_VERTEXPROCESSING, &D3dParams, &D3dDevice)))
				{
					D3d->Release();
					D3d = 0;
					return false;
				}
		}
		D3DXMATRIX Projection;
		//set default projections
		D3DXMatrixOrthoLH(&Projection,
			(float)D3dParams.BackBufferHeight, (float)D3dParams.BackBufferHeight,
			0, 1000000);
		D3dDevice->SetTransform(D3DTS_PROJECTION, &Projection);

		//Set vertex shader <-- not using any by default
		D3dDevice->SetVertexShader(NULL);
		D3dDevice->SetFVF(dx::VertexBasic::FVF);

		//Create vertex buffer and set as stream source
		D3dDevice->CreateVertexBuffer(sizeof(dx::VertexBasic) * 4,
			NULL, dx::VertexBasic::FVF, D3DPOOL_MANAGED, &VertexBufferBase, NULL);
		dx::VertexBasic* vb = NULL;
		VertexBufferBase->Lock(NULL,NULL, (void**)&vb, NULL);

		//quad
		vb[0] = dx::VertexBasic(-0.5,-0.5,0, 0,0, 0xFFFFFFFF);
		vb[1] = dx::VertexBasic( 0.5,-0.5,0, 1,0, 0xFFFFFFFF);
		vb[2] = dx::VertexBasic( 0.5, 0.5,0, 1,1, 0xFFFFFFFF);
		vb[3] = dx::VertexBasic(-0.5, 0.5,0, 0,1, 0xFFFFFFFF);

		VertexBufferBase->Unlock();

		D3dDevice->SetStreamSource (0, VertexBufferBase, 0, sizeof(dx::VertexBasic));

		//Setup rendering states
		D3dDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
		D3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
		D3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
		D3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
		D3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
		D3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
		D3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);

		//get pointer to render target
		D3dDevice->GetRenderTarget(0, &BackBuffer);

		//Successfully initalized Direct3D
		return true;
	}

	LibAdvObject::LibAdvObject(Core* _core, Texture* _tex, unsigned _icount, unsigned _vcount)
	: ib(0), vb(0), TriCount(_icount), VerCount(_vcount)
	{
		Owner = _core;
		Tex = _tex;
	}
	void LibAdvObject::Release()
	{
		SafeRelease(&vb);
		SafeRelease(&ib);
		SafeRelease(&Tex);
		delete this;
	}
	bool LibAdvObject::Render(const std::vector<fw::math::Matrix<4,4>> &Transforms)
	{
		//This will eventualy use a cutom vertex shader to link vertices to additional matrixces.
		//only changes them if there no already set
		((LibCore*)Owner)->SetTex(((LibTexture*)Tex)->Tex);
		((LibCore*)Owner)->SetVb (vb, sizeof(AdvObjectVertex));
		((LibCore*)Owner)->SetIb (ib);

		((LibCore*)Owner)->D3dDevice->SetTransform(D3DTS_WORLD, &D3DXMATRIX((float*)&Transforms[0].m));
		((LibCore*)Owner)->D3dDevice->DrawIndexedPrimitive(
			D3DPT_TRIANGLELIST, 0, 0, VerCount, 0, TriCount);
		//4 vertices, 6 indexes
		//VerCount = 4, TriCount = 3
		return true;
	}
	DLL AdvObject* AdvObjectCreate(Core* Owner, Texture* Tex,
		const std::vector<AdvObjectVertex> &Vertices,
		const std::vector<unsigned short>  &Indexes)
	{
		LibAdvObject *Obj = new LibAdvObject(Owner,Tex, Indexes.size()/3, Vertices.size());
		//VERTEX
		{
			((LibCore*)Obj->Owner)->D3dDevice->CreateVertexBuffer(
				Vertices.size()*sizeof(AdvObjectVertex), D3DUSAGE_WRITEONLY,
				AdvObjectVertex::FVF, D3DPOOL_MANAGED, &Obj->vb, NULL);
			
			AdvObjectVertex* v=NULL;
			Obj->vb->Lock(0, 0, (void**)&v, NULL);
			std::vector<AdvObjectVertex>::const_iterator it = Vertices.begin();
			while(it != Vertices.end())*v++ = *it++;
			Obj->vb->Unlock();
		}

		//INDEX
		{
			((LibCore*)Obj->Owner)->D3dDevice->CreateIndexBuffer(
				Indexes.size(), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16,
				D3DPOOL_MANAGED, &Obj->ib, NULL);
			 
			unsigned short* i=NULL;//short == 16 bit == format
			Obj->ib->Lock(0, 0, (void**)&i, NULL);
			std::vector<unsigned short>::const_iterator it = Indexes.begin();
			while(it != Indexes.end())*i++ = *it++;
			Obj->ib->Unlock();
		}
		return Obj;
	}

After d3d int
		dx::Texture* Tex  = dx::TextureCreateFromFile(This, "circle.png");
		//0-----1
		//|   / |
		//| /   |
		//3-----2
		std::vector<dx::AdvObjectVertex> v;
		v.push_back(dx::AdvObjectVertex(-50,-50,0, 0,0, 0xFF0000FF, 0));
		v.push_back(dx::AdvObjectVertex( 50,-50,0, 1,0, 0xFF0000FF, 0));
		v.push_back(dx::AdvObjectVertex( 50, 50,0, 1,1, 0xFF0000FF, 0));
		v.push_back(dx::AdvObjectVertex(-50, 50,0, 0,1, 0xFF0000FF, 0));

		std::vector<unsigned short> i;
		i.push_back(0);
		i.push_back(1);
		i.push_back(3);
		i.push_back(1);
		i.push_back(2);
		i.push_back(3);

		Circle = dx::AdvObjectCreate(This, Tex, v,i);

Render
D3dDevice->Clear(0,NULL,D3DCLEAR_TARGET,0xFF000000, 0, 0);
D3dDevice->BeginScene();

math::Matrix<4,4> Mat;
math::MatrixCreateTranslation(400,300,0, &Mat);
std::vector<math::Matrix<4,4>> Transforms;
Transforms.push_back(Mat);
Circle->Render(Transforms);

D3dDevice->EndScene();
D3dDevice->Present(NULL,NULL,NULL,NULL);

Share this post


Link to post
Share on other sites
Advertisement
A few things I noticed:

1) You're not specifying your vertices correctly (assuming you want the order to match the ascii drawing). It should be:

v.push_back(dx::AdvObjectVertex(-50, 50,0, 0,0, 0xFF0000FF, 0));
v.push_back(dx::AdvObjectVertex( 50, 50,0, 1,0, 0xFF0000FF, 0));
v.push_back(dx::AdvObjectVertex( 50,-50,0, 1,1, 0xFF0000FF, 0));
v.push_back(dx::AdvObjectVertex(-50,-50,0, 0,1, 0xFF0000FF, 0));


2) Specifying a very distant far clip plane is a bad idea and can cause artifacts. Better set it to something like 1000. Also, set the near clip plane to 1.

3) Make sure you are setting up the view matrix properly (i.e., make sure the camera is looking at the object).

4) Are you using the debug runtimes?

Share this post


Link to post
Share on other sites
Quote:
Original post by Gage64
A few things I noticed:

1) You're not specifying your vertices correctly (assuming you want the order to match the ascii drawing). It should be:

v.push_back(dx::AdvObjectVertex(-50, 50,0, 0,0, 0xFF0000FF, 0));
v.push_back(dx::AdvObjectVertex( 50, 50,0, 1,0, 0xFF0000FF, 0));
v.push_back(dx::AdvObjectVertex( 50,-50,0, 1,1, 0xFF0000FF, 0));
v.push_back(dx::AdvObjectVertex(-50,-50,0, 0,1, 0xFF0000FF, 0));


isn't the screen like this thouh?

- y
|
- --0-- + x
|
+

Original post by Gage64
3) Make sure you are setting up the view matrix properly (i.e., make sure the camera is looking at the object).
...ok what should i set the view transform as for a starting point?
Quote:
Original post by Gage64
4) Are you using the debug runtimes?

yes, with max debug output

Share this post


Link to post
Share on other sites
Quote:
Original post by Sync Views

- y
|
- --0-- + x
|
+

...ok what should i set the view transform as for a starting point?


Nope, +y is upwards, +x is right, and +z is "into" the screen.

For those vertices, I'd put the camera at (0, 0, -200) or so, looking at (0, 0, 0):

D3DXMATRIX matView;
D3DXVECTOR3 vEye(0, 0, -200.0f);
D3DXVECTOR3 vAt(0, 0, 0);
D3DXVECTOR3 vUp(0, 1.0f, 0);
D3DXMatrixLookAtLH(&matView, &vEye, &vAt, &vUp);
pDevice->SetTransform(D3DTS_VIEW, &matView);


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!