c ++ opengl纹理不起作用

时间:2016-05-22 14:32:29

标签: c++ opengl textures

我在Opengl首次亮相,我不会在OpenGL的小项目中制作纹理。

This is my code : 

// myglwidget.cpp

    #include <QtWidgets>
#include <QtOpenGL>
#include <QDebug>
#include <QGLFormat>
#include<QOpenGlTexture>
#include<qimage.h>

#include "myglwidget.h"

int MyGLWidget::loadTextures()
{
    Image = QImage("D:\\workspace\\logo.png");
    Image = QGLWidget::convertToGLFormat(Image);
    if(Image.isNull())
    {
        qDebug() << "isNull"<<endl;
        return 0;
    }
    textures = 1;
    glGenTextures(1, (GLuint*)textures);
    glBindTexture(GL_TEXTURE_2D,(GLuint)textures);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
    glTexImage2D(GL_TEXTURE_2D, (GLuint)textures, GL_RGBA, Image.width(), Image.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, Image.bits());
    return 1;

}

MyGLWidget::MyGLWidget(QWidget *parent) : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
{
    xRot = 0;
    yRot = 0;
    zRot = 0;
}

MyGLWidget::~MyGLWidget()
{
}

QSize MyGLWidget::minimumSizeHint() const
{
    return QSize(50, 50);
}

QSize MyGLWidget::sizeHint() const
{
    return QSize(400, 400);
}

static void qNormalizeAngle(int &angle)
{
    while (angle < 0)
        angle += 360 * 16;
    while (angle > 360)
        angle -= 360 * 16;
}

void MyGLWidget::setXRotation(int angle)
{
    qNormalizeAngle(angle);
    if (angle != xRot) {
        xRot = angle;
        emit xRotationChanged(angle);
        updateGL();
    }
}

void MyGLWidget::setYRotation(int angle)
{
    qNormalizeAngle(angle);
    if (angle != yRot) {
        yRot = angle;
        emit yRotationChanged(angle);
        updateGL();
    }
}

void MyGLWidget::setZRotation(int angle)
{
    qNormalizeAngle(angle);
    if (angle != zRot) {
        zRot = angle;
        emit zRotationChanged(angle);
        updateGL();
    }
}

void MyGLWidget::initializeGL()
{
    glEnable(GL_DEPTH_TEST); //On initialise la profondeur
    glEnable(GL_TEXTURE_2D); //On initialise les textures !
}

void MyGLWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glEnable(GL_TEXTURE_2D);
    glTranslatef(0.0, 0.0, -10.0);
    glRotatef(xRot / 16.0, 1.0, 0.0, 0.0);
    glRotatef(yRot / 16.0, 0.0, 1.0, 0.0);
    glRotatef(zRot / 16.0, 0.0, 0.0, 1.0);
    draw();
}

void MyGLWidget::resizeGL(int width, int height)
{
    int side = qMin(width, height);
    glViewport((width - side) / 2, (height - side) / 2, side, side);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
#ifdef QT_OPENGL_ES_1
    glOrthof(-2, +2, -2, +2, 1.0, 15.0);
#else
    glOrtho(-2, +2, -2, +2, 1.0, 15.0);
#endif
    glMatrixMode(GL_MODELVIEW);
}

void MyGLWidget::mousePressEvent(QMouseEvent *event)
{
    lastPos = event->pos();
}

void MyGLWidget::mouseMoveEvent(QMouseEvent *event)
{
    int dx = event->x() - lastPos.x();
    int dy = event->y() - lastPos.y();

    if (event->buttons() & Qt::LeftButton) {
        setXRotation(xRot + 8 * dy);
        setYRotation(yRot + 8 * dx);
    } else if (event->buttons() & Qt::RightButton) {
        setXRotation(xRot + 8 * dy);
        setZRotation(zRot + 8 * dx);
    }

    lastPos = event->pos();
}

void MyGLWidget::draw()
{
    qglColor(Qt::red);
    glPushMatrix();
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, textures);
    glBegin(GL_QUADS);
    glNormal3f(0,0,-1);
    glVertex3f(-1,-1,0);
    glVertex3f(-1,1,0);
    glVertex3f(1,1,0);
    glVertex3f(1,-1,0);
    glEnd();
    glDisable(GL_TEXTURE_2D);
    glBegin(GL_TRIANGLES);
    glNormal3f(0,-1,0.707);
    glVertex3f(-1,-1,0);
    glVertex3f(1,-1,0);
    glVertex3f(0,0,1.2);
    glEnd();
    glBegin(GL_TRIANGLES);
    glNormal3f(1,0, 0.707);
    glVertex3f(1,-1,0);
    glVertex3f(1,1,0);
    glVertex3f(0,0,1.2);
    glEnd();
    glBegin(GL_TRIANGLES);
    glNormal3f(0,1,0.707);
    glVertex3f(1,1,0);
    glVertex3f(-1,1,0);
    glVertex3f(0,0,1.2);
    glEnd();
    glBegin(GL_TRIANGLES);
    glNormal3f(-1,0,0.707);
    glVertex3f(-1,1,0);
    glVertex3f(-1,-1,0);
    glVertex3f(0,0,1.2);
    glEnd();
    glPopMatrix();
}

我尝试了很多图像格式(bmp,png,tga),我从未到过看到纹理。 我还尝试了很多功能,以便在网上加载纹理,我没有到达显示它。我知道图像的路径是正常的,函数没有返回任何错误。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

我不确定您使用的是哪个版本的openGL。但是我注意到你的代码中存在一些问题。 将glTexImage2D API的第二个参数更正为0(Mipmap级别), glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Image.width(), Image.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, Image.bits());

使用glTexCoord2f()

为每个涡旋添加纹理坐标
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textures);
glBegin(GL_QUADS);
glNormal3f(0,0,-1);
glVertex3f(-1,-1,0);
glTexCoord2f(0, 0); //tex Coord (0, 0)
glVertex3f(-1,1,0);
glTexCoord2f(1, 0); //tex Coord (1, 0)
glVertex3f(1,1,0);
glTexCoord2f(1, 1); //tex Coord (1, 1)
glVertex3f(1,-1,0);
glTexCoord2f(0, 1); //tex Coord (0, 1)
glEnd();
glDisable(GL_TEXTURE_2D);

希望这会奏效。