我在尝试让我的纹理在某些几何图形上正确显示时遇到问题。基本上,我有一些描述人脸形状的三角形,然后有一个我必须用 opencv 加载的 png。我有以下代码
void Geometry::loadTex(const char* fileName){
RenderUtils * u = new RenderUtils();
IplImage* img = u->getImg(fileName);
GLuint texture;
// allocate a texture name
glGenTextures( 1, &texture );
// select our current texture
glBindTexture( GL_TEXTURE_2D, texture );
// select modulate to mix texture with color for shading
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
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_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, img->imageData);
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
//delete [] t;
}
IplImage* RenderUtils::getImg(const char *fileName){
IplImage* img = 0;
img = cvLoadImage(fileName, CV_LOAD_IMAGE_COLOR);
if(!img) cout << "Couldn't load image " << fileName << endl;
return img;
}
void Geometry::display(){
glPushMatrix();
glTranslatef(20.0f, 20.0f, -40.f);
glRotatef(180.f, 0.0, 0.0, 1.0);
for(int i = 0; i < numTris; i+=3){
glBegin(GL_TRIANGLES);
glTexCoord3f(mean[tri[i]] / texWidth, ((mean[meanLength/2 + tri[i]] - maximumMu) * -1) / texHeight, 0 );
glVertex3f(mean[tri[i]], (mean[meanLength/2 + tri[i]] - maximumMu) * -1, 0);
glTexCoord3f(mean[tri[i+1]] / texWidth, ((mean[meanLength/2 + tri[i+1]] - maximumMu) * -1) / texHeight, 0 );
glVertex3f(mean[tri[i+1]], (mean[meanLength/2 + tri[i+1]] - maximumMu) * -1, 0);
glTexCoord3f(mean[tri[i+2]] / texWidth, ((mean[meanLength/2 + tri[i+2]] - maximumMu) * -1) / texHeight, 0 );
glVertex3f(mean[tri[i+2]], (mean[meanLength/2 + tri[i+2]] - maximumMu) * -1, 0);
glEnd();
}
glPopMatrix();
glutSwapBuffers();
glFlush();
}
所以先调用loadTex,它调用load getImg,然后从Glut调用display方法。