我无法将纹理映射到导入到Opengl中的对象。我找到了一个导入obj文件的教程,但我无法弄清楚映射纹理。
这是我写的代码:
class Model
{
public:
Model();
Model(string modelFilename, string textureFilename);
void LoadTexture(string fileName);
void LoadObj(const string& filename, vector<glm::vec4> &vertices, vector<glm::vec3> &normals, vector<GLushort> &elements);
void Draw();
private:
vector<glm::vec4> m_Vertices;
vector<glm::vec3> m_Normals;
vector<GLushort> m_Elements;
string m_ModelFilename;
int m_Texture;
string m_TextureName;
};
Model::Model(string modelFilename, string textureFilename)
{
m_ModelFilename = modelFilename;
m_TextureName = textureFilename;
LoadObj(m_ModelFilename, m_Vertices, m_Normals, m_Elements);
LoadTexture(m_TextureName);
}
void Model::LoadTexture(string TextureName)
{
// Local storage for bmp image data.
BitMapFile *image[1];
string filename = TextureName;
filename.append(".bmp");
// Load the texture.
image[0] = getBMPData(filename);
// Bind grass image to texture index[i].
glBindTexture(GL_TEXTURE_2D, m_Texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image[0]->sizeX, image[0]->sizeY, 0,
GL_RGB, GL_UNSIGNED_BYTE, image[0]->data);
}
void Model::LoadObj(const string& filename, vector<glm::vec4> &vertices, vector<glm::vec3> &normals, vector<GLushort> &elements) {
ifstream in(filename, ios::in);
if (!in) { cerr << "Cannot open " << filename << endl; exit(1); }
string line;
while (getline(in, line)) {
if (line.substr(0,2) == "v ") {
istringstream s(line.substr(2));
glm::vec4 v; s >> v.x; s >> v.y; s >> v.z; v.w = 1.0f;
vertices.push_back(v);
} else if (line.substr(0,2) == "f ") {
istringstream s(line.substr(2));
GLushort a,b,c;
s >> a; s >> b; s >> c;
a--; b--; c--;
elements.push_back(a); elements.push_back(b); elements.push_back(c);
}
else if (line[0] == '#') { /* ignoring this line */}
// else { /* ignoring this line */ }
}
normals.resize(vertices.size(), glm::vec3(0.0, 0.0, 0.0));
for (int i = 0; i < elements.size(); i+=3) {
GLushort ia = elements[i];
GLushort ib = elements[i+1];
GLushort ic = elements[i+2];
glm::vec3 normal = glm::normalize(glm::cross(glm::vec3(vertices[ib]) - glm::vec3(vertices[ia]), glm::vec3(vertices[ic]) - glm::vec3(vertices[ia])));
normals[ia] = normals[ib] = normals[ic] = normal;
}
}
void Model::Draw()
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_Texture);
for (int i = 0; i < m_Elements.size(); i+=3)
{
GLushort ia = m_Elements[i];
GLushort ib = m_Elements[i+1];
GLushort ic = m_Elements[i+2];
glBegin(GL_TRIANGLES);
glBegin(GL_TRIANGLES);
glNormal3f(m_Normals[ia].x,m_Normals[ia].y,m_Normals[ia].z);//
glNormal3f(m_Normals[ib].x,m_Normals[ib].y,m_Normals[ib].z);//
glNormal3f(m_Normals[ic].x,m_Normals[ic].y,m_Normals[ic].z);//
glTexCoord2f(0.0, 0.0);glVertex3f(m_Vertices[ia].x,m_Vertices[ia].y,m_Vertices[ia].z);
glTexCoord2f(1.0, 1.0);glVertex3f(m_Vertices[ib].x,m_Vertices[ib].y,m_Vertices[ib].z);
glTexCoord2f(0.0, 1.0);glVertex3f(m_Vertices[ic].x,m_Vertices[ic].y,m_Vertices[ic].z);
glEnd();
glEnd();
}
glDisable(GL_TEXTURE_2D);
}
Model model;
void setup(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
model = Model("monkey.obj", "launch");
// Specify how texture values combine with current surface color values.
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glEnable(GL_BLEND); // Enable blending.
// Cull back faces.
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
// stops GL_QUAD from showing faces by priority
glEnable(GL_DEPTH_TEST);
}
// Drawing routine.
void drawScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
model.Draw();
glutSwapBuffers();
}
当我运行程序时,会发生以下情况:http://imgur.com/okMul77 它似乎是将整个图像映射到每个三角形,但我不知道如何修复它。有人有主意吗?提前谢谢