Qt OpenGL纹理是黑色的

时间:2014-05-20 15:36:58

标签: c++ qt opengl textures

我需要将纹理加载到正方形上,但每当我开始编程时,我都看不到任何纹理,只是一个黑色正方形。

以下是代码:

GLuint texture;

interactiveScenes::interactiveScenes(QWidget *parent) :
    QGLWidget(parent), ui(new Ui::interactiveScenes)
{
    ui->setupUi(this);

    timer = new QTimer();
    connect( timer, SIGNAL(timeout()), this, SLOT(updateGL()) );

    setFocusPolicy(Qt::StrongFocus);

    lockMouse = true;

   camPosx = 2.0,  camPosy = 2.0,    camPosz = 25.0;
   camViewx = 2.0, camViewy = 2.0, camViewz = 0.0;
   camUpx = 0.0,   camUpy = 1.0,   camUpz = 0.0;

   mouseX = QCursor::pos().x();
   mouseY = QCursor::pos().y();
   setMouseTracking(true);

}

interactiveScenes::~interactiveScenes()
{
    delete ui;
}

void interactiveScenes::initializeGL()
{
    // Initialize QGLWidget (parent)
    QGLWidget::initializeGL();

    glShadeModel(GL_SMOOTH);

    // White canvas
    glClearColor(1.0f,1.0f,1.0f,0.0f);

    // Place light
    glEnable( GL_LIGHTING );
    glEnable( GL_LIGHT0 );
    glEnable(GL_DEPTH_TEST);

    GLfloat light0_position [] = {0.1f, 0.1f, 5.0f, 0.0f};
    GLfloat light_diffuse []={ 1.0, 1.0, 1.0, 1.0 };
    glLightfv ( GL_LIGHT0, GL_POSITION, light0_position );
    glLightfv ( GL_LIGHT0, GL_DIFFUSE, light_diffuse );

    glEnable(GL_TEXTURE_2D);
    img = new QImage(":/images/images.jpg", "JPG");
    if (img->isNull())
        std::cout << "error" <<std::endl;

    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);

    QImage tex = QGLWidget::convertToGLFormat(*img);
    if (tex.isNull())
        std::cout << "error" <<std::endl;

    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA,
                tex.width(), tex.height(),
                0, GL_RGBA, GL_UNSIGNED_BYTE, tex.bits() );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glDisable(GL_TEXTURE_2D);

    timer->start(50);
}

void interactiveScenes::resizeGL(GLint width, GLint height)
{
    if ((width<=0) || (height<=0))
        return;

    //set viewport
    glViewport(0,0,width,height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    //set persepective
    GLdouble aspect_ratio=(GLdouble)width/(GLdouble)height;
    gluPerspective(45.0f, aspect_ratio, 0.1, 40.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void interactiveScenes::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();

    // store current matrix
    glMatrixMode( GL_MODELVIEW );
    glPushMatrix( );

    gluLookAt(camPosx ,camPosy ,camPosz,
        camViewx,camViewy,camViewz,
        camUpx, camUpy, camUpz );

    //Draw Axes
    glDisable( GL_LIGHTING );
    glBegin(GL_LINES);
    glColor3f(1.0, 0.0, 0.0);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(10.0, 0.0, 0.0);
    glColor3f(0.0, 1.0, 0.0);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(0.0, 10.0, 0.0);
    glColor3f(0.0, 0.0, 1.0);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(0.0, 0.0, 10.0);
    glEnd();
    glEnable( GL_LIGHTING );

    glEnable(GL_LIGHTING);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture);

    glColor3f( 1.0,  0.0,  0.0 );
    glBegin(GL_POLYGON);

    glTexCoord2d( 0.0, 5.0 );
    glVertex3f(  0.0f, 3.0f, 0.0f );

    glTexCoord2d( 0.0, 0.0);
    glVertex3f(  0.0f, 0.0f, 0.0f );

    glTexCoord2d( 5.0, 0.0);
    glVertex3f( 3.0f, 0.0f, 0.0f );

    glTexCoord2d( 5.0, 5.0);
    glVertex3f( 3.0f, 3.0f, 0.0f );
    glEnd();
    glDisable(GL_TEXTURE_2D);

    glPushMatrix();

    glTranslated(2.0, 2.0 ,-4);

    GLfloat shin[] = { 12.8f };
    GLfloat amb[] = { 0.135f, 0.2225f, 0.1575f, 0.95f };
    GLfloat diff2 [] = { 0.54f , 0.89f , 0.63f, 0.95f };
    GLfloat specular[] = { 0.316228f, 0.316228f, 0.316228f, 0.95f };

    glMaterialfv ( GL_FRONT, GL_SPECULAR, specular);
    glMaterialfv ( GL_FRONT, GL_SHININESS, shin);
    glMaterialfv ( GL_FRONT, GL_AMBIENT, amb);
    glMaterialfv ( GL_FRONT, GL_DIFFUSE, diff2 );

    solidSphere(2, 25, 25);

    glPopMatrix();

    // restore current matrix
    glMatrixMode( GL_MODELVIEW );
    glPopMatrix( );
}

图像从我的资源文件中加载得很好,我认为它与convertToGLFormat一样,但也会返回一个图像。

1 个答案:

答案 0 :(得分:1)

感谢agrum,我发现了问题,我测试了方块是否显示任何颜色,但没有。 在应用纹理/颜色之前,我没有禁用光照。 在应用纹理时禁用光照后,一切正常!