examples/activeqt/opengl/glbox.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 /****************************************************************************
       
    43 **
       
    44 ** This is a simple QGLWidget displaying an openGL wireframe box
       
    45 **
       
    46 ** The OpenGL code is mostly borrowed from Brian Pauls "spin" example
       
    47 ** in the Mesa distribution
       
    48 **
       
    49 ****************************************************************************/
       
    50 
       
    51 #include "glbox.h"
       
    52 #include <QAxAggregated>
       
    53 #include <QUuid>
       
    54 //! [0]
       
    55 #include <objsafe.h>
       
    56 //! [0]
       
    57 
       
    58 #if defined(Q_CC_MSVC)
       
    59 #pragma warning(disable:4305) // init: truncation from const double to float
       
    60 #endif
       
    61 
       
    62 /*!
       
    63   Create a GLBox widget
       
    64 */
       
    65 
       
    66 GLBox::GLBox( QWidget* parent, const char* name )
       
    67     : QGLWidget( parent )
       
    68 {
       
    69     xRot = yRot = zRot = 0.0;		// default object rotation
       
    70     scale = 1.25;			// default object scale
       
    71     object = 0;
       
    72 }
       
    73 
       
    74 
       
    75 /*!
       
    76   Release allocated resources
       
    77 */
       
    78 
       
    79 GLBox::~GLBox()
       
    80 {
       
    81     makeCurrent();
       
    82     glDeleteLists( object, 1 );
       
    83 }
       
    84 
       
    85 
       
    86 /*!
       
    87   Paint the box. The actual openGL commands for drawing the box are
       
    88   performed here.
       
    89 */
       
    90 
       
    91 void GLBox::paintGL()
       
    92 {
       
    93     glClear( GL_COLOR_BUFFER_BIT );
       
    94 
       
    95     glLoadIdentity();
       
    96     glTranslatef( 0.0, 0.0, -10.0 );
       
    97     glScalef( scale, scale, scale );
       
    98 
       
    99     glRotatef( xRot, 1.0, 0.0, 0.0 ); 
       
   100     glRotatef( yRot, 0.0, 1.0, 0.0 ); 
       
   101     glRotatef( zRot, 0.0, 0.0, 1.0 );
       
   102 
       
   103     glCallList( object );
       
   104 }
       
   105 
       
   106 
       
   107 /*!
       
   108   Set up the OpenGL rendering state, and define display list
       
   109 */
       
   110 
       
   111 void GLBox::initializeGL()
       
   112 {
       
   113     qglClearColor(Qt::black); 		// Let OpenGL clear to black
       
   114     object = makeObject();		// Generate an OpenGL display list
       
   115     glShadeModel( GL_FLAT );
       
   116 }
       
   117 
       
   118 
       
   119 
       
   120 /*!
       
   121   Set up the OpenGL view port, matrix mode, etc.
       
   122 */
       
   123 
       
   124 void GLBox::resizeGL( int w, int h )
       
   125 {
       
   126     glViewport( 0, 0, (GLint)w, (GLint)h );
       
   127     glMatrixMode( GL_PROJECTION );
       
   128     glLoadIdentity();
       
   129     glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
       
   130     glMatrixMode( GL_MODELVIEW );
       
   131 }
       
   132 
       
   133 
       
   134 /*!
       
   135   Generate an OpenGL display list for the object to be shown, i.e. the box
       
   136 */
       
   137 
       
   138 GLuint GLBox::makeObject()
       
   139 {	
       
   140     GLuint list;
       
   141 
       
   142     list = glGenLists( 1 );
       
   143 
       
   144     glNewList( list, GL_COMPILE );
       
   145 
       
   146     qglColor(Qt::white);		      // Shorthand for glColor3f or glIndex
       
   147 
       
   148     glLineWidth( 2.0 );
       
   149 
       
   150     glBegin( GL_LINE_LOOP );
       
   151     glVertex3f(  1.0,  0.5, -0.4 );
       
   152     glVertex3f(  1.0, -0.5, -0.4 );
       
   153     glVertex3f( -1.0, -0.5, -0.4 );
       
   154     glVertex3f( -1.0,  0.5, -0.4 );
       
   155     glEnd();
       
   156 
       
   157     glBegin( GL_LINE_LOOP );
       
   158     glVertex3f(  1.0,  0.5, 0.4 );
       
   159     glVertex3f(  1.0, -0.5, 0.4 );
       
   160     glVertex3f( -1.0, -0.5, 0.4 );
       
   161     glVertex3f( -1.0,  0.5, 0.4 );
       
   162     glEnd();
       
   163 
       
   164     glBegin( GL_LINES );
       
   165     glVertex3f(  1.0,  0.5, -0.4 );   glVertex3f(  1.0,  0.5, 0.4 );
       
   166     glVertex3f(  1.0, -0.5, -0.4 );   glVertex3f(  1.0, -0.5, 0.4 );
       
   167     glVertex3f( -1.0, -0.5, -0.4 );   glVertex3f( -1.0, -0.5, 0.4 );
       
   168     glVertex3f( -1.0,  0.5, -0.4 );   glVertex3f( -1.0,  0.5, 0.4 );
       
   169     glEnd();
       
   170 
       
   171     glEndList();
       
   172 
       
   173     return list;
       
   174 }
       
   175 
       
   176 
       
   177 /*!
       
   178   Set the rotation angle of the object to \e degrees around the X axis.
       
   179 */
       
   180 
       
   181 void GLBox::setXRotation( int degrees )
       
   182 {
       
   183     xRot = (GLfloat)(degrees % 360);
       
   184     updateGL();
       
   185 }
       
   186 
       
   187 
       
   188 /*!
       
   189   Set the rotation angle of the object to \e degrees around the Y axis.
       
   190 */
       
   191 
       
   192 void GLBox::setYRotation( int degrees )
       
   193 {
       
   194     yRot = (GLfloat)(degrees % 360);
       
   195     updateGL();
       
   196 }
       
   197 
       
   198 
       
   199 /*!
       
   200   Set the rotation angle of the object to \e degrees around the Z axis.
       
   201 */
       
   202 
       
   203 void GLBox::setZRotation( int degrees )
       
   204 {
       
   205     zRot = (GLfloat)(degrees % 360);
       
   206     updateGL();
       
   207 }
       
   208 
       
   209 //! [1]
       
   210 class ObjectSafetyImpl : public QAxAggregated, 
       
   211 			 public IObjectSafety
       
   212 {
       
   213 public:
       
   214 //! [1] //! [2]
       
   215     ObjectSafetyImpl() {}
       
   216 
       
   217     long queryInterface( const QUuid &iid, void **iface )
       
   218     {
       
   219 	*iface = 0;
       
   220 	if ( iid == IID_IObjectSafety )
       
   221 	    *iface = (IObjectSafety*)this;
       
   222 	else
       
   223 	    return E_NOINTERFACE;
       
   224 
       
   225 	AddRef();
       
   226 	return S_OK;
       
   227     }
       
   228 
       
   229 //! [2] //! [3]
       
   230     QAXAGG_IUNKNOWN;
       
   231 
       
   232 //! [3] //! [4]
       
   233     HRESULT WINAPI GetInterfaceSafetyOptions( REFIID riid, DWORD *pdwSupportedOptions, DWORD *pdwEnabledOptions )
       
   234     {
       
   235 	*pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA | INTERFACESAFE_FOR_UNTRUSTED_CALLER;
       
   236 	*pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA | INTERFACESAFE_FOR_UNTRUSTED_CALLER;
       
   237 	return S_OK;
       
   238     }
       
   239     HRESULT WINAPI SetInterfaceSafetyOptions( REFIID riid, DWORD pdwSupportedOptions, DWORD pdwEnabledOptions )
       
   240     {
       
   241 	return S_OK;
       
   242     }
       
   243 };
       
   244 //! [4] //! [5]
       
   245 
       
   246 QAxAggregated *GLBox::createAggregate()
       
   247 {
       
   248     return new ObjectSafetyImpl();
       
   249 }
       
   250 //! [5]