Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: obj file loader dodecahedron

  1. #1
    Junior Member Newbie
    Join Date
    May 2013
    Posts
    6

    obj file loader dodecahedron

    Hello everyone,

    im new to opengl and have a question about loading an obj file into an already existing program. The file is a dodecahedron.
    And i want to load it as a edge list.

    I know that i have to use vertices, faces and normals like

    V= (V1,V2,V3,V4,V5)
    and the F like f1 = (1,5,2) f2=(2,3,5) f3= (5,3,4)

    So my obj Loader looks like this now:

    Code :
    struct Vertex
    {
        vec3 position;
        vec2 texcoord;
        vec3 normal;
    };
     
    struct VertRef
    {
        VertRef( int v, int vt, int vn )
            : v(v), vt(vt), vn(vn)
        { }
        int v, vt, vn;
    };
     
    vector< Vertex > LoadOBJ( istream& in )
    {
    	vector< Vertex > verts;
        vector< vec4 > positions( 1, vec4( 0, 0, 0, 0 ) );
        vector< vec3 > texcoords( 1, vec3( 0, 0, 0 ) );
        vector< vec3 > normals( 1, vec3( 0, 0, 0 ) );
        string lineStr;
        while( getline( in, lineStr ) )
        {
            istringstream lineSS( lineStr );
            string lineType;
            lineSS >> lineType;
            // vertex
            if( lineType == "v" )
            {
                float x = 0, y = 0, z = 0, w = 1;
                lineSS >> x >> y >> z >> w;
                positions.push_back( vec4( x, y, z, w ) );
            }
     
            // polygon
            if( lineType == "f" )
            {
                vector< VertRef > refs;
                string refStr;
                while( lineSS >> refStr )
                {
                    istringstream ref( refStr );
                    string vStr, vtStr, vnStr;
                    getline( ref, vStr, '/' );
                    getline( ref, vtStr, '/' );
                    getline( ref, vnStr, '/' );
                    int v = atoi( vStr.c_str() );
                    int vt = atoi( vtStr.c_str() );
                    int vn = atoi( vnStr.c_str() );
                    v  = (  v >= 0 ?  v : positions.size() +  v );
                    vt = ( vt >= 0 ? vt : texcoords.size() + vt );
                    vn = ( vn >= 0 ? vn : normals.size()   + vn );
                    refs.push_back( VertRef( v, vt, vn ) );
                }
                if( refs.size() < 3 )
                {
                    // error, skip
                    continue;
                }
                // triangulate, assuming n>3-gons are convex and coplanar
                VertRef* p[3] = { &refs[0], NULL, NULL };
                for( size_t i = 1; i+1 < refs.size(); ++i )
                {
                    p[1] = &refs[i+0];
                    p[2] = &refs[i+1];
     
                    vec3 U( positions[ p[1]->v ] - positions[ p[0]->v ] );
                    vec3 V( positions[ p[2]->v ] - positions[ p[0]->v ] );
                    vec3 faceNormal = normalize( cross( U, V ) );
     
                    for( size_t j = 0; j < 3; ++j )
                    {
                        Vertex vert;
                        vert.position = vec3( positions[ p[j]->v ] );
                        vert.texcoord = vec2( texcoords[ p[j]->vt ] );
                        vert.normal   = ( p[j]->vn != 0 ? normals[ p[j]->vn ] : faceNormal );
                        verts.push_back( vert );
                    }
                }
            }
        }
     
        return verts;
    }
    vector< Vertex > model;
     
    	glPushMatrix();
    	glTranslatef(0.0,0.0,0.0);
    	 glColor3ub(255, 255, 0);
     
       glEnableClientState( GL_VERTEX_ARRAY );
        glEnableClientState( GL_TEXTURE_COORD_ARRAY );
        glEnableClientState( GL_NORMAL_ARRAY );
     
         glVertexPointer( 2,GL_FLOAT, sizeof(Vertex), &model[0].position );
        glTexCoordPointer( 1, GL_FLOAT, sizeof(Vertex), &model[0].texcoord );
        glNormalPointer( GL_FLOAT, sizeof(Vertex), &model[0].normal );
     
        glDrawArrays( GL_TRIANGLES, 0, model.size() );
     
        glDisableClientState( GL_VERTEX_ARRAY );
        glDisableClientState( GL_TEXTURE_COORD_ARRAY );
        glDisableClientState( GL_NORMAL_ARRAY ); 
     
    	glPopMatrix();

    The dodecahedron obj file looks like this

    Code :
    v 1.21412 0 1.58931
    v 0.375185 1.1547 1.58931
    v -0.982247 0.713644 1.58931
    v -0.982247 -0.713644 1.58931
    v 0.375185 -1.1547 1.58931
    v 1.96449 0 0.375185
    v 0.607062 1.86835 0.375185
    v -1.58931 1.1547 0.375185
    v -1.58931 -1.1547 0.375185
    v 0.607062 -1.86835 0.375185
    v 1.58931 1.1547 -0.375185
    v -0.607062 1.86835 -0.375185
    v -1.96449 0 -0.375185
    v -0.607062 -1.86835 -0.375185
    v 1.58931 -1.1547 -0.375185
    v 0.982247 0.713644 -1.58931
    v -0.375185 1.1547 -1.58931
    v -1.21412 0 -1.58931
    v -0.375185 -1.1547 -1.58931
    v 0.982247 -0.713644 -1.58931
    f  1 2 3 4 5
    f  1 6 11 7 2
    f  2 7 12 8 3
    f  3 8 13 9 4
    f  4 9 14 10 5
    f  5 10 15 6 1
    f  16 11 6 15 20
    f  17 12 7 11 16
    f  18 13 8 12 17
    f  19 14 9 13 18
    f  20 15 10 14 19
    f  20 19 18 17 16

    Can you help me?

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    749
    What is wrong with it? (You'll save everybody a lot of time if you describe what to look for )
    Also, have you seen the other two recent threads on OBJ loading?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •