#### Archived

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

# losing primatives in Direct3D

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

## Recommended Posts

Help, please! I''ve implemented a trainglestrip heightfield textured with a jpg of grass (I call it my "lawn"), and every render I get missing primatives- single triangles and/or lines of them. I''m stumped! Some things to know: It''s a heavily modified version of D3D Tutorial 5: textures, with vertexbuffer verticies defining a heightfield, as opposed to a cylinder. I have varied the following, with no change in the error: 1)Vertexbuffer as both a trianglelist and a trianglestrip 2)base images for my texture 3)culling modes 4)dimensions of heightfield The non-rendered primatives depend on my set camera position- I can''t seem to detect any "overlap" in the verticies. Anyone have similar problems? -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- "Hey, I was''t joking... You really /could/ be "boinked" by some irate looney wielding a rubber malet." -Baldor the Bold

##### Share on other sites
..and every render I get missing primatives- single triangles and/or lines of them. I''m stumped!

my friend has problem what is very like to yours on Riva128 ZX card. My terrain editor, with height gird was showing properly on my Savage4 and RivaTNT2Pro+ but he has holes
p.s. can you post a pic?

##### Share on other sites
Need to ask a couple of questions first.

First,
Did you remember to set the Vertex buffer to the correct size? IE..

g_pd3dDevice->CreateVertexBuffer( 50*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB );

would create a VB the size of 50 vertices, change the (x*sizeof(CUSTOMVERTEX)) to whatever the ammount of vertices you are using.

2nd,
Did you turn on the z-buffer, and set it to a reasonable bit-depth?

##### Share on other sites
quote:

Need to ask a couple of questions first.

First,
Did you remember to set the Vertex buffer to the correct size? IE...
...would create a VB the size of 50 vertices, change the (x*sizeof(CUSTOMVERTEX)) to whatever the ammount of vertices you are using.

Yes, I'm using a 20x20 grid, comprised of 2 primatives per square. In my source (modified copy of the tutorial 5: textures source), is my creation of the vertex buffer, which I immediately populate with verticies. Here's the whole function for that:

  //-----------------------------------------------------------------------------// Name: InitGeometry()// Desc: Create the textures and vertex buffers//-----------------------------------------------------------------------------HRESULT InitGeometry(){ // Use D3DX to create a texture from a file based image// if(FAILED(D3DXCreateTextureFromFile( g_pd3dDevice, "Grass.jpg", &g_pTexture ))) if(FAILED(D3DXCreateTextureFromFile( g_pd3dDevice, "banana.bmp", &g_pTexture ))) { Bug("File: Textures.cpp\nFunction: InitGeometry()\nCall: D3DXCreateTextureFromFile\n"); return E_FAIL; } // Create the vertex buffer. if( FAILED( g_pd3dDevice->CreateVertexBuffer( 20*20*2*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB ) ) ) { return E_FAIL; } // Fill the vertex buffer. We are setting the tu and tv texture // coordinates, which range from 0.0 to 1.0 CUSTOMVERTEX* pVertices; if( FAILED( g_pVB->Lock( 0, 0, (BYTE**)&pVertices, 0 ) ) ) return E_FAIL; for( DWORD x=0; x<20; x++ ) for( DWORD z=0; z<20; z++ ) { // Point 1's coordiantes: pVertices[(40*x)+(2*z)].position = D3DXVECTOR3( ((float)x), 0.0f , ((float)z)); pVertices[(40*x)+(2*z)].color = 0xffffffff; pVertices[(40*x)+(2*z)].tu = ((float)x)*0.05f; pVertices[(40*x)+(2*z)].tv = ((float)z)*0.05f; // Point 2's coordinates: pVertices[(40*x)+(2*z)+1].position = D3DXVECTOR3( ((float)x) +1.0f, 0.0f , ((float)z)); pVertices[(40*x)+(2*z)+1].color = 0xffffffff; pVertices[(40*x)+(2*z)+1].tu = ((float)x)*0.05f + 0.05f; pVertices[(40*x)+(2*z)+1].tv = ((float)z)*0.05f; } g_pVB->Unlock(); return S_OK;}

In some experiements I did mess around with the vertex buffer size- usually finding that, when it was defined smaller than the calls made to drawprimative, my screen would flash green and pink. I assumed that was Direct3D saying there was an error of some sort, though I never found mention of it in the docs.

quote:

2nd,
Did you turn on the z-buffer, and set it to a reasonable bit-depth?

Yes, I initialized the z-buffer, although I'm new to the term "bit-depth". My z-buffer was assigned the same charictaristics as the current display (I think... that's how I read the code, but for the most part I didn't write that part). Again, this is (mostly) the source from tutorial 5.

  //-----------------------------------------------------------------------------// Name: InitD3D()// Desc: Initializes Direct3D//-----------------------------------------------------------------------------HRESULT InitD3D(){ // Create the D3D object. if( NULL == ( g_pD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) ) return E_FAIL; // Get the current desktop display mode, so we can set up a back // buffer of the same format D3DDISPLAYMODE d3ddm; if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) ) return E_FAIL; // Set up the structure used to create the D3DDevice. Since we are now // using more complex geometry, we will create a device with a zbuffer. D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = d3ddm.Format; d3dpp.EnableAutoDepthStencil = TRUE; d3dpp.AutoDepthStencilFormat = D3DFMT_D16; // Create the D3DDevice if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) ) { return E_FAIL; } g_pd3dDevice->SetRenderState( D3DRS_ZFUNC, D3DCMP_ALWAYS ); // Turn off culling g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE ); // Turn off D3D lighting g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE ); // Turn on the zbuffer g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE ); return S_OK;}

so... everything checks out with me, so far... hrm.

quote:

..and every render I get missing primatives- single triangles and/or lines of them. I'm stumped!

my friend has problem what is very like to yours on Riva128 ZX card. My terrain editor, with height gird was showing properly on my Savage4 and RivaTNT2Pro+ but he has holes
p.s. can you post a pic?

As far as the hardware goes, I'm running a (mouthful) ASUS AGP-v6600 GeForce256 32meg. (it was the only one I could find available in my area with tv in/out AND 3d glasses)

This would be the first time posting I pic (I *just* got it working... for me, in another prog)

Thanks all for helping.

-Tok.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
"Hey, I was't joking... You really /could/ be "boinked" by some irate looney wielding a rubber malet." -Baldor the Bold

Edited by - Tok on October 19, 2001 12:29:05 AM

##### Share on other sites
Great Texture. Was it easy to make?
Why don''t you make the map a little smaller, like say 5 * 5 and see if the same problems pop up. The main advantage is that if they do, then you could use the debugger and check out whether all the Vertices are being filled in correctly without going through 400+ steps. Only 25 would be needed.

Good luck...

##### Share on other sites
quote:
Original post by DLin
Great Texture. Was it easy to make?
Why don't you make the map a little smaller, like say 5 * 5 and see if the same problems pop up. The main advantage is that if they do, then you could use the debugger and check out whether all the Vertices are being filled in correctly without going through 400+ steps. Only 25 would be needed.

Good luck...

(I'll need it...)

First off, Thank you everyone who is helping me out. I'm sorry that I can't just figure this out on my own- but I've tried upwards of a dozen different coding methods to try to implement the same effect, and *ALL* give me this error. I'm just about ready to blame it on hardware.

As for the "grass", thank you, but I found the texture with a programming example on the net. I like it, and have ideas about how to create my own versions of it so I don't steal anyone else's work.

Regarding working it 5x5: I'm sorry to say that of the many, many things I've tried, I've also changed the defined size of "the lawn".

In the following

we have the camera placed at (-5.0, 7.0, 2.5) looking at the "2.5" center of a 5 by 5. Each square in this case has a width of 1, though I've tried a few scales of grid ranging from .01f to 1.0f/sq. As far as the verticies being filled in correctly they are, unless the initialization (in this case trianglestrip, though there have been others)
pVertices[(40*x)+(2*z)].position = D3DXVECTOR3( ((float)x), 0.0f , ((float)z));pVertices[(40*x)+(2*z)+1].position = D3DXVECTOR3( ((float)x) +1.0f, 0.0f , ((float)z));

is lying to me somehow. I doubt this, as the verticies missed depend on the angle of view. By simply changing the x camera coordinate from 2.5 to 3.5, we get

Only 2 of the 13 faces missed with the x = 2.5 implemention are missing with x = 3.5. Other camera coordinates show the entire grid rendered- some show almost none of it.

I am new to DirectX, but I am not new to programming. This bug is mocking every debugging method I know... short of compiling someone else's working copy and saying "here, this is my code."

A fairly exhausted (and fed up) thank you to anyone and everyone who has had questions/comments/suggestions for me. They are *all* appreciated.

-Tok.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
"Hey, I was't joking... You really /could/ be "boinked" by some irate looney wielding a rubber malet." -Baldor the Bold

Edited by - Tok on October 23, 2001 5:51:30 AM

##### Share on other sites
To me it seems like there is a culling problem (you are showing the backface of the triangles and they don''t have a texture attached to them).

To check if this is the problem set the renderstate to wireframe rendering (instead of solid) and if you (with your culling_mode == none) see all the triangles independent of the camera position then your creation of the triangle list/stripe has the wrong culling for some of the triangles.

##### Share on other sites
Sorry, I've tried wireframe, textured, and point- when they're not drawn in one method, they don't draw in any.
And in the examples, I have culling set to cull_none. I've tried every culling method I could find in the docx, and none work.

-Regards,
Tok.

Edited by - Tok on October 23, 2001 10:18:52 AM

##### Share on other sites
Any chance you''re not creating your triangles in the right order. Meaning, clockwise for the first, counter for second in the strip?

1--3
|\ |
| \|
0--2

Should be:

0,1,2
2,3,1

I''m not 100% sure on the order for the second triangle, but you should be able to find it in the SDK or on the web. The order is important because it determines which side is considered the backface for culling. You images seem like the usual problem with triangle point ordering in a strip.

Just a thought.

Rube.

##### Share on other sites
quote:
Original post by Rube
Any chance you''re not creating your triangles in the right order. Meaning, clockwise for the first, counter for second in the strip?

Since he''s tried it with CULL_NONE and still gets the problem, I don''t think that''s it.

Do you have an archive (source & exe)I could download? It might be a driver issue.

Stay Casual,

Ken
Drunken Hyena

##### Share on other sites
First off, I would like to thank each and every one of you, *again* for helping me with this bug. I'm actually kindof embarassed that I can't get it working, but I guess that probably happens to us all, at some time or another.

A 2.9meg zip of the entire working directory of this little project of mine can be found HERE.

As far as video goes, I'm running an GeForce 256 chipset, in an Asus v6600 32meg video card. NO idea what the driver is for that- but it hasn't dissapointed me in any way(that I know of yet).

Thanks again, all.

-Tok.

Edited by - Tok on October 25, 2001 3:18:59 PM

##### Share on other sites
Now, even with CULL_NONE set, if he didn''t map textures to the other side of the triangle, would it draw it? Just curious.

I''ll download your zip and try it on my Hercules Prophet II when I get home after work.

Rube.

##### Share on other sites
quote:
Original post by Rube
Now, even with CULL_NONE set, if he didn''t map textures to the other side of the triangle, would it draw it? Just curious.

Rube.

G''day!

You don''t map textures to a ''side'' of a triangle, they cover the entire face. Typically with culling on you can only see it from one side, but with culling turned off you can see it from either side, the texture will just be backwards when viewed from behind.

I''ll download it when I get home and see what I can see.

Stay Casual,

Ken
Drunken Hyena

##### Share on other sites
G''day!

Wow, I''m surprised it works at all! Just kidding.

First, do you have the Debug version of the SDK installed?
Second, do you have the Debug output turned up? (DirectX icon in your control panel)
If so, you''d see this in your debug output:
Direct3D8: (WARN) :Cannot compute WNear and WFar from the supplied projection matrix

Direct3D8: (WARN) :Setting wNear to 0.0 and wFar to 1.0

Direct3D8: (ERROR) :VertexBuffer not created with this Device. SetStreamSource fails.

The first problem is with your projection matrix. Don''t set the near plane to 0. I set it to 0.1f, and the first 2 WARNings went away.

Second, this is just plain wrong.
g_pd3dDevice->SetStreamSource( 0, &g_pVB[i*40], sizeof(CUSTOMVERTEX) );

g_pVB is just a pointer to a single vertex buffer, not an array of them. You do your indexing (when needed) buy giving offsets to your DrawPrimitive call. I changed it to:
g_pd3dDevice->SetStreamSource( 0,g_pVB, sizeof(CUSTOMVERTEX) );

And the ERROR went away.

The first thing to do is check the Debug Output with the debug level turned up. 90% of the time it at least points you in the direction of the problem.

Stay Casual,

Ken
Drunken Hyena

##### Share on other sites
Is it just me or is that the illustrious DrunkenHyena himself? The one with the tutorials on D3D? Enough sucking up now...ill go back to my lil box.

##### Share on other sites
quote:
Original post by EbonySeraphim
Is it just me or is that the illustrious DrunkenHyena himself? The one with the tutorials on D3D? Enough sucking up now...ill go back to my lil box.

G''day!

Illustrious, eh? Sucking up is definitely not required, it doesn''t hurt though.

Stay Casual,

Ken
Drunken Hyena

##### Share on other sites
quote:
Original post by the Illustrious DrunkenHyena
Wow, I'm surprised it works at all! Just kidding.

Lol... no, kidding required, I assure you. And thank you again for all your help.

quote:

First, do you have the Debug version of the SDK installed?

From DirectX Diagnostics in Control panel: "Several files ddraw.dll, ddraw16.dll, ddrawex.dll, etc.) are debug versions, which will run slower than the retail version." Umm... yes?
quote:
Second, do you have the Debug output turned up? (DirectX icon in your control panel)

It was set to the lowest possible setting. doh. And yes, with that little bar turned up (which I honestly didn't know about) I did see those errors.
quote:
The first problem is with your projection matrix. Don't set the near plane to 0. I set it to 0.1f, and the first 2 WARNings went away.

So DX has an error if the nearplane is 0.0f, as opposed to 0.1f? (oh, gee- now why didn't *I* think of that!?!?) (j/k)
quote:
Second, this is just plain wrong.
g_pd3dDevice->SetStreamSource( 0, &g_pVB[i*40], sizeof(CUSTOMVERTEX) );
g_pVB is just a pointer to a single vertex buffer, not an array of them. You do your indexing (when needed) buy giving offsets to your DrawPrimitive call. I changed it to:
g_pd3dDevice->SetStreamSource( 0,g_pVB, sizeof(CUSTOMVERTEX) );
And the ERROR went away.

Yes, it did.
*holds back tears*
As far as the &g_pVB[i*40] goes, I was trying to call subsets of verticies out of a single buffer. Reason being that after the end of the first row of primatives were drawn, the last two verticies were making a primative with the bottom left corner of my next row of verticies- thus making one very, very long triangle that screwed up the graphics. I knew I could have just implemented a trianglist as opposed to a strip (and had, as one attempt at fixing this error), but I guess I just thought I was saving *huge* chunks of memory with the list. My reasoning for the implementation goes: to render the rows independantly (tall order, having no example of how this might be accomplished), I could set the starting point in/with SetStreamSource to the first vertex of each new row, and tell it to draw just as many triangles as needed to make that row. I was thrown off, by the fact that it seemed to be working.

Well, the code is up and running better than ever, and I have a Dunken Hyena to ultimately thank for it. (I would love to know how you came up with that nick, btw) Thank you to everyone who contributed something to this- it was all very, very appreciated.

-Neil.
===========================
"We all just gotta ride this s**t out." -Baldor the Bold

Edited by - Tok on October 26, 2001 12:08:08 AM

##### Share on other sites
G''day!

quote:

It was set to the lowest possible setting. doh. And yes, with that little bar turned up (which I honestly didn''t know about) I did see those errors.

Well, now ya do. It''s one the the handiest debugging tools you have when dealing with D3D. Very few people run it at the highest level, I think 2nd highest is the the most people recommend you use as a rule. If you saw how much stuff it put it out, you understand why. But it''s a good idea to run it at the highest level every now and then, even if you''re not seeing problems, the warnings can be invaluable.
quote:

So DX has an error if the nearplane is 0.0f, as opposed to 0.1f? (oh, gee- now why didn''t *I* think of that!?!?) (j/k)

The short explanation is that the math involved ''likes'' it to be > 0. I think you can end up with division by zero errors and all sorts of nasty stuff. Someone better versed in the behind the scenes math could explain it much better than I could.
quote:

I would love to know how you came up with that nick, btw

It''s from a poem I wrote a long time ago. One of these days I''ll get off my butt and post the thing on my site.

Stay Casual,

Ken
Drunken Hyena