Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //***********************************************************
- // Model_obj.cpp
- //***********************************************************
- #include "Model_obj.h"
- #include "io.h" // _filelength
- // D E F I N E S ///////////////////////////////////////////////////
- // P R O T O T Y P E S /////////////////////////////////////////////
- Modelobj::~Modelobj(void){
- } // destructor
- /*----------------------------------------------------------------*/
- Modelobj::Modelobj(void){
- m_pDevice = NULL;
- m_pModel = NULL;
- m_pChild = NULL;
- m_uiAnzModels = 0;
- m_nIDstaticbuffer = 0;
- m_pLog = NULL;
- m_pEffect = NULL;
- m_pTechniqueRender = NULL;
- m_pTechniqueRenderLight = NULL;
- m_pLightDirVariable = NULL;
- m_pLightColorVariable = NULL;
- m_pOutputColorVariable = NULL;
- m_SkinID = 0;
- }
- // constructor
- /*----------------------------------------------------------------*/
- /**
- * load obj file format
- * -> IN: wchar_t - filename
- *
- */
- HRESULT Modelobj::Init(LPRenderDevice pDevice, wchar_t *wcFilename, wchar_t *wcGPUprg, UINT nSkinID, FILE *pLog) {
- HRESULT hr = _FAIL;
- FILE *pFile=NULL;
- UINT uiFaceNumber, uiNormalNumber, uiTexCoordsNumber, uiVertexNumber;
- FaceType *pFaces=NULL;
- VertexType *pVertices=NULL;
- NormalType *pNormals=NULL;
- TexcoordType *pTexcoords=NULL;
- uiFaceNumber=uiNormalNumber=uiTexCoordsNumber=uiVertexNumber=0;
- // Logging messages
- m_pLog = pLog;
- m_pDevice = pDevice;
- // start shader things
- m_pEffect = m_pDevice->CompileHLSLShader(wcGPUprg);
- if(!m_pEffect) return _FAIL;
- // Obtain the technique
- m_pTechniqueRender = m_pEffect->GetTechniqueByName( "Render" );
- if(!m_pTechniqueRender->IsValid())
- return _FAIL;
- m_pTechniqueRenderLight = m_pEffect->GetTechniqueByName( "RenderLight" );
- if(!m_pTechniqueRenderLight->IsValid())
- return _FAIL;
- // Obtain the variables
- //
- m_pDevice->SetShaderWorld(m_pEffect->GetVariableByName( "World" )->AsMatrix());
- m_pDevice->SetShaderView(m_pEffect->GetVariableByName( "View" )->AsMatrix());
- m_pDevice->SetShaderProj(m_pEffect->GetVariableByName( "Projection" )->AsMatrix());
- //
- m_pLightDirVariable = m_pEffect->GetVariableByName( "vLightDir" )->AsVector();
- m_pLightColorVariable = m_pEffect->GetVariableByName( "vLightColor" )->AsVector();
- m_pOutputColorVariable = m_pEffect->GetVariableByName( "vOutputColor" )->AsVector();
- hr = m_pDevice->SetInputLayout(VID_UU, m_pTechniqueRender);
- if(FAILED(hr)) {
- Log("SetInputLayout refuses to work");
- return _FAIL;
- }
- uiFaceNumber = GetPrimitiveNumber(wcFilename, FACE);
- uiNormalNumber = GetPrimitiveNumber(wcFilename, NORMAL);
- uiTexCoordsNumber = GetPrimitiveNumber(wcFilename, TEXTURECOORD);
- uiVertexNumber = GetPrimitiveNumber(wcFilename, VERT);
- pFaces = (FaceType*)malloc(sizeof(FaceType)*uiFaceNumber);
- if(!pFaces)
- return _FAIL;
- pVertices = (VertexType*)malloc(sizeof(VertexType)*uiVertexNumber);
- if(!pVertices)
- return _FAIL;
- pNormals = (NormalType*)malloc(sizeof(NormalType)*uiNormalNumber);
- if(!pNormals)
- return _FAIL;
- pTexcoords = (TexcoordType*)malloc(sizeof(TexcoordType)*uiTexCoordsNumber);
- if(!pTexcoords)
- return _FAIL;
- //load all vertices, facedefinitions, normals and texture coords from file
- hr = LoadPrimitive(pVertices, VERT, uiVertexNumber, wcFilename);
- if(pFaces) free(pFaces);
- if(pVertices) free(pVertices);
- if(pNormals) free(pNormals);
- if(pTexcoords) free(pTexcoords);
- return hr;
- }// Init
- /*----------------------------------------------------------------*/
- /*
- * loads data from obj file
- * In: VertexType* := will store data here
- * Primitive := type of data
- * UINT := amount of data
- */
- HRESULT Modelobj::LoadPrimitive(VertexType* pVertices, Primitive nIDprim, UINT count, wchar_t *wcFilename) {
- char* cpBuffer=NULL;
- char cNumberx[20]; ZeroMemory(&cNumberx, 20*sizeof(char));
- char cNumbery[20]; ZeroMemory(&cNumbery, 20*sizeof(char));
- char cNumberz[20]; ZeroMemory(&cNumberz, 20*sizeof(char));
- UINT uiQuelle=0; UINT uiZiel=0; UINT uiVertcount=0;
- FILE *pFile=NULL;
- UINT test=0;
- if( (pFile = _wfopen(wcFilename, L"r")) == NULL)
- return _FILENOTFOUND;
- do {
- // search for the first vertice data
- // get a row
- cpBuffer = GetRow(pFile);
- //if (!cpBuffer) {
- // return _OK; // get out of function if we reached file end
- //}
- // we are looking for vertices
- if(nIDprim==VERT) {
- // see if it is a vertex row
- if(cpBuffer!=NULL && cpBuffer[0]=='v' && cpBuffer[1]==' ') {
- // we got the vertex row
- // now get the data out
- // and write it as numerical data to buffer pVertices
- // find the blank between v and the data
- do {
- cNumberx[0]=cpBuffer[uiQuelle++];
- } while(cNumberx[0]!=' ');
- uiQuelle++; // move to first digit/minus of 1. number
- // copy 1. number
- do {
- cNumberx[uiZiel++]=cpBuffer[uiQuelle++];
- } while(cpBuffer[uiQuelle]!=' ');
- cNumberx[uiZiel] = 0x0a; // put a carriage return to the end of cNumber
- uiQuelle++; // move to first digit/minus of 2. number
- uiZiel=0; // reset cNumber buffer
- // copy 2. number
- do {
- cNumbery[uiZiel++]=cpBuffer[uiQuelle++];
- } while(cpBuffer[uiQuelle]!=' ');
- cNumbery[uiZiel] = 0x0a;
- uiQuelle++; // move to first digit/minus of 3. number
- uiZiel=0; // reset cNumber buffer
- do {
- cNumberz[uiZiel++]=cpBuffer[uiQuelle++];
- } while(cpBuffer[uiQuelle]!=0x0a); // till end of row
- cNumberz[uiZiel] = 0x0a; // put a carriage return to the end of cNumber
- uiQuelle=uiZiel=0; // reset buffer index
- pVertices[uiVertcount].x = (float)atof(cNumberx);
- pVertices[uiVertcount].y = (float)atof(cNumbery);
- pVertices[uiVertcount++].z = (float)atof(cNumberz);
- } // if cpBuffer
- } // if nIDprim
- } while(cpBuffer!=NULL);
- if(pFile) fclose(pFile);
- return _OK;
- }// Init
- /*----------------------------------------------------------------*/
- // returns number of primitve types from obj file
- // in: Filename of obj, primitive identification
- // out: number of primitive
- UINT Modelobj::GetPrimitiveNumber(wchar_t *wcFilename, Primitive nIDprim){
- FILE *pFile=NULL;
- char* cpBuffer=NULL;
- UINT count=0;
- if( (pFile = _wfopen(wcFilename, L"r")) == NULL)
- return _FILENOTFOUND;
- do {
- cpBuffer = GetRow(pFile);
- if(nIDprim==FACE) {
- if(cpBuffer!=NULL && cpBuffer[0]=='f' && cpBuffer[1]==' ')
- count++;
- }
- if(nIDprim==VERT) {
- if(cpBuffer!=NULL && cpBuffer[0]=='v' && cpBuffer[1]==' ')
- count++;
- }
- if(nIDprim==NORMAL) {
- if(cpBuffer!=NULL && cpBuffer[0]=='v' && cpBuffer[1]=='n')
- count++;
- }
- if(nIDprim==TEXTURECOORD) {
- if(cpBuffer!=NULL && cpBuffer[0]=='v' && cpBuffer[1]=='t')
- count++;
- }
- } while(cpBuffer!=NULL);
- if(pFile) fclose(pFile);
- return count;
- }// GetPrimitiveNumber
- /*----------------------------------------------------------------*/
- // reads one row of a char textfile
- // till it encounters a linefeed 0x0a or EOF
- // in: filepointer of data
- // out: buffer pointer to row or NULL for EOF
- char* Modelobj::GetRow(FILE *pFile){
- char buffer[512];
- char c[2], *t=NULL;
- int i=0;
- //lieΓt Zeichenweise
- while(t=fgets(c, 2, pFile)) {
- if(c[0]==0x0a)
- break;
- buffer[i++]=c[0];
- }
- if(t==NULL)
- return NULL;
- buffer[i]=0x0a;
- return buffer;
- } // GetRow
- /*----------------------------------------------------------------*/
- // reads one row of a char textfile
- // till it encounters a linefeed 0x0a or EOF
- // in: filepointer of data
- // out: buffer pointer to row or NULL for EOF
- //char* Modelobj::GetNumber(char *pcBuffer) {
- //} // GetNumber
- /*----------------------------------------------------------------*/
- // Renders Modell with given Technique
- // in: pTechnique
- // out: void
- void Modelobj::Render(ID3D10EffectTechnique* pTechnique) {
- D3D10_TECHNIQUE_DESC techDesc;
- Modelobj* pModel;
- pModel = m_pModel;
- pTechnique->GetDesc( &techDesc );
- while(pModel)
- {
- for( UINT p = 0; p < techDesc.Passes; ++p )
- {
- pTechnique->GetPassByIndex( p )->Apply( 0 );
- m_pDevice->GetVertexDaemon()->Render(pModel->m_nIDstaticbuffer);
- }
- pModel=pModel->m_pChild;
- }
- } // Render all
- /*----------------------------------------------------------------*/
- // Renders Modell with given static buffer id
- // in: static buffer id
- // out: void
- void Modelobj::Render(UINT uiStaticbufferID) {
- D3D10_TECHNIQUE_DESC techDesc;
- m_pTechniqueRender->GetDesc( &techDesc );
- for( UINT p = 0; p < techDesc.Passes; ++p )
- {
- m_pTechniqueRender->GetPassByIndex( p )->Apply( 0 );
- m_pDevice->GetVertexDaemon()->Render(uiStaticbufferID);
- }
- } // Render with buffer id
- /*----------------------------------------------------------------*/
- void Modelobj::SetLightDirs( float* vLightDirs) {
- m_pLightDirVariable->SetFloatVectorArray( ( float* )vLightDirs, 0, 3 );
- }
- /*----------------------------------------------------------------*/
- void Modelobj::SetLightColors( float* vLightColours) {
- m_pLightColorVariable->SetFloatVectorArray( ( float* )vLightColours, 0, 3 );
- }
- /*----------------------------------------------------------------*/
- void Modelobj::SetOutputColor(float* vOutputColour){
- m_pOutputColorVariable->SetFloatVector( ( float* )vOutputColour );
- }
- /*----------------------------------------------------------------*/
- ID3D10EffectTechnique* Modelobj::GetLightTechnique(void){
- return m_pTechniqueRenderLight;
- }
- /*----------------------------------------------------------------*/
- ID3D10EffectTechnique* Modelobj::GetTechnique(void){
- return m_pTechniqueRender;
- }
- /*----------------------------------------------------------------*/
- /**
- * write outputstring to attribut outputstream if exists
- * -> IN: bool - flush immediately
- * char - format string to output
- * ... - output values
- */
- void Modelobj::Log(char *chString, ...) {
- char ch[256];
- char *pArgs;
- pArgs = (char*) &chString + sizeof(chString);
- vsprintf(ch, chString, pArgs);
- fprintf(m_pLog, "[Modelobj]: ");
- fprintf(m_pLog, ch);
- fprintf(m_pLog, "\n");
- fflush(m_pLog);
- } // Log
- /*----------------------------------------------------------------*/
Add Comment
Please, Sign In to add comment