| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 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:BSD$
|
|---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
|---|
| 11 | **
|
|---|
| 12 | ** "Redistribution and use in source and binary forms, with or without
|
|---|
| 13 | ** modification, are permitted provided that the following conditions are
|
|---|
| 14 | ** met:
|
|---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
|---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
|---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
|---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
|---|
| 19 | ** the documentation and/or other materials provided with the
|
|---|
| 20 | ** distribution.
|
|---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
|---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
|---|
| 23 | ** products derived from this software without specific prior written
|
|---|
| 24 | ** permission.
|
|---|
| 25 | **
|
|---|
| 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
|---|
| 37 | ** $QT_END_LICENSE$
|
|---|
| 38 | **
|
|---|
| 39 | ****************************************************************************/
|
|---|
| 40 |
|
|---|
| 41 | /****************************************************************************
|
|---|
| 42 | **
|
|---|
| 43 | ** This is a simple QGLWidget displaying an openGL wireframe box
|
|---|
| 44 | **
|
|---|
| 45 | ** The OpenGL code is mostly borrowed from Brian Pauls "spin" example
|
|---|
| 46 | ** in the Mesa distribution
|
|---|
| 47 | **
|
|---|
| 48 | ****************************************************************************/
|
|---|
| 49 |
|
|---|
| 50 | #include "glbox.h"
|
|---|
| 51 | #include <QAxAggregated>
|
|---|
| 52 | #include <QUuid>
|
|---|
| 53 | //! [0]
|
|---|
| 54 | #include <objsafe.h>
|
|---|
| 55 | //! [0]
|
|---|
| 56 |
|
|---|
| 57 | #if defined(Q_CC_MSVC)
|
|---|
| 58 | #pragma warning(disable:4305) // init: truncation from const double to float
|
|---|
| 59 | #endif
|
|---|
| 60 |
|
|---|
| 61 | /*!
|
|---|
| 62 | Create a GLBox widget
|
|---|
| 63 | */
|
|---|
| 64 |
|
|---|
| 65 | GLBox::GLBox( QWidget* parent, const char* name )
|
|---|
| 66 | : QGLWidget( parent )
|
|---|
| 67 | {
|
|---|
| 68 | xRot = yRot = zRot = 0.0; // default object rotation
|
|---|
| 69 | scale = 1.25; // default object scale
|
|---|
| 70 | object = 0;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 | /*!
|
|---|
| 75 | Release allocated resources
|
|---|
| 76 | */
|
|---|
| 77 |
|
|---|
| 78 | GLBox::~GLBox()
|
|---|
| 79 | {
|
|---|
| 80 | makeCurrent();
|
|---|
| 81 | glDeleteLists( object, 1 );
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 | /*!
|
|---|
| 86 | Paint the box. The actual openGL commands for drawing the box are
|
|---|
| 87 | performed here.
|
|---|
| 88 | */
|
|---|
| 89 |
|
|---|
| 90 | void GLBox::paintGL()
|
|---|
| 91 | {
|
|---|
| 92 | glClear( GL_COLOR_BUFFER_BIT );
|
|---|
| 93 |
|
|---|
| 94 | glLoadIdentity();
|
|---|
| 95 | glTranslatef( 0.0, 0.0, -10.0 );
|
|---|
| 96 | glScalef( scale, scale, scale );
|
|---|
| 97 |
|
|---|
| 98 | glRotatef( xRot, 1.0, 0.0, 0.0 );
|
|---|
| 99 | glRotatef( yRot, 0.0, 1.0, 0.0 );
|
|---|
| 100 | glRotatef( zRot, 0.0, 0.0, 1.0 );
|
|---|
| 101 |
|
|---|
| 102 | glCallList( object );
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 | /*!
|
|---|
| 107 | Set up the OpenGL rendering state, and define display list
|
|---|
| 108 | */
|
|---|
| 109 |
|
|---|
| 110 | void GLBox::initializeGL()
|
|---|
| 111 | {
|
|---|
| 112 | qglClearColor(Qt::black); // Let OpenGL clear to black
|
|---|
| 113 | object = makeObject(); // Generate an OpenGL display list
|
|---|
| 114 | glShadeModel( GL_FLAT );
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 | /*!
|
|---|
| 120 | Set up the OpenGL view port, matrix mode, etc.
|
|---|
| 121 | */
|
|---|
| 122 |
|
|---|
| 123 | void GLBox::resizeGL( int w, int h )
|
|---|
| 124 | {
|
|---|
| 125 | glViewport( 0, 0, (GLint)w, (GLint)h );
|
|---|
| 126 | glMatrixMode( GL_PROJECTION );
|
|---|
| 127 | glLoadIdentity();
|
|---|
| 128 | glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
|
|---|
| 129 | glMatrixMode( GL_MODELVIEW );
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 | /*!
|
|---|
| 134 | Generate an OpenGL display list for the object to be shown, i.e. the box
|
|---|
| 135 | */
|
|---|
| 136 |
|
|---|
| 137 | GLuint GLBox::makeObject()
|
|---|
| 138 | {
|
|---|
| 139 | GLuint list;
|
|---|
| 140 |
|
|---|
| 141 | list = glGenLists( 1 );
|
|---|
| 142 |
|
|---|
| 143 | glNewList( list, GL_COMPILE );
|
|---|
| 144 |
|
|---|
| 145 | qglColor(Qt::white); // Shorthand for glColor3f or glIndex
|
|---|
| 146 |
|
|---|
| 147 | glLineWidth( 2.0 );
|
|---|
| 148 |
|
|---|
| 149 | glBegin( GL_LINE_LOOP );
|
|---|
| 150 | glVertex3f( 1.0, 0.5, -0.4 );
|
|---|
| 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 | glEnd();
|
|---|
| 155 |
|
|---|
| 156 | glBegin( GL_LINE_LOOP );
|
|---|
| 157 | glVertex3f( 1.0, 0.5, 0.4 );
|
|---|
| 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 | glEnd();
|
|---|
| 162 |
|
|---|
| 163 | glBegin( GL_LINES );
|
|---|
| 164 | glVertex3f( 1.0, 0.5, -0.4 ); glVertex3f( 1.0, 0.5, 0.4 );
|
|---|
| 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 | glEnd();
|
|---|
| 169 |
|
|---|
| 170 | glEndList();
|
|---|
| 171 |
|
|---|
| 172 | return list;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 | /*!
|
|---|
| 177 | Set the rotation angle of the object to \e degrees around the X axis.
|
|---|
| 178 | */
|
|---|
| 179 |
|
|---|
| 180 | void GLBox::setXRotation( int degrees )
|
|---|
| 181 | {
|
|---|
| 182 | xRot = (GLfloat)(degrees % 360);
|
|---|
| 183 | updateGL();
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 | /*!
|
|---|
| 188 | Set the rotation angle of the object to \e degrees around the Y axis.
|
|---|
| 189 | */
|
|---|
| 190 |
|
|---|
| 191 | void GLBox::setYRotation( int degrees )
|
|---|
| 192 | {
|
|---|
| 193 | yRot = (GLfloat)(degrees % 360);
|
|---|
| 194 | updateGL();
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 | /*!
|
|---|
| 199 | Set the rotation angle of the object to \e degrees around the Z axis.
|
|---|
| 200 | */
|
|---|
| 201 |
|
|---|
| 202 | void GLBox::setZRotation( int degrees )
|
|---|
| 203 | {
|
|---|
| 204 | zRot = (GLfloat)(degrees % 360);
|
|---|
| 205 | updateGL();
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | //! [1]
|
|---|
| 209 | class ObjectSafetyImpl : public QAxAggregated,
|
|---|
| 210 | public IObjectSafety
|
|---|
| 211 | {
|
|---|
| 212 | public:
|
|---|
| 213 | //! [1] //! [2]
|
|---|
| 214 | ObjectSafetyImpl() {}
|
|---|
| 215 |
|
|---|
| 216 | long queryInterface( const QUuid &iid, void **iface )
|
|---|
| 217 | {
|
|---|
| 218 | *iface = 0;
|
|---|
| 219 | if ( iid == IID_IObjectSafety )
|
|---|
| 220 | *iface = (IObjectSafety*)this;
|
|---|
| 221 | else
|
|---|
| 222 | return E_NOINTERFACE;
|
|---|
| 223 |
|
|---|
| 224 | AddRef();
|
|---|
| 225 | return S_OK;
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | //! [2] //! [3]
|
|---|
| 229 | QAXAGG_IUNKNOWN;
|
|---|
| 230 |
|
|---|
| 231 | //! [3] //! [4]
|
|---|
| 232 | HRESULT WINAPI GetInterfaceSafetyOptions( REFIID riid, DWORD *pdwSupportedOptions, DWORD *pdwEnabledOptions )
|
|---|
| 233 | {
|
|---|
| 234 | *pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA | INTERFACESAFE_FOR_UNTRUSTED_CALLER;
|
|---|
| 235 | *pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA | INTERFACESAFE_FOR_UNTRUSTED_CALLER;
|
|---|
| 236 | return S_OK;
|
|---|
| 237 | }
|
|---|
| 238 | HRESULT WINAPI SetInterfaceSafetyOptions( REFIID riid, DWORD pdwSupportedOptions, DWORD pdwEnabledOptions )
|
|---|
| 239 | {
|
|---|
| 240 | return S_OK;
|
|---|
| 241 | }
|
|---|
| 242 | };
|
|---|
| 243 | //! [4] //! [5]
|
|---|
| 244 |
|
|---|
| 245 | QAxAggregated *GLBox::createAggregate()
|
|---|
| 246 | {
|
|---|
| 247 | return new ObjectSafetyImpl();
|
|---|
| 248 | }
|
|---|
| 249 | //! [5]
|
|---|