Anwendung mit abgeleitetem OGLWidget wird fehlerhaft beendet

Alles rund um die Programmierung mit Qt
Antworten
gogonfa
Beiträge: 7
Registriert: 12. Januar 2010 20:48

Anwendung mit abgeleitetem OGLWidget wird fehlerhaft beendet

Beitrag von gogonfa »

Hallo Qt-Gemeinde,

habe ein ganz merkwürdiges Problem.....

Ich habe ein eigenes OpenGLWidget (natürlich von dem OGLWidget abgeleitet). Wenn ich das Ding in meiner Hauptanwendung einbaue

Code: Alles auswählen

this->m_glwidget=new OpenGlWidget(this);
this->setCentralWidget(m_glwidget);
und laufen lass ist zunächst alles prima....beende ich die Anwendung jedoch, bekomme ich einen Fehler (siehe Bild im Anhang)

Der Debuger schmeißt folgenden Fehler:

Code: Alles auswählen

ASSERT: "group->context() == q_ptr" in file qgl.cpp, line 1684
Invalid parameter passed to C runtime function.
Hat irgend wer eine Ahnung woran das hängen könnte?
Hier noch der Code des OpenGlViewers:

Code: Alles auswählen

#include "openglviewer.h"


#ifndef GL_MULTISAMPLE
#define GL_MULTISAMPLE  0x809D
#endif


OpenGlView::OpenGlView(QWidget *parent): QGLWidget(parent)
{

    this->m_background=new Background(QColor(89,133,255),QColor(225,225,225));

    this->m_camera=new Camera(this->height(),
                              this->width(),
                              QVector3D(0.0,0.0,10),
                              QVector3D(0,0,0),
                              QVector3D(0,1,0),::PERSPECTIVE);

    this->m_camera->setFarPlane(10000);
    this->m_camera->setNearPlane(1);
    this->m_camera->setFOV(90);

    this->m_clear_color=QColor(114,114,114);

    this->setFormat(QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer | QGL::AlphaChannel));
}

OpenGlView::~OpenGlView()
{
    delete this->m_background;
    delete this->m_camera;
}

void OpenGlView::initializeGL()
{
    qglClearColor(this->m_clear_color);
    glDisable(GL_TEXTURE_2D);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glEnable(GL_CULL_FACE);
    glShadeModel(GL_SMOOTH);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_MULTISAMPLE);
    glEnable(GL_COLOR_MATERIAL);

    static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 };
    glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
}

void OpenGlView::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    draw();
}

void OpenGlView::resizeGL(int width, int height)
{
    glViewport(0, 0, width , height);
    this->m_camera->RenderTargetSizeChanged(height,width);
    this->m_camera->Transform();
}

void OpenGlView::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() == Qt::LeftButton)
    {
        if (this->m_camera->getCameraMode() == PAN)
        {
            this->m_camera->Pan(event->x(), event->y());
            updateGL();
        }
        else if (this->m_camera->getCameraMode() == ROTATE)
        {
            this->m_camera->Rotate(event->x(), event->y());
            updateGL();
        }
    }
    else if (event->buttons() == Qt::RightButton)
    {
        this->m_camera->Pan(event->x(), event->y());
        updateGL();
    }
    else if (event->buttons() == Qt::MidButton)
    {
        this->m_camera->Rotate(event->x(), event->y());
        updateGL();
    }

}

void OpenGlView::wheelEvent(QWheelEvent *event)
{
    if (event->delta() < 0)
        this->m_camera->ZoomOut();
    else if (event->delta() > 0)
        this->m_camera->ZoomIn();

    updateGL();
}

void OpenGlView::mousePressEvent(QMouseEvent *event)
{
    if (event->button()==Qt::LeftButton)
    {
        if (this->m_camera->getCameraMode() == PAN)
        {
            this->m_camera->StartPan(event->x(), event->y());
        }
        else if (this->m_camera->getCameraMode() ==ROTATE)
        {
            this->m_camera->StartRotation(event->x(), event->y());
        }
    }
    else if (event->button()==Qt::RightButton)
    {
        this->m_camera->StartPan(event->x(), event->y());
    }
    else if (event->button()==Qt::MidButton)
    {
        this->m_camera->StartRotation(event->x(), event->y());
    }
}

void OpenGlView::draw()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);

    //Background zeichnen
    this->m_background->draw();

    this->m_camera->Transform();

    //Trihedron zeichnen
    this->drawAxis3D();

    glPushMatrix();

    //Hier wird gezeichnet!!!

    glPopMatrix();

}

void OpenGlView::drawAxis3D()
{
    GLUquadricObj *pObj;	// Temporary, used for quadrics

    // Measurements
    float fAxisRadius = 0.025f;
    float fAxisHeight = 1.0f;
    float fArrowRadius = 0.06f;
    float fArrowHeight = 0.1f;

    // Setup the quadric object
    pObj = gluNewQuadric();
    gluQuadricDrawStyle(pObj, GLU_FILL);
    gluQuadricNormals(pObj, GLU_SMOOTH);
    gluQuadricOrientation(pObj, GLU_OUTSIDE);
    gluQuadricTexture(pObj, GLU_FALSE);


    ///////////////////////////////////////////////////////
    // Draw the blue Z axis first, with arrowed head
    glColor4f(0.0f, 0.0f, 1.0f,1.0f);
    gluCylinder(pObj, fAxisRadius, fAxisRadius, fAxisHeight, 10, 1);
    glPushMatrix();
    glTranslatef(0.0f, 0.0f, 1.0f);
    gluCylinder(pObj, fArrowRadius, 0.0f, fArrowHeight, 10, 1);
    glRotatef(180.0f, 1.0f, 0.0f, 0.0f);
    gluDisk(pObj, fAxisRadius, fArrowRadius, 10, 1);
    glPopMatrix();

    ///////////////////////////////////////////////////////
    // Draw the Red X axis 2nd, with arrowed head
    glColor4f(1.0f, 0.0f, 0.0f,1.0f);
    glPushMatrix();
    glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
    gluCylinder(pObj, fAxisRadius, fAxisRadius, fAxisHeight, 10, 1);
    glPushMatrix();
    glTranslatef(0.0f, 0.0f, 1.0f);
    gluCylinder(pObj, fArrowRadius, 0.0f, fArrowHeight, 10, 1);
    glRotatef(180.0f, 0.0f, 1.0f, 0.0f);
    gluDisk(pObj, fAxisRadius, fArrowRadius, 10, 1);
    glPopMatrix();
    glPopMatrix();

    ///////////////////////////////////////////////////////
    // Draw the Green Y axis 3rd, with arrowed head
    glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
    glPushMatrix();
    glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
    gluCylinder(pObj, fAxisRadius, fAxisRadius, fAxisHeight, 10, 1);
    glPushMatrix();
    glTranslatef(0.0f, 0.0f, 1.0f);
    gluCylinder(pObj, fArrowRadius, 0.0f, fArrowHeight, 10, 1);
    glRotatef(180.0f, 1.0f, 0.0f, 0.0f);
    gluDisk(pObj, fAxisRadius, fArrowRadius, 10, 1);
    glPopMatrix();
    glPopMatrix();

    ////////////////////////////////////////////////////////
    // White Sphere at origin
    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    gluSphere(pObj, 0.05f, 15, 15);

    // Delete the quadric
    gluDeleteQuadric(pObj);
}
Dateianhänge
Fehler.png
Fehler.png (8.92 KiB) 3585 mal betrachtet
Christian81
Beiträge: 7319
Registriert: 26. August 2004 14:11
Wohnort: Bremen
Kontaktdaten:

Re: Anwendung mit abgeleitetem OGLWidget wird fehlerhaft bee

Beitrag von Christian81 »

Standardvorgehen - Programm soweit reduzieren bis es nicht mehr crasht und dann diesen Code hochladen wenn es nicht ersichtlich ist warum.
MfG Christian

'Funktioniert nicht' ist keine Fehlerbeschreibung
gogonfa
Beiträge: 7
Registriert: 12. Januar 2010 20:48

Re: Anwendung mit abgeleitetem OGLWidget wird fehlerhaft bee

Beitrag von gogonfa »

Hallo Christian,

hier die reduzierte Variante:

OpenGlViewer.h

Code: Alles auswählen

#ifndef OPENGLVIEWER_H
#define OPENGLVIEWER_H

#include <QGLWidget>

class OpenGlView : public QGLWidget
{
    Q_OBJECT

private:
    QColor m_clear_color;

public:
    explicit OpenGlView(QWidget *parent = 0);
    ~OpenGlView();

    void draw();
    void drawAxis3D();
    void redraw();

protected:
    void initializeGL();
    void paintGL();
    void resizeGL(int width, int height);
};

#endif // OPENGLVIEWER_H

OpenGLViewer.cpp

Code: Alles auswählen

#include "openglviewer.h"


#ifndef GL_MULTISAMPLE
#define GL_MULTISAMPLE  0x809D
#endif

OpenGlView::OpenGlView(QWidget *parent): QGLWidget(parent)
{
    this->m_clear_color=QColor(114,114,114);
    this->setFormat(QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer | QGL::AlphaChannel));
}

void OpenGlView::initializeGL()
{
    qglClearColor(this->m_clear_color);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glClearDepth(1.0f);

    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

    glEnable(GL_CULL_FACE);
    glShadeModel(GL_SMOOTH);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_MULTISAMPLE);
    glEnable(GL_COLOR_MATERIAL);

    static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 };
    glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
}

void OpenGlView::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    draw();
}

void OpenGlView::resizeGL(int width, int height)
{
    glViewport(0, 0, width , height);
}

void OpenGlView::draw()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);

    glPushMatrix();

    //Hier wird gezeichnet!!!

    glPopMatrix();

    glMatrixMode(GL_PROJECTION);

}

und hier der Code für das Einbinden im MainWindow

mainwindow.h

Code: Alles auswählen


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "openglviewer.h"


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    
private:
    Ui::MainWindow *ui;
    OpenGlView *m_glwidget;

};

#endif // MAINWINDOW_H

mainwindow.cpp

Code: Alles auswählen

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->m_glwidget=new OpenGlView(this);
    this->setCentralWidget(m_glwidget);

}

MainWindow::~MainWindow()
{
    delete ui;
    delete m_glwidget;
}
Die Anwendung crasht nach wie vor.....

grüße
Gogonfa
gogonfa
Beiträge: 7
Registriert: 12. Januar 2010 20:48

Re: Anwendung mit abgeleitetem OGLWidget wird fehlerhaft bee

Beitrag von gogonfa »

Huhu,

ich glaube ich habe den Fehler gefunden!!!! Das Problem liegt im Konstruktor....kann mir bitte noch jemand erklären warum!?

Dieser Code hier crasht:

Code: Alles auswählen

OpenGlViewer::OpenGlViewer(QWidget *parent): QGLWidget(parent)
{
    this->setFormat(QGLFormat(QGL::SampleBuffers));
}
dieser hier nicht

Code: Alles auswählen

OpenGlViewer::OpenGlViewer(QWidget *parent): QGLWidget(QGLFormat(QGL::SampleBuffers),parent)
{   
}
Grüße

Gogonfa
Phys1k3r
Beiträge: 4
Registriert: 21. August 2012 15:35

Re: Anwendung mit abgeleitetem OGLWidget wird fehlerhaft bee

Beitrag von Phys1k3r »

Hallo,
ich hatte das gleiche Problem.

Dank der Antworten stürzt mein Programm nun nicht mehr ab, mich würden die genaueren Hintergründe aber interessieren.

Danke
Antworten