Texture coordinate problem

Member
Posts: 227
Joined: 2008.08
Post: #1
So, i had gone to making a map loader when i realized that my texture coordinates were off on the map model.
So I figured I should work on the problem. No?

I eventually got it working perfectly and I decided to clean it up (the code of course). Then, I came to running it again and got this:
[Image: picture2rr9.png]

I forgot to make a snapshot and now I can't find what I did.

Here is the model loading and drawing code:
Code:
typedef struct{
    float x,y,z;
}oeVec3;
typedef struct{
    float x,y;
}oeVec2;
typedef struct{
    oeuint v[3],t[3],n[3];
}oeFace;
typedef struct{
    struct{oeuint v,f;}count;
    oeVec3* v;
    oeVec2* t;
    oeVec3* n;
    oeFace* f;
}oeMesh;



inline void oeMeshDraw(oeMesh* m){
    oeVec3* v;oeFace* f;oeVec3* n;oeVec2* t;
    glBegin(GL_TRIANGLES);
        for(int cf=0; cf<m->count.f; cf++){
            f=&m->f[cf];
            for (int cv = 0; cv <3; cv++) {
                v=&m->v[m->f[cf].v[cv]];
                t=&m->t[m->f[cf].t[cv]];
                n=&m->n[m->f[cf].n[cv]];
                
                glNormal3f(n->x,n->y,n->z);
                glTexCoord2f(t->x,t->y);
                glVertex3f(v->x,v->y,v->z);
            }
        }
    glEnd();

}


oeMesh* oeLoadObjFile(char* filename){
    oeMesh* m;
    int fcount,vcount,ncount,tcount;
    unsigned int cv,cf,ct,cn,v[3],n[3],t[3];
    char buffer[256];
    float x,y,z;
    
    cv=cf=ct=cn=fcount=vcount=ncount=tcount=0;
    
    FILE* file=fopen(filename,"r");
    if(file==NULL){
        printf("Failed to open %s\n",filename);
    //    return NULL;
    exit(1);
    }
    while(!feof(file)){
        fgets(buffer,255,file);
        if((buffer[0]=='v')&&(buffer[1]==' ')){
            vcount++;
            
        }
        else if((buffer[0]=='f')&&(buffer[1]==' ')){
            fcount++;
            
        }
        else if((buffer[0]=='v')&&(buffer[1]=='n')){
            ncount++;
            
        }
        else if((buffer[0]=='v')&&(buffer[1]=='t')){
            tcount++;
            
        }
    }
    rewind(file);


    m=calloc(1,sizeof(oeMesh));
    m->v=calloc(vcount,sizeof(oeVec3));
    m->f=calloc(fcount,sizeof(oeFace));
    m->n=calloc(ncount,sizeof(oeVec3));
    m->t=calloc(tcount,sizeof(oeVec2));
    
    if(m->f==NULL){
        printf("Couldn't allocate %i faces for file named %s\n",fcount,filename);
        return NULL;
    }

    if(m->v==NULL){
        printf("Couldn't allocate %i vertices for file named %s\n",vcount,filename);
        return NULL;
    }
    if(m->n==NULL){
        printf("Couldn't allocate %i normals for file named %s\n",ncount,filename);
        return NULL;
    }

    if(m->t==NULL){
        printf("Couldn't allocate %i uv's for file named %s\n",tcount,filename);
        return NULL;
    }
    
    while(!feof(file)){
        fgets(buffer,255,file);
        if(3==sscanf(buffer,"v %f %f %f",&x,&y,&z)){
            m->v[cv].x=x;
            m->v[cv].y=y;
            m->v[cv].z=z;
            cv++;
        }
        else if(2==sscanf(buffer,"vt %f %f",&x,&y)){
            m->t[ct].x=x;
            m->t[ct].y=y;
            
            m->v[ct].tex[0]=x;
            m->v[ct].tex[1]=y;
            ct++;
        }
        else if(3==sscanf(buffer,"vn %f %f %f",&x,&y,&z)){
            m->n[cn].x=x;
            m->n[cn].y=y;
            m->n[cn].z=z;
            
            m->v[cn].normal.x=x;
            m->v[cn].normal.y=y;
            m->v[cn].normal.z=z;
            cn++;
        }
        else if(9==sscanf(buffer,"f %i/%i/%i %i/%i/%i %i/%i/%i",&v[0],&t[0],&n[0],&v[1],&t[1],&n[1],&v[2],&t[2],&n[2])){
        
            if(v[0]<1||v[1]<1||v[2]<1){
                printf("Invalid vertex id in file %s\n",filename);
                exit(1);
            }
            m->f[cf].v[0]=v[0]-1;
            m->f[cf].v[1]=v[1]-1;
            m->f[cf].v[2]=v[2]-1;
            
            m->f[cf].t[0]=t[0]-1;
            m->f[cf].t[1]=t[1]-1;
            m->f[cf].t[2]=t[2]-1;
            
            m->f[cf].n[0]=n[0]-1;
            m->f[cf].n[1]=n[1]-1;
            m->f[cf].n[2]=n[2]-1;
            cf++;
        }
        else if(6==sscanf(buffer,"f %d//%d %d//%d %d//%d",&v[0],&n[0],&v[1],&n[1],&v[2],&n[2])){
        
            if(v[0]<1||v[1]<1||v[2]<1){
                printf("Invalid vertex id in file %s\n",filename);
                exit(1);
            }
            
            m->f[cf].v[0]=v[0]-1;
            m->f[cf].v[1]=v[1]-1;
            m->f[cf].v[2]=v[2]-1;
            
            m->f[cf].n[0]=n[0]-1;
            m->f[cf].n[1]=n[1]-1;
            m->f[cf].n[2]=n[2]-1;
            cf++;
        }
        else if(3==sscanf(buffer,"f %d %d %d",&v[0],&v[1],&v[2])){
        
            if(v[0]<1||v[1]<1||v[2]<1){
                printf("Invalid vertex id in file %s\n",filename);
                exit(1);
            }
            m->f[cf].v[0]=v[0]-1;
            m->f[cf].v[1]=v[1]-1;
            m->f[cf].v[2]=v[2]-1;
            cf++;
        }
    }
    if(cf==fcount&&cv==vcount&&ct==tcount&&cn==vcount){
        m->count.f=fcount;
        m->count.v=vcount;
    }else{
        printf("\nError loading %s\nFound|Stated to be\n",filename);
        printf("Vertex:%d|%d\n",cv,vcount);
        printf("Face:%d|%d\n",cf,fcount);
        printf("TexCoord:%d|%d\n",ct,tcount);
        printf("Normals:%d|%d\n",cn,ncount);
        exit(1);
    }
    
    fclose(file);
    return m;
}

And the model:
Code:
v -1.00000000 1.3953130e-2 1.00000000
v -1.00000000 3.21747141 1.00000000
v 1.00000000 3.21747141 1.00000000
v 1.00000000 1.3953130e-2 1.00000000
v -1.00000000 1.3953130e-2 -1.00000000
v -1.00000000 3.21747141 -1.00000000
v 1.00000000 3.21747141 -1.00000000
v 1.00000000 1.3953130e-2 -1.00000000

vt 0.0000000e+0 0.0000000e+0
vt 0.0000000e+0 1.00000000
vt 8.3266727e-17 0.64152600
vt 0.22380019 1.00000000
vt 0.22380019 0.64152600
vt 0.24516113 0.15709447
vt 0.27791990 0.64152600
vt 0.38696908 0.48443154
vt 0.42226629 0.15709447
vt 0.53131547 5.5511151e-17
vt 0.56407424 0.48443154
vt 0.80923537 0.64152600

vn -0.57735027 -0.57735027 0.57735027
vn -0.57735027 0.57735027 0.57735027
vn 0.57735027 0.57735027 0.57735027
vn 0.57735027 -0.57735027 0.57735027
vn -0.57735027 -0.57735027 -0.57735027
vn -0.57735027 0.57735027 -0.57735027
vn 0.57735027 0.57735027 -0.57735027
vn 0.57735027 -0.57735027 -0.57735027

f 1/9/1 4/10/4 2/11/2
f 1/9/1 5/6/5 4/10/4
f 2/11/2 4/10/4 3/12/3
f 2/11/2 5/6/5 1/9/1
f 2/11/2 7/7/7 6/8/6
f 3/12/3 7/7/7 2/11/2
f 4/10/4 5/6/5 8/1/8
f 4/3/4 7/4/7 3/2/3
f 5/6/5 7/7/7 8/1/8
f 6/8/6 5/6/5 2/11/2
f 6/8/6 7/7/7 5/6/5
f 8/5/8 7/4/7 4/3/4

Edit:
Problem goes away in fullscreen mode. What is going on?
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL global and local coordinate problem. mikey 4 3,815 May 26, 2009 08:48 AM
Last Post: mikey
  Porting SDL to Cocoa OpenGL view--texture problem smittyz 7 5,002 Jul 21, 2007 07:53 PM
Last Post: smittyz
  terrain texture coordinate calculation akb825 12 8,490 Feb 27, 2007 04:56 PM
Last Post: akb825
  problem with basic texture mapping class wyrmmage 8 4,152 Dec 22, 2006 02:25 PM
Last Post: wyrmmage
  Cocoa Texture Loading Code Problem Nick 1 3,017 Oct 28, 2005 11:44 PM
Last Post: OneSadCookie