Archived

This topic is now archived and is closed to further replies.

gdClash

Vertex Buffer Issues and Good Books on Dx8

Recommended Posts

Today is my third day getting down and dirty with Direct Graphics, which I think is an odd name since in the SDK demos they use Direct Draw 7 and Direct3D8. Anywho, I''m trying to figure out how I can render a scene using an object oriented approach, like I did when I would blit stuff in DDraw. So I wrote a class that has a draw() function, and I am calling it after I invoke BeginScene(). But my problem is that I''m getting an error from the VC++ debugger saying that "Streams do not have required number of vertices... DrawPrimitive failed." So I figured I would play around with the number of primitives I specified in DrawPrimitive() but I''ve been unable to get anything to work! So I''ve included the code for my class below, if anyone knows whats wrong with it please let me know! Also, I''ve been searching around for about a month or so looking for good tutorials and books on Direct Graphics. I found nexe, with the help of gamedev but thats about it. So if anyone knows of any good books or sites on Direct X 8 or Direct Graphics share them! BTW, the Draw function is where the problem is I think. Here''s my code:
  
// The Flexible Vertex Format Constant

// We are specifying XYZ coords and a diffuse color

const DWORD FVFCPoint = D3DFVF_XYZ | D3DFVF_DIFFUSE;
const DWORD FVFCProcessedPoint = D3DFVF_XYZRHW | D3DFVF_DIFFUSE;

// Holds the coords and color of a vertex

class CPoint {
	public:
		float x, // X coord

			  y, // Y coord

			  z; // Z coord

		DWORD dwColor; // Color


		// A helper function for filling in class members

		void Init(float fX, float fY, float fZ, float fR, float fG, float fB)
		{
			// Fill in our coords

			x = fX;
			y = fY;
			z = fZ;
			
			// Convert the arguments from % of 255 to an actual value

			// Then get D3D to convert it into a color value

			dwColor = D3DCOLOR_XRGB(BYTE(fR * 255), BYTE(fG * 255), BYTE(fB * 255));
		}
};

// Holds the coords of a processed point

class CProcessedPoint {
	public:
		float x, // X coord

			  y, // Y coord

			  z, // Z coord

			  RHW;
		
		DWORD dwColor; // Color

};

class C3DRect {
	public:	
		C3DRect(IDirect3DDevice8** D3DDevice) 
		{ 
			// get our D3D Device

			myD3DDevice = *D3DDevice;

			// Create our vertex buffer

			myD3DDevice->CreateVertexBuffer(sizeof(CPoint) * 8, NULL, FVFCPoint, D3DPOOL_MANAGED, &myBuffer);
			
			// Create our processed vertex buffer

			myD3DDevice->CreateVertexBuffer(sizeof(CProcessedPoint) * 8, NULL, FVFCProcessedPoint, D3DPOOL_MANAGED, &myProcessedBuffer);

			// Set our initialization value to false

			init = false; 
		}

		// Destructor

		~C3DRect() 
		{ 
			SafeRelease(myBuffer);
			SafeRelease(myProcessedBuffer);
		}

		//------------------------------------------

		//Name: SetPoints

		//Desc: Sets the internal variables of the C3DRect class

		//------------------------------------------

		void SetPoints(float nx, float ny, float nz, float nheight, float nwidth, float ndepth)
		{
			x = nx;
			y = ny;
			z = nz;
			height = nheight;
			width = nwidth;
			depth = ndepth;
			
			myBuffer->Lock(0, 0, (BYTE**) &myVertices, NULL);
			
			myVertices[0].Init(x, y, z, .80, .80, .80);
			myVertices[1].Init(x, y + height, z, .80, .80, .80);
			myVertices[2].Init(x, y, z + depth, .80, .80, .80);
			myVertices[3].Init(x, y + height, z + depth, .80, .80, .80);
			myVertices[4].Init(x + width, y , z + depth, .80, .80, .80);
			myVertices[5].Init(x + width, y + height, z + depth, .80, .80, .80);
			myVertices[6].Init(x + width, y, z , .80, .80, .80);
			myVertices[7].Init(x + width, y + height, z , .80, .80, .80);
			
			myBuffer->Unlock();
			
			init = true;
		}
		
		// Change the internal D3D Device

		void setD3DDevice(IDirect3DDevice8* D3DDevice)
		{	
			myD3DDevice = D3DDevice; 
		}

		// Draw me!

		void Draw()
		{
			if(!init)
				return;

			// Tell it which buffer we are using

			myD3DDevice->SetStreamSource(0, myBuffer, sizeof(CPoint));
			
			// Give it out FVF

			myD3DDevice->SetVertexShader(FVFCPoint);

			// Do some D3D Voodoo (ehh????)

			myD3DDevice->ProcessVertices(0, 0, 8, myProcessedBuffer, NULL);

			//myD3DDevice->SetTexture() NOT YET! hehe


			// Set the new buffer we are working with

			myD3DDevice->SetStreamSource(0, myProcessedBuffer, sizeof(CProcessedPoint));

			// Give it out new FVF

			myD3DDevice->SetVertexShader(FVFCProcessedPoint);

			// Render the rectangle

			myD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 8);
		}

	private:
		float x, 
			  y, 
			  z, 
			  height,   // object properties

			  width, 
			  depth;
		bool init;
		IDirect3DDevice8 *myD3DDevice; //inter d3d device

		IDirect3DVertexBuffer8 *myBuffer; //our vertex buffer

		IDirect3DVertexBuffer8 *myProcessedBuffer; //our vertex buffer

		CPoint *myVertices; //our vertex info

};
  
- Clash - www.zeroinfinity.net - A New World, Built By New Minds

Share this post


Link to post
Share on other sites
Well, first of all, the way you have it setup you should be rendering as a triangle strip, not a triangle list. To do that change the primitive type to D3DPT_TRIANGLESTRIP. Next, in DrawPrimitive it wants the number of primitives, not the number of vertices.

Share this post


Link to post
Share on other sites
Ok I changed it to a trianglestrip (I had that at one point but it still wasn''t working) and it still doesn''t render it. Perhaps I''m not assigning the verticies properly. Im trying to render a cube, any insight into how I can change my vertices so It Works?

- Clash
- www.zeroinfinity.net
- A New World, Built By New Minds

Share this post


Link to post
Share on other sites
Well, for a cube, you''ll need 6 sides, so that''s 18 vertices if you use a triangle list.

As to the other sites part of your post, I''d strongly advise taking a visit to drunkenhyena.com, there are some great DXGraphics tutorials there.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi,

1) the primitive count in the DP function is the number of primitives depending on the primitive type, e.g. you want to render 3 triangles using D3DPT_TRIANGLELIST means you''ll have to provide 9 points (3 points per triangle).

2) you don''t need to call via ProcessVertices() if you just want to draw something. ProcessVertices() does not use the GPU so you''ll loose performance.

BTW, IDirect3D8 is a part of DirectX Graphics and the D3D samples do not use IDirectDraw7 interface.

Hope this helps,
Bjørn.

Share this post


Link to post
Share on other sites
Thanks for the tips, I''ll try them out and see how things go. And regarding the Demos in the SDK, the use two wrapper classes that are part of DDUtil.h called CDisplay and CSurface. I borrowed them for a demo I wrote. They make using DD7 "so" much easier, oh and I''m going to check out that site. Any more sites or books that anyone knows of that are good, post post post!

- Clash
- www.zeroinfinity.net
- A New World, Built By New Minds

Share this post


Link to post
Share on other sites
Bjorn (AP) -> ProcessVertices uses the GPU just as much as DrawPrimitive does for just that section. All ProcessVertices does is the transformation and lighting step, if there''s hardware T&L it''ll use it.

Share this post


Link to post
Share on other sites
I'm also on the search for DirectX 8 books. I've gone through all the tutorials but I still want a book. I just like having a open book for reference on my side. Maybe I need a second screen so I can flip through the tutorials while I code. Anyway, as far as I know there are no DX8 books out there. The only thing I found is "Introducting to Computer Game Programming with DirectX8". It is the same book as "Introductin toComputer Game Programming with DirectX7" but with a nice and shiny CD with the DX8 SDK on it.Whooho. So don't buy it :-)



Humanity's first sin was faith; the first virtue was doubt

Edited by - SkyRat on June 20, 2001 12:51:08 AM

Share this post


Link to post
Share on other sites
There is one other one called "Beginning Direct3D Game Programming". It looks pretty good, and I am planning on getting it.

Share this post


Link to post
Share on other sites
Moe do not buy that book! If you''re thinking of the green\black book by Prima, for D3D 8 do not get it. In the latest issue of GD-Mag they did a review on the book. Basically they said there is practically 1 error per page and that it''s horrible. It uses some weird framework (not the standard D3D D3DX stuff). I did pick it up at the book store and I was going to buy it, but after reading that review from a trusted magazine I''ve changed my mind. Anywho, I highly recommend going to that drunken hyena site, that was posted earlier in this thread. With the help of that site, and NeXe in addition to my own hair-wrenching I was succesfully able to make my first D3D8 demo thingy. If you want to check it out goto www.zeroinfinity.net and click on "BigList." It''s the first thing on there, with a little explanation of what it is and how it works (and howto get it running). Its about .7MB so make sure you have the time or the bandwidth.

Anyway, thanks for everyones help! But this problem of mine is solved Thanks once again, btw I think we should open another thread about good Dx8 books... cause we need some! Heheh

- Clash
- www.zeroinfinity.net
- A New World, Built By New Minds

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Just to proof my statement about ProcessVertices: searching the DXDev mailing list archives [1] yields this:

"Remember - ProcessVertices is _always always always_ done of the CPU, even if hardware T&L is available." [2]

Bjørn.

[1] http://discuss.microsoft.com/archives/DIRECTXDEV.html
[2] http://discuss.microsoft.com/SCRIPTS/WA-MSD.EXE?A2=ind0001D&L=DIRECTXDEV&P=R3014

Share this post


Link to post
Share on other sites
Well a good book coming out is "The Zen of Direct3D Game Programming. Check out Amazon.com and search for it. I think you can pre-order it. I''ll just wait until it comes in stores.
Hope this helps!

Share this post


Link to post
Share on other sites