Skip to main content
gamedev.net
🔒 Locked

Problem rendering a 3D cube...

Started by Stev Studio 10 replies 1.3k views
Original Post
Stev Studio
Stev Studio
Hi, I'm new to direct3D. I'm currently trying to render a 3D cube, but it doesn't seem to work correctly. I'm trying to render it using a vertex buffer only... without any index. My problem is that I only see 1 triangle, instead of the full cube. My vertices are:

	VertexData vData[] = 
	{ 
		{ -1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 255, 0) }, //face 1 
		{ 1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 255, 0) }, 
		{ -1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 255, 0) }, 
		{ -1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 255, 0) }, 
		{ 1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 255, 0) }, 
		{ 1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 255, 0) }, 

		{ -1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(255, 255, 0, 0) }, // face 2 
		{ 1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(255, 255, 0, 0) }, 
		{ -1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 255, 0, 0) }, 
		{ -1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 255, 0, 0) }, 
		{ 1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(255, 255, 0, 0) }, 
		{ 1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 255, 0, 0) }, 

		{ -1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(255, 0, 0, 255) }, // face 3 
		{ 1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(255, 0, 0, 255) }, 
		{ -1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 0, 255) }, 
		{ -1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 0, 255) }, 
		{ 1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(255, 0, 0, 255) }, 
		{ 1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 0, 255) }, 

		{ -1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 128, 0, 0) }, // face 4 
		{ 1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 128, 0, 0) }, 
		{ -1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(255, 128, 0, 0) }, 
		{ -1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(255, 128, 0, 0) }, 
		{ 1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 128, 0, 0) }, 
		{ 1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(255, 128, 0, 0) }, 

		{ -1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(255, 0, 128, 128) }, // face 5 
		{ -1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 0, 128, 128) }, 
		{ -1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 128, 128) }, 
		{ -1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 128, 128) }, 
		{ -1.0f, 1.0f,-1.0f, D3DCOLOR_ARGB(255, 0, 128, 128) }, 
		{ -1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 128, 128) }, 

		{ 1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(255, 128, 128, 128) }, // face 6 
		{ 1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 128, 128, 128) }, 
		{ 1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 128, 128, 128) }, 
		{ 1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 128, 128, 128) }, 
		{ 1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 128, 128, 128) }, 
		{ 1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(255, 128, 128, 128) }, 
	}; 

VertexData is:

struct VertexData
{
	Vector3D vPos;
	//float fNormal;
	DWORD color;
	void Set(float _uiX, float _uiY, float _uiZ, DWORD _uColor)
	{
		vPos.fX = _uiX;
		vPos.fY = _uiY;
		vPos.fZ = _uiZ;
		color = _uColor;
	};
};

and Vector3D:

struct Vector3D
{
	float fX;
	float fY;
	float fZ;
	void Set(float _fX, float _fY, float _fZ)
	{
		fX = _fX;
		fY = _fY;
		fZ = _fZ;
	};

	void operator=(Vector3D* _vData)
	{
		fX = _vData->fX;
		fY = _vData->fY;
		fZ = _vData->fZ;
	};
};

[Edited by - Stev Studio on August 20, 2006 10:21:55 PM]
Stev Studio
Stev Studio
I also enable the ZBuffer, just before rendering, using this call:
mpDevice->SetRenderState(D3DRS_ZENABLE, true);

Then I set the culling to none, to see all the triangles:
mpDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); 

Then, I render everything using this:

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)mpDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(255,255,255), 1.0f, 0);		if (SUCCEEDED(mpDevice->BeginScene()))		{			mpDevice->SetStreamSource(0, mpVertexBuffer, 0, sizeof(VertexData));			mpDevice->SetFVF( D3DFVF_CUSTOMVERTEX );			mpDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 12);//a cube has 12 faces						mpDevice->EndScene();		}		mpDevice->Present(NULL, NULL, NULL, NULL);


[Edited by - Stev Studio on August 20, 2006 10:03:08 PM]
Stev Studio
Stev Studio
Does anyone sees a problem here? DirectX doesn't give me any error while debugging with direct3D in debug mode.

Thanks all for your suggestions,
Stev
Mushu
Mushu
Could we also see the code for the creation and filling of your vertex buffers?

And, for future reference, please use the edit button to modify posts instead of posting new replies. Additionally, you should surround large code blocks (>5 lines) with [source][/source] tags, and small ones (<5 lines) with [code][/code] tags.

Oh, and 12 isn't the right number to pass. DrawPrimitive expects the number of vertices, not the number of primitives, so you should be passing 36.
Stev Studio
Stev Studio
bool Dx9Buffers::AddVertexData(VertexData* _vData, uint32 _uiDataSize, uint32 _uiVertexOffset){	if (mpVertexBuffer == 0)		return false;	void* pData;	if (FAILED(mpVertexBuffer->Lock(_uiVertexOffset, _uiDataSize, (void**) &pData, muiLockingFlag)))		return false;	memcpy(pData, _vData, _uiDataSize);	mpVertexBuffer->Unlock();	return true;}



This is my function to load data into my vertex buffer.

VertexData vData[] = 	{ 		{ -1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 255, 0) }, //face 1 		{ 1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 255, 0) }, 		{ -1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 255, 0) }, 		{ -1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 255, 0) }, 		{ 1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 255, 0) }, 		{ 1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 255, 0) }, 		{ -1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(255, 255, 0, 0) }, // face 2 		{ 1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(255, 255, 0, 0) }, 		{ -1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 255, 0, 0) }, 		{ -1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 255, 0, 0) }, 		{ 1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(255, 255, 0, 0) }, 		{ 1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 255, 0, 0) }, 		{ -1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(255, 0, 0, 255) }, // face 3 		{ 1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(255, 0, 0, 255) }, 		{ -1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 0, 255) }, 		{ -1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 0, 255) }, 		{ 1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(255, 0, 0, 255) }, 		{ 1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 0, 255) }, 		{ -1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 128, 0, 0) }, // face 4 		{ 1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 128, 0, 0) }, 		{ -1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(255, 128, 0, 0) }, 		{ -1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(255, 128, 0, 0) }, 		{ 1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 128, 0, 0) }, 		{ 1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(255, 128, 0, 0) }, 		{ -1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(255, 0, 128, 128) }, // face 5 		{ -1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 0, 128, 128) }, 		{ -1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 128, 128) }, 		{ -1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 128, 128) }, 		{ -1.0f, 1.0f,-1.0f, D3DCOLOR_ARGB(255, 0, 128, 128) }, 		{ -1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 128, 128) }, 		{ 1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(255, 128, 128, 128) }, // face 6 		{ 1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 128, 128, 128) }, 		{ 1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 128, 128, 128) }, 		{ 1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 128, 128, 128) }, 		{ 1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 128, 128, 128) }, 		{ 1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(255, 128, 128, 128) }, 	}; Dx9Buffers::AddVertexData(vpData, sizeof(vpData), 0);


This is how I call AddVerData function.

Thanks a lot for your help.
Stev

[Edited by - Stev Studio on August 20, 2006 11:18:18 PM]
Stev Studio
Stev Studio
Quote:
Original post by Mushu

Oh, and 12 isn't the right number to pass. DrawPrimitive expects the number of vertices, not the number of primitives, so you should be passing 36.


I tried to write 36, and now my application crashes. On the other hand, in the directx documentation, I read that I had to write the amount of faces there, not the amount of vertices... Any ideas what could be wrong?

Unhandled exception at 0x77f65554 in StevStudio.exe: User breakpoint.

Thnaks,
Stev

[Edited by - Stev Studio on August 20, 2006 10:19:29 PM]
UriKiller
UriKiller
Ok, Mushu - You're wrong: DrawPrimitive takes the number of primitives, not the number of vertices. speciefying 36 will tell Direct3D to look for 36 triangles in the memory which will obviously cause the application to crash.

Did you check that all the coordinates of the points are correct?
Whats the FVF you use?
about this line:
Dx9Buffers::AddVertexData(vpData, sizeof(vpData), 0);
The long array you declare is called vData, so whats vpData?
Stev Studio
Stev Studio
Quote:
Original post by UriKiller
Ok, Mushu - You're wrong: DrawPrimitive takes the number of primitives, not the number of vertices. speciefying 36 will tell Direct3D to look for 36 triangles in the memory which will obviously cause the application to crash.

Did you check that all the coordinates of the points are correct?
Whats the FVF you use?
about this line:
Dx9Buffers::AddVertexData(vpData, sizeof(vpData), 0);
The long array you declare is called vData, so whats vpData?



Yes, the coordinates are correct... I checked and re-checked them all on paper.

I use this FVF: D3DFVF_CUSTOMVERTEX

The vertex array and the call to the AddVertexData are made in 2 different methods.

uint32 AppManager::LoadNativeObject(){	VertexData vData[] = 	{ 		{ -1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 255, 0) }, //face 1 		{ 1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 255, 0) }, 		{ -1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 255, 0) }, 		{ -1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 255, 0) }, 		{ 1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 255, 0) }, 		{ 1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 255, 0) }, 		{ -1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(255, 255, 0, 0) }, // face 2 		{ 1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(255, 255, 0, 0) }, 		{ -1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 255, 0, 0) }, 		{ -1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 255, 0, 0) }, 		{ 1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(255, 255, 0, 0) }, 		{ 1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 255, 0, 0) }, 		{ -1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(255, 0, 0, 255) }, // face 3 		{ 1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(255, 0, 0, 255) }, 		{ -1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 0, 255) }, 		{ -1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 0, 255) }, 		{ 1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(255, 0, 0, 255) }, 		{ 1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 0, 255) }, 		{ -1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 128, 0, 0) }, // face 4 		{ 1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 128, 0, 0) }, 		{ -1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(255, 128, 0, 0) }, 		{ -1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(255, 128, 0, 0) }, 		{ 1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 128, 0, 0) }, 		{ 1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(255, 128, 0, 0) }, 		{ -1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(255, 0, 128, 128) }, // face 5 		{ -1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 0, 128, 128) }, 		{ -1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 128, 128) }, 		{ -1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 128, 128) }, 		{ -1.0f, 1.0f,-1.0f, D3DCOLOR_ARGB(255, 0, 128, 128) }, 		{ -1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(255, 0, 128, 128) }, 		{ 1.0f, -1.0f, -1.0f, D3DCOLOR_ARGB(255, 128, 128, 128) }, // face 6 		{ 1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 128, 128, 128) }, 		{ 1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 128, 128, 128) }, 		{ 1.0f, -1.0f, 1.0f, D3DCOLOR_ARGB(255, 128, 128, 128) }, 		{ 1.0f, 1.0f, -1.0f, D3DCOLOR_ARGB(255, 128, 128, 128) }, 		{ 1.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(255, 128, 128, 128) }, 	}; 	Geometry* geo = new Geometry(vData, sizeof(vData));	return GeometryManager::AddGeometry(geo);}bool AppManager::LoadObjectsDataToVideoCard(){	//we create the vertex data for all the geometries:	if (Dx9Buffers::CreateVertexBuffer(GeometryManager::GetTotalVertexSize()))	{		uint32 uiOffSet = 0;		for (uint32 i = 0; i < GeometryManager::GetNbrGeoLoaded(); i++)		{			VertexData* vpData;			vpData = GeometryManager::GetVertexData(i);			uint32 uiVPSize;			uiVPSize = GeometryManager::GetVertexSize(i);			if ((vpData != NULL) && (uiVPSize != NULL))			{				Dx9Buffers::AddVertexData(vpData, uiVPSize, uiOffSet);				uiOffSet += uiVPSize;			}		}		return true;	}	return false;}void AppManager::Render(){	//we render all the geo: - must render objects later...	for (uint32 i = 0; i < GeometryManager::GetNbrGeoLoaded(); i++)		GeometryManager::Render(i);	//we render all the active cameras, and for each, we render the world:	Dx9RenderTransform::SetDevice(Dx9Buffers::GetDevice());	for (uint32 i = 0; i < CameraManager::GetNbrCamera(); i++)	{		Camera* cam;		cam = CameraManager::FindCamera(i);		if (cam != NULL)		{			if (cam->IsActive())			{				//we render the camera:				cam->Render();				//then, we render what we have in our buffers:				if (Dx9RenderTransform::EnableZBuffer())				{					//we set the right view matrix:					//Dx9RenderTransform::SetViewMatrix(cam->GetViewMatrix());					Dx9Buffers::Render(); //we must specify what is the 2D RECT of the camera in that method				}			}		}	}	Dx9Buffers::Render();}


Thanks all for your help,
Stev
UriKiller
UriKiller
D3DFVF_CUSTOMVERTEX is not a vertex format, its a define you made. What is the combinations of D3DFVF flags you combine for defining D3DFVF_CUSTOMVERTEX?
Stev Studio
Stev Studio
Quote:
Original post by UriKiller
D3DFVF_CUSTOMVERTEX is not a vertex format, its a define you made. What is the combinations of D3DFVF flags you combine for defining D3DFVF_CUSTOMVERTEX?


(D3DFVF_XYZ|D3DFVF_DIFFUSE)

Thanks a lot for your help,
Stev
UriKiller
UriKiller
Ok I don't see any problem in your code but maybe you do the projection, world, view matrices wrong. It would be alot easier if you pasted all the code, well at least the essential part of the rendering.
Stev Studio
Stev Studio
Quote:
Original post by UriKiller
Ok I don't see any problem in your code but maybe you do the projection, world, view matrices wrong. It would be alot easier if you pasted all the code, well at least the essential part of the rendering.



After you said that, I checked my transformation matrices, and saw that I wasn't doiing any peojection matrix transformation.

I just did it, and my cube is now rendering correctly.

Thanks a lot for your help, it is very appreciated!
Stev

Topic Locked

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

Sign in to reply to this topic.