Archived

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

MrMonkey

Crazy geometry (help)

Recommended Posts

Hey all, I am tearing my hair out over a seemingly stupid problem, and I hope there''s salvation here. I have been to integrate a very simple terrain rendering class, and rendering it as an indexed primitive... but I''ve had an inordinate amount of problems doing so. I have finally achieved the following, and can''t seem to do any better. Anyone have any thoughts? Thanks for the thoughts

Share this post


Link to post
Share on other sites
I had a similar problem. In my case I had some "rubbish" data in my vertex buffer. Try looking at the code that fills the vertex buffer and make sure that the numbers ok.

I''ve got some tutorials on my site, including a simple terrain generator (looks very similar to yours). Take a look at tutorial 8, it may help you.

http://www.andypike.com/tutorials/directx8/

All the best
Andy

-----------------------------
Andy Pike
DirectX 8 Tutorials: www.andypike.com

Share this post


Link to post
Share on other sites
Haha... that''s great.

I was actually attempting to use your exact demo.
I followed your steps exactly (only renaming some variables here and there). I have checked the VB line by line numerous times...but to no avail. It works great in your download... but looks like the above when I run mine.
sigh.

Share this post


Link to post
Share on other sites
Yup, in making my own map parser I also had the same problem... what is happening is that somewhere you are adding vertices that are being set using uninitialised variables... the reason they stretch off to infinity is because of the garbage value the variables have if you don''t initialise your variables. So try running through with a debug watching the variables that form your vertices and watch for the bad values, then you can begin to track the problem to its source.

Share this post


Link to post
Share on other sites
Thanks for the help guys.
So here''s the update. I found my problem (sort of).
in my render() I had the following lines (didn''t work):
g_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, m_NumVerts/2, 0, m_NumPolys/2);
g_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, m_NumVerts/2, m_NumVerts/2, m_NumPolys/2, m_NumPolys/2);

So I replaced it w/ the simple:
g_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, m_NumVerts, 0, m_NumPolys);


Which does work. The problem is, I''m using a test map, and the one I want to use is too big to render in one shot (which is why I tried to break it up in the first place). Am I way off base here, or is my syntax repairable?
Thanks.

Share this post


Link to post
Share on other sites
Yup as stated above your 4th parameter needs to indicate the starting index... you may be able to temporarily fix it by using these two lines:

g_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, m_NumVerts/2, 0, m_NumPolys/2);
g_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, m_NumVerts/2, m_NumVerts/2, m_NumVerts/2, m_NumPolys/2);

But thats assuming you have the same number of indices as you have vertices which is probably not the case. I would probably fix that by keeping track of the number of indices in a member variable and using that variable when setting your start index in DrawIndexedPrimitive, or perform a more accurate calculation on your number of vertices to get the number of indices you have but thats dependant on how you are using your index buffer so without seeing more code I couldn''t give you the exact calculation.

Share this post


Link to post
Share on other sites
Thanks again for all your help- you guys rock.

So, as it happens, I do have a member variable that holds the amount of Indices (m_NumIndices). But I doubt that the following would be valid (or am I wrong?):

g_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, m_NumVerts/2, 0, m_NumPolys/2);
g_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, m_NumVerts/2, m_NumVerts/2, m_NumIndices/2, m_NumPolys/2);


Can I assume that, by the second DIP call,I have rendered exacly 1/2 of the Indices?

(BTW, my code is very closely based on Mr. Andy Pike''s stuff, at:
His Terrain Tutorial )

Share this post


Link to post
Share on other sites
Well the number of indices you use halved should be a sufficent number of indices to render half of your vertices. Say you have a quad using 4 vertices and are using an index buffer containing 6 indices to tell DrawIndexedPrimitve how to use the four vertices, the first 3 indices would be vertices 0, 1 and 2. Your 4th, 5th and 6th indices would be vertices 0, 2 and 3.

So yes, the first half of the indices would be sufficient indices to draw the first triangle and the second half of the indices would be sufficient to draw the second triangle. This should scale up absolutely fine when using more vertices and indices.

So in answer to your question, I think yes, your new calls to DrawIndexedPrimitive should in theory work fine... let us know if it doesnt work though.

Share this post


Link to post
Share on other sites
Darnit.
Well, it almost works (but apparently, I live a long way from theory).
Using the following code to render, I results in the image below.
g_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, m_NumVerts/2, 0, m_NumPolys/2);
g_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, m_NumVerts/2 , m_NumVerts/2, m_NumIndices/2, m_NumPolys/2 );



I was beginning to question my VB and IB, but since the sucker renders fine w/o spliting up the DIP calls, I think all my structures are ok.
BTW, it appears that the vertex that exploded is the first one that I define in my IB.

Share this post


Link to post
Share on other sites
I would guess that you are probably doing something wrong with arrays and overflowing one somewhere. Perhaps your number of primitives, number of vertices, or even indices is represented by 1 to NumberOfVertices rather than 0 to (NumberOfVertices-1), remember arrays work such that the first element is given the index 0, not 1. This would explain why you only have one artifact in your terrain, you skip the first element zero and add an extra element on the end. You may find you have to do something like:

g_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, m_NumVerts/2, 0, m_NumPolys/2);
g_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, m_NumVerts/2 , m_NumVerts/2, (m_NumIndices/2)-1, m_NumPolys/2 );

that is if your problem is referring to an index value that does not exist. Adjust accordingly depending where you are overflowing.

I''m guessing you are referring to a non existant index value because the first call to DrawIndexedPrimitive works fine.. and there you specify 0 for the starting index. In the second call you specify starting index by reference to a variable and that is where your error comes in.

Share this post


Link to post
Share on other sites