⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nehewidget.cpp

📁 一些OpenGL的开发实例
💻 CPP
字号:
/*    Qt OpenGL Tutorial - Lesson 12    nehewidget.cpp    v 1.00    2002/12/20    Copyright (C) 2002 Cavendish                       cavendish@qiliang.net                       http://www.qiliang.net/nehe_qt    This program is free software; you can redistribute it and/or modify    it under the terms of the GNU General Public License as published by    the Free Software Foundation; either version 2 of the License, or    (at your option) any later version.*/#include <math.h>#include <qimage.h>#include "nehewidget.h"static GLfloat boxcol[5][3] ={  { 1.0, 0.0, 0.0 },  { 1.0, 0.5, 0.0 },  { 1.0, 1.0, 0.0 },  { 0.0, 1.0, 0.0 },  { 0.0, 1.0, 1.0 }};static GLfloat topcol[5][3] ={  { 0.5, 0.0, 0.0 },  { 0.5, 0.25, 0.0 },  { 0.5, 0.5, 0.0 },  { 0.0, 0.5, 0.0 },  { 0.0, 0.5, 0.5 }};NeHeWidget::NeHeWidget( QWidget* parent, const char* name, bool fs )    : QGLWidget( parent, name ){  xRot = yRot = zRot = 0.0;  box = top = 0;    xLoop = yLoop = 0;  fullscreen = fs;  setGeometry( 0, 0, 640, 480 );  setCaption( "NeHe's Display List Tutorial" );  if ( fullscreen )    showFullScreen();}NeHeWidget::~NeHeWidget(){  glDeleteLists( box, 2 );}void NeHeWidget::initializeGL(){  loadGLTextures();  buildLists();  glEnable( GL_TEXTURE_2D );  glShadeModel( GL_SMOOTH );  glClearColor( 0.0, 0.0, 0.0, 0.5 );  glClearDepth( 1.0 );  glEnable( GL_DEPTH_TEST );  glDepthFunc( GL_LEQUAL );  glEnable( GL_LIGHT0 );  glEnable( GL_LIGHTING );  glEnable( GL_COLOR_MATERIAL );  glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );}void NeHeWidget::paintGL(){  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );  glBindTexture( GL_TEXTURE_2D, texture[0] );    for ( yLoop = 1; yLoop < 6; yLoop++ )  {    for ( xLoop = 0; xLoop < yLoop; xLoop++ )    {      glLoadIdentity();      glTranslatef( 1.4 + (float(xLoop) * 2.8) - (float(yLoop) * 1.4),          ( (6.0 - (float(yLoop)) ) * 2.4 ) - 7.0, -20.0 );      glRotatef( 45.0 - (2.0 * yLoop) + xRot, 1.0, 0.0, 0.0 );      glRotatef( 45.0 + yRot, 0.0, 1.0, 0.0 );      glColor3fv( boxcol[yLoop-1] );      glCallList( box );      glColor3fv( topcol[yLoop-1] );      glCallList( top );    }  }}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_Up:    xRot -= 0.2;    updateGL();    break;  case Qt::Key_Down:    xRot += 0.2;    updateGL();    break;  case Qt::Key_Left:    yRot -= 0.2;    updateGL();    break;  case Qt::Key_Right:    yRot += 0.2;    updateGL();    break;  case Qt::Key_F2:    fullscreen = !fullscreen;    if ( fullscreen )    {      showFullScreen();    }    else    {      showNormal();      setGeometry( 0, 0, 640, 480 );    }    update();    break;  case Qt::Key_Escape:    close();  }}void NeHeWidget::loadGLTextures(){  QImage tex, buf;  if ( !buf.load( "./data/Cube.bmp" ) )  {    qWarning( "Could not read image file, using single-color instead." );    QImage dummy( 128, 128, 32 );    dummy.fill( Qt::green.rgb() );    buf = dummy;  }  tex = QGLWidget::convertToGLFormat( buf );    glGenTextures( 1, &texture[0] );    glBindTexture( GL_TEXTURE_2D, texture[0] );  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );  glTexImage2D( GL_TEXTURE_2D, 0, 3, tex.width(), tex.height(), 0,      GL_RGBA, GL_UNSIGNED_BYTE, tex.bits() );}void NeHeWidget::buildLists(){  box = glGenLists( 2 );    glNewList( box, GL_COMPILE );    glBegin( GL_QUADS );    glNormal3f( 0.0, -1.0, 0.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 );    glNormal3f( 0.0, 0.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 );    glNormal3f( 0.0, 0.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 );    glNormal3f( 1.0, 0.0, 0.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 );    glNormal3f( -1.0, 0.0, 0.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();    glEndList();    top = box + 1;  glNewList( top, GL_COMPILE );    glBegin( GL_QUADS );    glNormal3f( 0.0, 1.0, 0.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 );  glEnd();    glEndList();}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -