   Passing parameters to the Mesh Object
  Hey all, so recently i had been working on writing a loader for OBJ files.  I got to the point where i was sure it was working through debugging but unfortunately there seems to be a problem with using the mesh to draw it properly.  The mesh was working properly before when i had manually entered indice and vertex values.  So to make sure it was the mesh object i went back to a much earlier version of some code, long before i added texture application and the mesh object.  Here is the code i have now that works just fine as long as my topology is set to triangle list since it's such a small box.  If anyone has some reasons that adding a texture or mesh would be a problem off the top of your head it would be much appreciated.  Might save me a few hours =p#include <windows.h>#include <tchar.h>#include "resource.h"#include <iostream>#include <sstream>#include <fstream>#include <string>#include <d3d10.h>#include <d3dx10.h>// GlobalsHINSTANCE hInst;HWND mainhWnd;int  windowWidth  = 640;int  windowHeight = 480;// Direct3D global varsID3D10Device* pD3DDevice = NULL;IDXGISwapChain*    pSwapChain = NULL;ID3D10RenderTargetView* pRenderTargetView = NULL;// World MatrixD3DXMATRIX   WorldMatrix;// View MatrixD3DXMATRIX   ViewMatrix;// Projection MatrixD3DXMATRIX   ProjectionMatrix;ID3D10EffectMatrixVariable* pProjectionMatrixVariable = NULL;// Triangle Varsstruct VertexPosColorStruct{    D3DXVECTOR3 Pos;D3DXVECTOR4 Color;};typedef struct{    ID3D10Effect* pEffect;    ID3D10EffectTechnique*  pTechnique;    // Vertex information    ID3D10Buffer* pVertexBuffer;UINTnumVertices;UINTnumIndices;    ID3D10InputLayout* pVertexLayout;ID3D10Buffer*pIndexBuffer;}ModelObject;LPCWSTR effectFilename = L"..\\simple.fx";LPCSTR effectTechniqueName = "Render";// The single model objectModelObject gModelObject = {0};// Forward declarationsbool InitWindow(HINSTANCE hInstance, int width, int height);LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);// Direct3D functionsbool InitDirect3D(HWND hWnd, int width, int height);bool importModelData(struct VertexPosColorStruct *pVertices, DWORD *pIndices);void ShutdownDirect3D();void Render();bool InitTriangle(ModelObject *modelObject);void PrepareModelObject(const ModelObject *modelObject);void DrawModelObject(const ModelObject *modelObject);int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow){// save off the instance handlehInst = hInstance;// Set up the application windowif (!InitWindow(hInstance, windowWidth, windowHeight)){return 0;}    // Initialize Direct3Dif (!InitDirect3D(mainhWnd, windowWidth, windowHeight)){return 0;}if (!InitTriangle(&gModelObject)){return 0;}// Main message loop    MSG msg = {0};    while (WM_QUIT != msg.message)    {// Process Windows messages first   while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) == TRUE)   {  TranslateMessage(&msg);  DispatchMessage(&msg);   }    Render();}// Clean up the resources we allocatedShutdownDirect3D();return (int) msg.wParam;}/******************************************************************** InitWindow* Inits and creates and main app window* Inputs - application instance - HINSTANCE   Window width - int   Window height - int* Outputs - true if successful, false if failed - bool*******************************************************************/bool InitWindow(HINSTANCE hInstance, int width, int height){    // Register class    WNDCLASSEX wcex;    wcex.cbSize = sizeof(WNDCLASSEX);wcex.style= CS_HREDRAW | CS_VREDRAW;    wcex.lpfnWndProc    = WndProc;    wcex.cbClsExtra= 0;    wcex.cbWndExtra= 0;    wcex.hInstance = hInstance;    wcex.hIcon= 0;    wcex.hCursor   = LoadCursor(NULL, IDC_ARROW);    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);    wcex.lpszMenuName   = NULL;    wcex.lpszClassName  = TEXT("DirectXExample");    wcex.hIconSm   = 0;    if(!RegisterClassEx(&wcex)){   return false;}    // Create window    RECT rect = { 0, 0, width, height };    AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, false);// create the window from the class above    mainhWnd = CreateWindow(TEXT("DirectXExample"), TEXT("DirectXExample"), WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, hInstance, NULL);    if(!mainhWnd){   return false;}    ShowWindow(mainhWnd, SW_SHOW);UpdateWindow(mainhWnd);  return true;}/******************************************************************** WndProc* The main window procedure for the application* Inputs - application window handle - HWND   message sent to the window - UINT   wParam of the message being sent - WPARAM   lParam of the message being sent - LPARAM* Outputs - LRESULT*******************************************************************/LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){    switch (message){// Allow the user to press the escape key to end the application   case WM_KEYDOWN:switch(wParam){// Check if the user hit the escape keycase VK_ESCAPE:PostQuitMessage(0);break;}   break;// The user hit the close button, close the application   case WM_DESTROY:  PostQuitMessage(0);  break;    }    return DefWindowProc(hWnd, message, wParam, lParam);}/******************************************************************** InitDirect3D* Initializes Direct3D* Inputs - Parent window handle - HWND,   Window width - int   Window height - int* Outputs - true if successful, false if failed - bool*******************************************************************/bool InitDirect3D(HWND hWnd, int width, int height){// Create the clear the DXGI_SWAP_CHAIN_DESC structureDXGI_SWAP_CHAIN_DESC swapChainDesc;    ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));// Fill in the needed values    swapChainDesc.BufferCount = 1;    swapChainDesc.BufferDesc.Width = width;    swapChainDesc.BufferDesc.Height = height;    swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;    swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;    swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;    swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;    swapChainDesc.OutputWindow = hWnd;    swapChainDesc.SampleDesc.Count = 1;    swapChainDesc.SampleDesc.Quality = 0;    swapChainDesc.Windowed = TRUE;// Create the D3D device and the swap chain    HRESULT hr = D3D10CreateDeviceAndSwapChain(NULL,D3D10_DRIVER_TYPE_HARDWARE, NULL, 0,D3D10_SDK_VERSION, &swapChainDesc,&pSwapChain, &pD3DDevice);// Error checking. Make sure the device was created    if (FAILED(hr)){   MessageBox(hWnd, TEXT("A DX10 Compliant Video Card is Required"), TEXT("ERROR"), MB_OK);return false;}    // Get the back buffer from the swapchain    ID3D10Texture2D *pBackBuffer;    hr = pSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&pBackBuffer);    if (FAILED(hr)){   return false;}// create the render target view    hr = pD3DDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRenderTargetView);// release the back buffer    pBackBuffer->Release();// Make sure the render target view was created successfullyif (FAILED(hr)){   return false;}// set the render target    pD3DDevice->OMSetRenderTargets(1, &pRenderTargetView, NULL);    // create and set the viewport    D3D10_VIEWPORT viewPort;    viewPort.Width = width;    viewPort.Height = height;    viewPort.MinDepth = 0.0f;    viewPort.MaxDepth = 1.0f;    viewPort.TopLeftX = 0;    viewPort.TopLeftY = 0;    pD3DDevice->RSSetViewports(1, &viewPort);// Set up the World MatrixD3DXMatrixIdentity(&WorldMatrix);D3DXMatrixLookAtLH(&ViewMatrix, new D3DXVECTOR3(0.0f, 10.0f, -20.0f), new D3DXVECTOR3(0.0f, 0.0f, 0.0f), new D3DXVECTOR3(0.0f, 1.0f, 0.0f));// Set up the projection matrix    D3DXMatrixPerspectiveFovLH(&ProjectionMatrix, (float)D3DX_PI * 0.5f, (float)width/(float)height, 0.1f, 100.0f);return true;}/******************************************************************** ShutdownDirect3D* Closes down and releases the resources for Direct3D* Inputs - void* Outputs - void*******************************************************************/void ShutdownDirect3D(){// release the effect objectif (gModelObject.pEffect){gModelObject.pEffect->Release();}// release the vertex bufferif (gModelObject.pVertexBuffer){gModelObject.pVertexBuffer->Release();}// Release the vertex layoutif (gModelObject.pVertexLayout){gModelObject.pVertexLayout->Release();}// release the rendertargetif (pRenderTargetView) {pRenderTargetView->Release();}// release the swapchain    if (pSwapChain){pSwapChain->Release();}// release the D3D Device    if (pD3DDevice) {pD3DDevice->Release();}}/******************************************************************** Render* All drawing happens in the Render function* Inputs - void* Outputs - void*******************************************************************/void Render(){if (pD3DDevice != NULL){// clear the target bufferpD3DDevice->ClearRenderTargetView(pRenderTargetView, D3DXCOLOR(1.0f, 1.0f, 1.0f, 0.0f));// Prepare the pipeline to draw the model objectPrepareModelObject(&gModelObject);// Draw a single model objectDrawModelObject(&gModelObject);// display the next item in the swap chainpSwapChain->Present(0, 0);}}/******************************************************************** InitTriangle* Initializes a ModelObject struct with the data to draw a single*triangle* Inputs - ModelObject *modelObject - the modelObject struct to fill* Outputs - bool - true if successful*******************************************************************/bool InitTriangle(ModelObject *modelObject){    // Create the effectHRESULT hr = D3DX10CreateEffectFromFile(effectFilename, NULL, NULL, "fx_4_0",D3D10_SHADER_ENABLE_STRICTNESS, 0, pD3DDevice, NULL, NULL, &modelObject->pEffect, NULL,NULL);    if (FAILED(hr))    {   return false;    }// Obtain the techniquemodelObject->pTechnique = modelObject->pEffect->GetTechniqueByName(effectTechniqueName);// Bind the projection matrix to the Projection variable in the shaderpProjectionMatrixVariable = modelObject->pEffect->GetVariableByName("Projection")->AsMatrix();    // The vertex input layout    D3D10_INPUT_ELEMENT_DESC layout[] =    {   { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },  { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 },};// Calculate the number of elements in the layout array    UINT numElements = (sizeof(layout) / sizeof(layout[0]));    // Create the vertex input layout    D3D10_PASS_DESC PassDesc;    modelObject->pTechnique->GetPassByIndex(0)->GetDesc(&PassDesc);    hr = pD3DDevice->CreateInputLayout(layout, numElements, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &modelObject->pVertexLayout);    if(FAILED(hr)){   return false;}    // Create vertex buffer    VertexPosColorStruct vertices[8];//create Index bufferDWORD indices[36];importModelData(vertices, indices);for (int x = 0; x < 36; x++){indices[x] = indices[x] - 1;}// Calculate the number of vertices in the arraymodelObject->numVertices = sizeof(vertices) / sizeof(VertexPosColorStruct);//Calculate he number of indices in the arraymodelObject->numIndices = sizeof(indices) / sizeof(DWORD);//Set vertex buffer dataD3D10_BUFFER_DESC bd;    bd.Usage = D3D10_USAGE_DEFAULT;bd.ByteWidth = sizeof(VertexPosColorStruct) * modelObject->numVertices;    bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;    bd.CPUAccessFlags = 0;    bd.MiscFlags = 0;    D3D10_SUBRESOURCE_DATA InitData;    InitData.pSysMem = vertices;// Do the creation of the actual vertex buffer    hr = pD3DDevice->CreateBuffer(&bd, &InitData, &modelObject->pVertexBuffer);    if(FAILED(hr)){   return false;}bd.Usage = D3D10_USAGE_DEFAULT;    bd.ByteWidth = sizeof(DWORD) * modelObject->numIndices;    bd.BindFlags = D3D10_BIND_INDEX_BUFFER;    bd.CPUAccessFlags = 0;    bd.MiscFlags = 0;    InitData.pSysMem = indices;   // Create the index bufferhr = pD3DDevice->CreateBuffer(&bd, &InitData, &modelObject->pIndexBuffer);    if (FAILED(hr)){   return false;}return true;}/******************************************************************** PrepareModelObject* All drawing happens in the Render function* Inputs - const ModelObject *modelObject - the ModelObject to *draw* Outputs - void*******************************************************************/void PrepareModelObject(const ModelObject *modelObject){static float rotationAngle = 0.0f;// create the rotation matrix using the rotation angle    D3DXMatrixRotationY(&WorldMatrix, rotationAngle);    rotationAngle += (float)D3DX_PI * 0.00002125f;    // Set the input layout    pD3DDevice->IASetInputLayout(modelObject->pVertexLayout);    // Set vertex buffer    UINT stride = sizeof(VertexPosColorStruct);    UINT offset = 0;    pD3DDevice->IASetVertexBuffers(0, 1, &modelObject->pVertexBuffer, &stride, &offset);// Set index bufferpD3DDevice->IASetIndexBuffer(modelObject->pIndexBuffer, DXGI_FORMAT_R32_UINT, 0 );    // Set primitive topologypD3DDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);// Combine and send the final matrix to the shaderD3DXMATRIX finalMatrix = (WorldMatrix * ViewMatrix * ProjectionMatrix);    pProjectionMatrixVariable->SetMatrix((float*)&finalMatrix);}/******************************************************************** DrawModelObject* Draws a single model object* Inputs - const ModelObject *modelObject* Outputs - void*******************************************************************/void DrawModelObject(const ModelObject *modelObject){// make sure modelObject is validif (modelObject == NULL){return;}    // Render a model object    D3D10_TECHNIQUE_DESC techniqueDescription;    modelObject->pTechnique->GetDesc(&techniqueDescription);// Loop through the technique passes    for(UINT p=0; p < techniqueDescription.Passes; ++p)    {   modelObject->pTechnique->GetPassByIndex(p)->Apply(0);// draw the cube using all 36 vertices and 12 trianglespD3DDevice->DrawIndexed(modelObject->numIndices, 0, 0 );    }}bool importModelData(VertexPosColorStruct *pVertices, DWORD *pIndices){using namespace std;FILE *pFile;char *mode = "r";char objFile[] = "D:\\Final\\commonCode\\TheBox.obj";pFile = fopen(objFile, mode);if (!pFile){return false;}ifstream fs( pFile );int numVertex = 0;int numIndice = 0;unsigned lineAt = 0;while ( fs ){++lineAt;string line;getline(fs, line);  //get line of textstringstream ss;ss << line;  //tokenize the linestring type;ss >> type;  //firt token identifies type of dataif ( type == "v" ) //vertex coordinates, next 3 values should be floats{ss >> pVertices[numVertex].Pos.x >> pVertices[numVertex].Pos.y >> pVertices[numVertex].Pos.z;++numVertex;}else if (type == "f" ){ss >> pIndices[numIndice] >> pIndices[numIndice + 1] >> pIndices[numIndice + 2];numIndice += 3;}}}