Problem with vertices

Started by
5 comments, last by gerbrost 22 years, 5 months ago
Hi all! I have a problem rendering a simple plane. This is my code (i only posted the parts concerning the vertices, its put together from an init- and a render-function):
      
//This is how my custom vertex is declared

struct CUSTOMVERTEX
{
    D3DXVECTOR3 position; // The position.

	D3DXVECTOR3 normal;
    FLOAT       tu, tv;   // The texture coordinates.

};

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1)

LPDIRECT3DVERTEXBUFFER8 g_pVB; // Buffer to hold vertices

LPDIRECT3DTEXTURE8      g_pGroundTexture; // Our ground texture

D3DMATERIAL8 Material;
//Then i load the texture:

D3DXCreateTextureFromFile( device, "ground.bmp", &g_pGroundTexture));
// and create the vertex buffer

device->CreateVertexBuffer( 5*sizeof(CUSTOMVERTEX),0,D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT, &g_pVB))
//and set up the material

ZeroMemory(&Material,sizeof(Material));
Material.Diffuse.r = 1.0f;
Material.Diffuse.g = 1.0f;
Material.Diffuse.b = 1.0f;

CUSTOMVERTEX* pVertices;
// lock the vertex buffer

g_pVB->Lock( 0, 0, (BYTE**)&pVertices, 0 )

// and initialize the vertices

// i want to show a plane in the x/z axis, the normals point up

pVertices[0].position=D3DXVECTOR3(0.0f,0.0f,1.0f);
pVertices[0].normal=D3DXVECTOR3(0.0f,1.0f,0.0f);
pVertices[0].tu=0.0f;
pVertices[0].tv=0.0f;
pVertices[1].position=D3DXVECTOR3(0.0f,0.0f,0.0f);
pVertices[1].normal=D3DXVECTOR3(0.0f,1.0f,0.0f);
pVertices[1].tu=0.0f;
pVertices[1].tv=1.0f;
pVertices[2].position=D3DXVECTOR3(1.0f,0.0f,1.0f);
pVertices[2].normal=D3DXVECTOR3(0.0f,1.0f,0.0f);
pVertices[2].tu=1.0f;
pVertices[2].tv=0.0f;
pVertices[3].position=D3DXVECTOR3(1.0f,0.0f,0.0f);
pVertices[3].normal=D3DXVECTOR3(0.0f,1.0f,0.0f);
pVertices[3].tu=1.0f;
pVertices[3].tv=1.0f;
g_pVB->Unlock();


// then render it

device->SetTexture( 0, g_pGroundTexture);
device->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
device->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
device->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
device->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );

device->SetMaterial(&Material);
device->SetVertexShader( D3DFVF_CUSTOMVERTEX );
device->SetStreamSource( 0, g_pVB, sizeof(CUSTOMVERTEX) );
device->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0,4);

      
It must be something very simple i forgot, but i just cant see it... Thank you! Edited by - gerbrost on November 5, 2001 4:53:34 PM
Advertisement
device->CreateVertexBuffer( 5*sizeof(CUSTOMVERTEX),0,D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT, &g_pVB))

Change 5 to 4 (setting 4 vertices in code)


Also, vertex order means the triangle is only visible from below, normals don''t match. Change ordering of only the positions to 0,0,0 - 0,0,1 - 1,0,0 - 1,0,1


device->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0,4);

Change 4 to 2 (2 polygon primitives to draw, not 4)


Jim Adams
Shame on me

Thank you very much!!

I still seem to have a problem: The plane is not lit...
The only lighting processed is ambient light.
Directional or other light types do not affect the plane, it stays dark.
Is there something up with the normals?


Edited by - gerbrost on November 6, 2001 7:13:21 AM
For lighting, enable lighting render state as well as light:

pd3dDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
pd3dDevice->SetLight(0, &Light);
pd3dDevice->LightEnable(0, TRUE);

If you''ve done that, then make sure the D3DLIGHT8::Range is properly set to encompass the plane.


Jim Adams
Here is my lighting code:
  	//*** Set up some lights to make the scene look cool	D3DXVECTOR3 vecDir;	D3DLIGHT8 light;	ZeroMemory( &light, sizeof(D3DLIGHT8) );	light.Type       = D3DLIGHT_DIRECTIONAL;	light.Diffuse.r  = 1.0f;	light.Diffuse.g  = 1.0f;	light.Diffuse.b  = 1.0f;	light.Ambient.r  = 0.0f;	light.Ambient.g  = 0.0f;	light.Ambient.b  = 0.0f;	light.Specular.r = 0.0f;	light.Specular.g = 0.0f;	light.Specular.b = 0.0f;	vecDir = D3DXVECTOR3(0.0f,-1.0f,0.0f);		D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &vecDir );		light.Range       = 1000.0f;	device->SetLight( 0, &light );	device->LightEnable( 0, TRUE );	device->SetRenderState( D3DRS_LIGHTING, TRUE );	//*** Finally, turn on some ambient light.	device->SetRenderState( D3DRS_AMBIENT, 0x00202020 );  


The ambient light affects the plane.
But whatever values i set for the directional light, it doesnt change anything.
When i load an x-file, the model is displayed correctly and affected by the light as it should be...

Im really confused...


I dont understand any of the lighting stuff. Not like its hard but I have simpler concepts to understand first...onto my point : Do you have to set the specular light to 0.0f if you hvae already called ZeroMemory on the light struct?(reffering to the code above)

"Ogun''s Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.
"Ogun's Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.
Specular is set to zero and the light struct has been sent through ZeroMemory().

This topic is closed to new replies.

Advertisement