/** * import umdraw mesh list */ bool UMSoftwareIO::import_mesh_list(UMMeshList& dst, const umio::UMObjectPtr src, const umstring& absolute_file_path) { if (!src) return false; bool result = false; umio::UMMesh::IDToMeshMap::iterator it = src->mutable_mesh_map().begin(); for (; it != src->mutable_mesh_map().end(); ++it) { umio::UMMesh& ummesh = it->second; UMMeshPtr mesh(std::make_shared<UMMesh>()); dst.push_back(mesh); result = true; sort_by_material(ummesh); load_material(absolute_file_path, mesh, ummesh); load_vertex_index(mesh, ummesh); load_vertex(mesh, ummesh); load_normal(mesh, ummesh); load_uv(mesh, ummesh); load_skin(mesh, ummesh); mesh->update_box(); } return result; }
obj_loader::obj_loader(string file_name) : movable() { sc_x = 1; sc_y = 1; sc_z = 1; init(0,0,0,0,1,0); load_object(file_name); string material_file = file_name.substr(0, file_name.length() - 4); load_material(material_file.append(".mtl")); }
/** * FbxNode からメッシュ情報を再帰的に読み込む * * @param node FbxNode */ void FbxFileLoader::load_mesh_recursive( FbxNode* node ) { load_mesh( node->GetMesh() ); // std::cout << "material count : " << node->GetMaterialCount() << std::endl; for ( int n = 0; n < node->GetMaterialCount(); n++ ) { fbx_material_index_ = n; load_material( node->GetMaterial( n ) ); } for ( int n = 0; n < node->GetChildCount(); n++ ) { load_mesh_recursive( node->GetChild( n ) ); } }
struct model *load_obj_model(char *filename) { char dirname[1024]; char line[200]; struct model *model; struct mesh *curmesh = NULL; int fvp[20], fvt[20], fvn[20]; char *p, *s; int i, n; FILE *fp; strlcpy(dirname, filename, sizeof dirname); p = strrchr(dirname, '/'); if (!p) p = strrchr(dirname, '\\'); if (p) *p = 0; else strlcpy(dirname, ".", sizeof dirname); printf("loading obj model: %s\n", filename); fp = fopen(filename, "r"); if (!fp) { fprintf(stderr, "error: cannot load model '%s'\n", filename); return NULL; } model = malloc(sizeof(struct model)); memset(model, 0, sizeof(struct model)); model->min[0] = model->min[1] = model->min[2] = 1e10; model->max[0] = model->max[1] = model->max[2] = -1e10; model->radius = 0; while (1) { if (!fgets(line, sizeof line, fp)) break; s = strtok(line, SEP); if (!s) { continue; } else if (!strcmp(s, "v")) { char *x = strtok(NULL, SEP); char *y = strtok(NULL, SEP); char *z = strtok(NULL, SEP); add_position(model, atof(x), atof(y), atof(z)); } else if (!strcmp(s, "vt")) { char *u = strtok(NULL, SEP); char *v = strtok(NULL, SEP); add_texcoord(model, atof(u), atof(v)); } else if (!strcmp(s, "vn")) { char *x = strtok(NULL, SEP); char *y = strtok(NULL, SEP); char *z = strtok(NULL, SEP); add_normal(model, atof(x), atof(y), atof(z)); } else if (!strcmp(s, "f")) { n = 0; s = strtok(NULL, SEP); while (s) { if (*s) { splitfv(s, fvp+n, fvt+n, fvn+n); n++; } s = strtok(NULL, SEP); } for (i = 1; i < n - 1; i++) { add_triangle(curmesh, fvp[0], fvt[0], fvn[0], fvp[i], fvt[i], fvn[i], fvp[i+1], fvt[i+1], fvn[i+1]); } } else if (!strcmp(s, "mtllib")) { s = strtok(NULL, SEP); model->material = load_material(dirname, s); } else if (!strcmp(s, "usemtl")) { s = strtok(NULL, SEP); curmesh = find_mesh(model, s); } } model->radius = sqrtf(model->radius); fclose(fp); return model; }
void initScene() { cubeShader = new Shader( "basic.vert", "basic.frag" ); lampShader = new Shader( "basic.vert", "lamp.frag" ); glClearColor( 0.2f, 0.2f, 0.3f, 1.0f ); glEnable( GL_DEPTH_TEST ); //-------------------------------------------------- // Geometry //-------------------------------------------------- // cube glGenVertexArrays( 1, &cubeVAO ); glGenBuffers( 1, &VBO ); glBindVertexArray( cubeVAO ); { // vbo glBindBuffer( GL_ARRAY_BUFFER, VBO ); glBufferData( GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW ); // position glEnableVertexAttribArray( 0 ); glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0 ); // normals glEnableVertexAttribArray( 1 ); glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)) ); } glBindVertexArray( 0 ); glBindBuffer( GL_ARRAY_BUFFER, 0 ); // lamp glGenVertexArrays( 1, &lampVAO ); glBindVertexArray( lampVAO ); { glBindBuffer( GL_ARRAY_BUFFER, VBO ); // positions glEnableVertexAttribArray( 0 ); glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0 ); } glBindVertexArray( 0 ); cubeShader->use(); uniformLocLightColor = glGetUniformLocation( cubeShader->program, "lightColor" ); uniformLocObjectColor = glGetUniformLocation( cubeShader->program, "objectColor" ); glUniform3f( uniformLocLightColor, 1.0f, 1.0f, 1.0f ); glUniform3f( uniformLocObjectColor, 0.5f, 0.2f, 0.0f ); //-------------------------------------------------- // Textures //-------------------------------------------------- /* glGenTextures( 1, &texture1 ); glBindTexture( GL_TEXTURE_2D, texture1 ); //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT ); //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); int width, height; unsigned char* image = SOIL_load_image( "../../container.jpg", &width, &height, 0, SOIL_LOAD_RGB ); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image ); glGenerateMipmap( GL_TEXTURE_2D ); SOIL_free_image_data( image ); glBindTexture( GL_TEXTURE_2D, 0 ); // second texture glGenTextures( 1, &texture2 ); glBindTexture( GL_TEXTURE_2D, texture2 ); image = SOIL_load_image( "../../awesomeface.png", &width, &height, 0, SOIL_LOAD_RGB ); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image ); glGenerateMipmap( GL_TEXTURE_2D ); SOIL_free_image_data( image ); glBindTexture( GL_TEXTURE_2D, 0 ); */ //-------------------------------------------------- // MVP Matrix //-------------------------------------------------- // setup camera camera = new Camera( glm::vec3(1.0f,0.0f,3.0f), // position glm::vec3(0.0f,0.0f,-1.0f), // front glm::vec3(0.0f,1.0f,0.0f), // up 45.0f, // fov 0.1f, // near 100.0f ); // far model = glm::mat4(1.0f); view = glm::mat4(1.0f); view = glm::lookAt(camera->getPos(), camera->getPos() + camera->getFront(), camera->getUp() ); projection = glm::mat4(1.0f); projection = glm::perspective( camera->getFov(), (GLfloat)WIN_WIDTH/WIN_HEIGHT, camera->getNear(), camera->getFar() ); cubeShader->use(); uniformLocView = glGetUniformLocation( cubeShader->program, "V" ); glUniformMatrix4fv( glGetUniformLocation(cubeShader->program, "M"), 1, GL_FALSE, glm::value_ptr(model) ); glUniformMatrix4fv( uniformLocView, 1, GL_FALSE, glm::value_ptr(view) ); glUniformMatrix4fv( glGetUniformLocation(cubeShader->program, "P"), 1, GL_FALSE, glm::value_ptr(projection) ); // setup material material = load_material( "./Materials/ruby.material" ); glUniform3f( glGetUniformLocation(cubeShader->program, "material.ambient"), material->getAmbient().x, material->getAmbient().y, material->getAmbient().z ); glUniform3f( glGetUniformLocation(cubeShader->program, "material.diffuse"), material->getDiffuse().x, material->getDiffuse().y, material->getDiffuse().z ); glUniform3f( glGetUniformLocation(cubeShader->program, "material.specular"), material->getSpecular().x, material->getSpecular().y, material->getSpecular().z ); glUniform1f( glGetUniformLocation(cubeShader->program, "material.shininess"), material->getShininess() ); // setup lighting parameters lampPos = glm::vec3( 1.2f, 0.8f, -1.0f ); glUniform3f( glGetUniformLocation(cubeShader->program, "light.position"), lampPos.x, lampPos.y, lampPos.z ); glUniform3f( glGetUniformLocation(cubeShader->program, "light.ambient"), 0.5f, 0.5f, 0.5f ); glUniform3f( glGetUniformLocation(cubeShader->program, "light.diffuse"), 0.8f, 0.8f, 0.8f ); glUniform3f( glGetUniformLocation(cubeShader->program, "light.specular"), 1.0f, 1.0f, 1.0f ); // setup lamp vertices lampShader->use(); uniformLocView = glGetUniformLocation( lampShader->program, "V" ); model = glm::mat4( 1.0f ); model = glm::translate( model, lampPos ); model = glm::scale( model, glm::vec3(0.2f, 0.2f, 0.2f) ); glUniformMatrix4fv( glGetUniformLocation(lampShader->program, "M"), 1, GL_FALSE, glm::value_ptr(model) ); glUniformMatrix4fv( uniformLocView, 1, GL_FALSE, glm::value_ptr(view) ); glUniformMatrix4fv( glGetUniformLocation(lampShader->program, "P"), 1, GL_FALSE, glm::value_ptr(projection) ); }
static bool load_objects(Lib3dsFile *file, Scene *scene) { // load meshes unsigned long poly_count = 0; Lib3dsMesh *m = file->meshes; while(m) { Lib3dsNode *node = lib3ds_file_node_by_name(file, m->name, LIB3DS_OBJECT_NODE); if(!node) { warning("object \"%s\" does not have a corresponding node!", m->name); } Vector3 node_pos = node ? CONV_VEC3(node->data.object.pos) : Vector3(); Quaternion node_rot = node ? CONV_QUAT(node->data.object.rot) : Quaternion(); Vector3 node_scl = node ? CONV_VEC3(node->data.object.scl) : Vector3(1,1,1); Vector3 pivot = node ? CONV_VEC3(node->data.object.pivot) : Vector3(); // load the vertices Vertex *varray = new Vertex[m->points]; Vertex *vptr = varray; for(int i=0; i<(int)m->points; i++) { vptr->pos = CONV_VEC3(m->pointL[i].pos) - node_pos; vptr->pos.transform(node_rot); if(m->texels) { vptr->tex[0] = vptr->tex[1] = CONV_TEXCOORD(m->texelL[i]); } vptr++; } if(m->faces) { poly_count += m->faces; // -------- object --------- Object *obj = new Object; obj->set_dynamic(false); obj->name = m->name; obj->set_position(node_pos - pivot); obj->set_rotation(node_rot); obj->set_scaling(node_scl); obj->set_pivot(pivot); // load the polygons Triangle *tarray = new Triangle[m->faces]; Triangle *tptr = tarray; for(int i=0; i<(int)m->faces; i++) { *tptr = CONV_TRIANGLE(m->faceL[i]); tptr->normal = CONV_VEC3(m->faceL[i].normal); tptr->smoothing_group = m->faceL[i].smoothing; tptr++; } // set the geometry data to the object obj->get_mesh_ptr()->set_data(varray, m->points, tarray, m->faces); obj->get_mesh_ptr()->calculate_normals(); delete [] tarray; // load the material load_material(file, m->faceL[0].material, obj->get_material_ptr()); // load the keyframes (if any) if(load_keyframes(file, m->name, LIB3DS_OBJECT_NODE, obj)) { obj->set_position(Vector3()); obj->set_rotation(Quaternion()); obj->set_scaling(Vector3(1, 1, 1)); } scene->add_object(obj); } else { // --------- curve ------------ Curve *curve = new CatmullRomSplineCurve; curve->name = m->name; Vector3 offs = node_pos - pivot; for(int i=0; i<(int)m->points; i++) { curve->add_control_point(varray[i].pos + offs); } scene->add_curve(curve); } delete [] varray; m = m->next; } scene->set_poly_count(poly_count); return true; }