Hi there

I am absolutly new to the OSG stuff (and it's the first time I attend in 
a mailing list) and trying to get my OSG work in a Qt environment. I 
made my own OSG/Qt class, which inherits from QGLWidget and 
osgViewer::Viewer. Most of the OSG stuff works, but I can't enable the 
anti aliasing or the multi sampling.

I tried the following two configurations:

1.
ref_ptr< DisplaySettings > displaySettings = new DisplaySettings;
displaySettings->setNumMultiSamples(16);
modelView->setDisplaySettings( displaySettings.get() );

2.
osg::DisplaySettings::instance()->setNumMultiSamples(8);

But this didn't work. Does anybody had the same problems or have some 
ideas to solve this problem. I attached my class and header files.

If I'm doing something wrong concerning the "mailing list rules", just 
let me know.

Thanks a lot and regards
Dominic
/**
 * File:     qwtqlwidget.cpp
 * Authors:  Dominic Stalder, Thomas Bruederli
 *
 * Implementation of a <code>QGLWidget/code> and a 
<code>osgViewer::Viewer</code> extension
 * with implemented virtual functions:
 * 
 * - initializeGL()
 * - resizeGL(int width, int height)
 * - paintGL()
 *
 * Furthermore the class is the base <code>osgViewer::Viewer</code> 
implementation to support
 * osg in our game.
 *
 * This file is part of the Qt-based cross-platform Wii Tetris Game
 *
 * Copyright (C) 2009-2010, Dominic Stalder & Thomas Bruederli
 * Licensed under the GNU Lesser General Public License (LGPL) as published
 * by the Free Software Foundation at http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 * $Id: qwtglwidget.cpp 237 2009-11-01 17:08:27Z stald3 $
 */

#include <QtOpenGL>
#include "ui/components/qwtglwidget.h"

/** 
 * Class constructor.
 * 
 * Inherited from <code>QGLWidget</code> and <code>osgViewer::Viewer</code> for 
the use with wii tetris.
 *
 * @param  parent  parent instance of an <code>QGLWidget</code> object
 */
QwtGLWidget::QwtGLWidget(QWidget *parent) : QGLWidget(parent)
{
        osg::DisplaySettings::instance()->setNumMultiSamples(4);
        
        // creates a new osg graphics window
        osgGraphicsWindow = new osgViewer::GraphicsWindowEmbedded(0, 0, 
width(), height());
        
        // sets the open gl widget to accept focus by clicking
        setFocusPolicy(Qt::ClickFocus);
        
        // enables the automatic OpenGL buffer swapping (default = true) of the 
QGLWidget
        setAutoBufferSwap(true);
        
        // enables the mouse tracking of the QGLWidget
        setMouseTracking(true);
}

/** 
 * Class destructor.
 * 
 * Frees all the memory used in the <code>QwtGLWidget</code> instance.
 */
QwtGLWidget::~QwtGLWidget()
{
        // adapts close window events
        osgGraphicsWindow->getEventQueue()->closeWindow();
}

/**
 * Implementation of the virtual function, is called before the first call of 
paintGL() and resizeGL
 * and once whenever the <code>QGLWidget</code> has been assigned a new 
<code>QGLContext</code>.
 */
void QwtGLWidget::initializeGL()
{
        // gets the master camera of the view
        osg::Camera *masterCamera = getCamera();
        
        // sets the default viewport (full width and full height) of the master 
camera
        masterCamera->setViewport(new osg::Viewport(0, 0, width(), height()));
        
        // sets the graphics context that provides the machanism for managing 
the OpenGL graphcis context associated with this camera
        masterCamera->setGraphicsContext(osgGraphicsWindow.get());
        
        // sets the threading model the rendering traversals will use
        setThreadingModel(osgViewer::Viewer::SingleThreaded);
        
        // connects the timeout() signal of the QTimer with the updateGL slot
        // the updateGL slot will call glDraw() and this will call paintGL()
        connect(&repaintTimer, SIGNAL(timeout()), this, SLOT(updateGL()));
        
        // starts the repaint timer with an interval of 10 ms -> repaint with a 
frequency of 100 Hz
        repaintTimer.start(10);
}

/**
 * Implementation of the virtual function, is called whenever the 
<code>QGLWidget</code> has been resized.
 *
 * @param  width   new width of the <code>QGLWidget</code>
 * @param  height  new height of the <code>QGLWidget</code>
 */
void QwtGLWidget::resizeGL(int width, int height)
{
        // returns the viewers event queue and placing the window resize event 
on the back of the event queue 
        osgGraphicsWindow->getEventQueue()->windowResize(0, 0, width, height);
        
        // calls the resized method and updates the underlyning window and the 
associated cameras to stay in sync
        osgGraphicsWindow->resized(0, 0, width, height);
}

/**
 * Implementation of the virtual function, is called whenever the 
<code>QGLWidget</code> needs to be painted.
 */
void QwtGLWidget::paintGL()
{
        // draws one frame of the osg scene on to the screen
        frame();
}

/**
 * Translates the <code>QInputEvent</code> to a 
<code>osgGA::GUIEventAdapter</code>.
 *
 * @param  event  user input of type <code>QInputEvent</code>
 */
void QwtGLWidget::setKeyboardModifiers( QInputEvent* event )
{
        unsigned int mask = 0;
        
        // extracts the input key modifiers
        int modkey = event->modifiers() & (Qt::ShiftModifier | 
Qt::ControlModifier | Qt::AltModifier);
        
        // masks the SHIFT key with a bitwise OR
        if (modkey & Qt::ShiftModifier)
                mask |= osgGA::GUIEventAdapter::MODKEY_SHIFT;
        
        // masks the CTRL key with a bitwise OR
        if (modkey & Qt::ControlModifier)
                mask |= osgGA::GUIEventAdapter::MODKEY_CTRL;
        
        // masks the ALT key with a bitwise OR
        if (modkey & Qt::AltModifier)
                mask |= osgGA::GUIEventAdapter::MODKEY_ALT;
        
        // returns the viewers event queue and placing the event on the back of 
the event queue 
        
osgGraphicsWindow->getEventQueue()->getCurrentEventState()->setModKeyMask( mask 
);
}

/**
 * Implementation of the virtual function, is called whenever a 
<code>QKeyEvent</code> occured in the the <code>QGLWidget</code>.
 * Accts as a wrapper and translates the <code>QEvent</code> to a 
<code>osgGA::GUIEventAdapter</code>.
 *
 * @param  event  key pressed event of type <code>QKeyEvent</code>
 */
void QwtGLWidget::keyPressEvent(QKeyEvent* event)
{
        // translates the keyboard modifiers for osg
        setKeyboardModifiers( event );
        
        // returns the viewers event queue and placing the event on the back of 
the event queue 
        
getEventQueue()->keyPress((osgGA::GUIEventAdapter::KeySymbol)*(event->text().toAscii().data()));
}

/**
 * Implementation of the virtual function, is called whenever a 
<code>QKeyEvent</code> occured in the the <code>QGLWidget</code>.
 * Accts as a wrapper and translates the <code>QEvent</code> to a 
<code>osgGA::GUIEventAdapter</code>.
 *
 * @param  event  key release event of type <code>QKeyEvent</code>
 */
void QwtGLWidget::keyReleaseEvent(QKeyEvent* event)
{
        // translates the keyboard modifiers for osg
        setKeyboardModifiers( event );
        
        // returns the viewers event queue and placing the event on the back of 
the event queue   
        
getEventQueue()->keyRelease((osgGA::GUIEventAdapter::KeySymbol)*(event->text().toAscii().data()));
}

/**
 * Implementation of the virtual function, is called whenever a 
<code>QMouseEvent</code> occured in the the <code>QGLWidget</code>.
 * Accts as a wrapper and translates the <code>QEvent</code> to a 
<code>osgGA::GUIEventAdapter</code>.
 *
 * @param  event  mouse press event of type <code>QMouseEvent</code>
 */
void QwtGLWidget::mousePressEvent(QMouseEvent* event)
{
        int button = 0;
        
        // compares the mouse event with the predifined Qt mouse events
        switch (event->button())
        {
                case Qt::LeftButton:
                        button = 1;
                        break;
                case Qt::MidButton:
                        button = 2;
                        break;
                case Qt::RightButton:
                        button = 3;
                        break;
                case Qt::NoButton:
                        button = 0;
                        break;
                default:
                        button = 0;
                        break;
        }
        
        // translates the keyboard modifiers for osg
        setKeyboardModifiers( event );
        
        // returns the viewers event queue and placing the event on the back of 
the event queue  
        getEventQueue()->mouseButtonPress(event->x(), event->y(), button);
}

/**
 * Implementation of the virtual function, is called whenever a 
<code>QMouseEvent</code> occured in the the <code>QGLWidget</code>.
 * Accts as a wrapper and translates the <code>QEvent</code> to a 
<code>osgGA::GUIEventAdapter</code>.
 *
 * @param  event  mouse release event of type <code>QMouseEvent</code>
 */
void QwtGLWidget::mouseReleaseEvent(QMouseEvent* event)
{
        int button = 0;
        
        // compares the mouse event with the predifined Qt mouse events
        switch (event->button())
        {
                case Qt::LeftButton:
                        button = 1;
                        break;
                case Qt::MidButton:
                        button = 2;
                        break;
                case Qt::RightButton:
                        button = 3;
                        break;
                case Qt::NoButton:
                        button = 0;
                        break;
                default:
                        button = 0;
                        break;
        }
        
        // translates the keyboard modifiers for osg
        setKeyboardModifiers( event );
        
        // returns the viewers event queue and placing the event on the back of 
the event queue  
        getEventQueue()->mouseButtonRelease(event->x(), event->y(), button);
}

/**
 * Implementation of the virtual function, is called whenever a 
<code>QMouseEvent</code> occured in the the <code>QGLWidget</code>.
 * Accts as a wrapper and translates the <code>QEvent</code> to a 
<code>osgGA::GUIEventAdapter</code>.
 *
 * @param  event  mouse double click event of type <code>QMouseEvent</code>
 */
void QwtGLWidget::mouseDoubleClickEvent(QMouseEvent* event)
{
        int button = 0;
        
        // compares the mouse event with the predifined Qt mouse events
        switch (event->button())
        {
                case Qt::LeftButton:
                        button = 1;
                        break;
                case Qt::MidButton:
                        button = 2;
                        break;
                case Qt::RightButton:
                        button = 3;
                        break;
                case Qt::NoButton:
                        button = 0;
                        break;
                default:
                        button = 0;
                        break;
        }
        
        // translates the keyboard modifiers for osg
        setKeyboardModifiers( event );
        
        // returns the viewers event queue and placing the event on the back of 
the event queue  
        getEventQueue()->mouseDoubleButtonPress(event->x(), event->y(), button);
}

/**
 * Implementation of the virtual function, is called whenever a 
<code>QMouseEvent</code> occured in the the <code>QGLWidget</code>.
 * Accts as a wrapper and translates the <code>QEvent</code> to a 
<code>osgGA::GUIEventAdapter</code>.
 *
 * @param  event  mouse move event of type <code>QMouseEvent</code>
 */
void QwtGLWidget::mouseMoveEvent(QMouseEvent* event)
{
        // translates the keyboard modifiers for osg
        setKeyboardModifiers( event );
        
        // returns the viewers event queue and placing the event on the back of 
the event queue  
        getEventQueue()->mouseMotion(event->x(), event->y());
}

/**
 * Implementation of the virtual function, is called whenever a 
<code>QWheelEvent</code> occured in the the <code>QGLWidget</code>.
 * Accts as a wrapper and translates the <code>QEvent</code> to a 
<code>osgGA::GUIEventAdapter</code>.
 *
 * @param  event  mouse wheel event of type <code>QWheelEvent</code>
 */
void QwtGLWidget::wheelEvent(QWheelEvent* event)
{
        // translates the keyboard modifiers for osg
        setKeyboardModifiers( event );
        
        // returns the viewers event queue and placing the event on the back of 
the event queue  
        getEventQueue()->mouseScroll(event->delta()>0 ? 
osgGA::GUIEventAdapter::SCROLL_UP : osgGA::GUIEventAdapter::SCROLL_DOWN );
}
/**
 * File:     qwtqlwidget.h
 * Authors:  Dominic Stalder, Thomas Bruederli
 *
 * Definition of a <code>QGLWidget/code> and a <code>osgViewer::Viewer</code> 
extension
 * with the implemented virtual functions:
 * 
 * - initializeGL()
 * - resizeGL(int width, int height)
 * - paintGL()
 *
 * Furthermore the class is the base <code>osgViewer::Viewer</code> 
implementation to support
 * osg in our game.
 *
 * This file is part of the Qt-based cross-platform Wii Tetris Game
 *
 * Copyright (C) 2009-2010, Dominic Stalder & Thomas Bruederli
 * Licensed under the GNU Lesser General Public License (LGPL) as published
 * by the Free Software Foundation at http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 * $Id: qwtglwidget.h 284 2009-11-09 21:01:35Z stald3 $
 */

#ifndef QWTQLWIDGET_H
#define QWTQLWIDGET_H

#include <QGLWidget>
#include <QTimer>
#include <osgViewer/Viewer> 
#include <osg/ref_ptr>
#include <osg/DisplaySettings>

// forward declarations
class QInputEvent;

/**
 * <code>QGLWidget</code> extended for the use with wii tetris.
 */
class QwtGLWidget : public QGLWidget, public osgViewer::Viewer
{
        Q_OBJECT
        
private:
        QTimer repaintTimer;
        osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> osgGraphicsWindow;
        
public:
        QwtGLWidget(QWidget *parent = 0);
        ~QwtGLWidget(void);

protected:
        void initializeGL(void);
        void resizeGL(int width, int height);
        void paintGL(void);
        
        // the windows implementation already takes care of message handling
        #ifndef WIN32
                void setKeyboardModifiers(QInputEvent* event);
                void keyPressEvent(QKeyEvent* event);
                void keyReleaseEvent(QKeyEvent* event);
                void mousePressEvent(QMouseEvent* event);
                void mouseReleaseEvent(QMouseEvent* event);
                void mouseDoubleClickEvent(QMouseEvent* event);
                void mouseMoveEvent(QMouseEvent* event);
                void wheelEvent(QWheelEvent* event);
        #endif
};

#endif // QWTQLWIDGET_H
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to