Opengl problem with texture in model from obj
Posted
by subSeven
on Stack Overflow
See other posts from Stack Overflow
or by subSeven
Published on 2010-05-25T22:01:48Z
Indexed on
2010/05/25
23:21 UTC
Read the original article
Hit count: 312
Hello! I writing small program in OpenGL, and I have problem ( textures are skew, and I dont know why, this model work in another obj viewer) What I have: http://img696.imageshack.us/i/obrazo.png/ What I want http://img88.imageshack.us/i/obraz2d.jpg/
This code where I load texture:
bool success;
ILuint texId;
GLuint image;
ilGenImages(1, &texId);
ilBindImage(texId);
success = ilLoadImage((WCHAR*)fileName.c_str());
if(success)
{
success = ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);
if(!success)
{
return false;
}
}
else
{
return false;
}
glGenTextures(1, &image);
glBindTexture(GL_TEXTURE_2D, image);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH),
ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE,
ilGetData());
ilDeleteImages(1, &texId);
Code to load obj:
triangles.clear();
std::ifstream in;
std::string cmd;
in.open (fileName.c_str());
if (in.is_open())
{
while(!in.eof())
{
in>>cmd;
if(cmd=="v")
{
Vector3d vector;
in>>vector.x;
in>>vector.y;
in>>vector.z;
points.push_back(vector);
}
if(cmd=="vt")
{
Vector2d texcord;
in>>texcord.x;
in>>texcord.y;
texcords.push_back(texcord);
}
if(cmd=="vn")
{
Vector3d normal;
in>>normal.x;
in>>normal.y;
in>>normal.z;
normals.push_back(normal);
}
if(cmd=="f")
{
Triangle triangle;
std::string str;
std::string str1,str2,str3;
std::string delimeter("/");
int pos;
int n;
std::stringstream ss (std::stringstream::in | std::stringstream::out);
in>>str;
pos = str.find(delimeter);
str1 = str.substr(0,pos);
str2 = str.substr(pos+delimeter.length());
pos = str2.find(delimeter);
str3 = str2.substr(pos+delimeter.length());
str2 = str2.substr(0,pos);
ss<<str1;
ss>>n;
triangle.a= n-1;
ss.clear();
ss<<str3;
ss>>n;
triangle.an =n-1;
ss.clear();
ss<<str2;
ss>>n;
ss.clear();
triangle.atc = n-1;
in>>str;
pos = str.find(delimeter);
str1 = str.substr(0,pos);
str2 = str.substr(pos+delimeter.length());
pos = str2.find(delimeter);
str3 = str2.substr(pos+delimeter.length());
str2 = str2.substr(0,pos);
ss<<str1;
ss>>n;
triangle.b= n-1;
ss.clear();
ss<<str3;
ss>>n;
triangle.bn =n-1;
ss.clear();
ss<<str2;
ss>>n;
ss.clear();
triangle.btc = n-1;
in>>str;
pos = str.find(delimeter);
str1 = str.substr(0,pos);
str2 = str.substr(pos+delimeter.length());
pos = str2.find(delimeter);
str3 = str2.substr(pos+delimeter.length());
str2 = str2.substr(0,pos);
ss<<str1;
ss>>n;
triangle.c= n-1;
ss.clear();
ss<<str3;
ss>>n;
triangle.cn =n-1;
ss.clear();
ss<<str2;
ss>>n;
ss.clear();
triangle.ctc = n-1;
triangles.push_back(triangle);
}
cmd = "";
}
in.close();
return true;
}
return false;
Code to draw model:
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glBindTexture(GL_TEXTURE_2D,image);
glBegin(GL_TRIANGLES);
for(int i=0;i<triangles.size();i++)
{
glTexCoord2f(texcords[triangles[i].ctc].x,
texcords[triangles[i].ctc].y);
glNormal3f(normals[triangles[i].cn].x,
normals[triangles[i].cn].y,
normals[triangles[i].cn].z);
glVertex3f( points[triangles[i].c].x,
points[triangles[i].c].y,
points[triangles[i].c].z);
glTexCoord2f(texcords[triangles[i].btc].x,
texcords[triangles[i].btc].y);
glNormal3f(normals[triangles[i].bn].x,
normals[triangles[i].bn].y,
normals[triangles[i].bn].z);
glVertex3f( points[triangles[i].b].x,
points[triangles[i].b].y,
points[triangles[i].b].z);
glTexCoord2f(texcords[triangles[i].atc].x,
texcords[triangles[i].atc].y);
glNormal3f(normals[triangles[i].an].x,
normals[triangles[i].an].y,
normals[triangles[i].an].z);
glVertex3f( points[triangles[i].a].x,
points[triangles[i].a].y,
points[triangles[i].a].z);
}
glEnd();
glDisable(GL_TEXTURE_2D);
Mayby someone find mistake in this
© Stack Overflow or respective owner