   Texture on .X file
  I guess I'm not understanding something here. When I compile I'm getting a black sphere, with no texture. Is there something I need to do after Locking, or am I not rendering correctly?IDirect3DDevice9*Device = 0; IDirect3DTexture9*    earth;IDirect3DVertexBuffer9* ppVB;IDirect3DIndexBuffer9* ppIB;const int Width  = 640;const int Height = 480;const int nRings = 20;const int nSegments = 20;// Store 1 objectsID3DXMesh* Objects[1] = {0};// World matrices for each object.  These matrices// specify the locations of the objects in the world.D3DXMATRIX ObjWorldMatrices[1];struct Vertex{Vertex() {}Vertex(float x, float y, float z){x = x; y = y; z = z;}float x, y, z;static const DWORD FVF;};const DWORD Vertex::FVF = D3DFVF_XYZ;//// Framework Functions//bool Setup(){D3DXCreateSphere(Device,6.0f, // radiusnRings,   // slicesnSegments,   // stacks&Objects[0],0);// set vertex count and index count DWORD dwVertices = ( nRings + 1 ) * ( nSegments + 1 ) ;DWORD dwIndices = 2 * nRings * ( nSegments + 1 ) ;// Create the vertex bufferif( FAILED ( Device->CreateVertexBuffer( dwVertices * sizeof (Vertex), 0, D3DFVF_XYZ,D3DPOOL_DEFAULT, &ppVB,0) ) )return E_FAIL;// Create the index bufferif( FAILED ( Device->CreateIndexBuffer( dwIndices * 2 , 0, D3DFMT_INDEX16,D3DPOOL_MANAGED, &ppIB,0) ) )return E_FAIL;// Lock the vertex bufferVertex* vertices;//BUMPSPHEREVERTEX * pVertex ; ppVB->Lock( 0, 0, (void**)&vertices, 0 );// lock the index buffer WORD * pIndices;ppIB->Lock( 0,dwIndices,(void**)&pIndices, 0 );//// Build world matrices - position the objects in world space.// For example, ObjWorldMatrices[1] will position Objects[1] at// (-5, 0, 5).  Likewise, ObjWorldMatrices[2] will position// Objects[2] at (5, 0, 5).//D3DXMatrixTranslation(&ObjWorldMatrices[0],  0.0f, 0.0f,  0.0f);//D3DXMatrixTranslation(&ObjWorldMatrices[1], -5.0f, 0.0f,  5.0f);//// Set the projection matrix.//D3DXMATRIX proj;D3DXMatrixPerspectiveFovLH(&proj,D3DX_PI * 0.5f, // 90 - degree(float)Width / (float)Height,1.0f,1000.0f);Device->SetRenderState(D3DRS_NORMALIZENORMALS, true);Device->SetRenderState(D3DRS_SPECULARENABLE, true);    Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);//d3dLight.Direction = D3DVECTOR3(-1.0, -1.0, 0.0);//Device->SetLight(0,&d3dLight);D3DXCreateTextureFromFile(Device, "earth.jpg", &earth);Device->SetTransform(D3DTS_PROJECTION, &proj);Device->SetTexture(0, earth);return true;}[Edited by - xhermit on June 8, 2006 12:08:53 PM]
  Made some changes, but now I'm getting just a white screen. Tried debugging through it, but couldn't find any problems. Also, I don't know how to format html for source code, so here's a dry cut/paste.#include "d3dUtility.h"//// Globals//IDirect3DDevice9*Device = 0; IDirect3DTexture9*    earth;IDirect3DVertexBuffer9* ppVB;IDirect3DIndexBuffer9* ppIB;const int Width  = 640;const int Height = 480;// Store 5 objects.ID3DXMesh* Objects[1] = {0};// World matrices for each object.  These matrices// specify the locations of the objects in the world.D3DXMATRIX ObjWorldMatrices[1];struct VERTEX{    D3DXVECTOR3 pos;// vertex position    D3DXVECTOR3 norm;    // vertex normal    float tu;  // texture coordinates    float tv;static const DWORD FVF;};const DWORD VERTEX::FVF = D3DFVF_XYZ;#define FVF_VERTEX    D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1//LPD3DXMESH CreateMappedSphere(LPDIRECT3DDEVICE8 pDev,float fRad,UINT slices,UINT stacks)//{    // create the sphere   // return pointer to caller   // return texMesh;//}bool Setup(){LPD3DXMESH mesh;    D3DXCreateSphere(Device,6.0f,20,20,&mesh,NULL);    // create a copy of the mesh with texture coordinates,    // since the D3DX function doesn't include them    LPD3DXMESH texMesh;    mesh-&gt;CloneMeshFVF(D3DXMESH_SYSTEMMEM,FVF_VERTEX,Device,&texMesh);   // failed, return un-textured mesh  // finished with the original mesh, release it    mesh-&gt;Release();    // lock the vertex buffer    VERTEX* pVerts;    texMesh-&gt;LockVertexBuffer(0,(void**) &pVerts);   // get vertex count   int numVerts=texMesh-&gt;GetNumVertices();   // loop through the vertices   for (int i=0;i&lt;numVerts;i++) {  // calculate texture coordinates  pVerts-&gt;tu=asinf(pVerts-&gt;norm.x)/D3DX_PI+0.5f;  pVerts-&gt;tv=asinf(pVerts-&gt;norm.y)/D3DX_PI+0.5f;    // go to next vertex  pVerts++;   }   // unlock the vertex buffer   texMesh-&gt;UnlockVertexBuffer();    //// Create the cube.////// Set a directional light.//D3DLIGHT9 light;::ZeroMemory(&light, sizeof(light));light.Type = D3DLIGHT_DIRECTIONAL;light.Ambient   = D3DXCOLOR(0.8f, 0.8f, 0.8f, 1.0f);light.Diffuse   = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);light.Specular  = D3DXCOLOR(0.2f, 0.2f, 0.2f, 1.0f);light.Direction = D3DXVECTOR3(1.0f, -1.0f, 0.0f);Device-&gt;SetLight(0, &light);Device-&gt;LightEnable(0, true);Device-&gt;SetRenderState(D3DRS_NORMALIZENORMALS, true);Device-&gt;SetRenderState(D3DRS_SPECULARENABLE, true);//// Create texture.//D3DXCreateTextureFromFile(Device, "earth.jpg", &earth);Device-&gt;SetTexture(0, earth);// // Set Texture Filter States.//Device-&gt;SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);Device-&gt;SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);    Device-&gt;SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);//// Set the projection matrix.//D3DXMATRIX proj;D3DXMatrixPerspectiveFovLH(&proj,D3DX_PI * 0.5f, // 90 - degree(float)Width / (float)Height,1.0f,1000.0f);Device-&gt;SetTransform(D3DTS_PROJECTION, &proj); Device-&gt;BeginScene();texMesh-&gt;DrawSubset(0);Device-&gt;EndScene();Device-&gt;Present(0, 0, 0, 0);return true;}void Cleanup(){//d3d::Delete&lt;Cube*&gt;(Box);d3d::Release&lt;IDirect3DTexture9*&gt;(earth);}bool Display(float timeDelta){if( Device ){// Animate the camera:// The camera will circle around the center of the scene.  We use the// sin and cos functions to generate points on the circle, then scale them// by 10 to further the radius.  In addition the camera will move up and down// as it circles about the scene.static float angle = (3.0f * D3DX_PI) / 2.0f;static float cameraHeight = 0.0f;static float cameraHeightDirection = 5.0f;D3DXVECTOR3 position( cosf(angle) * 10.0f, cameraHeight, sinf(angle) * 10.0f );// the camera is targetted at the origin of the worldD3DXVECTOR3 target(0.0f, 0.0f, 0.0f);// the worlds up vectorD3DXVECTOR3 up(0.0f, 1.0f, 0.0f);D3DXMATRIX V;D3DXMatrixLookAtLH(&V, &position, &target, &up);//Device-&gt;SetTransform(D3DTS_VIEW, &V);// compute the position for the next frameangle += timeDelta;if( angle &gt;= 6.28f )angle = 0.0f;// compute the height of the camera for the next framecameraHeight += cameraHeightDirection * timeDelta;if( cameraHeight &gt;= 10.0f )cameraHeightDirection = -5.0f;if( cameraHeight &lt;= -10.0f )cameraHeightDirection = 5.0f;//// Draw the Scene://Device-&gt;Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);  //for(int i = 0; i &lt; 1; i++)//{ //Set the world matrix that positions the object.//Device-&gt;SetTransform(D3DTS_WORLD, &proj);// Draw the object using the previously set world matrix.//texmesh-&gt;DrawSubset(0);//}}return true;}//// WndProc//LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){switch( msg ){case WM_DESTROY:::PostQuitMessage(0);break;case WM_KEYDOWN:if( wParam == VK_ESCAPE )::DestroyWindow(hwnd);break;}return ::DefWindowProc(hwnd, msg, wParam, lParam);}//// WinMain//int WINAPI WinMain(HINSTANCE hinstance,   HINSTANCE prevInstance,    PSTR cmdLine,   int showCmd){if(!d3d::InitD3D(hinstance,Width, Height, true, D3DDEVTYPE_HAL, &Device)){::MessageBox(0, "InitD3D() - FAILED", 0, 0);return 0;}if(!Setup()){::MessageBox(0, "Setup() - FAILED", 0, 0);return 0;}d3d::EnterMsgLoop( Display );Cleanup();Device-&gt;Release();return 0;} [Edited by - xhermit on June 5, 2006 7:19:49 AM];  Hey, since I am a nice guy, I will format your code for you.#include "d3dUtility.h"//// Globals//IDirect3DDevice9* Device = 0; IDirect3DTexture9* earth;IDirect3DVertexBuffer9* ppVB;IDirect3DIndexBuffer9* ppIB;const int Width = 640;const int Height = 480;// Store 5 objects.ID3DXMesh* Objects[1] = {0};// World matrices for each object. These matrices// specify the locations of the objects in the world.D3DXMATRIX ObjWorldMatrices[1];struct VERTEX{D3DXVECTOR3 pos; // vertex positionD3DXVECTOR3 norm; // vertex normalfloat tu; // texture coordinatesfloat tv;static const DWORD FVF;};const DWORD VERTEX::FVF = D3DFVF_XYZ;#define FVF_VERTEX D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1//LPD3DXMESH CreateMappedSphere(LPDIRECT3DDEVICE8 pDev,float fRad,UINT slices,UINT stacks)//{// create the sphere// return pointer to caller// return texMesh;//}bool Setup(){LPD3DXMESH mesh;D3DXCreateSphere(Device,6.0f,20,20,&mesh,NULL);// create a copy of the mesh with texture coordinates,// since the D3DX function doesn't include themLPD3DXMESH texMesh;mesh->CloneMeshFVF(D3DXMESH_SYSTEMMEM,FVF_VERTEX,Device,&texMesh);// failed, return un-textured mesh// finished with the original mesh, release itmesh->Release();// lock the vertex bufferVERTEX* pVerts;texMesh->LockVertexBuffer(0,(void**) &pVerts);// get vertex countint numVerts=texMesh->GetNumVertices();// loop through the verticesfor (int i=0;i<numVerts;i++) {// calculate texture coordinatespVerts->tu=asinf(pVerts->norm.x)/D3DX_PI+0.5f;pVerts->tv=asinf(pVerts->norm.y)/D3DX_PI+0.5f;// go to next vertexpVerts++;}// unlock the vertex buffertexMesh->UnlockVertexBuffer();//// Create the cube.////// Set a directional light.//D3DLIGHT9 light;::ZeroMemory(&light, sizeof(light));light.Type = D3DLIGHT_DIRECTIONAL;light.Ambient = D3DXCOLOR(0.8f, 0.8f, 0.8f, 1.0f);light.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);light.Specular = D3DXCOLOR(0.2f, 0.2f, 0.2f, 1.0f);light.Direction = D3DXVECTOR3(1.0f, -1.0f, 0.0f);Device->SetLight(0, &light);Device->LightEnable(0, true);Device->SetRenderState(D3DRS_NORMALIZENORMALS, true);Device->SetRenderState(D3DRS_SPECULARENABLE, true);//// Create texture.//D3DXCreateTextureFromFile(Device, "earth.jpg", &earth);Device->SetTexture(0, earth);// // Set Texture Filter States.//Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);//// Set the projection matrix.//D3DXMATRIX proj;D3DXMatrixPerspectiveFovLH(&proj,D3DX_PI * 0.5f, // 90 - degree(float)Width / (float)Height,1.0f,1000.0f);Device->SetTransform(D3DTS_PROJECTION, &proj);Device->BeginScene();texMesh->DrawSubset(0);Device->EndScene();Device->Present(0, 0, 0, 0);return true;}void Cleanup(){//d3d::Delete<Cube*>(Box);d3d::Release<IDirect3DTexture9*>(earth);}bool Display(float timeDelta){if( Device ){// Animate the camera:// The camera will circle around the center of the scene. We use the// sin and cos functions to generate points on the circle, then scale them// by 10 to further the radius. In addition the camera will move up and down// as it circles about the scene.static float angle = (3.0f * D3DX_PI) / 2.0f;static float cameraHeight = 0.0f;static float cameraHeightDirection = 5.0f;D3DXVECTOR3 position( cosf(angle) * 10.0f, cameraHeight, sinf(angle) * 10.0f );// the camera is targetted at the origin of the worldD3DXVECTOR3 target(0.0f, 0.0f, 0.0f);// the worlds up vectorD3DXVECTOR3 up(0.0f, 1.0f, 0.0f);D3DXMATRIX V;D3DXMatrixLookAtLH(&V, &position, &target, &up);//Device->SetTransform(D3DTS_VIEW, &V);// compute the position for the next frameangle += timeDelta;if( angle >= 6.28f )angle = 0.0f;// compute the height of the camera for the next framecameraHeight += cameraHeightDirection * timeDelta;if( cameraHeight >= 10.0f )cameraHeightDirection = -5.0f;if( cameraHeight <= -10.0f )cameraHeightDirection = 5.0f;//// Draw the Scene://Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);//for(int i = 0; i < 1; i++)//{//Set the world matrix that positions the object.//Device->SetTransform(D3DTS_WORLD, &proj);// Draw the object using the previously set world matrix.//texmesh->DrawSubset(0);//}}return true;}//// WndProc//LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){switch( msg ){case WM_DESTROY:::PostQuitMessage(0);break;case WM_KEYDOWN:if( wParam == VK_ESCAPE )::DestroyWindow(hwnd);break;}return ::DefWindowProc(hwnd, msg, wParam, lParam);}//// WinMain//int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE prevInstance, PSTR cmdLine,int showCmd){if(!d3d::InitD3D(hinstance,Width, Height, true, D3DDEVTYPE_HAL, &Device)){::MessageBox(0, "InitD3D() - FAILED", 0, 0);return 0;}if(!Setup()){::MessageBox(0, "Setup() - FAILED", 0, 0);return 0;}d3d::EnterMsgLoop( Display );Cleanup();Device->Release();return 0;}How did I do that?  Like this:[ source]code goes here [/ source]Just take the spaces out, and there you go!!Chad   ;  Still having trouble with this one. Is there something else I need to do with pVerts to correctly map the texture? Currently its not even showing a sphere or texture, just a white screen.Thanks   ;  I made some changes. I'm currently using a .X file for the sphere borrowed from Frank Luna's solar system example. Still getting a white screen, no sphere or texture ... although the application isn't using up 98% of the CPU this time. Please help, I think I almost got it!#include "d3dUtility.h"  // // Globals //  IDirect3DDevice9*  Device = 0;  IDirect3DTexture9* earth; ID3DXMesh*    sphere;const int Width  = 640; const int Height = 480;  // World matrices for each object.  These matrices // specify the locations of the objects in the world. D3DXMATRIX ObjWorldMatrices[1];   bool Setup() { HRESULT hr = 0;hr = D3DXLoadMeshFromX("sphere.x", D3DXMESH_MANAGED, Device, 0, 0, 0, 0, &sphere);if(FAILED(hr)){::MessageBox(0, "D3DXLoadMeshFromX() FAILED", 0, 0);return E_FAIL;}D3DXMatrixTranslation(&ObjWorldMatrices[0],  0.0f, 0.0f,  0.0f);  //  // Set the projection matrix.  //   D3DXMATRIX proj;  D3DXMatrixPerspectiveFovLH(    &proj,    D3DX_PI * 0.5f, // 90 - degree    (float)Width / (float)Height,    1.0f,    1000.0f);  Device->SetTransform(D3DTS_PROJECTION, &proj); //  // Set a directional light.  //   Device->SetRenderState(D3DRS_LIGHTING, true);  Device->SetRenderState(D3DRS_SPECULARENABLE, true);  Device->SetRenderState(D3DRS_NORMALIZENORMALS, true); //  // Create texture.  //   D3DXCreateTextureFromFile(Device, "earth.jpg", &earth);  Device->SetTexture(0, earth);  //   // Set Texture Filter States.  //   Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);  Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);  Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);  //Device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); return true; } void Cleanup() {  d3d::Release<IDirect3DTexture9*>(earth);   d3d::Release<ID3DXMesh*>(sphere);}  bool Display(float timeDelta) {  if( Device )  {   // Animate the camera:   // The camera will circle around the center of the scene.  We use the   // sin and cos functions to generate points on the circle, then scale them   // by 10 to further the radius.  In addition the camera will move up and down   // as it circles about the scene.   static float angle = (3.0f * D3DX_PI) / 2.0f;   static float cameraHeight = 0.0f;   static float cameraHeightDirection = 5.0f; D3DXVECTOR3 position( cosf(angle) * 10.0f, cameraHeight, sinf(angle) * 10.0f );    // the camera is targetted at the origin of the world   D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);    // the worlds up vector   D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);    D3DXMATRIX V;   D3DXMatrixLookAtLH(&V, &position, &target, &up);   // compute the position for the next frame   angle += timeDelta;   if( angle >= 6.28f )    angle = 0.0f;    // compute the height of the camera for the next frame   cameraHeight += cameraHeightDirection * timeDelta;   if( cameraHeight >= 10.0f )    cameraHeightDirection = -5.0f;    if( cameraHeight <= -10.0f )    cameraHeightDirection = 5.0f;    //   // Draw the Scene:   //    Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0); //Set the world matrix that positions the object.    Device->BeginScene(); Device->SetTexture(0, earth);Device->SetTransform(D3DTS_WORLD, &ObjWorldMatrices[0]);sphere->DrawSubset(0);   Device->EndScene();   Device->Present(0, 0, 0, 0);  }  return true; }  // // WndProc // LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {  switch( msg )  {  case WM_DESTROY:   ::PostQuitMessage(0);   break;case WM_KEYDOWN:   if( wParam == VK_ESCAPE )    ::DestroyWindow(hwnd);   break;  }  return ::DefWindowProc(hwnd, msg, wParam, lParam); }  // // WinMain // int WINAPI WinMain(HINSTANCE hinstance,   HINSTANCE prevInstance,    PSTR cmdLine,   int showCmd) {  if(!d3d::InitD3D(hinstance,   Width, Height, true, D3DDEVTYPE_HAL, &Device))  {   ::MessageBox(0, "InitD3D() - FAILED", 0, 0);   return 0;  }if(!Setup())  {   ::MessageBox(0, "Setup() - FAILED", 0, 0);   return 0;  }   d3d::EnterMsgLoop( Display );   Cleanup();   Device->Release();   return 0; }  [Edited by - xhermit on June 8, 2006 2:41:48 PM];  Fixed it! Had to do with the camera angle :(   