Zdravim, prednedavnom som zacal robit pokusy s directx a teraz som narazil na jeden problem (tych problemov bolo viac ale vecsinu som vyriesil) mam nasledovne kody:
nacitanie:
Kód:
HeightMap::HeightMap(char *fname, IDirect3DDevice9* pd3dDevice)
{
FILE *f=NULL;
f=fopen(fname,"rb");
if (!f)
{
x_width=0;
z_height=0;
map=NULL;
return;
}
int a=0,b=0;
fread(&x_width, sizeof(word),1, f);
fread(&z_height, sizeof(word),1, f);
map = new float*[x_width];
for (int i = 0; i < x_width; ++i) map[i] = new float[z_height];
for (int i=0; i<x_width; i++)
for (int j=0; j<z_height; j++) map[i][j]=1;
Vertices = new CUSTOMVERTEX[x_width*z_height];
// Create the vertex buffer.
if( FAILED( pd3dDevice->CreateVertexBuffer( x_width*z_height* sizeof( CUSTOMVERTEX ),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &pVB, NULL ) ) )
{
return;
}
Indices = new UINT[(x_width-1)*(z_height-1)*6];
if( FAILED( pd3dDevice->CreateIndexBuffer( (x_width-1)*(z_height-1)*6 *sizeof(UINT),
D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT,
&pIB, NULL ) ) )
{
return;
}
VOID *pVertices;
if( FAILED( pVB->Lock( 0, 0, ( void** )&pVertices, 0 ) ) )
return;
do
{
fread(&map[a][b], sizeof(float),1, f);
Vertices[a*x_width+b].x=a;
Vertices[a*x_width+b].y=map[a][b];
Vertices[a*x_width+b].z=b;
Vertices[a*x_width+b].tu=a%2;
Vertices[a*x_width+b].tv=b%2;
a++;
if (a==x_width) {b++;a=0;}
}
while(b<z_height);
memcpy( pVertices, Vertices, sizeof(Vertices) );
pVB->Unlock();
VOID* pIndices;
if( FAILED( pIB->Lock( 0, sizeof(Indices), (VOID**)&pIndices, 0) ) )
{
SAFE_RELEASE(pIB);
return;
}
int k=0;
for (int i=0; i<x_width-1; i++)
for (int j=0; j<z_height-1; j++)
{
Indices[k]=i*x_width+j;
k++;
Indices[k]=(i+1)*x_width+j;
k++;
Indices[k]=i*x_width+j+1;
k++;
Indices[k]=(i+1)*x_width+j;
k++;
Indices[k]=(i+1)*x_width+j+1;
k++;
Indices[k]=i*x_width+j+1;
k++;
}
memcpy( pIndices, Indices, sizeof(Indices) );
pIB->Unlock();
fclose(f);
Texture=NULL;
if( FAILED( D3DXCreateTextureFromFile( pd3dDevice, L"banana.bmp", &Texture ) ) )
{
// If texture is not in current folder, try parent folder
if( FAILED( D3DXCreateTextureFromFile( pd3dDevice, L"..\\banana.bmp", &Texture ) ) )
{
MessageBox( NULL, L"Could not find banana.bmp", L"Textures.exe", MB_OK );
return;
}
}
}
vykreslenie:
Kód:
mProj = *g_Camera.GetProjMatrix();
mView = *g_Camera.GetViewMatrix();
// Get the projection & view matrix from the camera class
D3DXMatrixTranslation(&mWorld,0,-6000,0);
mWorldViewProjection = mWorld * mView * mProj;
// Update the effect's variables. Instead of using strings, it would
// be more efficient to cache a handle to the parameter by calling
// ID3DXEffect::GetParameterByName
UINT iPass, cPasses;
V( g_pEffect->SetMatrix( "g_mWorldViewProjection", &mWorldViewProjection ) );
V( g_pEffect->SetMatrix( "g_mWorld", &mWorld ) );
V( g_pEffect->SetFloat( "g_fTime", ( float )fTime ) );
g_pEffect->SetTechnique( "RenderScene" );
pd3dDevice->SetStreamSource( 0, mapa->pVB, 0, sizeof( CUSTOMVERTEX ) );
pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
pd3dDevice->SetIndices(mapa->pIB);
V( g_pEffect->Begin( &cPasses, 0 ) );
for( iPass = 0; iPass < cPasses; iPass++ )
{
V( g_pEffect->BeginPass( iPass ) );
pd3dDevice->SetTexture( 0, mapa->Texture );
pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, mapa->x_width*mapa->z_height, 0, (mapa->x_width-1)*(mapa->z_height-1));
g_pEffect->EndPass();
}
g_pEffect->End();
pouzivam MS VC++ 2010, Microsoft DirectX SDK (June 2010).
problem je ten ze DrawIndexedPrimitive nevykresli nic, mam kameru ovladam sipkami a klavesami W S. Skusal som ju najst

ale nepodarilo sa.
vykreslenie bez indexov funguje:
Kód:
D3DXMatrixTranslation(&mWorld,5,-4,5);
mWorldViewProjection = mWorld * mView * mProj;
V( g_pEffect->SetMatrix( "g_mWorldViewProjection", &mWorldViewProjection ) );
V( g_pEffect->SetMatrix( "g_mWorld", &mWorld ) );
V( g_pEffect->SetFloat( "g_fTime", ( float )fTime ) );
g_pEffect->SetTechnique( "RenderScene" );
pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof( CUSTOMVERTEX ) );
pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
V( g_pEffect->Begin( &cPasses, 0 ) );
for( iPass = 0; iPass < cPasses; iPass++ )
{
V( g_pEffect->BeginPass( iPass ) );
pd3dDevice->SetTexture( 0, g_pTexture );
pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 1 );
g_pEffect->EndPass();
}
g_pEffect->End();
Jedna sa o tom ze som naprogramoval hru s grafikou v Opengl, teraz by som to rad prepisal do Directx.