Russian Qt Forum
Апрель 30, 2024, 05:22 *
Добро пожаловать, Гость. Пожалуйста, войдите или зарегистрируйтесь.
Вам не пришло письмо с кодом активации?

Войти
 
  Начало   Форум  WIKI (Вики)FAQ Помощь Поиск Войти Регистрация  

Страниц: 1 [2]   Вниз
  Печать  
Автор Тема: Примеры из SuperBible (3-го издания) на Qt  (Прочитано 28543 раз)
8Observer8
Гость
« Ответ #15 : Сентябрь 07, 2014, 13:48 »

03. Linesw. Рисует линии переменной ширины



Scene.h
Код
C++ (Qt)
#ifndef SCENE_H
#define SCENE_H
 
#include <QGLWidget>
#include <QKeyEvent>
 
class Scene : public QGLWidget
{
public:
   Scene( QWidget *parent = 0 );
 
private:
   void initializeGL();
   void paintGL();
   void resizeGL( int w, int h );
 
   void keyPressEvent( QKeyEvent *event );
 
private:
   // Rotation amounts
   GLfloat xRot;
   GLfloat yRot;
};
 
#endif // SCENE_H
 

Scene.cpp
Код
C++ (Qt)
 
#include <math.h>
#include "Scene.h"
 
// Define a constant for the value of PI
#define GL_PI 3.1415f
 
Scene::Scene( QWidget *parent ) :
   QGLWidget( parent ),
   xRot( 0.0f ),
   yRot( 0.0f )
{
   this->setFocusPolicy( Qt::StrongFocus );
}
 
void Scene::initializeGL()
{
   // Black background
   glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
 
   // Set drawing color to green
   glColor3f( 0.0f, 1.0f, 0.0f );
}
 
void Scene::paintGL()
{
   GLfloat y;                  // Storeage for varying Y coordinate
   GLfloat fCurrSize;          // Save current size
 
   // Clear the window with current clearing color
   glClear( GL_COLOR_BUFFER_BIT );
 
   // Save matrix state and do the rotation
   glPushMatrix();
   glRotatef( xRot, 1.0f, 0.0f, 0.0f );
   glRotatef( yRot, 0.0f, 1.0f, 0.0f );
 
   // Save the smallest value
   glGetFloatv( GL_LINE_WIDTH_GRANULARITY, &fCurrSize );
 
   // Step up Y axis 20 units at a time
   for( y = -90.0f; y < 90.0f; y += 20.0f ) {
       // Set the line width
       glLineWidth( fCurrSize );
 
       // Draw the line
       glBegin( GL_LINES );
       glVertex2f( -80.0f, y );
       glVertex2f( 80.0f, y );
       glEnd();
 
       // Increase the line width
       fCurrSize += 1.0f;
   }
 
   // Restore transformations
   glPopMatrix();
}
 
void Scene::resizeGL( int w, int h )
{
   // Prevent a divide by zero
   if( h == 0 ) {
       h = 1;
   }
 
   // Set Viewport to window dimensions
   glViewport( 0, 0, w, h );
 
   // Reset projection matrix stack
   glMatrixMode( GL_PROJECTION );
   glLoadIdentity();
 
   GLfloat nRange = 100.0f;
 
   // Establish clipping volume (left, right, bottom, top, near, far)
   if ( w <= h ) {
       glOrtho ( -nRange, nRange, -nRange*h/w, nRange*h/w,
                 -nRange, nRange );
   } else {
       glOrtho ( -nRange*w/h, nRange*w/h, -nRange, nRange,
                 -nRange, nRange );
   }
 
   // Reset Model view matrix stack
   glMatrixMode( GL_MODELVIEW );
   glLoadIdentity();
}
 
void Scene::keyPressEvent( QKeyEvent *event )
{
   switch ( event->key() ) {
   case Qt::Key_Up:
       xRot -= 5.0f;
       break;
   case Qt::Key_Down:
       xRot += 5.0f;
       break;
   case Qt::Key_Left:
       yRot -= 5.0f;
       break;
   case Qt::Key_Right:
       yRot += 5.0f;
       break;
   }
 
   if( xRot > 356.0f ) {
       xRot = 0.0f;
   }
 
   if( xRot < -1.0f ) {
       xRot = 355.0f;
   }
 
   if( yRot > 356.0f) {
       yRot = 0.0f;
   }
 
   if( yRot < -1.0f) {
       yRot = 355.0f;
   }
 
   updateGL();
}
 
« Последнее редактирование: Сентябрь 08, 2014, 14:19 от 8Observer8 » Записан
8Observer8
Гость
« Ответ #16 : Сентябрь 08, 2014, 15:22 »

03. LStipple. Пунктирные линии



Scene.h
Код
C++ (Qt)
#ifndef SCENE_H
#define SCENE_H
 
#include <QGLWidget>
#include <QKeyEvent>
 
class Scene : public QGLWidget
{
public:
   Scene( QWidget *parent = 0 );
 
private:
   void initializeGL();
   void paintGL();
   void resizeGL( int w, int h );
 
   void keyPressEvent( QKeyEvent *event );
 
private:
   // Rotation amounts
   GLfloat xRot;
   GLfloat yRot;
};
 
#endif // SCENE_H
 

Scene.cpp
Код
C++ (Qt)
 
#include <math.h>
#include "Scene.h"
 
// Define a constant for the value of PI
#define GL_PI 3.1415f
 
Scene::Scene( QWidget *parent ) :
   QGLWidget( parent ),
   xRot( 0.0f ),
   yRot( 0.0f )
{
   this->setFocusPolicy( Qt::StrongFocus );
}
 
void Scene::initializeGL()
{
   // Black background
   glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
 
   // Set drawing color to green
   glColor3f( 0.0f, 1.0f, 0.0f );
 
   // Enable Stippling
   glEnable( GL_LINE_STIPPLE );
}
 
void Scene::paintGL()
{
   GLfloat y;                  // Storeage for varying Y coordinate
   GLint factor = 3;           // Stippling factor
   GLushort pattern = 0x5555;  // Stipple pattern
 
   // Clear the window with current clearing color
   glClear( GL_COLOR_BUFFER_BIT );
 
   // Save matrix state and do the rotation
   glPushMatrix();
   glRotatef( xRot, 1.0f, 0.0f, 0.0f );
   glRotatef( yRot, 0.0f, 1.0f, 0.0f );
 
   // Step up Y axis 20 units at a time
   for( y = -90.0f; y < 90.0f; y += 20.0f ) {
       // Reset the repeat factor and pattern
       glLineStipple( factor, pattern );
 
       // Draw the line
       glBegin( GL_LINES );
       glVertex2f( -80.0f, y );
       glVertex2f( 80.0f, y );
       glEnd();
 
       factor++;
   }
 
   // Restore transformations
   glPopMatrix();
}
 
void Scene::resizeGL( int w, int h )
{
   // Prevent a divide by zero
   if( h == 0 ) {
       h = 1;
   }
 
   // Set Viewport to window dimensions
   glViewport( 0, 0, w, h );
 
   // Reset projection matrix stack
   glMatrixMode( GL_PROJECTION );
   glLoadIdentity();
 
   GLfloat nRange = 100.0f;
 
   // Establish clipping volume (left, right, bottom, top, near, far)
   if ( w <= h ) {
       glOrtho ( -nRange, nRange, -nRange*h/w, nRange*h/w,
                 -nRange, nRange );
   } else {
       glOrtho ( -nRange*w/h, nRange*w/h, -nRange, nRange,
                 -nRange, nRange );
   }
 
   // Reset Model view matrix stack
   glMatrixMode( GL_MODELVIEW );
   glLoadIdentity();
}
 
void Scene::keyPressEvent( QKeyEvent *event )
{
   switch ( event->key() ) {
   case Qt::Key_Up:
       xRot -= 5.0f;
       break;
   case Qt::Key_Down:
       xRot += 5.0f;
       break;
   case Qt::Key_Left:
       yRot -= 5.0f;
       break;
   case Qt::Key_Right:
       yRot += 5.0f;
       break;
   }
 
   if( xRot > 356.0f ) {
       xRot = 0.0f;
   }
 
   if( xRot < -1.0f ) {
       xRot = 355.0f;
   }
 
   if( yRot > 356.0f) {
       yRot = 0.0f;
   }
 
   if( yRot < -1.0f) {
       yRot = 355.0f;
   }
 
   updateGL();
}
 
Записан
8Observer8
Гость
« Ответ #17 : Сентябрь 08, 2014, 19:09 »

03. Triangle. Конус, составленный из вееров треугольников



Scene.h
Код
C++ (Qt)
#ifndef SCENE_H
#define SCENE_H
 
#include <QGLWidget>
#include <QKeyEvent>
 
class Scene : public QGLWidget
{
   Q_OBJECT
 
public:
   Scene( QWidget *parent = 0 );
 
private slots:
   void slotShowContextMenu( const QPoint &pos );
 
private:
   void initializeGL();
   void paintGL();
   void resizeGL( int w, int h );
 
   void keyPressEvent( QKeyEvent *event );
 
private:
   // Rotation amounts
   GLfloat xRot;
   GLfloat yRot;
 
   // Flags for effects
   int iCull;
   int iOutline;
   int iDepth;
};
 
#endif // SCENE_H
 

Scene.cpp
Код
C++ (Qt)
 
#include <QAbstractScrollArea>
#include <QMenu>
#include <math.h>
#include "Scene.h"
 
// Define a constant for the value of PI
#define GL_PI 3.1415f
 
Scene::Scene( QWidget *parent ) :
   QGLWidget( parent ),
   xRot( 0.0f ),
   yRot( 0.0f ),
   iCull( 0 ),
   iOutline( 0 ),
   iDepth( 0 )
{
   this->setFocusPolicy( Qt::StrongFocus );
   this->setContextMenuPolicy( Qt::CustomContextMenu );
 
   connect( this, SIGNAL( customContextMenuRequested( QPoint ) ),
            this, SLOT( slotShowContextMenu( QPoint ) ) );
}
 
void Scene::slotShowContextMenu(const QPoint &pos)
{
   QPoint globalPos;
   if ( sender()->inherits( "QAbstractScrollArea" ) ) {
       globalPos = ( ( QAbstractScrollArea* )sender() )->
               viewport()->mapToGlobal( pos );
   }
   else {
       globalPos = ( ( QWidget* )sender() )->mapToGlobal( pos );
   }
 
   QString depthTestText( "Toggle depth test" );
   QString cullBackfaceText( "Toggle cull backface" );
   QString outlineBackText( "Toggle outline back" );
 
   QMenu menu;
   QAction *actionDepthTest = new QAction( depthTestText, this );
   QAction *actionCullBackface = new QAction( cullBackfaceText, this );
   QAction *actionOutlineBack = new QAction( outlineBackText, this );
   menu.addAction( actionDepthTest );
   menu.addAction( actionCullBackface );
   menu.addAction( actionOutlineBack );
 
   QAction* selectedItem = menu.exec( globalPos );
 
   if ( selectedItem == 0 ) {
       return;
   }
 
   if ( selectedItem->text() == depthTestText ) {
       iDepth = !iDepth;
   } else if ( selectedItem->text() == cullBackfaceText ) {
       iCull = !iCull;
   } else if ( selectedItem->text() == outlineBackText ) {
       iOutline = !iOutline;
   }
}
 
void Scene::initializeGL()
{
   // Black background
   glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
 
   // Set drawing color to green
   glColor3f( 0.0f, 1.0f, 0.0f );
 
   // Set color shading model to flat
   glShadeModel( GL_FLAT );
 
   // Clock wise wound polygons are front facing, this is reversed
   // because we are using triangle fans
   glFrontFace( GL_CW );
}
 
void Scene::paintGL()
{
   GLfloat x, y, angle;  // Storage for coordinates and angles
   int iPivot = 1;     // Used to flag alternating colors
 
   // Clear the window and the depth buffer
   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
 
   // Turn culling on if flag is set
   if( iCull ) {
       glEnable( GL_CULL_FACE );
   } else {
       glDisable( GL_CULL_FACE );
   }
 
   // Enable depth testing if flag is set
   if( iDepth ) {
       glEnable( GL_DEPTH_TEST );
   } else {
       glDisable( GL_DEPTH_TEST );
   }
 
   // Draw back side as a polygon only, if flag is set
   if( iOutline ) {
       glPolygonMode( GL_BACK, GL_LINE );
   } else {
       glPolygonMode( GL_BACK, GL_FILL );
   }
 
   // Save matrix state and do the rotation
   glPushMatrix();
   glRotatef( xRot, 1.0f, 0.0f, 0.0f );
   glRotatef( yRot, 0.0f, 1.0f, 0.0f );
 
   // Begin a triangle fan
   glBegin( GL_TRIANGLE_FAN );
 
   // Pinnacle of cone is shared vertex for fan, moved up Z axis
   // to produce a cone instead of a circle
   glVertex3f( 0.0f, 0.0f, 75.0f );
 
   // Loop around in a circle and specify even points along the circle
   // as the vertices of the triangle fan
   for( angle = 0.0f; angle < ( 2.0f*GL_PI ); angle += ( GL_PI/8.0f ) ) {
       // Calculate x and y position of the next vertex
       x = 50.0f * sin( angle );
       y = 50.0f * cos( angle );
 
       // Alternate color between red and green
       if( ( iPivot %2 ) == 0 ) {
           glColor3f(0.0f, 1.0f, 0.0f);
       } else {
           glColor3f(1.0f, 0.0f, 0.0f);
       }
 
       // Increment pivot to change color next time
       iPivot++;
 
       // Specify the next vertex for the triangle fan
       glVertex2f( x, y );
   }
 
   // Done drawing fan for cone
   glEnd();
 
   // Begin a new triangle fan to cover the bottom
   glBegin( GL_TRIANGLE_FAN );
 
   // Center of fan is at the origin
   glVertex2f( 0.0f, 0.0f );
   for(angle = 0.0f; angle < ( 2.0f*GL_PI ); angle += ( GL_PI/8.0f ) ) {
       // Calculate x and y position of the next vertex
       x = 50.0f * sin( angle );
       y = 50.0f * cos( angle );
 
       // Alternate color between red and green
       if( ( iPivot %2 ) == 0 ) {
           glColor3f( 0.0f, 1.0f, 0.0f );
       } else {
           glColor3f( 1.0f, 0.0f, 0.0f );
       }
 
       // Increment pivot to change color next time
       iPivot++;
 
       // Specify the next vertex for the triangle fan
       glVertex2f( x, y );
   }
 
   // Done drawing the fan that covers the bottom
   glEnd();
 
   // Restore transformations
   glPopMatrix();
}
 
void Scene::resizeGL( int w, int h )
{
   // Prevent a divide by zero
   if( h == 0 ) {
       h = 1;
   }
 
   // Set Viewport to window dimensions
   glViewport( 0, 0, w, h );
 
   // Reset projection matrix stack
   glMatrixMode( GL_PROJECTION );
   glLoadIdentity();
 
   GLfloat nRange = 100.0f;
 
   // Establish clipping volume (left, right, bottom, top, near, far)
   if ( w <= h ) {
       glOrtho ( -nRange, nRange, -nRange*h/w, nRange*h/w,
                 -nRange, nRange );
   } else {
       glOrtho ( -nRange*w/h, nRange*w/h, -nRange, nRange,
                 -nRange, nRange );
   }
 
   // Reset Model view matrix stack
   glMatrixMode( GL_MODELVIEW );
   glLoadIdentity();
}
 
void Scene::keyPressEvent( QKeyEvent *event )
{
   switch ( event->key() ) {
   case Qt::Key_Up:
       xRot -= 5.0f;
       break;
   case Qt::Key_Down:
       xRot += 5.0f;
       break;
   case Qt::Key_Left:
       yRot -= 5.0f;
       break;
   case Qt::Key_Right:
       yRot += 5.0f;
       break;
   }
 
   if( xRot > 356.0f ) {
       xRot = 0.0f;
   }
 
   if( xRot < -1.0f ) {
       xRot = 355.0f;
   }
 
   if( yRot > 356.0f) {
       yRot = 0.0f;
   }
 
   if( yRot < -1.0f) {
       yRot = 355.0f;
   }
 
   updateGL();
}
 
« Последнее редактирование: Сентябрь 26, 2014, 14:31 от 8Observer8 » Записан
8Observer8
Гость
« Ответ #18 : Октябрь 13, 2014, 07:39 »

03. PStipple (Polygin Stippling). Наложение на многоугольник монохромного растрового изображения 32x32 (фактура)



Scene.h
Код
C++ (Qt)
#ifndef SCENE_H
#define SCENE_H
 
#include <QGLWidget>
#include <QKeyEvent>
 
class Scene : public QGLWidget
{
   Q_OBJECT
 
public:
   Scene( QWidget *parent = 0 );
 
private:
   void initializeGL( );
   void paintGL( );
   void resizeGL( int w, int h );
 
   void keyPressEvent( QKeyEvent *event );
 
private:
   // Rotation amounts
   GLfloat xRot;
   GLfloat yRot;
};
 
#endif // SCENE_H
 

Scene.cpp
Код
C++ (Qt)
 
#include "Scene.h"
 
// Define a constant for the value of PI
#define GL_PI 3.1415f
 
// Bitmap of camp fire
GLubyte fire[128] = { 0x00, 0x00, 0x00, 0x00,
                     0x00, 0x00, 0x00, 0x00,
                     0x00, 0x00, 0x00, 0x00,
                     0x00, 0x00, 0x00, 0x00,
                     0x00, 0x00, 0x00, 0x00,
                     0x00, 0x00, 0x00, 0x00,
                     0x00, 0x00, 0x00, 0xc0,
                     0x00, 0x00, 0x01, 0xf0,
                     0x00, 0x00, 0x07, 0xf0,
                     0x0f, 0x00, 0x1f, 0xe0,
                     0x1f, 0x80, 0x1f, 0xc0,
                     0x0f, 0xc0, 0x3f, 0x80,
                     0x07, 0xe0, 0x7e, 0x00,
                     0x03, 0xf0, 0xff, 0x80,
                     0x03, 0xf5, 0xff, 0xe0,
                     0x07, 0xfd, 0xff, 0xf8,
                     0x1f, 0xfc, 0xff, 0xe8,
                     0xff, 0xe3, 0xbf, 0x70,
                     0xde, 0x80, 0xb7, 0x00,
                     0x71, 0x10, 0x4a, 0x80,
                     0x03, 0x10, 0x4e, 0x40,
                     0x02, 0x88, 0x8c, 0x20,
                     0x05, 0x05, 0x04, 0x40,
                     0x02, 0x82, 0x14, 0x40,
                     0x02, 0x40, 0x10, 0x80,
                     0x02, 0x64, 0x1a, 0x80,
                     0x00, 0x92, 0x29, 0x00,
                     0x00, 0xb0, 0x48, 0x00,
                     0x00, 0xc8, 0x90, 0x00,
                     0x00, 0x85, 0x10, 0x00,
                     0x00, 0x03, 0x00, 0x00,
                     0x00, 0x00, 0x10, 0x00 };
 
Scene::Scene( QWidget *parent ) :
   QGLWidget( parent ),
   xRot( 0.0f ),
   yRot( 0.0f )
{
}
 
void Scene::initializeGL( )
{
   // Black background
   glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
 
   // Set drawing color to green
   glColor3f( 1.0f, 0.0f, 0.0f );
 
   // Enable polygon stippling
   glEnable( GL_POLYGON_STIPPLE );
 
   // Specify a specific stipple pattern
   glPolygonStipple( fire );
}
 
void Scene::paintGL( )
{
   // Clear the window
   glClear( GL_COLOR_BUFFER_BIT );
 
   // Save matrix state and do the rotation
   glPushMatrix( );
   glRotatef( xRot, 1.0f, 0.0f, 0.0f );
   glRotatef( yRot, 0.0f, 1.0f, 0.0f );
 
   // Begin the stop sign shape,
   // use a standard polygon for simplicity
   glBegin( GL_POLYGON );
   glVertex2f( -20.0f, 50.0f );
   glVertex2f( 20.0f, 50.0f );
   glVertex2f( 50.0f, 20.0f );
   glVertex2f( 50.0f, -20.0f );
   glVertex2f( 20.0f, -50.0f );
   glVertex2f( -20.0f, -50.0f );
   glVertex2f( -50.0f, -20.0f );
   glVertex2f( -50.0f, 20.0f );
   glEnd( );
 
   // Restore transformations
   glPopMatrix( );
}
 
void Scene::resizeGL( int w, int h )
{
   // Prevent a divide by zero
   if ( h == 0 ) {
       h = 1;
   }
 
   // Set Viewport to window dimensions
   glViewport( 0, 0, w, h );
 
   // Reset projection matrix stack
   glMatrixMode( GL_PROJECTION );
   glLoadIdentity( );
 
   GLfloat nRange = 100.0f;
 
   // Establish clipping volume (left, right, bottom, top, near, far)
   if ( w <= h ) {
       glOrtho( -nRange, nRange, -nRange * h / w, nRange * h / w,
                -nRange, nRange );
   } else {
       glOrtho( -nRange * w / h, nRange * w / h, -nRange, nRange,
                -nRange, nRange );
   }
 
   // Reset Model view matrix stack
   glMatrixMode( GL_MODELVIEW );
   glLoadIdentity( );
}
 
void Scene::keyPressEvent( QKeyEvent *event )
{
   switch ( event->key( ) ) {
       case Qt::Key_Up:
           xRot -= 5.0f;
           break;
       case Qt::Key_Down:
           xRot += 5.0f;
           break;
       case Qt::Key_Left:
           yRot -= 5.0f;
           break;
       case Qt::Key_Right:
           yRot += 5.0f;
           break;
   }
 
   if ( xRot > 356.0f ) {
       xRot = 0.0f;
   }
 
   if ( xRot < -1.0f ) {
       xRot = 355.0f;
   }
 
   if ( yRot > 356.0f ) {
       yRot = 0.0f;
   }
 
   if ( yRot < -1.0f ) {
       yRot = 355.0f;
   }
 
   updateGL( );
}
 
« Последнее редактирование: Октябрь 13, 2014, 07:44 от 8Observer8 » Записан
8Observer8
Гость
« Ответ #19 : Октябрь 14, 2014, 14:06 »

03. Star. Активизация и диактивизация отображения сторон многоугольников с помощью функции glEdgeFlag( GLboolean flag )







Scene.h
Код
C++ (Qt)
#ifndef SCENE_H
#define SCENE_H
 
#include <QGLWidget>
#include <QKeyEvent>
 
class Scene : public QGLWidget
{
   Q_OBJECT
 
public:
   Scene( QWidget *parent = 0 );
 
private slots:
   void slotShowContextMenu( const QPoint &pos );
 
private:
   void initializeGL( );
   void paintGL( );
   void resizeGL( int w, int h );
 
   void keyPressEvent( QKeyEvent *event );
 
private:
   // Rotation amounts
   GLfloat xRot;
   GLfloat yRot;
 
   int iMode;
   GLboolean bEdgeFlag;
 
   // Flags for effects
   int iCull;
   int iOutline;
   int iDepth;
};
 
#endif // SCENE_H
 

Scene.cpp
Код
C++ (Qt)
#include <QAbstractScrollArea>
#include <QMenu>
#include <math.h>
#include "Scene.h"
 
// Define a constant for the value of PI
#define GL_PI 3.1415f
 
// Flags for effects
#define MODE_SOLID 0
#define MODE_LINE  1
#define MODE_POINT 2
 
Scene::Scene( QWidget *parent ) :
   QGLWidget( parent ),
   xRot( 0.0f ),
   yRot( 0.0f ),
   iCull( 0 ),
   iOutline( 0 ),
   iDepth( 0 )
{
   iMode = MODE_SOLID;
   bEdgeFlag = TRUE;
 
   this->setFocusPolicy( Qt::StrongFocus );
   this->setContextMenuPolicy( Qt::CustomContextMenu );
 
   connect( this, SIGNAL( customContextMenuRequested( QPoint ) ),
            this, SLOT( slotShowContextMenu( QPoint ) ) );
}
 
void Scene::slotShowContextMenu( const QPoint &pos )
{
   QPoint globalPos;
   if ( sender( )->inherits( "QAbstractScrollArea" ) ) {
       globalPos = ( ( QAbstractScrollArea* ) sender( ) )->
                   viewport( )->mapToGlobal( pos );
   } else {
       globalPos = ( ( QWidget* ) sender( ) )->mapToGlobal( pos );
   }
 
   QString solidText( "Solid" );
   QString outlineText( "Outline" );
   QString pointsText( "Points" );
 
   QString onEdgesText( "On" );
   QString offEdgesText( "Off" );
 
   QMenu *menu = new QMenu( );
   QAction *solidAction = new QAction( solidText, this );
   QAction *outlineAction = new QAction( outlineText, this );
   QAction *pointsAction = new QAction( pointsText, this );
   QAction *onEdgesAction = new QAction( onEdgesText, this );
   QAction *offEdgesAction = new QAction( offEdgesText, this );
 
   QMenu *modeSubmenu = menu->addMenu( "Mode" );
   QMenu *edgesSubmenu = menu->addMenu( "Edges" );
   modeSubmenu->addAction( solidAction );
   modeSubmenu->addAction( outlineAction );
   modeSubmenu->addAction( pointsAction );
   edgesSubmenu->addAction( onEdgesAction );
   edgesSubmenu->addAction( offEdgesAction );
 
   QAction* selectedItem = menu->exec( globalPos );
 
   if ( selectedItem == 0 ) {
       return;
   }
 
   if ( selectedItem->text( ) == solidText ) {
       iMode = MODE_SOLID;
   } else if ( selectedItem->text( ) == outlineText ) {
       iMode = MODE_LINE;
   } else if ( selectedItem->text( ) == pointsText ) {
       iMode = MODE_POINT;
   } else if ( selectedItem->text( ) == onEdgesText ) {
       bEdgeFlag = TRUE;
   } else if ( selectedItem->text( ) == offEdgesText ) {
       bEdgeFlag = FALSE;
   }
}
 
void Scene::initializeGL( )
{
   // Black background
   glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
 
   // Set drawing color to green
   glColor3f( 0.0f, 1.0f, 0.0f );
 
   // Set color shading model to flat
   glShadeModel( GL_FLAT );
 
   // Clock wise wound polygons are front facing, this is reversed
   // because we are using triangle fans
   glFrontFace( GL_CW );
}
 
void Scene::paintGL( )
{
   // Clear the window
   glClear( GL_COLOR_BUFFER_BIT );
 
   // Draw back side as a polygon only, if flag is set
   if ( iMode == MODE_LINE )
       glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
 
   if ( iMode == MODE_POINT )
       glPolygonMode( GL_FRONT_AND_BACK, GL_POINT );
 
   if ( iMode == MODE_SOLID )
       glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
 
   // Save matrix state and do the rotation
   glPushMatrix( );
   glRotatef( xRot, 1.0f, 0.0f, 0.0f );
   glRotatef( yRot, 0.0f, 1.0f, 0.0f );
 
   // Begin the triangles
   glBegin( GL_TRIANGLES );
 
   glEdgeFlag( bEdgeFlag );
   glVertex2f( -20.0f, 0.0f );
   glEdgeFlag( TRUE );
   glVertex2f( 20.0f, 0.0f );
   glVertex2f( 0.0f, 40.0f );
 
   glVertex2f( -20.0f, 0.0f );
   glVertex2f( -60.0f, -20.0f );
   glEdgeFlag( bEdgeFlag );
   glVertex2f( -20.0f, -40.0f );
   glEdgeFlag( TRUE );
 
   glVertex2f( -20.0f, -40.0f );
   glVertex2f( 0.0f, -80.0f );
   glEdgeFlag( bEdgeFlag );
   glVertex2f( 20.0f, -40.0f );
   glEdgeFlag( TRUE );
 
   glVertex2f( 20.0f, -40.0f );
   glVertex2f( 60.0f, -20.0f );
   glEdgeFlag( bEdgeFlag );
   glVertex2f( 20.0f, 0.0f );
   glEdgeFlag( TRUE );
 
   // Center square as two triangles
   glEdgeFlag( bEdgeFlag );
   glVertex2f( -20.0f, 0.0f );
   glVertex2f( -20.0f, -40.0f );
   glVertex2f( 20.0f, 0.0f );
 
   glVertex2f( -20.0f, -40.0f );
   glVertex2f( 20.0f, -40.0f );
   glVertex2f( 20.0f, 0.0f );
   glEdgeFlag( TRUE );
 
   // Done drawing Triangles
   glEnd( );
 
   // Restore transformations
   glPopMatrix( );
}
 
void Scene::resizeGL( int w, int h )
{
   // Prevent a divide by zero
   if ( h == 0 ) {
       h = 1;
   }
 
   // Set Viewport to window dimensions
   glViewport( 0, 0, w, h );
 
   // Reset projection matrix stack
   glMatrixMode( GL_PROJECTION );
   glLoadIdentity( );
 
   GLfloat nRange = 100.0f;
 
   // Establish clipping volume (left, right, bottom, top, near, far)
   if ( w <= h ) {
       glOrtho( -nRange, nRange, -nRange * h / w, nRange * h / w,
                -nRange, nRange );
   } else {
       glOrtho( -nRange * w / h, nRange * w / h, -nRange, nRange,
                -nRange, nRange );
   }
 
   // Reset Model view matrix stack
   glMatrixMode( GL_MODELVIEW );
   glLoadIdentity( );
}
 
void Scene::keyPressEvent( QKeyEvent *event )
{
   switch ( event->key( ) ) {
       case Qt::Key_Up:
           xRot -= 5.0f;
           break;
       case Qt::Key_Down:
           xRot += 5.0f;
           break;
       case Qt::Key_Left:
           yRot -= 5.0f;
           break;
       case Qt::Key_Right:
           yRot += 5.0f;
           break;
   }
 
   if ( xRot > 356.0f ) {
       xRot = 0.0f;
   }
 
   if ( xRot < -1.0f ) {
       xRot = 355.0f;
   }
 
   if ( yRot > 356.0f ) {
       yRot = 0.0f;
   }
 
   if ( yRot < -1.0f ) {
       yRot = 355.0f;
   }
 
   updateGL( );
}
 
Записан
8Observer8
Гость
« Ответ #20 : Октябрь 14, 2014, 19:24 »

03. Scissor. Вырезание прямоугольника, в котором выполняется визуализация



Scene.h
Код
C++ (Qt)
#ifndef SCENE_H
#define SCENE_H
 
#include <QGLWidget>
 
class Scene : public QGLWidget
{
   Q_OBJECT
 
public:
   Scene( QWidget *parent = 0 );
 
private:
   void initializeGL( );
   void paintGL( );
   void resizeGL( int w, int h );
};
 
#endif // SCENE_H
 

Scene.cpp
Код
C++ (Qt)
#include <QAbstractScrollArea>
#include <QMenu>
#include <math.h>
#include "Scene.h"
 
Scene::Scene( QWidget *parent ) :
   QGLWidget( parent )
{
}
 
void Scene::initializeGL( )
{
}
 
void Scene::paintGL( )
{
   // Clear blue window
   glClearColor( 0.0f, 0.0f, 1.0f, 0.0f );
   glClear( GL_COLOR_BUFFER_BIT );
 
   // Now set scissor to smaller red sub region
   glClearColor( 1.0f, 0.0f, 0.0f, 0.0f );
   glScissor( 100, 100, 600, 400 );
   glEnable( GL_SCISSOR_TEST );
   glClear( GL_COLOR_BUFFER_BIT );
 
   // Finally, an even smaller green rectangle
   glClearColor( 0.0f, 1.0f, 0.0f, 0.0f );
   glScissor( 200, 200, 400, 200 );
   glClear( GL_COLOR_BUFFER_BIT );
 
   // Turn scissor back off for next render
   glDisable( GL_SCISSOR_TEST );
}
 
void Scene::resizeGL( int w, int h )
{
   // Prevent a divide by zero
   if ( h == 0 ) {
       h = 1;
   }
 
   // Set Viewport to window dimensions
   glViewport( 0, 0, w, h );
 
   // Reset projection matrix stack
   glMatrixMode( GL_PROJECTION );
   glLoadIdentity( );
 
   GLfloat nRange = 100.0f;
 
   // Establish clipping volume (left, right, bottom, top, near, far)
   if ( w <= h ) {
       glOrtho( -nRange, nRange, -nRange * h / w, nRange * h / w,
                -nRange, nRange );
   } else {
       glOrtho( -nRange * w / h, nRange * w / h, -nRange, nRange,
                -nRange, nRange );
   }
 
   // Reset Model view matrix stack
   glMatrixMode( GL_MODELVIEW );
   glLoadIdentity( );
}
 
Записан
Страниц: 1 [2]   Вверх
  Печать  
 
Перейти в:  


Страница сгенерирована за 0.433 секунд. Запросов: 21.