Results 1 to 7 of 7

Thread: opengl

  1. #1
    Join Date
    Jun 2008
    Posts
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default opengl

    I wrote a simple code to create an opengl window with a square in it...
    here are the relevent files....


    #ifndef HEADER_H
    #define HEADER_H

    #include<QtOpenGl>

    class QGLWidget;


    class ric:public QGLWidget
    {

    public:


    GLvoid glInitialize();
    GLvoid glResize(int width,int height);
    GLvoid paintGL();

    };

    #endif


    #include<QGLWidget>

    #include"header.h"


    GLvoid ric::glInitialize()
    {
    glClearColor(0.0f,0.0f,0.0f,0.0f);
    glShadeModel(GL_SMOOTH);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    }

    GLvoid ric::glResize(int width,int height)
    {
    if (height==0)
    height = 1;

    glViewport(0,0,width,height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    }

    GLvoid ric::paintGL()
    {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glTranslatef(-1.5f,0.0f,-6.0f);

    glColor3f(0.0f,1.0f,0.25f);
    glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
    glBegin(GL_POLYGON);
    glVertex3f(0.25f,0.25f,0.0f);
    glVertex3f(0.25f,0.75f,0.0f);
    glVertex3f(0.75f,0.75f,0.0f);
    glVertex3f(0.75f,0.25f,0.0f);
    glEnd();
    glFlush();
    }

    #include<QApplication>

    #include"header.h"

    int main(int argc,char *argv[])
    {
    QApplication app(argc,argv);

    ric window;
    window.glInitialize();
    window.glResize(640,480);
    window.paintGL();
    window.show();

    return app.exec();
    }
    now after compiling and running the project, all I get is a plane blank window... I cannot understand the problem... please help..!! thanks..

  2. #2
    Join Date
    Jan 2006
    Location
    Third rock from the sun
    Posts
    106
    Thanks
    17
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: opengl

    Try changing the method definitions to:

    Qt Code:
    1. #ifndef HEADER_H
    2. #define HEADER_H
    3.  
    4. #include<QtOpenGl>
    5.  
    6. class QGLWidget;
    7.  
    8.  
    9. class ric:public QGLWidget
    10. {
    11.  
    12. protected: // Notice these are protected, they are called automatically by QGlWidget
    13. void initializeGL();
    14. void resizeGL();
    15. void paintGL( );
    16. };
    17.  
    18. #endif
    To copy to clipboard, switch view to plain text mode 
    OpenGL is looking for these functions to be overloaded specifically. You dont need to call initializeGL, resizeGL or paintGL directly as they will be handled by the QGlWidget calls. If you need to repaint the scene, call updateGL( ). So your main would look like:
    Qt Code:
    1. #include<QApplication>
    2.  
    3. #include"header.h"
    4.  
    5. int main(int argc,char *argv[])
    6. {
    7. QApplication app(argc,argv);
    8.  
    9. ric window;
    10. window.show(); // Show calls updateGL( ) for you, no need to for this example
    11.  
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    See the API docs http://doc.trolltech.com/4.4/qglwidget.html for a good setup example, assuming you are using Qt4.4
    Every little child knows
    if you cant see dreams
    your eyes are blind

    Moxy Fruvous

  3. #3
    Join Date
    Jun 2008
    Posts
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: opengl

    but i am using the resize function with 2 arguments... so if i dont provide the values explicitly then how would the compiler react..?? i haven't initialized the height & width as well...

    and i made the changes as suggested...i am still getting the blank window... please help..!!

  4. #4
    Join Date
    Jan 2006
    Location
    Third rock from the sun
    Posts
    106
    Thanks
    17
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: opengl

    Whoops, forgot to put the width and height into the resize.

    Qt Code:
    1. void resizeGL(int w, int h)
    To copy to clipboard, switch view to plain text mode 

    Also, try setting the matrix mode before and after setting the ortho in paintGL:
    Qt Code:
    1. glMatrixMode( GL_PROJECTION );
    2. glLoadIdentity( );
    3. glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
    4. glMatrixMode( GL_MODELVIEW );
    5. glLoadIdentity ( ) ;
    To copy to clipboard, switch view to plain text mode 

    Every little child knows
    if you cant see dreams
    your eyes are blind

    Moxy Fruvous

  5. #5
    Join Date
    Jun 2008
    Posts
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: opengl

    there you go...!!! thanks a ton mate... but can you jus explain to me what you just did..??

  6. #6
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: opengl

    You need to read the documentation for QGLWidget(and probably for QWidget as well)
    The three funcions initializeGL(), resizeGL() and paintGL( ) are event handlers that get called by the Qt framework when appropriate.
    • initializeGL, once before the widget is shown the first time
    • resizeGL, every time the user changes the size of the widget
    • paintGL, every time the widget needs to redraw itself

    You never need to call these functions explicitely.
    If you want to change the size of the window, you need to call QWidget::resize()

  7. #7
    Join Date
    Jun 2008
    Posts
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: opengl

    ahh... got it... thanks mate...

Similar Threads

  1. OpenGL and Qt Question
    By ToddAtWSU in forum Qt Programming
    Replies: 2
    Last Post: 18th April 2009, 18:04
  2. Replies: 3
    Last Post: 12th February 2008, 21:17
  3. Qtopia Core & OpenGL ES?
    By zelko in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 28th May 2007, 07:21
  4. OpenGL ES, Qt/11 - Qtopia Core?
    By zelko in forum Qt Programming
    Replies: 0
    Last Post: 3rd May 2007, 10:56

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.