本文介绍了纹理openGl. C ++,qt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用草纹理覆盖我的地形(由高程图制成),但无法正常工作.我什至无法在简单的GL_QUAD上获得纹理,结果是多色网.

I'm trying to cover my terrain(which is made from heightmap) with a grass texture, but it's not working as it should. I can't even get the texture on a simple GL_QUAD, the result is multicolor net.

void GLWidget::initializeGL()
{
//
    glEnable(GL_TEXTURE_2D);
//
}

在我致电的QGLwidget中

in QGLwidget I call

openTextureImg();

openTextureImg()的代码:

code of 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;


}

我在这里尝试绘制四边形:

Here I'm trying to draw a quad:

   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();

}

我在做什么错了.

推荐答案

来自文档:

将图像img转换为OpenGL函数(例如glTexImage2D())期望的未命名格式.返回的图像不能用作QImage,但是QImage :: width(),QImage :: height()和QImage :: bits()可以与OpenGL一起使用.所使用的GL格式为 GL_RGBA [强调我的].

Converts the image img into the unnamed format expected by OpenGL functions such as glTexImage2D(). The returned image is not usable as a QImage, but QImage::width(), QImage::height() and QImage::bits() may be used with OpenGL. The GL format used is GL_RGBA [emphasis mine].

在呼叫glTexImage2D时,请使用GL_RGBA而不是GL_RGB.

In your call to glTexImage2D use GL_RGBA instead of GL_RGB.

这篇关于纹理openGl. C ++,qt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 20:52