C++ (Qt)#ifndef SCENE_H#define SCENE_H #include <QGLWidget> class Scene : public QGLWidget{public: Scene( QWidget *parent = 0 ); private: void initializeGL(); void paintGL();}; #endif // SCENE_H
C++ (Qt)#include "Scene.h" Scene::Scene( QWidget *parent ) : QGLWidget( parent ){} void Scene::initializeGL(){ glClearColor( 0.0f, 0.0f, 1.0f, 1.0f );} void Scene::paintGL(){ // Clear the window with current clearing color glClear( GL_COLOR_BUFFER_BIT );}
C++ (Qt)QT += core gui opengl
C++ (Qt)#ifndef SCENE_H#define SCENE_H #include <QGLWidget> class Scene : public QGLWidget{public: Scene( QWidget *parent = 0 ); private: void initializeGL(); void paintGL(); void resizeGL( int w, int h );}; #endif // SCENE_H
C++ (Qt)#include "Scene.h" Scene::Scene( QWidget *parent ) : QGLWidget( parent ){} void Scene::initializeGL(){ glClearColor( 0.0f, 0.0f, 1.0f, 1.0f );} void Scene::paintGL(){ // Clear the window with current clearing color glClear( GL_COLOR_BUFFER_BIT ); // Set current drawing color to red // R G B glColor3f( 1.0f, 0.0f, 0.0f ); // Draw a filled rectangle with current color glRectf( -25.0f, 25.0f, 25.0f, -25.0f );} 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 coordinate system glMatrixMode( GL_PROJECTION ); glLoadIdentity(); // Establish clipping volume (left, right, bottom, top, near, far) GLfloat aspectRatio = ( GLfloat )w / ( GLfloat )h; if ( w <= h ) { glOrtho( -100.0, 100.0, -100 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0); } else { glOrtho( -100.0 * aspectRatio, 100.0 * aspectRatio, -100.0, 100.0, 1.0, -1.0 ); } glMatrixMode( GL_MODELVIEW ); glLoadIdentity();}
C++ (Qt)#ifndef SCENE_H#define SCENE_H #include <QGLWidget>#include <QTimer> class Scene : public QGLWidget{ Q_OBJECT public: Scene( QWidget *parent = 0 ); private slots: void slotMoveRect(); private: void initializeGL(); void paintGL(); void resizeGL( int w, int h ); private: // Square position and size GLfloat x; GLfloat y; GLfloat rsize; // Step size in x and y directions // (number of pixels to move each time) GLfloat xstep; GLfloat ystep; // Keep track of windows changing width and height GLfloat windowWidth; GLfloat windowHeight; // Timer QTimer *timer;}; #endif // SCENE_H
C++ (Qt) #include "Scene.h" Scene::Scene( QWidget *parent ) : QGLWidget( parent ), x( 0.0f ), y( 0.0f ), rsize( 25.0f ), xstep( 1.0f ), ystep( 1.0f ){ timer = new QTimer( this ); connect( timer, SIGNAL( timeout() ), this, SLOT( slotMoveRect() ) ); timer->start( 33 );} void Scene::slotMoveRect(){ // Reverse direction when you reach left or right edge if( x > windowWidth - rsize || x < -windowWidth ) { xstep = -xstep; } // Reverse direction when you reach top or bottom edge if( y > windowHeight || y < -windowHeight + rsize ) { ystep = -ystep; } // Actually move the square x += xstep; y += ystep; // Check bounds. This is in case the window is made // smaller while the rectangle is bouncing and the // rectangle suddenly finds itself outside the new // clipping volume if( x > ( windowWidth-rsize + xstep ) ) { x = windowWidth - rsize - 1; } else if( x < -( windowWidth + xstep ) ) { x = -windowWidth - 1; } if( y > ( windowHeight + ystep ) ) { y = windowHeight - 1; } else if( y < -( windowHeight - rsize + ystep ) ) { y = -windowHeight + rsize - 1; } updateGL();} void Scene::initializeGL(){ glClearColor( 0.0f, 0.0f, 1.0f, 1.0f );} void Scene::paintGL(){ // Clear the window with current clearing color glClear( GL_COLOR_BUFFER_BIT ); // Set current drawing color to red glColor3f( 1.0f, 0.0f, 0.0f ); // Draw a filled rectangle with current color glRectf( x, y, x + rsize, y - rsize );} 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 coordinate system glMatrixMode( GL_PROJECTION ); glLoadIdentity(); // Establish clipping volume (left, right, bottom, top, near, far) GLfloat aspectRatio = ( GLfloat ) w / ( GLfloat ) h; if ( w <= h ) { windowWidth = 100.0f; windowHeight = 100.0f / aspectRatio; glOrtho( -100.0, 100.0, -windowHeight, windowHeight, 1.0, -1.0 ); } else { windowWidth = 100.0 * aspectRatio; windowHeight = 100.0; glOrtho( -windowWidth, windowWidth, -100.0, 100.0, 1.0, -1.0 ); } glMatrixMode( GL_MODELVIEW ); glLoadIdentity();}
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
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(){ // Storeage for coordinates and angles GLfloat x, y, z, angle; // 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 ); // Call only once for all remaining points glBegin( GL_POINTS ); z = -50.0f; for( angle = 0.0f; angle <= ( 2.0f * GL_PI ) * 3.0f; angle += 0.1f ) { x = 50.0f * sin( angle ); y = 50.0f * cos( angle ); // Specify the point and move the Z value up a little glVertex3f( x, y, z ); z += 0.5f; } // Done drawing points 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();}
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 x, y, z, angle; // Storeage for coordinates and angles GLfloat sizes[2]; // Store supported point size range GLfloat step; // Store supported point size increments GLfloat curSize; // Store 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 ); // Get supported point size range and step size glGetFloatv( GL_POINT_SIZE_RANGE, sizes ); glGetFloatv( GL_POINT_SIZE_GRANULARITY, &step ); // Set the initial point size curSize = sizes[0]; // Set beginning z coordinate z = -50.0f; // Loop around in a circle three times for( angle = 0.0f; angle <= ( 2.0f * 3.1415f ) * 3.0f; angle += 0.1f ) { // Calculate x and y values on the circle x = 50.0f * sin( angle ); y = 50.0f * cos( angle ); // Specify the point size before the primative is specified glPointSize( curSize ); // Draw the point glBegin( GL_POINTS ); glVertex3f( x, y, z ); glEnd(); // Bump up the z value and the point size z += 0.5f; curSize += step; } // Restore matrix state 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();}
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(){ // Storeage for coordinates and angles GLfloat x, y, z, angle; // 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 ); // Call only once for all remaining points glBegin( GL_LINES ); z = 0.0f; for( angle = 0.0f; angle <= GL_PI; angle += ( GL_PI / 20.0f ) ) { // Top half of the circle x = 50.0f * sin( angle ); y = 50.0f * cos( angle ); glVertex3f( x, y, z ); // Bottom half of the circle x = 50.0f * sin( angle + GL_PI); y = 50.0f * cos( angle + GL_PI ); glVertex3f( x, y, z ); } // Done drawing points 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();}
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(){ // Storeage for coordinates and angles GLfloat x, y, z, angle; // 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 ); // Call only once for all remaining points glBegin( GL_LINE_STRIP ); z = -50.0f; for( angle = 0.0f; angle <= ( 2.0f*3.1415f )*3.0f; angle += 0.1f ) { x = 50.0f * sin( angle ); y = 50.0f * cos( angle ); // Specify the point and move the Z value up a little glVertex3f( x, y, z ); z += 0.5f; } // Done drawing points 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();}