Ok here are some code snippets from a program I made. All it does right now is draw a triangle in 3d space. For some reason though, it won't appear on my screen, even though all the vectors for the view matrix and projection matrix appear to be ok.
This first one initializes my vertices and vertex buffer:
void InitVertices()
{
triVERTEX vertices[] =
{
{0,1,0,0xFFFF0000},{1,-1,0,0xFF00FF00},{-1,-1,0,0xFF0000FF}
};
int vert_count=sizeof(vertices)/sizeof(triVERTEX);
int byte_count=vert_count*sizeof(triVERTEX);
void *vb_vertices;
g_list_count=vert_count/3;
D3DDevice->CreateVertexBuffer(byte_count, //Length
D3DUSAGE_WRITEONLY,//Usage
tri_fvf, //FVF
D3DPOOL_MANAGED, //Pool
&VBuffer, //ppVertexBuffer
NULL); //Handle
VBuffer->Lock(0, //Offset
0, //SizeToLock
&vb_vertices, //Vertices
0); //Flags
memcpy( vb_vertices, vertices ,byte_count);
VBuffer->Unlock();
}
This one is initializing my matrices. Oh yeah, the variables width and height were set at the beginning of the program to GetSystemMetrics(CMXSCREEN) and (CM_YSCREEN) (dont worry bout the probably wrong flags there, I just dont remember exactly what they were. But basically width and height are the windows width and height, so I don't know maybe that makes a difference):
void InitMatrices()
{
D3DXMATRIX view_matrix;
D3DXMATRIX projection_matrix;
D3DXVECTOR3 eye_vector;
D3DXVECTOR3 lookat_vector;
D3DXVECTOR3 up_vector;
D3DXMATRIX world_matrix;
float aspect;
//Here we build our View Matrix, think of it as our camera.
//First we specify that our viewpoint is 8 units back on the Z-axis
eye_vector=D3DXVECTOR3( 0.0f, 0.0f,-8.0f );
//We are looking towards the origin
lookat_vector=D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
//The "up" direction is the positive direction on the y-axis
up_vector=D3DXVECTOR3(0.0f,1.0f,0.0f);
D3DXMatrixLookAtLH(&view_matrix,&eye_vector,
&lookat_vector,
&up_vector);
//Since our 'camera' will never move, we can set this once at the
//beginning and never worry about it again
D3DDevice->SetTransform(D3DTS_VIEW,&view_matrix);
aspect=((float)width / (float)height);
D3DXMatrixPerspectiveFovLH(&projection_matrix, //Result Matrix
D3DX_PI/4, //Field of View, in radians.
aspect, //Aspect ratio
0.5f, //Near view plane
100.0f ); //Far view plane
//Our Projection matrix won't change either, so we set it now and never touch
//it again.
D3DDevice->SetTransform(D3DTS_PROJECTION, &projection_matrix);
//The World Matrix transforms Model Coordinates into World Space coordinates.
//Setting it to Identity means there is no transformation, so Model Space is directly
//mapped onto World Space.
D3DXMatrixIdentity(&world_matrix);
D3DDevice->SetTransform(D3DTS_WORLD,&world_matrix);
}
And this last one renders my triangle (but it doesn't appear on screen for some reason)
void Render()
{
D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 0, 0);
D3DDevice->BeginScene();
D3DDevice->SetFVF(tri_fvf);
D3DDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_SOLID);
//Bind our Vertex Buffer
D3DDevice->SetStreamSource(0, //StreamNumber
VBuffer, //StreamData
0, //OffsetInBytes
sizeof(triVERTEX)); //Stride
//Render from our Vertex Buffer
D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, //PrimitiveType
0, //StartVertex
g_list_count); //PrimitiveCount
D3DDevice->EndScene();
D3DDevice->Present(NULL, NULL, NULL, NULL);
}
So does anyone see something wrong with this? Because I sure dont.
Thanks
[Edited by - Coder on November 5, 2005 12:08:21 PM]