[solved] I get some QGLWidget + OpenGLES 2.0 problems



  • I try to use QGLWidget and OpenGLES 2.0 to write a game, my problem is the game always render the (0,0) coordinate in center of screen instead of top left corner. The code like this
    @QGraphicsScene scene;
    scene.setSceneRect(QRect(0,0,SCREEN_WIDTH,SCREEN_HEIGHT));
    GraphicsView viewer(&scene;);
    _qtgame._glwidget->resize(SCREEN_WIDTH,SCREEN_HEIGHT);
    _qtgame._glwidget->rect().topLeft();
    viewer.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    viewer.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    viewer.setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    viewer.setFrameStyle(0);
    viewer.setViewport(_qtgame._glwidget);
    viewer.showFullScreen();@

    and
    @
    void GLWidget::resizeGL(int width, int height){
    glViewport(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT);
    }@

    Did I forget some things?



  • Just do this:

    @
    viewer.setViewport(new QGLWidget)
    @

    there is no need to sub-class QGLWidget and reimplement resizeGL().



  • Thanks ZapB for help. But I need overwritten QGLWidget to make GLSL programe, I try no reimplement resizeGL() and rewrite my code as follow, but the (0,0) still stay at center of screen. Maybe I do something wrong here, but I don't know.

    @int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    QApplication::setGraphicsSystem("opengl");
    QtGame _qtgame;
    _qtgame._viewer->showFullScreen();//now i handle the viewer inside _qtgame.

    QTimer *timer = new QTimer();
    timer->setInterval(500);
    QObject::connect(timer, SIGNAL(timeout()), &_qtgame, SLOT(ProcessSignal()));
    timer->start();
    return app.exec();
    

    }
    @

    QtGame show like this
    @
    //_viewer and _glwidget are public.
    QtGame::QtGame(){
    QGraphicsScene scene;
    scene.setSceneRect(QRect(0,0,k_SCREEN_WIDTH,k_SCREEN_HEIGHT));
    _viewer = new GraphicsView(&scene;);
    _viewer->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    _viewer->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    _viewer->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    _viewer->setFrameStyle(0);
    _viewer->setViewport(new GLWidget());
    _glwidget = (GLWidget*)_viewer->viewport();
    }
    void QtGame::ProcessSignal(){
    _glwidget->updateGL();
    _glwidget->paintGL2();
    }
    @

    and the GLWidget: public QGLWidget

    @
    void GLWidget::initializeGL ()
    {
    //makeCurrent();
    ConfigOpenGL();// I create my GLSL programe here.
    glClearColor(1.0f, 0.972f, 0.862f, 1.0f);
    glViewport(0, 0, k_DEVICE_WIDTH, k_DEVICE_HEIGHT);
    }
    void GLWidget::paintGL2(){
    swapBuffers();
    makeCurrent();
    glViewport(0, 0, k_DEVICE_WIDTH, k_DEVICE_HEIGHT);
    glClearColor(0.989f, 0.989f, 0.872f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    Draw();//and draw the regtangle (0.0f, 0.0f , 0.9f, 0.9f) here
    }
    @
    And result
    !http://files.myopera.com/tratstil/files/20110728-181150.png(Screen shot)!



  • After read the opengl_es2 exsample, i know that by default the (0,0) coordinate (in my case) is center of screen.

    and i do this to move the (0,0) coordinate to top-left corner. I multiply vertex with a (4x4) matrix.

    @
    _projectionMatrix.Identity();
    _projectionMatrix.Ortho(-(float)k_SCREEN_WIDTH,+0.0f,+0.0f,-(float)k_SCREEN_HEIGHT,-1.0f,+1.0f);
    @
    the code to Identity and Ortho can be found "here ":http://db-in.com/blog/2011/04/cameras-on-opengl-es-2-x/

    Thanks everybody!
    p/s : My english is very limited to representing my idear :D


Log in to reply
 

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