Source

dbsdev / sources / tools / mesh_importer / main.cpp

// ----------------------------------------------------------------------------
// Simple sample to prove that Assimp is easy to use with OpenGL.
// It takes a file name as command line parameter, loads it using standard
// settings and displays it.
//
// ----------------------------------------------------------------------------

#include <iostream>

#include <cstdio>
#include <cstdlib>
#include <cstring>


// assimp include files. These three are usually needed.
#include <assimp/config.h>
#include <assimp/cimport.h>
#include <assimp/cexport.h>
#include <assimp/scene.h>
#include <assimp/postprocess.h>

// threading
#include "amp/amp.h"


#include "renderer.h"

// the global Assimp scene object
const struct aiScene* scene = NULL;

// global variables
bool stop = false;
amp_mutex_t   mutex;



#define aisgl_min(x,y) (x<y?x:y)
#define aisgl_max(x,y) (y>x?y:x)

// ----------------------------------------------------------------------------
void get_bounding_box_for_node (const struct aiNode* nd, 
	aiVector3D* min, 
	aiVector3D* max, 
	aiMatrix4x4* trafo )
{
	aiMatrix4x4 prev;
	unsigned int n = 0, t;

	prev = *trafo;
	aiMultiplyMatrix4(trafo,&nd->mTransformation);

	for (; n < nd->mNumMeshes; ++n) {
		const struct aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]];
		for (t = 0; t < mesh->mNumVertices; ++t) {

			aiVector3D tmp = mesh->mVertices[t];
			aiTransformVecByMatrix4(&tmp,trafo);

			min->x = aisgl_min(min->x,tmp.x);
			min->y = aisgl_min(min->y,tmp.y);
			min->z = aisgl_min(min->z,tmp.z);

			max->x = aisgl_max(max->x,tmp.x);
			max->y = aisgl_max(max->y,tmp.y);
			max->z = aisgl_max(max->z,tmp.z);
		}
	}

	for (n = 0; n < nd->mNumChildren; ++n) {
		get_bounding_box_for_node(nd->mChildren[n],min,max,trafo);
	}
	*trafo = prev;
}

// ----------------------------------------------------------------------------
void get_bounding_box (aiVector3D* min, aiVector3D* max)
{
	aiMatrix4x4 trafo;
	aiIdentityMatrix4(&trafo);

	min->x = min->y = min->z =  1e10f;
	max->x = max->y = max->z = -1e10f;
	get_bounding_box_for_node(scene->mRootNode,min,max,&trafo);
}


// ////////////////// //
// set transformation //
// ////////////////// //

// rotate using Euler angles (XYZ)
int rotate( double angleX, double angleY, double angleZ )
{
	if (!scene)
		return 0;

	aiMatrix4x4 myRot;
	myRot.FromEulerAnglesXYZ(angleX,angleY,angleZ);
	scene->mRootNode->mTransformation = myRot * scene->mRootNode->mTransformation;
	return 1;
}


// translate
int translate( double translX, double translY, double translZ )
{
	if (!scene)
		return 0;

	aiMatrix4x4 myTransl;
	aiMatrix4x4::Translation(aiVector3D(translX,translY,translZ), myTransl);
	scene->mRootNode->mTransformation = myTransl * scene->mRootNode->mTransformation;
	return 1;
}


// scale
int scale( double scX, double scY, double scZ )
{
	if (!scene)
		return 0;

	aiMatrix4x4 myScaling;
	aiMatrix4x4::Scaling(aiVector3D(scX,scY,scZ), myScaling);
	scene->mRootNode->mTransformation = myScaling * scene->mRootNode->mTransformation;
	return 1;
}


// ///////////// //
// retrieve data //
// ///////////// //


// get center
aiVector3D getCenter( void )
{
	if (!scene)
		return aiVector3D(0,0,0);

	aiVector3D min, max, center;
	get_bounding_box (&min, &max);
	center.x = (min.x+max.x)/2.0;
	center.y = (min.y+max.y)/2.0;
	center.z = (min.z+max.z)/2.0;
	return center;
}


// get size
aiVector3D getSize( void )
{
	if (!scene)
		return aiVector3D(0,0,0);

	aiVector3D min, max, asize;
	get_bounding_box (&min, &max);
	asize.x = (max.x-min.x);
	asize.y = (max.y-min.y);
	asize.z = (max.z-min.z);
	return asize;
}


// ////// //
// export //
// ////// //

void exportCollada( const std::string &filename )
{
	aiExportScene(scene,"collada",filename.c_str(),0);
}



// ----------------------------------------------------------------------------
int loadAsset (const char* path)
{
	aiPropertyStore *ps = aiCreatePropertyStore();
	
	// set to remove points and lines
	aiSetImportPropertyInteger(ps, AI_CONFIG_PP_SBP_REMOVE, aiPrimitiveType_POINT 
		                                                  | aiPrimitiveType_LINE );
	// we are taking one of the postprocessing presets to avoid
	// spelling out 20+ single postprocessing flags here.
    // scene = aiImportFile(path, aiProcessPreset_TargetRealtime_MaxQuality);
	scene = aiImportFileExWithProperties(path, aiProcess_FindDegenerates
		                                     | aiProcess_SortByPType
		                                     | aiProcess_CalcTangentSpace
		                                     | aiProcess_JoinIdenticalVertices
		                                     | aiProcess_Triangulate
		                                     | aiProcess_GenSmoothNormals
		                                     | aiProcess_OptimizeMeshes
		                                     | aiProcess_OptimizeGraph, 
		                                       NULL,
		                                       ps );

	return scene ? 1 : 0;
}



void makeTransforms( void *unused )
{
	// usleep(2000000);
	aiVector3D center = getCenter();
	printf("center: %f %f %f\n",center.x,center.y,center.z);

	// usleep(2000000);
	aiVector3D asize = getSize();
	printf("size: %f %f %f\n",asize.x,asize.y,asize.z);

	// usleep(2000000);
	translate(-center.x,-center.y,-center.z);

	usleep(2000000);
	scale(0.5,0.5,0.5);

	usleep(2000000);
	scale(0.5,0.5,0.5);

	usleep(2000000);
	scale(0.5,0.5,0.5);

	// export scene
	exportCollada("myscene222.dae");
}


int main(int argc, char **argv)
{
    std::cout << "INSERT ANYTHING TO CONTINUE";
    std::string ciccio;
    std::cin >> ciccio;
    
	struct aiLogStream stream;

	// get a handle to the predefined STDOUT log stream and attach
	// it to the logging system. It remains active for all further
	// calls to aiImportFile(Ex) and aiApplyPostProcessing.
	stream = aiGetPredefinedLogStream(aiDefaultLogStream_STDOUT,NULL);
	aiAttachLogStream(&stream);

	// ... same procedure, but this stream now writes the
	// log messages to assimp_log.txt
	stream = aiGetPredefinedLogStream(aiDefaultLogStream_FILE,"assimp_log.txt");
	aiAttachLogStream(&stream);

	// the model name can be specified on the command line. If none
	// is specified, we try to locate one of the more expressive test 
	// models from the repository (/models-nonbsd may be missing in 
	// some distributions so we need a fallback from /models!).
	if( !loadAsset(argv[1]) )
	{ 
		printf("Error loading asset '%s'\n",argv[1]);
		return -1;
	}

	// printf("init renderer\n");
	// initRenderer(scene);

    // sync mutex
    printf("create threads\n");
    amp_mutex_create(&mutex,AMP_DEFAULT_ALLOCATOR);
    
    //create threads
    amp_thread_t thread1;
    int retval1 = amp_thread_create_and_launch(&thread1,
                                           AMP_DEFAULT_ALLOCATOR,
                                           NULL, 
                                           &makeTransforms);
    amp_thread_t thread2;
    int retval2 = amp_thread_create_and_launch(&thread2,
                                           AMP_DEFAULT_ALLOCATOR,
                                           (void*)scene, 
                                           &startRenderer);
                                                      
    amp_mutex_lock(mutex);
      stop = true;
    amp_mutex_unlock(mutex);
    
    // join threads
    amp_thread_join_and_destroy(&thread1, AMP_DEFAULT_ALLOCATOR);
    amp_thread_join_and_destroy(&thread2, AMP_DEFAULT_ALLOCATOR);
    

	// makeTransforms();
	

	// startRenderer((void*)scene);

	return 0;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.