• 4943阅读
  • 1回复

使用Qt+OpenGL纹理贴不上去? [复制链接]

上一主题 下一主题
离线claruarius
 

只看楼主 倒序阅读 楼主  发表于: 2013-02-02


各位高手,小弟是Qt初学者,用的是Qt4.8.3,帮忙看看为什么使用Qt+OpenGL纹理贴不上去?
//nehewidget.h/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef _NEHEWIDGET_H_
#define _NEHEWIDGET_H_


#include <QGLWidget>
#include <QKeyEvent>
#include <GL/glut.h>
#include <GL/gl.h>


class NeHeWidget: public QGLWidget
{
    Q_OBJECT
public:
    NeHeWidget(QWidget *parent);
    bool fullscreen;
    ~NeHeWidget();
protected:
    void initializedGL();
    void paintGL();
    void resizeGL(int width,int height);
    GLfloat rTri;
    void keyPressEvent(QKeyEvent *e);
    void loadGLTexture();
    GLfloat xRot, yRot, zRot;
    GLuint texture[1];
    void TimerFunction(int value);


};
#endif // _NEHEWIDGET_H_


//nehewidget.cpp////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "nehewidget.h"
#include <QMessageBox>
#include <QImage>
#include <QString>


NeHeWidget::NeHeWidget(QWidget * parent = 0):QGLWidget( parent )
{
    fullscreen = true;
    rTri =0.0;
    xRot = yRot = zRot = 0;
    setGeometry(50,60,640,480);
    setWindowTitle("NeHe's OpenGL Framework");


    switch(QMessageBox::information(0,"Start FullScreen","Would like to Run In FullScreen Mode?",QMessageBox::Yes,QMessageBox::No))
    {
        case QMessageBox::Yes: fullscreen = true;break;
        case QMessageBox::No : fullscreen = false;break;
    }
    if(fullscreen)
    {
        showFullScreen();
    }
};


NeHeWidget::~NeHeWidget()
{


};


void NeHeWidget::initializedGL()
{
    loadGLTexture();
    glEnable( GL_TEXTURE_2D );


    glShadeModel(GL_SMOOTH);
    glClearColor(0.0,0.0,0.0,0.0);
    glClearDepth(1.0);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
};


void NeHeWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(-1.5,0.0,-6.0);
    glRotatef( xRot, 1.0, 0.0, 0.0 );
glRotatef( yRot, 0.0, 1.0, 0.0 );
glRotatef( zRot, 0.0, 0.0, 1.0 );
glBindTexture( GL_TEXTURE_2D, texture[0] );
glBegin( GL_QUADS );
   glTexCoord2f( 0.0, 0.0 ); glVertex3f( -1.0, -1.0, 1.0 );
glTexCoord2f( 1.0, 0.0 ); glVertex3f( 1.0, -1.0, 1.0 );
glTexCoord2f( 1.0, 1.0 ); glVertex3f( 1.0, 1.0, 1.0 );
glTexCoord2f( 0.0, 1.0 ); glVertex3f( -1.0, 1.0, 1.0 );


glTexCoord2f( 1.0, 0.0 ); glVertex3f( -1.0, -1.0, -1.0 );
glTexCoord2f( 1.0, 1.0 ); glVertex3f( -1.0, 1.0, -1.0 );
glTexCoord2f( 0.0, 1.0 ); glVertex3f( 1.0, 1.0, -1.0 );
glTexCoord2f( 0.0, 0.0 ); glVertex3f( 1.0, -1.0, -1.0 );


glTexCoord2f( 0.0, 1.0 ); glVertex3f( -1.0, 1.0, -1.0 );
glTexCoord2f( 0.0, 0.0 ); glVertex3f( -1.0, 1.0, 1.0 );
glTexCoord2f( 1.0, 0.0 ); glVertex3f( 1.0, 1.0, 1.0 );
glTexCoord2f( 1.0, 1.0 ); glVertex3f( 1.0, 1.0, -1.0 );


glTexCoord2f( 1.0, 1.0 ); glVertex3f( -1.0, -1.0, -1.0 );
glTexCoord2f( 0.0, 1.0 ); glVertex3f( 1.0, -1.0, -1.0 );
glTexCoord2f( 0.0, 0.0 ); glVertex3f( 1.0, -1.0, 1.0 );
glTexCoord2f( 1.0, 0.0 ); glVertex3f( -1.0, -1.0, 1.0 );


glTexCoord2f( 1.0, 0.0 ); glVertex3f( 1.0, -1.0, -1.0 );
glTexCoord2f( 1.0, 1.0 ); glVertex3f( 1.0, 1.0, -1.0 );
glTexCoord2f( 0.0, 1.0 ); glVertex3f( 1.0, 1.0, 1.0 );
glTexCoord2f( 0.0, 0.0 ); glVertex3f( 1.0, -1.0, 1.0 );


glTexCoord2f( 0.0, 0.0 ); glVertex3f( -1.0, -1.0, -1.0 );
glTexCoord2f( 1.0, 0.0 ); glVertex3f( -1.0, -1.0, 1.0 );
glTexCoord2f( 1.0, 1.0 ); glVertex3f( -1.0, 1.0, 1.0 );
glTexCoord2f( 0.0, 1.0 ); glVertex3f( -1.0, 1.0, -1.0 );


    glEnd();
xRot += 0.3;
yRot += 0.2;
zRot += 0.4;
};


void NeHeWidget::resizeGL(int width ,int height)
{
    if(height == 0)
    {
        height = 1;
    }
    glViewport(0,0,(GLint)width,(GLint)height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0,(GLfloat)width/(GLfloat)height,0.1,100.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
};


void NeHeWidget::keyPressEvent(QKeyEvent *e)
{
    switch(e->key())
    {
    case Qt::Key_F2:
        fullscreen = !fullscreen;
        if(fullscreen)
        {
            showFullScreen();
        }
        else{showNormal();setGeometry(0,0,640,480);}
        updateGL();break;
    case Qt::Key_Escape:
        close();break;
    }
};


void NeHeWidget::loadGLTexture()
{
QImage tex, buf;


if ( !buf.load(":/data/1.jpg" ) ){
        qWarning("Can't not Open a Image!");
        QImage dummy( 128, 128, QImage::Format_ARGB32);
        dummy.fill( Qt::green );
        buf = dummy;
   }
    tex = QGLWidget::convertToGLFormat(buf);
    glGenTextures(1,&texture[0]);
    glBindTexture( GL_TEXTURE_2D, texture[0] );
    glTexImage2D( GL_TEXTURE_2D, 0, 3, tex.width(), tex.height(), 0,
        GL_RGBA, GL_UNSIGNED_BYTE, tex.bits() );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );


};
//main.cpp///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "nehewidget.h"
#include <QApplication>
#include <windows.h>


int main(int argc, char* argv[])
{
    bool fs = false;
    QApplication app(argc, argv);
    NeHeWidget w(0);
    w.show();


    return app.exec();
}
效果中正方体是白色的没有贴上纹理
window.open('http://www.qtcn.org/bbs/attachment/Mon_1302/59_140461_33643f965985c16.jpg?27');" style="max-width:700px;max-height:700px;" onload="if(is_ie6&&this.offsetWidth>700)this.width=700;" >
离线tianmaofu
只看该作者 1楼 发表于: 2013-02-06
将  void initializedGL()的前两名代码交换一下位置试一下
快速回复
限100 字节
 
上一个 下一个