[Solved]OpenGL clarification.



  • I m trying to build the following simple example (A white rectangle on a black background) But I m not able to see the same.

    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QPushButton>
    #include <QGridLayout>
    #include <QtOpenGL>
    #include <QDialog>

    class OGLPort : public QGLWidget
    {
    Q_OBJECT

    public:
    OGLPort(QWidget *parent = 0);
    void paintEvent(QPaintEvent *);
    void draw();
    private:
    QPushButton *button;
    QGLFramebufferObject *render_fbo;
    QGLFramebufferObject *texture_fbo;
    };

    #endif // MAINWINDOW_H
    @

    @#include "mainwindow.h"

    OGLPort::OGLPort(QWidget *parent) :
    QGLWidget(parent)
    {
    setWindowTitle("My OpenGL Trial");
    }

    void OGLPort::paintEvent(QPaintEvent *)
    {
    QPainter p(this);
    p.setPen(QColor(197, 197, 197, 157));
    p.setBrush(QColor(197, 197, 197, 127));
    p.drawRect(QRect(0, 0, width(), 50));
    p.setPen(Qt::blue);
    p.setBrush(Qt::NoBrush);
    const QString str1(tr("This is painted"));
    const QString str2(tr("This is also painted"));
    QFontMetrics fm(p.font());
    p.drawText(width()/2 - fm.width(str1)/2, 20, str1);
    p.drawText(width()/2 - fm.width(str2)/2, 20 + fm.lineSpacing(), str2);
    draw();
    }

    void OGLPort::draw()
    {
    glClearColor (0.0, 0.0, 0.0, 0.0);
    //glClear (GL_COLOR_BUFFER_BIT);
    glColor3f (0.0, 0.0, 0.0);
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
    glViewport(0, 0, width(), height());
    glBegin(GL_POLYGON);
    glVertex3f (0.25, 0.25, 0.0);
    glVertex3f (0.75, 0.25, 0.0);
    glVertex3f (0.75, 0.75, 0.0);
    glVertex3f (0.25, 0.75, 0.0);
    glEnd();
    glFlush();
    }
    @

    @#include <QtOpenGL/qgl.h>
    #include <QtCore/qvector.h>
    #include <QtGui/qmatrix4x4.h>
    #include <QtGui/qvector3d.h>
    #include <QtGui/qvector2d.h>
    #include <QApplication>

    #include "mainwindow.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    OGLPort w;
    w.show();

    return a.exec(&#41;;
    

    }
    @

    Please help.



  • Please read the docs for QGLWidget. It tells you to override the initializeGL(), resizeGL() and paintGL() functions. You should not override paintEvent() to do OpenGL drawing. Also worth checking out the OpenGL examples that ship with Qt.



  • Thankyou. I got it right.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.