Skip to content

Commit

Permalink
#5576: Place the old parser code inside AseModel::CreateFromStream() …
Browse files Browse the repository at this point in the history
…and get it to compile. Loader is not able to submit triangles yet, the adapter is missing.
  • Loading branch information
codereader committed Apr 4, 2021
1 parent 142122d commit 072a712
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 87 deletions.
92 changes: 23 additions & 69 deletions radiantcore/model/import/AseModel.cpp
Expand Up @@ -34,22 +34,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------------- */


/* marker */
#define PM_ASE_C

/* uncomment when debugging this module */
//#define DEBUG_PM_ASE
//#define DEBUG_PM_ASE_EX


/* dependencies */
#include "../picomodel/lib/picointernal.h"

#ifdef DEBUG_PM_ASE
#include "time.h"
#endif

/* plain white */
static picoColor_t white = { 255, 255, 255, 255 };

Expand Down Expand Up @@ -469,7 +456,7 @@ static void _ase_submit_triangles_unshared ( picoModel_t* model , aseMaterial_t*

#endif

static void _ase_submit_triangles( picoModel_t* model , aseMaterial_t* materials , aseVertex_t* vertices, aseTexCoord_t* texcoords, aseColor_t* colors, aseFace_t* faces, int numFaces )
static void _ase_submit_triangles( model::AseModel& model , aseMaterial_t* materials , aseVertex_t* vertices, aseTexCoord_t* texcoords, aseColor_t* colors, aseFace_t* faces, int numFaces )
{
aseFacesIter_t i = faces, end = faces + numFaces;
for(; i != end; ++i)
Expand Down Expand Up @@ -499,11 +486,8 @@ static void _ase_submit_triangles( picoModel_t* model , aseMaterial_t* materials
{
xyz[j] = &vertices[(*i).indices[j]].xyz;
normal[j] = &vertices[(*i).indices[j]].normal;
/* Old code
st[j] = &texcoords[(*i).indices[j + 3]].texcoord;
*/

/* greebo: Apply shift, scale and rotation */
/* greebo: Apply shift, scale and rotation */
/* Also check for NULL texcoords pointer, some models surfaces don't have any tverts */
u = texcoords != NULL ? texcoords[(*i).indices[j + 3]].texcoord[0] * subMtl->uScale + subMtl->uOffset : 0.0;
v = texcoords != NULL ? texcoords[(*i).indices[j + 3]].texcoord[1] * subMtl->vScale + subMtl->vOffset : 0.0;
Expand All @@ -527,7 +511,7 @@ static void _ase_submit_triangles( picoModel_t* model , aseMaterial_t* materials
}

/* submit the triangle to the model */
PicoAddTriangleToModel ( model , xyz , normal , 1 , stRef, 1 , color , subMtl->shader, smooth );
// TODO PicoAddTriangleToModel ( model , xyz , normal , 1 , stRef, 1 , color , subMtl->shader, smooth );
}
}
}
Expand Down Expand Up @@ -560,17 +544,8 @@ std::vector<AseModel::Surface>& AseModel::getSurfaces()

std::shared_ptr<AseModel> AseModel::CreateFromStream(std::istream& stream)
{
return std::make_shared<AseModel>();
}
auto model = std::make_shared<AseModel>();

}

/* _ase_load:
* loads a 3dsmax ase model file.
*/
static picoModel_t *_ase_load( PM_PARAMS_LOAD )
{
picoModel_t *model;
picoParser_t *p;
char lastNodeName[ 1024 ];

Expand All @@ -588,36 +563,23 @@ static picoModel_t *_ase_load( PM_PARAMS_LOAD )

aseMaterial_t* materials = NULL;

#ifdef DEBUG_PM_ASE
clock_t start, finish;
double elapsed;
start = clock();
#endif

/* helper */
#define _ase_error_return(m) \
{ \
_pico_printf( PICO_ERROR,"%s in ASE, line %d.",m,p->curLine); \
_pico_free_parser( p ); \
PicoFreeModel( model ); \
return NULL; \
return model; \
}

// Temporary buffer to get the pico parser up and running
std::stringstream buffer;
buffer << stream.rdbuf();
std::string stringBuffer = buffer.str();

/* create a new pico parser */
p = _pico_new_parser( (picoByte_t *)buffer,bufSize );
p = _pico_new_parser( (picoByte_t *)stringBuffer.c_str(), static_cast<int>(stringBuffer.length()));
if (p == NULL) return NULL;

/* create a new pico model */
model = PicoNewModel();
if (model == NULL)
{
_pico_free_parser( p );
return NULL;
}
/* do model setup */
PicoSetModelFrameNum( model, frameNum );
PicoSetModelName( model, fileName );
PicoSetModelFileName( model, fileName );

/* initialize some stuff */
memset( lastNodeName,0,sizeof(lastNodeName) );

Expand Down Expand Up @@ -653,7 +615,7 @@ static picoModel_t *_ase_load( PM_PARAMS_LOAD )
else if (!_pico_stricmp(p->token,"*mesh"))
{
/* finish existing surface */
_ase_submit_triangles(model, materials, vertices, texcoords, colors, faces, numFaces);
_ase_submit_triangles(*model, materials, vertices, texcoords, colors, faces, numFaces);
_pico_free(faces);
_pico_free(vertices);
_pico_free(texcoords);
Expand Down Expand Up @@ -1044,6 +1006,7 @@ static picoModel_t *_ase_load( PM_PARAMS_LOAD )
}

/* parse submaterial index */
#if 0
if (!_pico_stricmp(p->token,"*submaterial"))
{
/* allocate new pico shader */
Expand All @@ -1057,8 +1020,10 @@ static picoModel_t *_ase_load( PM_PARAMS_LOAD )
}
subMaterialLevel = level;
}
else
#endif
/* parse material name */
else if (!_pico_stricmp(p->token,"*material_name"))
if (!_pico_stricmp(p->token,"*material_name"))
{
char* name = _pico_parse(p,0);
if ( name == NULL)
Expand Down Expand Up @@ -1244,13 +1209,9 @@ static picoModel_t *_ase_load( PM_PARAMS_LOAD )

if( subMaterial == NULL )
{
/* allocate new pico shader */
shader = PicoNewShader( model );
if (shader == NULL)
{
PicoFreeModel( model );
return NULL;
}
/* allocate and clear */
shader = (picoShader_t*)_pico_alloc(sizeof(picoShader_t));
memset(shader, 0, sizeof(picoShader_t));

/* set material name */
shadername_convert(materialName);
Expand Down Expand Up @@ -1348,24 +1309,17 @@ static picoModel_t *_ase_load( PM_PARAMS_LOAD )
}

/* ydnar: finish existing surface */
_ase_submit_triangles(model, materials, vertices, texcoords, colors, faces, numFaces);
_ase_submit_triangles(*model, materials, vertices, texcoords, colors, faces, numFaces);
_pico_free(faces);
_pico_free(vertices);
_pico_free(texcoords);
_pico_free(colors);

#ifdef DEBUG_PM_ASE
_ase_print_materials(materials);
finish = clock();
elapsed = (double)(finish - start) / CLOCKS_PER_SEC;
_pico_printf( PICO_NORMAL, "Loaded model in in %-.2f second(s)\n", elapsed );
#endif //DEBUG_PM_ASE

_ase_free_materials(&materials);

_pico_free_parser( p );

/* return allocated pico model */
return model;
return model;
}

} // namespace
2 changes: 2 additions & 0 deletions radiantcore/model/import/AseModel.h
Expand Up @@ -26,6 +26,8 @@ class AseModel
// Read/Write access
std::vector<Surface>& getSurfaces();

// Create a new ASE model from the given stream
// throws parser::ParseException on any failure
static std::shared_ptr<AseModel> CreateFromStream(std::istream& stream);
};

Expand Down
45 changes: 27 additions & 18 deletions radiantcore/model/import/AseModelLoader.cpp
Expand Up @@ -7,6 +7,7 @@
#include "string/case_conv.h"

#include "../StaticModel.h"
#include "parser/ParseException.h"

namespace model
{
Expand All @@ -28,29 +29,37 @@ IModelPtr AseModelLoader::loadModelFromPath(const std::string& path)
return IModelPtr();
}

// Parse the ASE model data from the given stream
std::istream stream(&(file->getInputStream()));
auto model = AseModel::CreateFromStream(stream);
try
{
// Parse the ASE model data from the given stream
std::istream stream(&(file->getInputStream()));
auto model = AseModel::CreateFromStream(stream);

// Convert the AseModel to StaticModelSurfaces, destructing it during the process
std::vector<StaticModelSurfacePtr> staticSurfaces;
// Convert the AseModel to StaticModelSurfaces, destructing it during the process
std::vector<StaticModelSurfacePtr> staticSurfaces;

for (auto& aseSurface : model->getSurfaces())
{
// Move the vertex and index data to construct the StaticModelSurface
auto& staticSurface = staticSurfaces.emplace_back(std::make_shared<StaticModelSurface>(
std::move(aseSurface.vertices), std::move(aseSurface.indices)));
for (auto& aseSurface : model->getSurfaces())
{
// Move the vertex and index data to construct the StaticModelSurface
auto& staticSurface = staticSurfaces.emplace_back(std::make_shared<StaticModelSurface>(
std::move(aseSurface.vertices), std::move(aseSurface.indices)));

staticSurface->setDefaultMaterial(aseSurface.material);
}
staticSurface->setDefaultMaterial(aseSurface.material);
}

auto staticModel = std::make_shared<StaticModel>(staticSurfaces);

auto staticModel = std::make_shared<StaticModel>(staticSurfaces);

// Set the filename
staticModel->setFilename(os::getFilename(file->getName()));
staticModel->setModelPath(path);
// Set the filename
staticModel->setFilename(os::getFilename(file->getName()));
staticModel->setModelPath(path);

return staticModel;
return staticModel;
}
catch (const parser::ParseException& ex)
{
rError() << "AseModelLoader: " << ex.what() << std::endl;
return IModelPtr();
}
}

}

0 comments on commit 072a712

Please sign in to comment.