我正在尝试用草纹理覆盖我的地形(由高度图制成),但它不能正常工作。我什至无法在简单的 GL_QUAD 上获得纹理,结果是多色网。
void GLWidget::initializeGL()
{
//
glEnable(GL_TEXTURE_2D);
//
}
在 QGLwidget 我打电话
openTextureImg();
openTextureImg() 的代码:
bool GLWidget::openTextureImg()
{
QString fileName = QFileDialog::getOpenFileName(this,tr("Open Image"),QDir::homePath(), tr("Image Files (*.png *.tga *.bmp)"));
QImage textureImg;
if (!fileName.isEmpty())
{
textureImg = QImage(fileName, "PNG");
qDebug()<<"image loaded";
textureImg = QGLWidget::convertToGLFormat( textureImg );
glGenTextures( 1, &texHandle );
glBindTexture( GL_TEXTURE_2D, texHandle );
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureImg.width(), textureImg.height(), 0, GL_RGB,
GL_UNSIGNED_BYTE, textureImg.bits());
//glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glBindTexture( GL_TEXTURE_2D, 0 );
return true;
}
return false;
}
在这里,我正在尝试绘制一个四边形:
void GLWidget::drawRect()
{
glColor3f(0.5,0.5,1.0);
glBindTexture(GL_TEXTURE_2D,texHandle);
glBegin(GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex2d(0.0,0.0);
glTexCoord2d(1.0,0.0); glVertex2d(1000.0,0.0);
glTexCoord2d(1.0,1.0); glVertex2d(1000.0,1000.0);
glTexCoord2d(0.0,1.0); glVertex2d(0.0,1000.0);
glEnd();
}
我究竟做错了什么。