   Terrain Engine
  I Know, dont you guys get sick of my problems?  Hopefully, soon, i will get it to the point where i can solve problems on my own  until then, hers my problem: Ok, i run my terrain engine in windowed. (wrote some code to check if its windowed or not and assign present rate respectively) but it just starts up and gets a "unhandled exception, access violation" error.  Heres the details:    3DFIREFLY caused an invalid page fault inmodule 3DFIREFLY.EXE at 0197:004011f1.And, using the debugger, i have narrowed it down to this line: m_nHeightArray[y][x]=(getr(pBits[(y*256)+x]))/5;and heres the whole function that its it: HRESULT CTerrainEngine::Initialise(LPDIRECT3DDEVICE8 &pDevice, char *pszHeightMap){D3DXCreateTextureFromFile(pDevice, "Media\\Graphics\\Volcano2.jpg", &m_pTextureHeightmap);D3DXCreateTextureFromFile(pDevice, "Media\\Graphics\\grasstex.jpg", &m_pTextureGround);D3DLOCKED_RECT LockedRect;ZeroMemory(&LockedRect, sizeof(LockedRect));m_pTextureHeightmap->LockRect(0, &LockedRect, NULL, 0);DWORD* pBits=(DWORD*)LockedRect.pBits;int index=0;for(int y=0; y<256; y+=2){for(int x=0; x<256; y+=2){m_nHeightArray[y][x]=(getr(pBits[(y*256)+x]))/5;}}m_pTextureHeightmap->UnlockRect(0);m_pTextureHeightmap->Release();m_pTextureHeightmap=NULL;int z=128;float TexX, TexY;int nArrayIndex=0;for(int IndexY=0; IndexY<256; IndexY+=2){TexY=(float)(((float)IndexY/(float)2)/(float)128);for(int IndexX; IndexX<256; IndexX+=2){int x=IndexX-128;TexX=(float)(((float)IndexX/(float)2)/(float)128);m_vTerrain[((IndexY/2)*128)+(IndexX/2)].x=(float)x;m_vTerrain[((IndexY/2)*128)+(IndexX/2)].y=(float)m_nHeightArray[IndexY][IndexX];m_vTerrain[((IndexY/2) * 128)+(IndexX/2)].z=(float)z;m_vTerrain[((IndexY/2) * 128)+(IndexX/2)].tu1=TexX;   m_vTerrain[((IndexY/2) * 128)+(IndexX/2)].tv1=TexY;    int col=m_nHeightArray[IndexY][IndexX]*5;m_vTerrain[((IndexY/2) * 128)+(IndexX/2)].dwColour=D3DCOLOR_ARGB(255, col, col, col);}z-=2;}nArrayIndex=0;    for(int nIndexY=0; nIndexY<128; nIndexY++)    {   for(int nIndexX=0; nIndexX<127; nIndexX++)   {  m_sIndices[nArrayIndex]=((nIndexY+1) * 128)+nIndexX;  m_sIndices[nArrayIndex+1]=(nIndexY * 128)+nIndexX;  m_sIndices[nArrayIndex+2]=((nIndexY+1) * 128)+nIndexX+1; m_sIndices[nArrayIndex+3]=((nIndexY+1) * 128)+nIndexX+1;  m_sIndices[nArrayIndex+4]=(nIndexY * 128)+nIndexX;  m_sIndices[nArrayIndex+5]=(nIndexY * 128)+nIndexX+1;  nArrayIndex+=6;   }    }BYTE* pIndexData=0;// Create the index buffer...pDevice->CreateIndexBuffer(sizeof(m_sIndices), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &m_pIndexBuffer);// ...lock itm_pIndexBuffer->Lock(0, sizeof(m_sIndices), &pIndexData, 0);// ...copy the indices in, and unlockCopyMemory(pIndexData, &m_sIndices, sizeof(m_sIndices));m_pIndexBuffer->Unlock();return(true);}And heres the CTerrainEngine class: typedef struct _tagTerrainVertex{    float x, y, z;    D3DCOLOR dwColour;    float tu1, tv1;} TERRAINVERTEX;#define D3DFVF_TERRAIN (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1 )class CTerrainEngine{public:CTerrainEngine(){}~CTerrainEngine(){}    LPDIRECT3DTEXTURE8 m_pTextureHeightmap;    // heightmap texture    LPDIRECT3DTEXTURE8 m_pTextureGround;  // texture for ground level    LPDIRECT3DVERTEXBUFFER8 m_pVertexBuffer;   // VB for terrain mesh    LPDIRECT3DINDEXBUFFER8 m_pIndexBuffer;// Index buffer for terrain    int m_nHeightArray[256][256];    // 0-255*0-255 array for Y vals of each heightmap pixel    TERRAINVERTEX m_vTerrain[128*128];    // 6 verts per quad, one quad per pixel    short m_sIndices[(6*(128*128))]; // Index buffer indices    HRESULT Initialise(LPDIRECT3DDEVICE8& pDevice, char* pszHeightmap);};Nothing else has changed except for the Render function, and heres that: void DrawScene(){//Clearg_pDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(25,25,255), 1.0f, 0 );if(g_pDevice->BeginScene() == D3D_OK){//QuadD3DXMATRIX MatrixWorld;D3DXMatrixTranslation(&MatrixWorld, 3.0f, 0.0f, -2.0f);g_pDevice->SetTransform(D3DTS_WORLD, &MatrixWorld);g_pDevice->SetTexture(0, g_pPyramidTexture);g_pDevice->SetStreamSource(0,g_pQuantumVertexBuffer,sizeof(CQuantumVertex));g_pDevice->SetVertexShader(D3DFVF_CQuantumVertex);//Trianglesg_pDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 4, CQVPyramid, sizeof(CQuantumVertex));g_pDevice->SetVertexShader(D3DFVF_TERRAIN);g_pDevice->SetTexture(0, CTerrain.m_pTextureGround);g_pDevice->SetStreamSource(0, CTerrain.m_pVertexBuffer, sizeof(TERRAINVERTEX));g_pDevice->SetIndices(CTerrain.m_pIndexBuffer, 0);for(int nCount=0; nCount<128; nCount++){g_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 768, nCount*768, 256);}D3DXMatrixTranslation(&MatrixWorld, -4.0f, 0.0f, 25.0f);g_pDevice->SetTransform(D3DTS_WORLD, &MatrixWorld);g_pDevice->SetTexture(0, g_pPyramidTexture);g_pDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 4, CQVPyramid, sizeof(CQuantumVertex));//SkyBox Eastg_pDevice->EndScene();}//Presentg_pDevice->Present(NULL, NULL, NULL, NULL);}And, i also deleted my previous flat terrain.  Any other code requirements, either tel me and ill post them up, or look at my last post:  MAJOR PROBLEMThank you!   Edit:  Fixed Code BoxesQuantum[edited by - D_JildQuantum on July 29, 2003 9:39:08 PM]
  I didnt check the rest of the logic... the whole each height represents a quad instead of a vertex and theres 6 vertexs per quad??? is very confusing so you might want to look at that but regardless here is what I think is causing that particular problemfor(int y=0; y<256; y+=2)for(int x=0; x<256; y+=2).. your incrememnting y both times not x the second time. bad copy paste errors. Those drive me nuts I do them all the time.   // Full Sail Student with a passion for games// This post in no way indicates my being awake when writing it ;  Oh ok!  Now i fixed that and ran it, and it didnt work, and using the debugger it gave me this line this time: m_vTerrain[((IndexY/2)*128)+(IndexX/2)].x=(float)x;Im going to be checking for stupid errors, but if you see something let me know.   Quantum ;  Ok, i saw a stupid error... lemme fix it and try again.  I actually saw it this time ^_^   Quantum ;  Uhg, no matter what i do it wont work -.-  It says this line now: m_vTerrain[((IndexY/2)*128)+(IndexX/2)].x=(float)x; Quantum ;  I don''t get it. You took this example as it is from the www.32bits.co.uk the tutorial is there the working source code is there. Why don''t you compare the two source codes and find what you do wrong ?I mean that you ask help if there is no examples or such ...  ;  OK, your getting loads of out of bounds problems by the look of things. Plus why is your height map array 256*256 when your only doing a patch 128*128?You shouldn''t have been looping from 0 to 256 and going in increments of 2, then dividing your loop counter by 2 everywhere. You should always define your variables outside of all loops, preferably put all variable declarations as the first lines of your function.USE...the debugger, when it dies look to see what values things are, it was obvious what line it had broken on, you could have just checked the values of the variables to see that they were out of bounds.I''ve quickly gone through a patch of your code, try this instead of what you''ve already got, I can''t guarentee it''ll work as I only looked at it for a couple of mins. Theres still loads you could do to optimise it.int x, IndexY, IndexX, col;int nTerrainIdx;for(IndexY=0; IndexY<128; IndexY++){TexY=(float)((float)IndexY/(float)128);for(IndexX; IndexX<128; IndexX++){nTerrainIdx = (IndexY * 128) + IndexX;x=IndexX-64;TexX=(float)((float)IndexX/(float)128);m_vTerrain[].x=(float)x;m_vTerrain[nTerrainIdx].y=(float)m_nHeightArray[IndexY * 2][IndexX * 2];m_vTerrain[nTerrainIdx].z=(float)z;m_vTerrain[nTerrainIdx].tu1=TexX;m_vTerrain[nTerrainIdx].tv1=TexY;col=m_nHeightArray[IndexY * 2][IndexX * 2]*5;m_vTerrain[nTerrainIdx].dwColour=D3DCOLOR_ARGB(255, col, col, col);}z-=2;} ;  [OFFTOPIC]quote:You should always define your variables outside of all loops, preferably put all variable declarations as the first lines of your function.SNIFF SNIFF ... I smell Pascal here...No, really. That`s probably the single best thing in C++ compared to Pascal - that you don`t have to jump at the beginning of function just to add an index variable for simple loop which obviously distracts you from the idea that you wanted to implement. And therefore you don`t pile up 10 lines of variable declarations (not even talking about the blessing of scope variables).[/OFFTOPIC][ONTOPIC]You should get back to your previous working version and start modifying that version. You don`t have backed up your previous working version ? WRONG !!! You should back up your whole working directory daily (or if you work whole weekend then twice a day)! That way, you only loose 1 day (usually less, since you remember the logic that got you into next version that stoped working). Everytime you can`t seem to find an error for more than half a day, just scrap it and start adding litlle things into your code - each time trying if it works. Usually if you rewrite the code, you won`t make the same mistake twice (you`ll make another  ).[/ONTOPIC]  VladR Avenger game;  ::You don`t have backed up your previous working version ? ::WRONG !!! You should back up your whole working directory ::daily (or if you work whole weekend then twice a day)! That ::way, you only loose 1 day (usually less, since you remember ::the logic that got you into next version that stoped working). Sorry, wrong, too.You should use a VCS (Version Control System). There are free ones around.This way you never loose ANY change you do for a stable build.Daily backups are harder to maange than a nice VCS database. ;  CVS is no guarantee against corruption.There is no substitute for regular backups.   ____________________________________________________________Try RealityRift at www.planetrift.comFeel free to comment, object, laugh at or agree to this. I won''t engage in flaming because of what I have said. I could be wrong or right but the ideas are mine.