-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobj_opt.cpp
More file actions
69 lines (58 loc) · 2.47 KB
/
obj_opt.cpp
File metadata and controls
69 lines (58 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// obj_opt.cpp
// This module optimizes an object loaded with tiny_obj_loader and stores it in a binary format for fast loading.
// This is essentially the "compiler" for OBJ files - or any file that can be read with tiny_obj_loader.
// Include this in your project, build it using a custom build step, and then use it to compile your OBJs into
// optimized binary format which can be loaded very quickly at runtime.
// We must have a manner for an application to inject its Vertex implemnentation into the compilation - perhaps multiple
// different versions of a Vertex - depending on model type and usage. As such this Vertex impl should be included before this
// file is included in each "obj compiler executable" and the Vertex should be typdef'd vtyVertexType which is the type
// that will be used for each compilation using that vertex type.
// main() is defined in this modoule and thus each "vertex compiler executable" that is defined by a project needn't have a main().
__BIENUTIL_USING_NAMESPACE
string g_strProgramName;
int _TryMain( int _argc, char ** _argv );
int main( int _argc, char ** _argv )
{
#define USAGE "Usage: %s <input OBJ file> <output binary obj file>"
g_strProgramName = _argv[0];
n_SysLog::InitSysLog( g_strProgramName.c_str(), LOG_PERROR, LOG_USER );
if ( 3 != _argc )
{
LOGSYSLOG( eslmtError, USAGE, g_strProgramName.c_str() );
return EXIT_FAILURE;
}
try
{
return _TryMain( _argc-1, _argv + 1 );
}
catch( std::exception const & _rexc )
{
LOGEXCEPTION( _rexc, "Caught exception attempting to compile OBJ file." );
return EXIT_FAILURE;
}
catch( ... )
{
LOGSYSLOG( eslmtError, "Unknown exception caught." );
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int _TryMain( int _argc, char ** _argv )
{
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string szWarn, szErr;
VerifyThrowSz( tinyobj::LoadObj( &attrib, &shapes, &materials, &szWarn, &szErr, _argv[0] ), "Error loading model from [%s]. szWarn[%s], szErr[%s].", _argv[0], &szWarn[0], &szErr[0] );
typedef ObjOptimizer< vtyVertexType > _TyObjOptimizer;
try
{
_TyObjOptimizer::OptimizeTinyObjShapes( attrib, shapes.data(), shapes.data() + shapes.size(), _argv[1] );
}
catch( std::exception const & _rexc )
{
LOGEXCEPTION( _rexc, "Caught exception attempting to compile OBJ file from [%s] into [%s].", _argv[0], _argv[1] );
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}