Guest User

Untitled

a guest
Oct 18th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.81 KB | None | 0 0
  1. //***********************************************************
  2.  
  3. // Model_obj.cpp
  4.  
  5. //***********************************************************
  6.  
  7. #include "Model_obj.h"
  8.  
  9. #include "io.h" // _filelength
  10.  
  11.  
  12.  
  13.  
  14.  
  15. // D E F I N E S ///////////////////////////////////////////////////
  16.  
  17.  
  18.  
  19.  
  20.  
  21. // P R O T O T Y P E S /////////////////////////////////////////////
  22.  
  23.  
  24.  
  25.  
  26.  
  27. Modelobj::~Modelobj(void){
  28.  
  29.  
  30.  
  31. } // destructor
  32.  
  33. /*----------------------------------------------------------------*/
  34.  
  35.  
  36.  
  37. Modelobj::Modelobj(void){
  38.  
  39. m_pDevice = NULL;
  40.  
  41. m_pModel = NULL;
  42.  
  43. m_pChild = NULL;
  44.  
  45. m_uiAnzModels = 0;
  46.  
  47. m_nIDstaticbuffer = 0;
  48.  
  49. m_pLog = NULL;
  50.  
  51.  
  52.  
  53. m_pEffect = NULL;
  54.  
  55. m_pTechniqueRender = NULL;
  56.  
  57. m_pTechniqueRenderLight = NULL;
  58.  
  59. m_pLightDirVariable = NULL;
  60.  
  61. m_pLightColorVariable = NULL;
  62.  
  63. m_pOutputColorVariable = NULL;
  64.  
  65. m_SkinID = 0;
  66.  
  67. }
  68.  
  69. // constructor
  70.  
  71. /*----------------------------------------------------------------*/
  72.  
  73.  
  74.  
  75. /**
  76.  
  77. * load obj file format
  78.  
  79. * -> IN: wchar_t - filename
  80.  
  81. *
  82.  
  83. */
  84.  
  85. HRESULT Modelobj::Init(LPRenderDevice pDevice, wchar_t *wcFilename, wchar_t *wcGPUprg, UINT nSkinID, FILE *pLog) {
  86.  
  87. HRESULT hr = _FAIL;
  88.  
  89. FILE *pFile=NULL;
  90.  
  91. UINT uiFaceNumber, uiNormalNumber, uiTexCoordsNumber, uiVertexNumber;
  92.  
  93. FaceType *pFaces=NULL;
  94.  
  95. VertexType *pVertices=NULL;
  96.  
  97. NormalType *pNormals=NULL;
  98.  
  99. TexcoordType *pTexcoords=NULL;
  100.  
  101.  
  102.  
  103. uiFaceNumber=uiNormalNumber=uiTexCoordsNumber=uiVertexNumber=0;
  104.  
  105.  
  106.  
  107. // Logging messages
  108.  
  109. m_pLog = pLog;
  110.  
  111. m_pDevice = pDevice;
  112.  
  113.  
  114.  
  115. // start shader things
  116.  
  117. m_pEffect = m_pDevice->CompileHLSLShader(wcGPUprg);
  118.  
  119. if(!m_pEffect) return _FAIL;
  120.  
  121. // Obtain the technique
  122.  
  123. m_pTechniqueRender = m_pEffect->GetTechniqueByName( "Render" );
  124.  
  125. if(!m_pTechniqueRender->IsValid())
  126.  
  127. return _FAIL;
  128.  
  129. m_pTechniqueRenderLight = m_pEffect->GetTechniqueByName( "RenderLight" );
  130.  
  131. if(!m_pTechniqueRenderLight->IsValid())
  132.  
  133. return _FAIL;
  134.  
  135. // Obtain the variables
  136.  
  137. //
  138.  
  139. m_pDevice->SetShaderWorld(m_pEffect->GetVariableByName( "World" )->AsMatrix());
  140.  
  141. m_pDevice->SetShaderView(m_pEffect->GetVariableByName( "View" )->AsMatrix());
  142.  
  143. m_pDevice->SetShaderProj(m_pEffect->GetVariableByName( "Projection" )->AsMatrix());
  144.  
  145. //
  146.  
  147. m_pLightDirVariable = m_pEffect->GetVariableByName( "vLightDir" )->AsVector();
  148.  
  149. m_pLightColorVariable = m_pEffect->GetVariableByName( "vLightColor" )->AsVector();
  150.  
  151. m_pOutputColorVariable = m_pEffect->GetVariableByName( "vOutputColor" )->AsVector();
  152.  
  153.  
  154.  
  155. hr = m_pDevice->SetInputLayout(VID_UU, m_pTechniqueRender);
  156.  
  157. if(FAILED(hr)) {
  158.  
  159. Log("SetInputLayout refuses to work");
  160.  
  161. return _FAIL;
  162.  
  163. }
  164.  
  165.  
  166.  
  167. uiFaceNumber = GetPrimitiveNumber(wcFilename, FACE);
  168.  
  169. uiNormalNumber = GetPrimitiveNumber(wcFilename, NORMAL);
  170.  
  171. uiTexCoordsNumber = GetPrimitiveNumber(wcFilename, TEXTURECOORD);
  172.  
  173. uiVertexNumber = GetPrimitiveNumber(wcFilename, VERT);
  174.  
  175.  
  176.  
  177. pFaces = (FaceType*)malloc(sizeof(FaceType)*uiFaceNumber);
  178.  
  179. if(!pFaces)
  180.  
  181. return _FAIL;
  182.  
  183. pVertices = (VertexType*)malloc(sizeof(VertexType)*uiVertexNumber);
  184.  
  185. if(!pVertices)
  186.  
  187. return _FAIL;
  188.  
  189. pNormals = (NormalType*)malloc(sizeof(NormalType)*uiNormalNumber);
  190.  
  191. if(!pNormals)
  192.  
  193. return _FAIL;
  194.  
  195. pTexcoords = (TexcoordType*)malloc(sizeof(TexcoordType)*uiTexCoordsNumber);
  196.  
  197. if(!pTexcoords)
  198.  
  199. return _FAIL;
  200.  
  201. //load all vertices, facedefinitions, normals and texture coords from file
  202.  
  203. hr = LoadPrimitive(pVertices, VERT, uiVertexNumber, wcFilename);
  204.  
  205. if(pFaces) free(pFaces);
  206.  
  207. if(pVertices) free(pVertices);
  208.  
  209. if(pNormals) free(pNormals);
  210.  
  211. if(pTexcoords) free(pTexcoords);
  212.  
  213. return hr;
  214.  
  215. }// Init
  216.  
  217. /*----------------------------------------------------------------*/
  218.  
  219.  
  220.  
  221. /*
  222.  
  223. * loads data from obj file
  224.  
  225. * In: VertexType* := will store data here
  226.  
  227. * Primitive := type of data
  228.  
  229. * UINT := amount of data
  230.  
  231. */
  232.  
  233. HRESULT Modelobj::LoadPrimitive(VertexType* pVertices, Primitive nIDprim, UINT count, wchar_t *wcFilename) {
  234.  
  235. char* cpBuffer=NULL;
  236.  
  237. char cNumberx[20]; ZeroMemory(&cNumberx, 20*sizeof(char));
  238.  
  239. char cNumbery[20]; ZeroMemory(&cNumbery, 20*sizeof(char));
  240.  
  241. char cNumberz[20]; ZeroMemory(&cNumberz, 20*sizeof(char));
  242.  
  243. UINT uiQuelle=0; UINT uiZiel=0; UINT uiVertcount=0;
  244.  
  245. FILE *pFile=NULL;
  246.  
  247. UINT test=0;
  248.  
  249. if( (pFile = _wfopen(wcFilename, L"r")) == NULL)
  250.  
  251. return _FILENOTFOUND;
  252.  
  253.  
  254.  
  255. do {
  256.  
  257. // search for the first vertice data
  258.  
  259. // get a row
  260.  
  261. cpBuffer = GetRow(pFile);
  262.  
  263.  
  264.  
  265. //if (!cpBuffer) {
  266.  
  267. // return _OK; // get out of function if we reached file end
  268.  
  269. //}
  270.  
  271.  
  272.  
  273. // we are looking for vertices
  274.  
  275. if(nIDprim==VERT) {
  276.  
  277. // see if it is a vertex row
  278.  
  279. if(cpBuffer!=NULL && cpBuffer[0]=='v' && cpBuffer[1]==' ') {
  280.  
  281. // we got the vertex row
  282.  
  283. // now get the data out
  284.  
  285. // and write it as numerical data to buffer pVertices
  286.  
  287.  
  288.  
  289. // find the blank between v and the data
  290.  
  291. do {
  292.  
  293. cNumberx[0]=cpBuffer[uiQuelle++];
  294.  
  295. } while(cNumberx[0]!=' ');
  296.  
  297. uiQuelle++; // move to first digit/minus of 1. number
  298.  
  299. // copy 1. number
  300.  
  301. do {
  302.  
  303. cNumberx[uiZiel++]=cpBuffer[uiQuelle++];
  304.  
  305. } while(cpBuffer[uiQuelle]!=' ');
  306.  
  307. cNumberx[uiZiel] = 0x0a; // put a carriage return to the end of cNumber
  308.  
  309. uiQuelle++; // move to first digit/minus of 2. number
  310.  
  311. uiZiel=0; // reset cNumber buffer
  312.  
  313. // copy 2. number
  314.  
  315. do {
  316.  
  317. cNumbery[uiZiel++]=cpBuffer[uiQuelle++];
  318.  
  319. } while(cpBuffer[uiQuelle]!=' ');
  320.  
  321. cNumbery[uiZiel] = 0x0a;
  322.  
  323. uiQuelle++; // move to first digit/minus of 3. number
  324.  
  325. uiZiel=0; // reset cNumber buffer
  326.  
  327. do {
  328.  
  329. cNumberz[uiZiel++]=cpBuffer[uiQuelle++];
  330.  
  331. } while(cpBuffer[uiQuelle]!=0x0a); // till end of row
  332.  
  333. cNumberz[uiZiel] = 0x0a; // put a carriage return to the end of cNumber
  334.  
  335. uiQuelle=uiZiel=0; // reset buffer index
  336.  
  337. pVertices[uiVertcount].x = (float)atof(cNumberx);
  338.  
  339. pVertices[uiVertcount].y = (float)atof(cNumbery);
  340.  
  341. pVertices[uiVertcount++].z = (float)atof(cNumberz);
  342.  
  343. } // if cpBuffer
  344.  
  345. } // if nIDprim
  346.  
  347.  
  348.  
  349. } while(cpBuffer!=NULL);
  350.  
  351. if(pFile) fclose(pFile);
  352.  
  353. return _OK;
  354.  
  355. }// Init
  356.  
  357. /*----------------------------------------------------------------*/
  358.  
  359.  
  360.  
  361. // returns number of primitve types from obj file
  362.  
  363. // in: Filename of obj, primitive identification
  364.  
  365. // out: number of primitive
  366.  
  367. UINT Modelobj::GetPrimitiveNumber(wchar_t *wcFilename, Primitive nIDprim){
  368.  
  369. FILE *pFile=NULL;
  370.  
  371. char* cpBuffer=NULL;
  372.  
  373. UINT count=0;
  374.  
  375.  
  376.  
  377.  
  378.  
  379. if( (pFile = _wfopen(wcFilename, L"r")) == NULL)
  380.  
  381. return _FILENOTFOUND;
  382.  
  383. do {
  384.  
  385. cpBuffer = GetRow(pFile);
  386.  
  387. if(nIDprim==FACE) {
  388.  
  389. if(cpBuffer!=NULL && cpBuffer[0]=='f' && cpBuffer[1]==' ')
  390.  
  391. count++;
  392.  
  393. }
  394.  
  395. if(nIDprim==VERT) {
  396.  
  397. if(cpBuffer!=NULL && cpBuffer[0]=='v' && cpBuffer[1]==' ')
  398.  
  399. count++;
  400.  
  401. }
  402.  
  403. if(nIDprim==NORMAL) {
  404.  
  405. if(cpBuffer!=NULL && cpBuffer[0]=='v' && cpBuffer[1]=='n')
  406.  
  407. count++;
  408.  
  409. }
  410.  
  411. if(nIDprim==TEXTURECOORD) {
  412.  
  413. if(cpBuffer!=NULL && cpBuffer[0]=='v' && cpBuffer[1]=='t')
  414.  
  415. count++;
  416.  
  417. }
  418.  
  419. } while(cpBuffer!=NULL);
  420.  
  421.  
  422.  
  423.  
  424.  
  425. if(pFile) fclose(pFile);
  426.  
  427. return count;
  428.  
  429. }// GetPrimitiveNumber
  430.  
  431. /*----------------------------------------------------------------*/
  432.  
  433.  
  434.  
  435. // reads one row of a char textfile
  436.  
  437. // till it encounters a linefeed 0x0a or EOF
  438.  
  439. // in: filepointer of data
  440.  
  441. // out: buffer pointer to row or NULL for EOF
  442.  
  443. char* Modelobj::GetRow(FILE *pFile){
  444.  
  445. char buffer[512];
  446.  
  447. char c[2], *t=NULL;
  448.  
  449.  
  450.  
  451.  
  452.  
  453. int i=0;
  454.  
  455. //ließt Zeichenweise
  456.  
  457. while(t=fgets(c, 2, pFile)) {
  458.  
  459. if(c[0]==0x0a)
  460.  
  461. break;
  462.  
  463. buffer[i++]=c[0];
  464.  
  465.  
  466.  
  467. }
  468.  
  469. if(t==NULL)
  470.  
  471. return NULL;
  472.  
  473. buffer[i]=0x0a;
  474.  
  475. return buffer;
  476.  
  477. } // GetRow
  478.  
  479. /*----------------------------------------------------------------*/
  480.  
  481.  
  482.  
  483. // reads one row of a char textfile
  484.  
  485. // till it encounters a linefeed 0x0a or EOF
  486.  
  487. // in: filepointer of data
  488.  
  489. // out: buffer pointer to row or NULL for EOF
  490.  
  491. //char* Modelobj::GetNumber(char *pcBuffer) {
  492.  
  493. //} // GetNumber
  494.  
  495. /*----------------------------------------------------------------*/
  496.  
  497.  
  498.  
  499. // Renders Modell with given Technique
  500.  
  501. // in: pTechnique
  502.  
  503. // out: void
  504.  
  505. void Modelobj::Render(ID3D10EffectTechnique* pTechnique) {
  506.  
  507. D3D10_TECHNIQUE_DESC techDesc;
  508.  
  509. Modelobj* pModel;
  510.  
  511.  
  512.  
  513. pModel = m_pModel;
  514.  
  515. pTechnique->GetDesc( &techDesc );
  516.  
  517.  
  518.  
  519. while(pModel)
  520.  
  521. {
  522.  
  523. for( UINT p = 0; p < techDesc.Passes; ++p )
  524.  
  525. {
  526.  
  527. pTechnique->GetPassByIndex( p )->Apply( 0 );
  528.  
  529. m_pDevice->GetVertexDaemon()->Render(pModel->m_nIDstaticbuffer);
  530.  
  531. }
  532.  
  533. pModel=pModel->m_pChild;
  534.  
  535. }
  536.  
  537. } // Render all
  538.  
  539. /*----------------------------------------------------------------*/
  540.  
  541.  
  542.  
  543. // Renders Modell with given static buffer id
  544.  
  545. // in: static buffer id
  546.  
  547. // out: void
  548.  
  549. void Modelobj::Render(UINT uiStaticbufferID) {
  550.  
  551. D3D10_TECHNIQUE_DESC techDesc;
  552.  
  553.  
  554.  
  555. m_pTechniqueRender->GetDesc( &techDesc );
  556.  
  557. for( UINT p = 0; p < techDesc.Passes; ++p )
  558.  
  559. {
  560.  
  561. m_pTechniqueRender->GetPassByIndex( p )->Apply( 0 );
  562.  
  563. m_pDevice->GetVertexDaemon()->Render(uiStaticbufferID);
  564.  
  565. }
  566.  
  567. } // Render with buffer id
  568.  
  569. /*----------------------------------------------------------------*/
  570.  
  571.  
  572.  
  573. void Modelobj::SetLightDirs( float* vLightDirs) {
  574.  
  575. m_pLightDirVariable->SetFloatVectorArray( ( float* )vLightDirs, 0, 3 );
  576.  
  577. }
  578.  
  579. /*----------------------------------------------------------------*/
  580.  
  581.  
  582.  
  583. void Modelobj::SetLightColors( float* vLightColours) {
  584.  
  585. m_pLightColorVariable->SetFloatVectorArray( ( float* )vLightColours, 0, 3 );
  586.  
  587. }
  588.  
  589. /*----------------------------------------------------------------*/
  590.  
  591.  
  592.  
  593. void Modelobj::SetOutputColor(float* vOutputColour){
  594.  
  595. m_pOutputColorVariable->SetFloatVector( ( float* )vOutputColour );
  596.  
  597. }
  598.  
  599. /*----------------------------------------------------------------*/
  600.  
  601.  
  602.  
  603. ID3D10EffectTechnique* Modelobj::GetLightTechnique(void){
  604.  
  605. return m_pTechniqueRenderLight;
  606.  
  607. }
  608.  
  609. /*----------------------------------------------------------------*/
  610.  
  611.  
  612.  
  613. ID3D10EffectTechnique* Modelobj::GetTechnique(void){
  614.  
  615. return m_pTechniqueRender;
  616.  
  617. }
  618.  
  619. /*----------------------------------------------------------------*/
  620.  
  621.  
  622.  
  623. /**
  624.  
  625. * write outputstring to attribut outputstream if exists
  626.  
  627. * -> IN: bool - flush immediately
  628.  
  629. * char - format string to output
  630.  
  631. * ... - output values
  632.  
  633. */
  634.  
  635. void Modelobj::Log(char *chString, ...) {
  636.  
  637.  
  638.  
  639. char ch[256];
  640.  
  641. char *pArgs;
  642.  
  643.  
  644.  
  645. pArgs = (char*) &chString + sizeof(chString);
  646.  
  647. vsprintf(ch, chString, pArgs);
  648.  
  649. fprintf(m_pLog, "[Modelobj]: ");
  650.  
  651. fprintf(m_pLog, ch);
  652.  
  653. fprintf(m_pLog, "\n");
  654.  
  655.  
  656.  
  657. fflush(m_pLog);
  658.  
  659. } // Log
  660.  
  661. /*----------------------------------------------------------------*/
Add Comment
Please, Sign In to add comment