Archived

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

Draw Primitive Weirdness

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

Recommended Posts

I have a forest of lsystem trees being drawn and I have a terrain that I draw. They are in seperate classes. My main loop calls "terrain.draw" and then "forest.draw". Now what happens when I do that is it runs for a while until a certain amount of trees are drawn and my ENTIRE screen becomes garbage. But the mouse a split second before the screen becomes a block of garbage then the entire screen goes. The program does not have this problem if I ONLY draw trees or if I ONLY draw the terrain. I have no idea what to try or what to do next. I thought that maybe it was my the way I was allocating vertex buffers so I tried changing that but that was okay. I thought maybe it was the way I was filling the buffers so I changed that but it also was okay. I found the problem was that I could fill the vertex buffers just fine, it was my call to draw the primitive that created the issue. So I thought, okay so I might be doing something wrong... why not let directx take my array and do it itself through the use of drawprimitiveUP. STILL SAME ISSUE! I've also reinstalled video drivers. If anyone has any ideas or answers I'd be greatful. ~Wave [edited by - Wavewash on November 16, 2003 3:57:31 PM]

Share on other sites
Okay I got the problem solved apparently my vertex format was "too large" it seems. I have no clue what the deal is.

struct CUSTOMVERTEX
{
FLOAT x, y, z; // The untransformed, 3D position for the vertex
DWORD color; // The vertex color
//FLOAT tu1, tv1;
};

struct CUSTOMVERTEX1
{
FLOAT x, y, z; // The untransformed, 3D position for the vertex
DWORD color; // The vertex color
FLOAT tu1, tv1;
//FLOAT tu2, tv2;
};

// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_TERRAINVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
#define D3DFVF_TERRAINVERTEX1 (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)

if I render with customvertex1 with the second tu,tv on the system goes crazy. Has anyone else had this experience?

~Wave

Share on other sites
I think your problem is this:

//This is defined correctly and matches the following struct.
#define D3DFVF_TERRAINVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)

struct CUSTOMVERTEX {
FLOAT x, y, z; // The untransformed, 3D position for the vertex
DWORD color; // The vertex color
};

//This isn't, and doesn't match the following struct.
#define D3DFVF_TERRAINVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)

struct CUSTOMVERTEX {
FLOAT x, y, z; // The untransformed, 3D position for the vertex
DWORD color; // The vertex color
FLOAT tu1, tv1; // NOT DEFINED
};

// correct
#define D3DFVF_TERRAINVERTEX1 (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX2)

struct CUSTOMVERTEX1 {
FLOAT x, y, z; // The untransformed, 3D position for the vertex
DWORD color; // The vertex color
FLOAT tu1, tv1;
FLOAT tu2, tv2;
};

// not correct because we defined that
// we were using 1 set of coords
#define D3DFVF_TERRAINVERTEX1 (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)

struct CUSTOMVERTEX1 {
FLOAT x, y, z; // The untransformed, 3D position for the vertex
DWORD color; // The vertex color
FLOAT tu1, tv1;
FLOAT tu2, tv2; // NOT DEFINED
};

I hope that helps out. :]
CD

Jesus is Lord!!

[edited by - CD579 on November 17, 2003 4:04:49 AM]

Share on other sites

But I think you missed that I had the coordinates commented out there. If the problem was that the custom vertex didn''t match the FVF then it wouldn''t render at all. It''d spit out a message and flicker.

~Wave

Share on other sites
What CD579 means is that if you wanted to use more than 1 texture coord then you''d have to change your vertex decleration to suit so:

struct CUSTOMVERTEX
{
D3DXVECTOR3 pos; //D3DFVF_XYZ
DWORD col; //D3DFVF_DIFFUSE
D3DXVECTOR2 t1; //D3DFVF_TEX1
}

#define FVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1);

that''s right for 1 tex coord and the next shows 2 tex coords

struct CUSTOMVERTEX2
{
D3DXVECTOR3 pos; //D3DFVF_XYZ
DWORD col; //D3DFVF_DIFFUSE
D3DXVECTOR2 t1; //D3DFVF_TEX2
D3DXVECTOR2 t2; //D3DFVF_TEX2 as well
}

#define FVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX2);

de_matt

Share on other sites
Well, just to clarify, DirectX WILL render if you get the FVF wrong, AS LONG AS you set the vertex stride correctly (but it won''t render the part you omitted). Anyway, your problem might lie in the size of the vertex structure, like you suspected. If the vertex format is not a mutliple of 32-bits, some cards ''bumps'' the size of the vertex up to a multiple of 32-bits and if the new size is too big, it will crash. Therefore, try updating your card drivers. That''s the only advice I can offer other than using the Debug DirectX build for a bit of extra help.

Chris Pergrossi
My Realm | "Good Morning, Dave"

Share on other sites
Thank all of you guys for the help. After beating my head silly trying to figure it out. I found out the problem.

My custom vertex and FVF were okay. The real problem came about when I rendered my terrain. I used the following code:

m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE,   TRUE );		m_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);	m_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);	m_pd3dDevice->SetTexture( 1, m_pTexture2 );	m_pd3dDevice->SetTexture( 0, m_pTexture1 );	m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MIPFILTER, D3DTEXF_LINEAR);	m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);	m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR);	m_pd3dDevice->SetTextureStageState( 1, D3DTSS_MIPFILTER, D3DTEXF_LINEAR);	m_pd3dDevice->SetTextureStageState( 1, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);	m_pd3dDevice->SetTextureStageState( 1, D3DTSS_MINFILTER, D3DTEXF_LINEAR);	m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );	m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_BLENDDIFFUSEALPHA);    // Render the vertex buffer contents	m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(CUSTOMVERTEX1) );	m_pd3dDevice->SetVertexShader( D3DFVF_TERRAINVERTEX1);	m_pd3dDevice->SetIndices (m_pIndexBuffer, 0);	//m_pd3dDevice->DrawPrimitive(D3DPT_POINTLIST,0,256*256-1);	m_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 256*256-3,		0,IndCount/3-1);

notice anything? No? Well neither did I. The code worked perfectly up till a point until the screen went bazerk. The problem was that I wasn't setting my textures to null when I was done. It's crazy, I know, but all that I needed was:

m_pd3dDevice->SetTexture( 1, NULL );
m_pd3dDevice->SetTexture( 0, NULL );

and I was set.

From what I understand. By not setting the textures to null when I was through using them I began a memory leak.

from the dx sdk:
quote:
When the texture is no longer needed, set the texture at the appropriate stage to NULL. Failure to do this results in a memory leak.

As the lsystem trees were drawn on the terrain I'm guessing the system kept allocating memory for the textures to use on each tree as they multiplied. For that reason when the tree amount reached this certain point, the computer video memory got all used up and the video was trashed. If someone understands it in more depth I would be intrested to hear the explanation.

Anyways, Thankyou guys soooo much. The help was very much appreciated.

~Wave

[edited by - Wavewash on November 18, 2003 3:40:38 AM]

Share on other sites
Wow, you''re not using DirectX 9.0 are you? I''m not using C# at all, just C++, and I found upgrading to 9.0 very helpful, as many things are changed. That EXACT problem of not calling SetTexture(0,NULL) was addressed, and now they have the function automatically dereference past textures when a new one is passed in. The strange part is, if you kept drawing with the same texture, why would it take up all your video card memory? More like, you rendered so many times that the RefCounter variable overflowed and the RefCounter==0 condition kicked in to delete the texture and DirectX crashed; cause there should only really be one copy of the texture floating around... Something to look at.

Chris Pergrossi
My Realm | "Good Morning, Dave"

• 20
• 10
• 19
• 14
• 20