Пример #1
0
void ModelManager::loadOBJModel(int index)
{
	if(this->model != NULL)
		free(this->model);

	// read an OBJ model here
	if(this->displayModel != NULL)
		glmDelete(this->displayModel);
	
	this->displayModel = glmReadOBJ((char*)this->modelNameList[index].c_str());
	printf("%s\n", this->modelNameList[index].c_str());

	// traverse the color model
	this->model = new Model();
	this->traverseColorModel(this->displayModel);

	// print object info
	printf("x=[%f, %f] dx=%f\n",
				this->model->minVal[0],
				this->model->maxVal[0],
				this->model->xLen);
	printf("y=[%f, %f] dy=%f\n",
				this->model->minVal[1],
				this->model->maxVal[1],
				this->model->yLen);
	printf("z=[%f, %f] dz=%f\n\n",
				this->model->minVal[2],
				this->model->maxVal[2],
				this->model->zLen);
}
Пример #2
0
void
screen_menu(int value)
{
    char* name = 0;

    switch (value) {
    case 'a':
        name = (char*)"../data/al.obj";
        break;
    case 's':
        name = (char*)"../data/soccerball.obj";
        break;
    case 'd':
        name = (char*)"../data/dolphins.obj";
        break;
    case 'f':
        name = (char*)"../data/flowers.obj";
        break;
    case 'j':
        name = (char*)"../data/f-16.obj";
        break;
    case 'p':
        name = (char*)"../data/porsche.obj";
        break;
    case 'r':
        name = (char*)"../data/rose+vase.obj";
        break;
    case 'n':
        if (pmodel) glmDelete(pmodel);
        pmodel = NULL;
        redisplay_all();
        return;
    }

    if (name) {
        if (pmodel) glmDelete(pmodel);
        pmodel = glmReadOBJ(name);
        if (!pmodel) exit(0);
        glmUnitize(pmodel);
        glmFacetNormals(pmodel);
        glmVertexNormals(pmodel, 90.0);
    }

    redisplay_all();
}
COBJNode::~COBJNode(void) {

	// clean up
	if(_model_p != NULL) {
		glDeleteLists(_modelDL, 1);
		glmDelete(_model_p);
		_model_p = NULL;
	}
}
JNIEXPORT void JNICALL JNIFUNCTION_DEMO(demoSurfaceCreated(JNIEnv* env, jobject object)) {
	glStateCacheFlush(); // Make sure we don't hold outdated OpenGL state.
	for (int i = 0; i < NUM_MODELS; i++) {
	    if (models[i].obj) {
	        glmDelete(models[i].obj, 0);
	        models[i].obj = NULL;
	    }
	}
}
Пример #5
0
void ofxObjLoader::save(string path, ofMesh& mesh){
    path = ofToDataPath(path);
    
    GLuint writeMode = GLM_NONE;
    
    GLMmodel* m = new GLMmodel();
    if(mesh.getNumVertices() > 0){
        m->numvertices = mesh.getNumVertices();
	    m->vertices = new GLfloat[m->numvertices*3+1];
        memcpy(&m->vertices[3], &mesh.getVertices()[0].x, sizeof(ofVec3f) * mesh.getNumVertices());
    }
    else {
        ofLogError("ofxObjLoader::save -- No vertices to save!");
        return;
    }
    
    if(mesh.getNumNormals() > 0){
        m->numnormals = mesh.getNumNormals();
        m->normals = new GLfloat[m->numnormals*3+1];
        memcpy(&m->normals[3], &mesh.getNormals()[0].x, sizeof(ofVec3f)*mesh.getNumNormals());
        writeMode |= GLM_SMOOTH;
    }
	
    if(mesh.getNumTexCoords() > 0){
        m->numtexcoords = mesh.getNumTexCoords();
        m->texcoords = new GLfloat[m->numtexcoords*2+1];
        memcpy(&m->texcoords[2], &mesh.getTexCoords()[0].x, sizeof(ofVec2f)*mesh.getNumTexCoords());
        writeMode |= GLM_TEXTURE;
    }
    
    if(mesh.getNumIndices() > 0){
        //create triangles
		m->numtriangles = mesh.getNumIndices()/3;
        m->triangles = new GLMtriangle[m->numtriangles];
        
        //add them all to one group
        m->groups = new GLMgroup();
        m->groups->next = NULL;
        m->groups->material = NULL;
        string name = "ofMesh";
        m->groups->name = (char*)malloc(sizeof(char) * name.length()+1);
        strcpy(m->groups->name, name.c_str());
        
        m->groups->numtriangles = mesh.getNumIndices()/3;
		m->groups->triangles = new GLuint[m->groups->numtriangles];
        m->numgroups = 1;
        for(int i = 0; i < mesh.getNumIndices()/3; i++){
            memcpy(m->triangles[i].vindices, &mesh.getIndices()[i*3], sizeof(GLuint)*3);
            memcpy(m->triangles[i].nindices, &mesh.getIndices()[i*3], sizeof(GLuint)*3);
            memcpy(m->triangles[i].tindices, &mesh.getIndices()[i*3], sizeof(GLuint)*3);
            m->groups->triangles[i] = i;
        }
    }
    
    glmWriteOBJ(m, (char*)path.c_str(), writeMode);
    glmDelete(m);
}
Пример #6
0
int main(int argc, char ** argv)
{
	//get into a rendering loop
	initAndRunLViewer(screenWidth, screenHeight, render, init);
	
	//free the model obj
	glmDelete(OBJ);

	return 0;
}
Пример #7
0
void CloseModels()
{
  int i,j;
  for(j = 0;j < MaxModels;j++)
  {
    for(i=0;i <= ModelList[j].numFrames;i++)
    {
      if(ModelList[j].object[i] != NULL)glmDelete(ModelList[j].object[i]);
    }
  }
}
Пример #8
0
OFX_OBJLOADER_BEGIN_NAMESPACE

void load(string path, ofMesh& mesh, bool generateNormals, bool flipFace)
{
	path = ofToDataPath(path);

	mesh.clear();

	GLMmodel* m;

	m = glmReadOBJ((char*)path.c_str());

	if (generateNormals)
	{
		glmFacetNormals(m);
		glmVertexNormals(m, 90);
	}

	if (flipFace)
	{
		glmReverseWinding(m);
	}

	for (int j = 0; j < m->numtriangles; j++)
	{
		const GLMtriangle &tri = m->triangles[j];

		for (int k = 0; k < 3; k++)
		{
			GLfloat *v = m->vertices + (tri.vindices[k] * 3);
			mesh.addVertex(ofVec3f(v[0], v[1], v[2]));

			if (m->colors)
			{
				GLfloat *c = m->colors + (tri.vindices[k] * 3);
				mesh.addColor(ofFloatColor(c[0], c[1], c[2]));
			}

			if (m->normals && ofInRange(tri.nindices[k], 0, m->numnormals))
			{
				GLfloat *n = m->normals + (tri.nindices[k] * 3);
				mesh.addNormal(ofVec3f(n[0], n[1], n[2]));
			}

			if (m->texcoords && ofInRange(tri.tindices[k], 0, m->numtexcoords))
			{
				GLfloat *c = m->texcoords + (tri.tindices[k] * 2);
				mesh.addTexCoord(ofVec2f(c[0], c[1]));
			}
		}
	}

	glmDelete(m);
}
Пример #9
0
//-----------------------------------------------------------------------------
// initialize the class
//-----------------------------------------------------------------------------
bool C3DObject_Init()
{
    if (m_poModel) {

    free(m_poModel);
    m_poModel = NULL;
    }

    glmDelete(m_poModel);

    return true;
}
Пример #10
0
void FreeModel(Model *model)
{
  int i;
  model->used--;
  if(model->used == 0)
  {
    strcpy(model->filename,"\0");
    for(i=0;i <= model->numFrames;i++)
    {
      glmDelete(model->object[i]);
    }
  }
}
Пример #11
0
ARTApp::~ARTApp()
{
	arPattDetach(arHandle);
	arPattDeleteHandle(pattHandle);
	arVideoCapStop();
	ar3DDeleteHandle(&ar3DHandle);
	arDeleteHandle(arHandle);
	arParamLTFree(&cParam);
	arVideoClose();

	if (objModel)
		glmDelete(objModel);
}
Пример #12
0
Model::~Model(void)
{
	if(model)
		glmDelete(model);

	if(colors)
		delete[] colors;

	if(bbox)
		delete bbox;

	unbindVBO();
	deleteVBO();

}
Пример #13
0
static void cleanup(void)
{
	arglCleanup(gArglSettings);
	gArglSettings = NULL;
	arPattDetach(gARHandle);
	arPattDeleteHandle(gARPattHandle);
	arVideoCapStop();
	ar3DDeleteHandle(&gAR3DHandle);
	arDeleteHandle(gARHandle);
	arParamLTFree(&gCparamLT);
	arVideoClose();

	if (gObj != NULL)
	{
		glmDelete(gObj);
		gObj = NULL;
	}
}
Пример #14
0
int main(int argc, char **argv)
{
	// glut init
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);

	// create window
	glutInitWindowPosition(800, 150);
	glutInitWindowSize(uiWidth, uiHeight);
	glutCreateWindow("10420 CS550000 CG HW4 TA");

	glewInit();
	if(glewIsSupported("GL_VERSION_2_0")){
		printf("Ready for OpenGL 2.0\n");
	}else{
		printf("OpenGL 2.0 not supported\n");
		system("pause");
		exit(1);
	}

	glutDisplayFunc(onDisplay);
	glutIdleFunc(onDisplay);
	glutReshapeFunc(onWindowReshape);
	glutMouseFunc(onMouse);
	glutMotionFunc(onMouseMotion);
	glutKeyboardFunc(onKeyboard);
	glutSpecialFunc(onKeyboardSpecial);

	// init
	preTranslateX = preTranslateY = 0;
	scale = 1.0;
	currentModel = 0;
	loadModel(currentModel);
	setShaders();

	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);
	glCullFace(GL_BACK);

	glutMainLoop();

	glmDelete(OBJ);
	return 0;
}
Пример #15
0
void ofxObjLoader::load(string path, ofMesh& mesh, bool generateNormals) {
	path = ofToDataPath(path);
    
    mesh.clear();
    
	GLMmodel* m;
	
	m = glmReadOBJ((char*)path.c_str());
	
    if(generateNormals){
        glmFacetNormals(m);
        glmVertexNormals(m, 90);
    }
    
    GLfloat *v, *n, *c;
    for(int i = 1; i <= m->numvertices; i++){
        v = &m->vertices[3 * i];
		mesh.addVertex(ofVec3f(v[0], v[1], v[2]));
    }
    
	for(int i = 1; i <= m->numnormals; i++){
        n = &m->normals[3 * i];
        mesh.addNormal(ofVec3f(n[0], n[1], n[2]));
    }
    
    for(int i = 1; i <= m->numtexcoords; i++){
        c = &m->texcoords[2 * i];
        mesh.addTexCoord(ofVec2f(c[0], c[1]));
    }
    
	for (int i = 0; i < m->numtriangles; i++) {
		GLMtriangle &t = m->triangles[i];
        
        //NOTE: ofMesh does not have support for different indices for tex coords and mormals
		mesh.addIndex(t.vindices[0]);
        mesh.addIndex(t.vindices[1]);
        mesh.addIndex(t.vindices[2]);
	}
	
	glmDelete(m);
	
	return mesh;
}
Пример #16
0
int main(int argc, char **argv) {
	// glut init
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);

	// create window
	glutInitWindowPosition(460, 40);
	glutInitWindowSize(800, 800);
	glutCreateWindow("10320 CS550000 CG HW2 TA");

	glewInit();
	if(glewIsSupported("GL_VERSION_2_0")){
		printf("Ready for OpenGL 2.0\n");
	}else{
		printf("OpenGL 2.0 not supported\n");
		system("pause");
		exit(1);
	}

	// load obj models through glm
	loadOBJModel();

	// register glut callback functions
	glutDisplayFunc (renderScene);
	glutIdleFunc    (idle);
	glutKeyboardFunc(processNormalKeys);
	glutMouseFunc   (processMouse);
	glutMotionFunc  (processMouseMotion);

	glEnable(GL_DEPTH_TEST);

	// set up shaders here
	setShaders();

	// main loop
	glutMainLoop();

	// free
	glmDelete(OBJ);

	return 0;
}
Пример #17
0
void ModelManager::loadOBJModel(int index)
{
	if (this->model != NULL)
		free(this->model);

	// read an OBJ model here
	if (this->displayModel != NULL)
		glmDelete(this->displayModel);

	this->displayModel = glmReadOBJ((char*)this->modelNameList[index].c_str());
	printf("%s\n", this->modelNameList[index].c_str());

	// traverse the color model
	this->model = new Model();
	this->traverseColorModel(this->displayModel);

	//glmFacetNormals(this->displayModel);
	//glmVertexNormals(this->displayModel, 90.0);

	// set normalization matrix
	this->setNormalizeMatrix();

	// print object info
	printf("x = [%.6f, %.6f] dx=%.6f\n",
		this->model->minVal[0],
		this->model->maxVal[0],
		this->model->xLen);
	printf("y = [%.6f, %.6f] dy=%.6f\n",
		this->model->minVal[1],
		this->model->maxVal[1],
		this->model->yLen);
	printf("z = [%.6f, %.6f] dz=%.6f\n\n",
		this->model->minVal[2],
		this->model->maxVal[2],
		this->model->zLen);
}
Пример #18
0
void save(string path, const ofMesh& mesh_, const ofPixels& tex, string imgext)
{
    ofMesh mesh = mesh_;
    
    path = ofToDataPath(path);
    
    ofFilePath::createEnclosingDirectory(path);
    
    GLuint writeMode = GLM_NONE;
    GLMmodel* m = new GLMmodel();
    
    {
        ofFile file(path);
        string base_path = file.getEnclosingDirectory();
        string material_name = file.getBaseName();
        
        string image_name = material_name + "." + imgext;

        ofPixels pix = tex;
        
        // flip save texture
        pix.mirror(true, false);
        ofSaveImage(pix, ofFilePath::join(base_path, image_name));
        
        string mtl_filename = material_name + ".mtl";
        
        writeMode |= GLM_MATERIAL;
        m->mtllibname = (char*)malloc(mtl_filename.size());
        strcpy(m->mtllibname, mtl_filename.c_str());
        
        m->nummaterials = 1;
        m->materials = (GLMmaterial*)malloc(sizeof(GLMmaterial));
        GLMmaterial *mat = &m->materials[0];
        memset(mat, 0, sizeof(GLMmaterial));
        
        for (int i = 0; i < 4; i++)
        {
            mat->diffuse[i] = 1;
            mat->ambient[i] = 1;
            mat->specular[i] = 1;
            mat->emmissive[i] = 1;
        }
        mat->shininess = 1;
        
        mat->name = (char*)malloc(material_name.size());
        strcpy(mat->name, material_name.c_str());
        
        mat->texture_path = (char*)malloc(image_name.size());
        strcpy(mat->texture_path, image_name.c_str());
    }
    
    if (mesh.getNumVertices() > 0)
    {
        m->numvertices = mesh.getNumVertices();
        m->vertices = new GLfloat[(m->numvertices + 1) * 3];
        memcpy(&m->vertices[3], &mesh.getVertices()[0].x, sizeof(ofVec3f) * mesh.getNumVertices());
    }
    else
    {
        ofLogError("ofxObjLoader::save -- No vertices to save!");
        return;
    }
    
    if (mesh.getNumNormals() > 0)
    {
        m->numnormals = mesh.getNumNormals();
        m->normals = new GLfloat[(m->numnormals + 1) * 3];
        vector<ofVec3f> normals = mesh.getNormals();
        memcpy(&m->normals[3], &normals[0].x, sizeof(ofVec3f) * normals.size());
        writeMode |= GLM_SMOOTH;
    }
    
    if (mesh.getNumTexCoords() > 0)
    {
        m->numtexcoords = mesh.getNumTexCoords();
        m->texcoords = new GLfloat[(m->numtexcoords + 1) * 2];
        memcpy(&m->texcoords[2], &mesh.getTexCoords()[0].x, sizeof(ofVec2f) * mesh.getNumTexCoords());
        writeMode |= GLM_TEXTURE;
    }
    
    if (mesh.getNumIndices() > 0)
    {
        m->numtriangles = mesh.getNumIndices() / 3;
        m->triangles = new GLMtriangle[m->numtriangles];
        
        m->groups = new GLMgroup();
        m->groups->next = NULL;
        m->groups->material = NULL;
        
        string name = "ofMesh";
        m->groups->name = (char*)malloc(sizeof(char) * name.length() + 1);
        strcpy(m->groups->name, name.c_str());
        
        m->groups->numtriangles = mesh.getNumIndices() / 3;
        m->groups->triangles = new GLuint[m->groups->numtriangles];
        m->numgroups = 1;
        
        for (int i = 0; i < mesh.getNumIndices(); i += 3)
        {
            int idx = i / 3;
            for (int j = 0; j < 3; j++)
            {
                m->triangles[idx].vindices[j] = mesh.getIndices()[i + j] + 1;
                m->triangles[idx].nindices[j] = mesh.getIndices()[i + j] + 1;
                m->triangles[idx].tindices[j] = mesh.getIndices()[i + j] + 1;
            }
            m->groups->triangles[idx] = idx;
        }
    }
    else
    {
        m->numtriangles = mesh.getNumVertices() / 3;
        m->triangles = new GLMtriangle[m->numtriangles];
        
        m->groups = new GLMgroup();
        m->groups->next = NULL;
        m->groups->material = NULL;
        
        string name = "ofMesh";
        m->groups->name = (char*)malloc(sizeof(char) * name.length() + 1);
        strcpy(m->groups->name, name.c_str());
        
        m->groups->numtriangles = mesh.getNumVertices() / 3;
        m->groups->triangles = new GLuint[m->groups->numtriangles];
        m->numgroups = 1;
        
        for (int i = 0; i < mesh.getNumVertices(); i += 3)
        {
            int idx = i / 3;
            for (int j = 0; j < 3; j++)
            {
                m->triangles[idx].vindices[j] = i + j + 1;
                m->triangles[idx].nindices[j] = i + j + 1;
                m->triangles[idx].tindices[j] = i + j + 1;
            }
            m->groups->triangles[idx] = idx;
        }
    }

    glmWriteOBJ(m, (char*)path.c_str(), writeMode);
    glmDelete(m);
}
Пример #19
0
int loadModelRaw( const char* filename,
                   int*    num_triangles,
                   int**   triangle_indices,
                   int*    num_vertices,
                   float** vertices,
                   float   bbox_min[3],
                   float   bbox_max[3] )
{
  // Init all output variables to zero as failure state
  SET_ZERO_IF_NOT_NULL( num_triangles    );
  SET_ZERO_IF_NOT_NULL( triangle_indices );
  SET_ZERO_IF_NOT_NULL( num_vertices     );
  SET_ZERO_IF_NOT_NULL( vertices         );

  // Now report error if any are NULL 
  REQUIRE_NOT_NULL( num_triangles    );
  REQUIRE_NOT_NULL( triangle_indices );
  REQUIRE_NOT_NULL( num_vertices     );
  REQUIRE_NOT_NULL( vertices         );

  if( ObjLoader::isMyFile( filename ) )
  {
    GLMmodel* model = glmReadOBJ( filename );
    if( !model )
    {
      std::cerr << "loadModelRaw: glmReadOBJ( " << filename << ") failed."
                << std::endl;
      return 0;
    }

    //
    // Vertices
    //
    *num_vertices = model->numvertices;
    *vertices = new (std::nothrow) float[ *num_vertices*3 ];
    if( !(*vertices) )
    {
      std::cerr << "loadModelRaw: failed to allocate memory.";
      return 0;
    }
    std::copy( model->vertices + 3, // Skip dummy vert
               model->vertices + 3 + model->numvertices*3,
               *vertices );

    //
    // Triangle indices
    //
    *num_triangles = model->numtriangles;
    *triangle_indices = new (std::nothrow) int[ *num_triangles*3 ];
    if( !(*triangle_indices) )
    {
      std::cerr << "loadModelRaw: failed to allocate memory.";
      return 0;
    }

    for( int i=0; i < *num_triangles; ++i )
    {
      (*triangle_indices)[ 3*i+0 ] = model->triangles[i].vindices[0]-1; // 
      (*triangle_indices)[ 3*i+1 ] = model->triangles[i].vindices[1]-1; // Account for dummy vert
      (*triangle_indices)[ 3*i+2 ] = model->triangles[i].vindices[2]-1; //
    }

    glmBoundingBox( model, bbox_min, bbox_max );
    glmDelete( model );

    return 1;

  } else {
    std::cerr << "loadModelRaw: '" << filename << "' extension not recognized." 
              << std::endl;
    return 0;
  }
}
Object::~Object()
{
    glmDelete(model);
}
Пример #21
0
void destroyCarDrawer(CarDrawer* carDrawer)
{
	glmDelete(carDrawer->carModel);
	free(carDrawer);
}
Пример #22
0
void Mesh::Preprocess( Scene *s ) 
{ 
	if (displayListID>0) return;

	if (modelType == TYPE_HEM_FILE)
	{
		if (renderMode == MESH_RENDER_AS_DISPLAY_LIST)
		{
			displayListID = hem->CreateOpenGLDisplayList( WITH_NORMALS );
			if (objectOptionFlags & OBJECT_OPTION_USE_LOWRES)
				displayListID_low = hem_lowRes->CreateOpenGLDisplayList( WITH_NORMALS );
			if (flags & OBJECT_FLAGS_ALLOWDRAWEDGESONLY)
				hem->CreateOpenGLDisplayList( USE_LINES | WITH_NORMALS | WITH_ADJACENT_FACE_NORMS );
			if ( (flags & OBJECT_FLAGS_ALLOWDRAWEDGESONLY) && (objectOptionFlags & OBJECT_OPTION_USE_LOWRES) )
				hem_lowRes->CreateOpenGLDisplayList( USE_LINES | WITH_NORMALS | WITH_ADJACENT_FACE_NORMS );
		}
		else if (renderMode == MESH_RENDER_AS_VBO_VERTEX_ARRAY)
		{
			interleavedVertDataVBO = hem->CreateOpenGLVBO( WITH_NORMALS );
			if (flags & OBJECT_FLAGS_ALLOWDRAWEDGESONLY)
				hem->CreateOpenGLVBO( USE_LINES | WITH_NORMALS | WITH_ADJACENT_FACE_NORMS );
			if (objectOptionFlags & OBJECT_OPTION_USE_LOWRES)
				interleavedVertDataVBO_low = hem_lowRes->CreateOpenGLVBO( WITH_NORMALS );
			if ( (flags & OBJECT_FLAGS_ALLOWDRAWEDGESONLY) && (objectOptionFlags & OBJECT_OPTION_USE_LOWRES) )
				hem_lowRes->CreateOpenGLVBO( USE_LINES | WITH_NORMALS | WITH_ADJACENT_FACE_NORMS );
		}
			
	}
	else if (modelType == TYPE_OBJ_FILE || modelType == TYPE_SMF_FILE)
	{
		if (renderMode == MESH_RENDER_AS_DISPLAY_LIST)
		{
			displayListID = glmList( glm, GLM_SMOOTH );
			if (objectOptionFlags & OBJECT_OPTION_USE_LOWRES)
				displayListID_low = glmList( glm_lowRes, GLM_SMOOTH );
		}
		else if (renderMode == MESH_RENDER_AS_VBO_VERTEX_ARRAY)
		{
			// Setup element vertex array for vertex buffer objects
			unsigned int *arrayIndices = (unsigned int *)malloc( glm->numtriangles * 3 * sizeof( unsigned int ) );
			for (unsigned int i=0; i<glm->numtriangles; i++)
			{
				arrayIndices[3*i+0] = glm->triangles[i].vindices[0];
				arrayIndices[3*i+1] = glm->triangles[i].vindices[1];
				arrayIndices[3*i+2] = glm->triangles[i].vindices[2];
			}
			glGenBuffers( 1, &elementVBO );
			glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, elementVBO );
			glBufferData( GL_ELEMENT_ARRAY_BUFFER, glm->numtriangles*3*sizeof(unsigned int), 
						  arrayIndices, GL_STATIC_DRAW );
			glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
			free( arrayIndices );

			// Setup data array for vertex buffer objects
			unsigned int dataSize = (glm->numvertices+1)*6*sizeof(float);
			float *floatData = (float *)malloc( dataSize );
			float *fptr = floatData;
			for (unsigned int i=0; i<(glm->numvertices+1)*3; i+=3)
			{
				*(fptr++) = glm->normArray[i];
				*(fptr++) = glm->normArray[i+1];
				*(fptr++) = glm->normArray[i+2];
				*(fptr++) = glm->vertices[i];
				*(fptr++) = glm->vertices[i+1];
				*(fptr++) = glm->vertices[i+2];
			}
			glGenBuffers( 1, &interleavedVertDataVBO );
			glBindBuffer( GL_ARRAY_BUFFER, interleavedVertDataVBO );
			glBufferData( GL_ARRAY_BUFFER, dataSize, floatData, GL_STATIC_DRAW );
			glBindBuffer( GL_ARRAY_BUFFER, 0 );
			free( floatData );
			elementCount = glm->numtriangles*3;

			if (objectOptionFlags & OBJECT_OPTION_USE_LOWRES)
			{
				// Setup element vertex array for vertex buffer objects
				arrayIndices = (unsigned int *)malloc( glm_lowRes->numtriangles * 3 * sizeof( unsigned int ) );
				for (unsigned int i=0; i<glm_lowRes->numtriangles; i++)
				{
					arrayIndices[3*i+0] = glm_lowRes->triangles[i].vindices[0];
					arrayIndices[3*i+1] = glm_lowRes->triangles[i].vindices[1];
					arrayIndices[3*i+2] = glm_lowRes->triangles[i].vindices[2];
				}
				glGenBuffers( 1, &elementVBO_low );
				glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, elementVBO_low );
				glBufferData( GL_ELEMENT_ARRAY_BUFFER, glm_lowRes->numtriangles*3*sizeof(unsigned int), 
							  arrayIndices, GL_STATIC_DRAW );
				glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
				free( arrayIndices );

				// Setup data array for vertex buffer objects
				dataSize = (glm_lowRes->numvertices+1)*6*sizeof(float);
				floatData = (float *)malloc( dataSize );
				fptr = floatData;
				for (unsigned int i=0; i<(glm_lowRes->numvertices+1)*3; i+=3)
				{
					*(fptr++) = glm_lowRes->normArray[i];
					*(fptr++) = glm_lowRes->normArray[i+1];
					*(fptr++) = glm_lowRes->normArray[i+2];
					*(fptr++) = glm_lowRes->vertices[i];
					*(fptr++) = glm_lowRes->vertices[i+1];
					*(fptr++) = glm_lowRes->vertices[i+2];
				}
				glGenBuffers( 1, &interleavedVertDataVBO_low );
				glBindBuffer( GL_ARRAY_BUFFER, interleavedVertDataVBO_low );
				glBufferData( GL_ARRAY_BUFFER, dataSize, floatData, GL_STATIC_DRAW );
				glBindBuffer( GL_ARRAY_BUFFER, 0 );
				free( floatData );
				elementCount_low = glm_lowRes->numtriangles*3;
			}
		}
	}

	if (glm) glmDelete( glm );
	//if (hem) hem->FreeNonGLMemory();
}
Пример #23
0
void saveGroup(string path, const vector<ofMesh> & meshGroup, bool flipFace, bool flipNormals) {
	path = ofToDataPath(path);
	
	ofFilePath::createEnclosingDirectory(path);
    
	GLuint writeMode = GLM_NONE;
	GLMmodel * m = new GLMmodel();
    GLMgroup * group = NULL;

    int numOfVerticesTotal = 0;
    int numOfNormalsTotal = 0;
    int numOfTexCoordsTotal = 0;
    int numOfIndicesTotal = 0;
    int numOfTrianglesTotal = 0;
    
    for(int i=0; i<meshGroup.size(); i++) {
        const ofMesh & mesh = meshGroup[i];
        
        numOfVerticesTotal += mesh.getNumVertices();
        numOfNormalsTotal += mesh.getNumNormals();
        numOfTexCoordsTotal += mesh.getNumTexCoords();

        if(mesh.getNumIndices() > 0) {
            numOfIndicesTotal += mesh.getNumIndices();
        } else {
            numOfIndicesTotal += mesh.getNumVertices();
        }
    }
    
    // glm skips the first array element when exporting.
    // not sure why?
    // but it means everything needs to be padded by 1.
    
    numOfVerticesTotal += 1;
    numOfNormalsTotal += 1;
    numOfTexCoordsTotal += 1;
    
    if(numOfVerticesTotal > 0) {
        m->numvertices = numOfVerticesTotal;
        m->vertices = new GLfloat[m->numvertices * 3];
    }
    
    if(numOfNormalsTotal > 0) {
        m->numnormals = numOfNormalsTotal;
        m->normals = new GLfloat[m->numnormals * 3];
    }
    
    if(numOfTexCoordsTotal > 0) {
        m->numtexcoords = numOfTexCoordsTotal;
        m->texcoords = new GLfloat[m->numtexcoords * 2];
    }
    
    if(numOfIndicesTotal > 0) {
        numOfTrianglesTotal = numOfIndicesTotal / 3;
        m->numtriangles = numOfTrianglesTotal;
        m->triangles = new GLMtriangle[m->numtriangles];
    }
    
    int numOfVertices = 0;
    int numOfNormals = 0;
    int numOfTexCoords = 0;
    int numOfIndices = 0;
    int numOfTriangles = 0;
    
    int indexVertices = 1;
    int indexNormals = 1;
    int indexTexCoords = 1;
    int indexIndices = 0;
    int indexTriangles = 0;
    
    for(int k=0; k<meshGroup.size(); k++) {
        const ofMesh & mesh = meshGroup[k];
        
        if(numOfVerticesTotal > 0) {
            numOfVertices = mesh.getNumVertices();
            memcpy(&m->vertices[indexVertices * 3], &mesh.getVertices()[0].x, sizeof(ofVec3f) * numOfVertices);
            indexVertices += numOfVertices;
        } else {
            ofLogError("ofxObjLoader::save -- No vertices to save!");
            return;
        }
        
        if(numOfNormalsTotal > 0) {
            numOfNormals = mesh.getNumNormals();
            vector<ofVec3f> normals = mesh.getNormals();
            
            if(flipNormals) {
                for(int i = 0; i < normals.size(); i++) {
                    normals[i] *= -1;
                }
            }
            
            memcpy(&m->normals[indexNormals * 3], &normals[0].x, sizeof(ofVec3f) * numOfNormals);
            indexNormals += numOfNormals;
            
            writeMode |= GLM_SMOOTH;
        }
        
        if(numOfTexCoordsTotal > 0) {
            numOfTexCoords = mesh.getNumTexCoords();
            memcpy(&m->texcoords[indexTexCoords * 2], &mesh.getTexCoords()[0].x, sizeof(ofVec2f) * numOfTexCoords);
            indexTexCoords += numOfTexCoords;

            writeMode |= GLM_TEXTURE;
        }
        
        numOfIndices = mesh.getNumIndices();
        if(numOfIndices > 0) {
            numOfTriangles = numOfIndices / 3;
        } else {
            numOfTriangles = numOfVertices / 3;
        }
        
        GLMgroup * groupPrev = group;
        group = new GLMgroup();
        group->next = NULL;
        group->material = NULL;
        
        if(m->groups == NULL) {
            m->groups = group;
            m->numgroups = 1;
        }
        if(groupPrev != NULL) {
            groupPrev->next = group;
            m->numgroups += 1;
        }
        
        string name = "ofMesh_" + ofToString(k);
        group->name = (char*)malloc(sizeof(char) * name.length() + 1);
        strcpy(group->name, name.c_str());
        
        group->numtriangles = numOfTriangles;
        group->triangles = new GLuint[group->numtriangles];
        
        const vector<ofIndexType> & indices = mesh.getIndices();
        int index = 0;
        
        for(int i=0; i<numOfTriangles; i++) {
            
            int t = i + indexTriangles;
            
            for(int j=0; j<3; j++) {
                
                int idx = i * 3 + j;
            
                if(numOfIndices > 0) {
                    index = indices[idx];
                } else {
                    index = idx;
                }
                
                index += (indexIndices + 1);
                
                m->triangles[t].vindices[j] = index;
                m->triangles[t].nindices[j] = index;
                m->triangles[t].tindices[j] = index;
            }
            
            group->triangles[i] = t;
        }
        
        indexIndices += numOfVertices;
        indexTriangles += numOfTriangles;
    }
	
	if(flipFace == true) {
		glmReverseWinding(m);
    }
    
	glmWriteOBJ(m, (char*)path.c_str(), writeMode);
	glmDelete(m);
}
Пример #24
0
ModelOBJ::~ModelOBJ(){
	if(model){
		glmDelete(model);
	}
}
Пример #25
0
cModelLoader::~cModelLoader()
{
	glmDelete(m_model);
}
Пример #26
0
ModelManager::~ModelManager()
{
	if(this->displayModel != NULL)
		glmDelete(this->displayModel);
}
Пример #27
0
void
keyboard(unsigned char key, int x, int y)
{
    GLint params[2];
    
    switch (key) {
    case 'h':
        printf("help\n\n");
        printf("w         -  Toggle wireframe/filled\n");
        printf("c         -  Toggle culling\n");
        printf("n         -  Toggle facet/smooth normal\n");
        printf("b         -  Toggle bounding box\n");
        printf("r         -  Reverse polygon winding\n");
        printf("m         -  Toggle color/material/none mode\n");
        printf("p         -  Toggle performance indicator\n");
        printf("s/S       -  Scale model smaller/larger\n");
        printf("t         -  Show model stats\n");
        printf("o         -  Weld vertices in model\n");
        printf("+/-       -  Increase/decrease smoothing angle\n");
        printf("W         -  Write model to file (out.obj)\n");
        printf("q/escape  -  Quit\n\n");
        break;
        
    case 't':
        stats = !stats;
        break;
        
    case 'p':
        performance = !performance;
        break;
        
    case 'm':
        material_mode++;
        if (material_mode > 2)
            material_mode = 0;
        printf("material_mode = %d\n", material_mode);
        lists();
        break;
        
    case 'd':
        glmDelete(model);
        init();
        lists();
        break;
        
    case 'w':
        glGetIntegerv(GL_POLYGON_MODE, params);
        if (params[0] == GL_FILL)
            glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
        else
            glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
        break;
        
    case 'c':
        if (glIsEnabled(GL_CULL_FACE))
            glDisable(GL_CULL_FACE);
        else
            glEnable(GL_CULL_FACE);
        break;
        
    case 'b':
        bounding_box = !bounding_box;
        break;
        
    case 'n':
        facet_normal = !facet_normal;
        lists();
        break;
        
    case 'r':
        glmReverseWinding(model);
        lists();
        break;
        
    case 's':
        glmScale(model, 0.8);
        lists();
        break;
        
    case 'S':
        glmScale(model, 1.25);
        lists();
        break;
        
    case 'o':
        //printf("Welded %d\n", glmWeld(model, weld_distance));
        glmVertexNormals(model, smoothing_angle);
        lists();
        break;
        
    case 'O':
        weld_distance += 0.01;
        printf("Weld distance: %.2f\n", weld_distance);
        glmWeld(model, weld_distance);
        glmFacetNormals(model);
        glmVertexNormals(model, smoothing_angle);
        lists();
        break;
        
    case '-':
        smoothing_angle -= 1.0;
        printf("Smoothing angle: %.1f\n", smoothing_angle);
        glmVertexNormals(model, smoothing_angle);
        lists();
        break;
        
    case '+':
        smoothing_angle += 1.0;
        printf("Smoothing angle: %.1f\n", smoothing_angle);
        glmVertexNormals(model, smoothing_angle);
        lists();
        break;
        
    case 'W':
        glmScale(model, 1.0/scale);
        glmWriteOBJ(model, "out.obj", GLM_SMOOTH | GLM_MATERIAL);
        break;
        
    case 'R':
        {
            GLuint i;
            GLfloat swap;
            for (i = 1; i <= model->numvertices; i++) {
                swap = model->vertices[3 * i + 1];
                model->vertices[3 * i + 1] = model->vertices[3 * i + 2];
                model->vertices[3 * i + 2] = -swap;
            }
            glmFacetNormals(model);
            lists();
            break;
        }
        
    case 27:
        exit(0);
        break;
    }
    
    glutPostRedisplay();
}
Пример #28
0
void loadGroup(string path, map<string, ofMesh>& groups, bool generateNormals)
{
	path = ofToDataPath(path);

	groups.clear();

	GLMmodel* m;

	m = glmReadOBJ((char*)path.c_str());

	if (generateNormals)
	{
		glmFacetNormals(m);
	}

	glmReverseWinding(m);

	GLMgroup *g = m->groups;

	while (g)
	{
		string name = g->name;

		ofMesh t;
		GLMtriangle *p = m->triangles;

		for (int j = 0; j < g->numtriangles; j++)
		{
			GLMtriangle tri = p[g->triangles[j]];

			for (int k = 0; k < 3; k++)
			{
				GLfloat *v = m->vertices + (tri.vindices[k] * 3);
				t.addVertex(ofVec3f(v[0], v[1], v[2]));

				if (m->colors)
				{
					GLfloat *c = m->colors + (tri.vindices[k] * 3);
					t.addColor(ofFloatColor(c[0], c[1], c[2]));
				}

				if (m->normals && ofInRange(tri.nindices[k], 0, m->numnormals))
				{
					GLfloat *n = m->normals + (tri.nindices[k] * 3);
					t.addNormal(ofVec3f(n[0], n[1], n[2]));
				}

				if (m->texcoords && ofInRange(tri.tindices[k], 0, m->numtexcoords))
				{
					GLfloat *c = m->texcoords + (tri.tindices[k] * 2);
					t.addTexCoord(ofVec2f(c[0], c[1]));
				}
			}
		}

		groups[name] = t;
		g = g->next;
	}

	glmDelete(m);
}
Пример #29
0
CGlObjLoader::~CGlObjLoader(){
    glmDelete(pmodel);
}