Jump to content
  • Advertisement
Sign in to follow this  
Syerjchep

OpenGL Adding png texture abilities to a triangulated wavefront object loader.

This topic is 2540 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I had code for a wavefront object file loader.
It loaded things fine, with materials setting colors and such.
Then I tried to add textures.
I believe the problem lies within the texture coordnate phrasing.
Code below:


struct vec2{float x,y;int e;};
struct vec3{float x,y,z;int e;};
struct vec4{float a,b,c,d;int e;};

class globjfaces
{
public:
int verts,fv,shapes,texs,tv,nmats;
vec3 vectors[36001];
vec3 facevector[36001];
vec3 texvex[36001];
vec3 texface[36001];
string mats[101];
vec3 matcolors[101];
string texnames[101];
void show(float x,float y,float z);
void readin(char *filenam);
void makelist();
globjfaces(){verts = 1;fv = 0;texs=1;tv=0;nmats=0;};
GLuint displaylist;
};

std::string v3str(vec3 in)
{
std::string ret = "";
ret+=fstr(in.x);
ret+=" ";
ret+=fstr(in.y);
ret+=" ";
ret+=fstr(in.z);
return ret;
}

string sideofslash(string in,bool side)
{
int spos = in.find("/");
if(spos == string::npos)
return in;
if(side)
return in.substr(0,spos);
else
return in.substr(spos+1,in.length()-spos);
}

void normalize(float * vect) //scales a vector a length of 1
{
float length;
int a;

length=sqrt( //A^2 + B^2 + C^2 = length^2
pow(vect[0],2)+
pow(vect[1],2)+
pow(vect[2],2)
);

for (a=0;a<3;++a) //divides vector by its length to normalise
{
vect[a]/=length;
}
}

void crossProduct(float *c,float a[3], float b[3]) //finds the cross product of two vectors
{
c[0]=a[1]*b[2] - b[1]*a[2];
c[1]=a[2]*b[0] - b[2]*a[0];
c[2]=a[0]*b[1] - b[0]*a[1];
}

void getFaceNormal(float *norm,float pointa[3],float pointb[3],float pointc[3])
{
float vect[2][3];
int a,b;
float point[3][3];

for (a=0;a<3;++a)
{
point[0][a]=pointa[a]; //copies points into point[][]
point[1][a]=pointb[a];
point[2][a]=pointc[a];
}

for (a=0;a<2;++a) //calculates vectors from point[0] to point[1]
{ //and point[0] to point[2]
for (b=0;b<3;++b)
{
vect[a]=point[2-a]-point[0];
}
}

crossProduct(norm,vect[0],vect[1]); //calculates vector at 90° to to 2 vectors
normalize(norm); //makes the vector length 1
}

int range = 110;

void globjfaces::readin(char *filename)
{
cerr<<"new file: "<<filename<<"\n";
int currentmat = -1;
std::string line;
ifstream infile(filename);
while(!infile.eof())
{
getline(infile,line);
if(numwords(line) > 1)
{
if(getword(line,0) == "usemtl")
{
for(int i = 0; i<11; i++)
{
if(mats == getword(line,1))
{
currentmat = i;
break;
}
}
}

else if(getword(line,0) == "v")
{
vec3 tmp;
tmp.x = atof(getword(line,1).c_str());
tmp.y = atof(getword(line,2).c_str());
tmp.z = atof(getword(line,3).c_str());

vectors[verts] = tmp;
verts++;
}

else if(getword(line,0) == "f")
{
for(int i = 1; i<numwords(line); i++)
{
facevector[fv] = vectors[atoi(sideofslash(getword(line,i),true).c_str())];
facevector[fv].e = currentmat;
texface[fv] = texvex[atoi(sideofslash(getword(line,i),false).c_str())];
fv++;
}
}

else if(getword(line,0) == "vt")
{
vec3 tmp;
tmp.x = atof(getword(line,1).c_str());
tmp.y = atof(getword(line,2).c_str());
tmp.z = atof(getword(line,3).c_str());

texvex[texs] = tmp;
texs++;
}
else if(getword(line,0) == "mtllib")
{
ifstream matfile(("media/"+getword(line,1)).c_str());
string nl;
while(!matfile.eof())
{
getline(matfile,nl);
if(numwords(nl) > 1)
{
if(getword(nl,0) == "newmtl")
{
nmats++;
mats[nmats] = getword(nl,1);
}
else if(getword(nl,0) == "Kd")
{
if(nmats > 0)
{
vec3 tmp;
tmp.x = atof(getword(nl,1).c_str());
tmp.y = atof(getword(nl,2).c_str());
tmp.z = atof(getword(nl,3).c_str());
matcolors[nmats] = tmp;
}
}
else if(getword(nl,0) == "map_Kd")
{
if(nmats > 0)
{
//cerr<<"Media/"+getword(nl,1)<<"---"<<getword(nl,1)<<"\n";
LoadGLTexture("Media/"+getword(nl,1),getword(nl,1));
texnames[nmats] = getword(nl,1);
}
}
}
}
}
}
}

displaylist = glGenLists(1);
glNewList(displaylist,GL_COMPILE);
glBindTexture(GL_TEXTURE_2D, NULL);
for(int i = 0; i<fv/3; i++)
{
float norm[3];
float testa[3];
testa[0] = facevector[(i*3)+0].x;
testa[1] = facevector[(i*3)+0].y;
testa[2] = facevector[(i*3)+0].z;
float testb[3];
testb[0] = facevector[(i*3)+1].x;
testb[1] = facevector[(i*3)+1].y;
testb[2] = facevector[(i*3)+1].z;
float testc[3];
testc[0] = facevector[(i*3)+2].x;
testc[1] = facevector[(i*3)+2].y;
testc[2] = facevector[(i*3)+2].z;
getFaceNormal(norm,testa,testb,testc);
glNormal3f(-norm[0],-norm[1],-norm[2]);

if(texnames[facevector[i*3].e].length() > 3)
{
cerr<<texnames[facevector[i*3].e]<<"-"<<gettex(texnames[facevector[i*3].e])<<"\n";
glBindTexture(GL_TEXTURE_2D, gettex(texnames[facevector[i*3].e]));
}
else
glBindTexture(GL_TEXTURE_2D, NULL);

glBegin(GL_TRIANGLES);

for(int b=0;b<3;b++)
{
cerr<<(i*3)+b<<"|||"<<texface[(i*3)+b].x<<"|||"<<texface[(i*3)+b].y<<"|||"<<texface[(i*3)+b].z<<"\n";
glTexCoord2f(texface[(i*3)+b].x,texface[(i*3)+b].y);
glColor3f(matcolors[facevector[(i*3)+b].e].x*0.75,matcolors[facevector[(i*3)+b].e].y*0.75,matcolors[facevector[(i*3)+b].e].z*0.75);
glVertex3f(facevector[(i*3)+b].x, facevector[(i*3)+b].y, facevector[(i*3)+b].z);
}
glEnd();

}

glBindTexture(GL_TEXTURE_2D, NULL);
glColor3f(baser,baseg,baseb);
glEndList();
}

void globjfaces::show(float x,float y,float z)
{
glTranslatef(x,y,z);
glCallList(displaylist);
glTranslatef(-x,-y,-z);
}


I either need someone to suggest a premade libary for loading wavefront object files and showing them in opengl, or help fixing this code.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!