Jump to content
  • Advertisement

wjcustode

Member
  • Content Count

    5
  • Joined

  • Last visited

Everything posted by wjcustode

  1. Okay, so this is incredibly weird. I have been working on this non-stop for about three days now and I have found no solution. I am trying to generate terrain as a mesh. Everything works (well, I guess not everything), but when I call ID3DXMesh::DrawSubset it does not draw the triangles correctly. From what I have investigated, which is seemingly everything that could go wrong, here is what I've found: - The vertices are all correct, meaning they are no vertices listed that should not be - The index buffer is correct, in that every set of three indices make the correct triangle - No data is improperly mutated during runtime. I know this because, right before the render call I printed out every index and its corresponding vertex. - No calls are failing, returning a bad HRESULT, no errors in the debug console. It's so confusing. I've posted a picture below to illustrate the problem. It should be drawing a rectangle made of triangles, obviously. But as you can see the triangles are all weird. Also, here is the code I am using for creating the mesh. Bitmap* b = new Bitmap( ); b->loadFromFile( "Assets\\Sprites\\bumpMap.png" ); D3DXVECTOR4** colorArray = *( b->getBits( ) ); HRESULT hr = 0; uint smallWidth = 8; uint smallHeight = 8; //Setup the mesh and create it uint faceCount = 2 * ( smallWidth - 1 ) * ( smallHeight - 1 ); uint vertCount = smallWidth * smallHeight; mesh = NULL; fvf = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1; //FVF Mesh hr = D3DXCreateMeshFVF( faceCount, vertCount, 0, fvf, g_Engine->getDevice( ), &mesh ); g_Engine->getDevice()->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE ); //Fill the vertices with data LPDIRECT3DVERTEXBUFFER9 pVBuf; mesh->GetVertexBuffer( &pVBuf ); vert* pVert; pVBuf->Lock(0,0,(void **) &pVert,D3DLOCK_DISCARD); for( uint i = 0; i < smallHeight; i++ ) { for( uint j = 0; j < smallWidth; j++ ) { int index = ( i * smallWidth ) + j; memset( (void*)&pVert[ index ], 0, sizeof(vert) ); float color = colorArray[ i ][ j ].x + colorArray[ i ][ j ].y + colorArray[ i ][ j ].z + colorArray[ i ][ j ].w; pVert[ index ].x = (float)i; pVert[ index ].z = (float)j; pVert[ index ].y = (float)color / 255.0f; pVert[ index ].tu = ((float)i) / ((float)smallWidth); pVert[ index ].tv = ((float)j) / ((float)smallHeight); } } pVBuf->Unlock( ); pVBuf->Release( ); //Setup the index buffer uint* indices; int topleft = 0; int topright = 0; int bottomleft = 0; int bottomright = 0; mesh->LockIndexBuffer( 0, (void**)&indices ); for( uint i = 0; i < smallHeight - 1; i++ ) { for( uint j = 0; j < smallWidth - 1; j++ ) { int index = ( i * (smallWidth - 1) * 6 ) + ( j * 6 ); //Helpers topleft = ( i * smallWidth ) + j; topright = ( i * smallWidth ) + j + 1; bottomleft = ( ( i + 1 ) * smallWidth ) + j; bottomright = ( ( i + 1 ) * smallWidth ) + j + 1; indices[ index + 0 ] = topleft; indices[ index + 1 ] = topright; indices[ index + 2 ] = bottomleft; indices[ index + 3 ] = bottomright; indices[ index + 4 ] = bottomleft; indices[ index + 5 ] = topright; index = 0; } } mesh->UnlockIndexBuffer( ); //Blank the attribute buffer DWORD* att; mesh->LockAttributeBuffer( 0, &att ); memset( (void*)att, 0, faceCount); mesh->UnlockAttributeBuffer( ); //Generate Normals DWORD* adj = new DWORD[ faceCount * 3 ]; memset( ( void* )adj, 0, sizeof(DWORD) * faceCount * 3 ); hr = mesh->GenerateAdjacency( 1.0f, adj ); D3DXComputeNormals( mesh, adj ); hr = mesh->OptimizeInplace( D3DXMESHOPT_ATTRSORT, adj, NULL, NULL, NULL ); delete adj; [attachment=5310:triangles_02.png][attachment=5311:triangles_01.png]
  2. wjcustode

    Terrain Generation Gone Awry

    I hope this helps everyone who ever has this problem: - Although I didn't think I was using numbers that were out of the range of a WORD, when I created my mesh with the D3DMESH_32BIT option, everything came out correct. Oh how I wish I understood why that works.
  3. wjcustode

    ID3DXMesh::UnlockIndexBuffer Fails

    I fixed the problem, but now I have others. The solution was to switch to the reference device while I'm working on these other computers. Simple.
  4. To Anyone Who Can Help: I'm working on terrain generation following the following logic: - Create the ID3DXMesh that will hold the terrain - Load a heightmap from a file - Set the vertices - Set the indices - Blank the attribute table - Compute normals Everything works fine on my home computer, but when I'm at school (which is usually the culprit of most of my problems) the IDE returns an access violation on the call to ID3DXMesh::UnlockIndexBuffer. What's killing me about this is that, although I understand how critical sections work and that they can be corrupted and therefore cause this kind of problem, is that the code works! The call to UnlockIndexBuffer only crashes when I access data between the Lock and Unlock methods. If I don't call these methods at all, the next call to UnlockAttributeBuffer fails. If I don't call that, the call to render fails. Following this train of thought, one would presume the problem is before all of this. I switched to the reference device to make sure there are no compatibility issues. This is so frustrating. I appreciate any help. [color="#0000ff"][color="#0000ff"]void SAMPLESTATE::generateMesh( ) { [color="#008000"][color="#008000"]//Load Height Map g_Engine->createTexture( [color="#a31515"][color="#a31515"]"Assets\\Sprites\\bumpMap.png", [color="#a31515"][color="#a31515"]"texBump", D3DCOLOR_XRGB( 255, 0, 0 ) ); TEXTURE* texture = ((TEXTURE*)g_Engine->getResource( [color="#a31515"][color="#a31515"]"texBump" )); LPDIRECT3DTEXTURE9 tex = texture->getTextureObject( ); [color="#008000"][color="#008000"]//Get the bits D3DLOCKED_RECT thisRect; HRESULT hr = tex->LockRect( 0, &thisRect, NULL, 0 ); BYTE* bits = (BYTE*)thisRect.pBits; tex->UnlockRect( 0 ); [color="#008000"][color="#008000"]//Create the array of bits uint width = texture->getWidth( ); uint height = texture->getHeight( ); D3DXVECTOR4** colorArray = [color="#0000ff"][color="#0000ff"]new D3DXVECTOR4*[ height ]; [color="#0000ff"][color="#0000ff"]for( uint i = 0; i < width; i++ ) { colorArray[ i ] = [color="#0000ff"][color="#0000ff"]new D3DXVECTOR4[ width ]; } [color="#008000"][color="#008000"]//Fill the array with some data [color="#0000ff"][color="#0000ff"]for( uint i = 0; i < height; i++ ) { [color="#0000ff"][color="#0000ff"]for( uint j = 0; j < width; j++ ) { [color="#0000ff"][color="#0000ff"]int location = ( j * 4 ) + ( i * width * 4 ); [color="#008000"][color="#008000"]//RGBA Identification colorArray[ i ][ j ].x = bits[ location + 0 ]; colorArray[ i ][ j ].y = bits[ location + 1 ]; colorArray[ i ][ j ].z = bits[ location + 2 ]; colorArray[ i ][ j ].w = bits[ location + 3 ]; } } [color="#008000"][color="#008000"]//Size of the terrain uint smallWidth = 100; uint smallHeight = 100; [color="#008000"][color="#008000"]//Setup the mesh and create it vert** vertices = [color="#0000ff"][color="#0000ff"]new vert*[ smallHeight ]; [color="#0000ff"][color="#0000ff"]for( [color="#0000ff"][color="#0000ff"]int i = 0; i < smallHeight; i++ ) { vertices[ i ] = [color="#0000ff"][color="#0000ff"]new vert[ smallWidth ]; } memset( ( [color="#0000ff"][color="#0000ff"]void* )vertices, 0, [color="#0000ff"][color="#0000ff"]sizeof(vertices) * smallWidth * smallHeight ); uint faceCount = 2 * ( smallWidth - 1 ) * ( smallHeight - 1 ); uint vertCount = smallWidth * smallHeight; mesh = NULL; [color="#008000"][color="#008000"]//Declaration Mesh [color="#008000"][color="#008000"]//Create Vertex Declaration D3DVERTEXELEMENT9 decl[] = { {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0}, {0, 20, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0}, D3DDECL_END() }; hr = D3DXCreateMesh( faceCount, vertCount, 0, decl, g_Engine->getDevice( ), &mesh ); [color="#008000"][color="#008000"]//g_Engine->getDevice()->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE ); [color="#008000"][color="#008000"]//Fill the vertices with data LPDIRECT3DVERTEXBUFFER9 pVBuf; mesh->GetVertexBuffer( &pVBuf ); vert* pVert; hr = pVBuf->Lock(0,0,([color="#0000ff"][color="#0000ff"]void **) &pVert,D3DLOCK_DISCARD); [color="#0000ff"][color="#0000ff"]for( [color="#0000ff"][color="#0000ff"]int i = 0; i < smallHeight; i++ ) { [color="#0000ff"][color="#0000ff"]for( [color="#0000ff"][color="#0000ff"]int j = 0; j < smallWidth; j++ ) { [color="#0000ff"][color="#0000ff"]int index = ( i * smallWidth ) + j; memset( ([color="#0000ff"][color="#0000ff"]void*)&pVert[ index ], 0, [color="#0000ff"][color="#0000ff"]sizeof(vert) ); [color="#0000ff"][color="#0000ff"]int color = colorArray[ i ][ j ].x + colorArray[ i ][ j ].y + colorArray[ i ][ j ].z + colorArray[ i ][ j ].w; pVert[ index ].x = i; pVert[ index ].z = j; [color="#008000"][color="#008000"]//pVert[ index ].y = (float)color / 255.0f; pVert[ index ].y = 0.0f; pVert[ index ].tu = (([color="#0000ff"][color="#0000ff"]float)i) / (([color="#0000ff"][color="#0000ff"]float)smallWidth); pVert[ index ].tv = (([color="#0000ff"][color="#0000ff"]float)j) / (([color="#0000ff"][color="#0000ff"]float)smallHeight); } } hr = pVBuf->Unlock( ); [color="#008000"][color="#008000"]//Setup the index buffer uint* indices; [color="#0000ff"][color="#0000ff"]int topleft = 0; [color="#0000ff"][color="#0000ff"]int topright = 0; [color="#0000ff"][color="#0000ff"]int bottomleft = 0; [color="#0000ff"][color="#0000ff"]int bottomright = 0; mesh->LockIndexBuffer( 0, ([color="#0000ff"][color="#0000ff"]void**)&indices ); [color="#0000ff"][color="#0000ff"]for( [color="#0000ff"][color="#0000ff"]int i = 0; i < smallHeight - 1; i++ ) { [color="#0000ff"][color="#0000ff"]for( [color="#0000ff"][color="#0000ff"]int j = 0; j < smallWidth - 1; j++ ) { [color="#0000ff"][color="#0000ff"]int index = ( i * (smallWidth - 1) * 6 ) + ( j * 6 ); [color="#008000"][color="#008000"]//Helpers topleft = ( i * smallWidth ) + j; topright = ( i * smallWidth ) + j + 1; bottomleft = ( ( i + 1 ) * smallWidth ) + j; bottomright = ( ( i + 1 ) * smallWidth ) + j + 1; indices[ index + 0 ] = topleft; indices[ index + 1 ] = topright; indices[ index + 2 ] = bottomleft; indices[ index + 3 ] = bottomright; indices[ index + 4 ] = bottomleft; indices[ index + 5 ] = topright; index = 0; } } mesh->UnlockIndexBuffer( ); [color="#008000"][color="#008000"]//Blank the attribute buffer DWORD* att; mesh->LockAttributeBuffer( 0, &att ); memset( ([color="#0000ff"][color="#0000ff"]void*)att, 0, faceCount); mesh->UnlockAttributeBuffer( ); [color="#008000"][color="#008000"]//Generate Normals DWORD* adj = [color="#0000ff"][color="#0000ff"]new DWORD[ faceCount * 3 ]; mesh->GenerateAdjacency( 0.001f, adj ); D3DXComputeNormals( mesh, adj ); }
  5. I'm not familiar with the DX11 API but, if it's font API works like most others, if you want to use non-standard characters, IE Unicode, you have to use a w_char* instead of a char* because you won't be able to index nonstandard characters with a char*.
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!