opengl 不同面贴不同的纹理

来源:互联网 发布:王小帮淘宝创业故事 编辑:程序博客网 时间:2024/06/08 06:14

参考:http://www.qiliang.net/old/nehe_qt/index.html
 

不同面纹理贴图在放在begin与end之间,类似下面,否则不生效.
  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 );
    glEnd(); 

 

 

/////nehewidget.h:

#ifndef NEHEWIDGET_H

#define NEHEWIDGET_H

 

#include <QtOpenGL> // Header File For OpenGL Module

 

class NeHeWidget : public QGLWidget

{

Q_OBJECT // Declaration For Q_OBJECT 

 

public:

NeHeWidget( QWidget* parent = 0 ); // Declaration For NeHeWidget

~NeHeWidget();

 

protected:

void initializeGL(); // All Setup For OpenGL Goes Here

void paintGL(); // Here's Where We Do All The Drawing

void resizeGL( int width, int height ); // Resize And Initialize The GL Window

 

  void keyPressEvent( QKeyEvent *e ); // Declaration For Key Press Event

void loadGLTextures();

 

protected:

bool fullscreen; // Declaration For Full Screen Switch

GLfloat rTri;

GLfloat rQuad;

GLfloat xRot, yRot, zRot;

GLfloat zoom;

GLfloat xSpeed, ySpeed;

GLuint texture[3];

GLuint filter;

 

bool light;

};

 

#endif//NEHEWIDGET_H

 

/////nehewidget.cpp:

 

 

#include "nehewidget.h"

 

/*!

 *  Declaration For NeHeWidget

 */

GLfloat lightAmbient[4] = { 0.5, 0.5, 0.5, 1.0 };

GLfloat lightDiffuse[4] = { 1.0, 1.0, 1.0, 1.0 };

GLfloat lightPosition[4] = { 0.0, 0.0, 2.0, 1.0 };

 

NeHeWidget::NeHeWidget( QWidget* parent )

    : QGLWidget( parent ), fullscreen(false)

{

    setGeometry( 0, 0, 640, 480 );

xRot = yRot = zRot = 0.0;

    //setCaption( "NeHe's OpenGL Framework" );

rTri = 0.0;

rQuad = 0.0;

zoom = -5.0;

xSpeed = ySpeed = 0.0;

 

filter = 0;

 

light = false;

    if ( fullscreen )

    {

        showFullScreen();

    }

}

 

NeHeWidget::~NeHeWidget()

{

}

 

/*!

 *   All Setup For OpenGL Goes Here

 */

void NeHeWidget::initializeGL()

{

loadGLTextures();

glEnable( GL_TEXTURE_2D );

 

    glShadeModel( GL_SMOOTH ); // Enables Smooth Shading

    glClearColor( 0.0, 0.0, 0.0, 0.5 ); // Black Background

    glClearDepth( 1.0 ); // Depth Buffer Setup

    glEnable( GL_DEPTH_TEST ); // Enables Depth Testing

    glDepthFunc( GL_LEQUAL ); // The Type Of Depth Test To Do

    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); // Really Nice Perspective Calculations

 

glLightfv( GL_LIGHT1, GL_AMBIENT, lightAmbient );

glLightfv( GL_LIGHT1, GL_DIFFUSE, lightDiffuse );

glLightfv( GL_LIGHT1, GL_POSITION, lightPosition );

 

glEnable( GL_LIGHT1 );

}

 

/*!

 *   Here's Where We Do All The Drawing

 */

void NeHeWidget::paintGL()

{

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // Clear The Screen And The Depth Buffer

    glLoadIdentity(); // Reset The Current Modelview Matrix

//if (rTri > 360) rTri = 0;

    glTranslatef( -3, 0.0, -9.0 );      // Move Left 1.5 Units And Into The Screen 6.0

glRotatef( rTri,  0.5,  0.2,  0.5 );

rTri +=2;

    glBegin( GL_TRIANGLES );              // Drawing Using Triangles

 

glColor3f( 1.0, 0.0, 0.0 );

glVertex3f(  0.0,  1.0,  0.0 );

//glColor3f( 0.0, 1.0, 0.0 );

glVertex3f( -1.0, -1.0,  1.0 );

//glColor3f( 0.0, 0.0, 1.0 );

glVertex3f(  1.0, -1.0,  1.0 );

 

glColor3f( 0.0, 1.0, 0.0 );

glVertex3f(  0.0,  1.0,  0.0 );

//glColor3f( 0.0, 0.0, 1.0 );

glVertex3f(  1.0, -1.0,  1.0 );

//glColor3f( 0.0, 1.0, 0.0 );

glVertex3f(  1.0, -1.0, -1.0 );

 

 

glColor3f( 0.0, 0.0, 1.0 );

glVertex3f(  0.0,  1.0,  0.0 );

//glColor3f( 0.0, 1.0, 0.0 );

glVertex3f(  1.0, -1.0, -1.0 );

//glColor3f( 0.0, 0.0, 1.0 );

glVertex3f( -1.0, -1.0, -1.0 );

 

glColor3f( 1.0, 0.0, 1.0 );

glVertex3f(  0.0,  1.0,  0.0 );

//glColor3f( 0.0, 0.0, 1.0 );

glVertex3f( -1.0, -1.0, -1.0 );

//glColor3f( 0.0, 1.0, 0.0 );

glVertex3f( -1.0, -1.0,  1.0 );

 

    glEnd();

 

glLoadIdentity(); // Reset The Current Modelview Matrix

glTranslatef(  0.0,  0.0, zoom );

 

glRotatef( xRot,  1.0,  0.0,  0.0 );

glRotatef( yRot,  0.0,  1.0,  0.0 );

 

//glBindTexture( GL_TEXTURE_2D, texture[filter] );

//if (rTri > 360) rTri = 0;

//glTranslatef( 2.0, 0.0, -5.0 );      // Move Left 1.5 Units And Into The Screen 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 );                  // Draw A Quad

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

glEnd();

//后面。

glBindTexture( GL_TEXTURE_2D, texture[1] );

glBegin( GL_QUADS ); 

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

//底面。

glEnd();

glBindTexture( GL_TEXTURE_2D, texture[2] );

glBegin( GL_QUADS ); 

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 += xSpeed;

yRot += ySpeed;

/*xRot += 0.3;

yRot += 0.2;

zRot += 0.4;*/

filter += 1;;

if ( filter > 2 )

{

filter = 0;

}

xSpeed = 1;

ySpeed = 2;

 

glLoadIdentity(); // Reset The Current Modelview Matrix

                          // Finished Drawing The Quad

glColor3f( 0.5, 0.5, 1.0 );

 

glTranslatef( -2.0, 0.0, 0.0 ); 

glBegin( GL_POLYGON );                  // Draw A Quad

glVertex3f( -1.0,  1.0,  0.0 );   // Top Left

glVertex3f(  1.0,  1.0,  0.0 );   // Top Right

glVertex3f(  2.0, 0.0,  0.0 );   // Bottom Right

glVertex3f(  1.0, -1.0,  0.0 );   // Bottom Right

glVertex3f( -1.0, -1.0,  0.0 );   // Bottom Left

glVertex3f( -1.0, -1.0,  0.0 );   // Bottom Left

glVertex3f( -2.0, 0.0,  0.0 );   // Bottom Left

glEnd();                              // Finished Drawing The Quad

 

/*rTri += 2;

rQuad-=0.15;*/

}

 

/*!

 *   Resize And Initialize The OpenGL Window

 */

void NeHeWidget::resizeGL( int width, int height )

{

    if ( height == 0 ) // Prevent A Divide By Zero By

    {

        height = 1; // Making Height Equal One

    }

 

    glViewport( 0, 0, (GLint)width, (GLint)height ); // Reset The Current Viewport

    glMatrixMode( GL_PROJECTION ); // Select The Projection Matrix

    glLoadIdentity(); // Reset The Projection Matrix

    gluPerspective( 45.0, (GLfloat)width/(GLfloat)height, 0.1, 100.0 ); // Select The Modelview Matrix

    glMatrixMode( GL_MODELVIEW );

    glLoadIdentity();

}

 

/*!

 *   Key events watcher

 */

void NeHeWidget::keyPressEvent( QKeyEvent *e )

{

    switch ( e->key() )

    {

case Qt::Key_L:

light = !light;

if ( !light )

{

glDisable( GL_LIGHTING );

}

else

{

glEnable( GL_LIGHTING );

}

updateGL();

break;

case Qt::Key_Up:

zoom -= 0.2;

updateGL();

break;

 

case Qt::Key_Down:

zoom += 0.2;

updateGL();

break;

        case Qt::Key_F2: //F2 key, full screen switch

            fullscreen = !fullscreen;

            if ( fullscreen )

            {

                showFullScreen();

            }else{

                showNormal();

                setGeometry( 0, 0, 640, 480 );

            }

            updateGL();

            break;

        case Qt::Key_Escape: // Esc key, close window

            close();

    }

}

 

 

void NeHeWidget::loadGLTextures()

{

QImage tex, buf;

if ( !buf.load( "./NeHe.bmp" ) )

{

qWarning( "Could not read image file, using single-color instead." );

QImage dummy( 128, 128, QImage::Format_ARGB32 );

dummy.fill( Qt::green );

buf = dummy;

}

tex = QGLWidget::convertToGLFormat( buf );

glGenTextures( 3, &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 );

 

buf.load( "./texture.png" );

tex = QGLWidget::convertToGLFormat( buf );

glBindTexture( GL_TEXTURE_2D, texture[1] );

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

 

buf.load( "./333.png" );

tex = QGLWidget::convertToGLFormat( buf );

glBindTexture( GL_TEXTURE_2D, texture[2] );

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

}

 

////////main.cpp

 

#include <QApplication>

#include <QMessageBox>

 

#include "nehewidget.h"

 

int main( int argc, char **argv )

{

    QApplication a(argc,argv);

 

    NeHeWidget w( 0 );

    w.show();

 

    return a.exec();

}

原创粉丝点击