Page 1 of 2 12 LastLast
Results 1 to 20 of 31

Thread: 3 Dimensions using opengl

  1. #1
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 3 Dimensions using opengl

    Hi all,

    I have implemented basic shapes using QFrame ,using normal painting. I want to
    achieve the same using opengl & I will have an option in the menu for opengl. If the
    user selects that option I want to redraw all the items on QFrame using opengl. How to achieve it using qt4??? or first off is that possible.

    Thanks

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 3 Dimensions using opengl

    How about using QAbstractScrollArea and using the function QAbstractScrollArea::setViewport ?? to set the viewport to QGLWidget....
    see the chips demo...

    will it solve ur purpose ??

  3. #3
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 3 Dimensions using opengl

    Quote Originally Posted by aamer4yu View Post
    How about using QAbstractScrollArea and using the function QAbstractScrollArea::setViewport ?? to set the viewport to QGLWidget....
    see the chips demo...

    will it solve ur purpose ??
    I wrote a program which reads some data(line circle, polygon etc) through buttons.
    For this I subclassed QAbstractScrollArea, reimplemented paintEvent() on viewport() to draw the items. But when I click circle button the display on viewport is very slow. There is so much of lag between drawings.
    Can anyone please tell me why?????

    Thanks

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 3 Dimensions using opengl

    cant say much... i was thinking of QGraphicsView.... as it is derived from QAbstractScrollArea

    but forgot u are trying for 3 Dimensions
    Last edited by aamer4yu; 12th April 2007 at 11:31.

  5. #5
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 3 Dimensions using opengl

    Hi guys,
    Please help!!!

    I'm really having problem with this and I cannot move ahead. Please throw some light.

    Thanks

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 3 Dimensions using opengl


  7. #7
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 3 Dimensions using opengl

    Quote Originally Posted by jacek View Post
    I know that site and I'm using it to learn opengl. It is a fantastic tutorial for newbie. Here is the toturial link http://digitalfanatics.org/projects/...chapter14.html

    But I want to know, when I use a QAbstractScrollArea why there is so much lag between displaying of items. I think the paintEvent is not called and requires some other event to occur to call it.
    I tried using QFrame but unable to proceed further. I now thought to use QGraphicsView to see how it goes.

    If you can tell me why there is a lag between displaying of items when I use QFrame, I'll be thankful to you.

    Thanks

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 3 Dimensions using opengl

    Quote Originally Posted by vermarajeev View Post
    I tried using QFrame but unable to proceed further. I now thought to use QGraphicsView to see how it goes.
    I'm not sure if I undertand your problem properly. Do you want to display those objects on QFrame and QGLWidget at the same time, or do you want to replace QFrame with QGLWidget?

  9. #9
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 3 Dimensions using opengl

    Quote Originally Posted by jacek View Post
    I'm not sure if I undertand your problem properly. Do you want to display those objects on QFrame and QGLWidget at the same time, or do you want to replace QFrame with QGLWidget?
    I draw some objects on QFrame, now on click of a button say '3D Viewer', I want to display those objects on QGLWidget with 3D representation. This means that I already have 2D represntation of objects now I want to get 3D representation.

    I just tried using QGraphicsView instead of QFrame by seeing the chips demo. When you see the chips demo, the objects are in 2D representation. Then you click some button 'openGl' the result is again 2D representation. Instead of 2D I want 3D such that I can see the object in all directions by rotating it.

    Please see the attached file for more details.

    Thanks and eagerly waiting for a reply
    Attached Files Attached Files

  10. #10
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 3 Dimensions using opengl

    do u mean rotating 2D objects in 3D space ??
    if yes, u need to check this


    also saw the image u gave... well as far as i got it, u work in 2D and want to show them in 3D.... well if u have fixes number of items, and know what items will be drawn, u can create a different view and add 3D objects to it. i guess a 2D item doesnt have any info how to draw itself in 3D,,,, while a 3D object can be scaled down to 2D projection.
    hope i am in right direction in understanding ur problem....

    and yeah, is ur 3D view editable ??? i mean do u want items to be manipulated in 3D view, or just show a 3D view of the items ??
    Last edited by aamer4yu; 16th April 2007 at 04:53.

  11. #11
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 3 Dimensions using opengl

    Hi aamer4yu,
    Thanks for your reply

    also saw the image u gave... well as far as i got it, u work in 2D and want to show them in 3D....
    Yes you are in right path. I already have some 2D objects and want to show them in 3D.

    well if u have fixes number of items, and know what items will be drawn, u can create a different view and add 3D objects to it
    Is different view a QGlwidget???. If yes I tried it. But get only 2D view.
    Now here is the main issue. How do I create a different view and add 3D objects to it. My sense says something like this

    //GraphicsView
    Qt Code:
    1. void graphicsView::paintEvent( QPaintEvent * event ){
    2. QPainter painter(this);
    3. paint( &painter );
    4. }
    5.  
    6. void graphicsView::paint( QPainter* painter ){
    7. //Draw the object you want
    8. }
    9.  
    10. void graphicsView::on3DViewerButtonClick(){
    11. m_glWid = new myGLWidget( this ); //here this is QGraphicsView
    12. m_glWid->show();
    13. }
    To copy to clipboard, switch view to plain text mode 

    Now I got graphicsView object in QGLWidget. How do I proceed further. I know I need to override
    Qt Code:
    1. paintGL()
    2. resizeGL()
    3. initializeGL()
    To copy to clipboard, switch view to plain text mode 
    I can even use paintEvent in GLWidget and the call paint member of GraphicsView like this

    Qt Code:
    1. void GLWidget::paintEvent( QPaintEvent * event ){
    2. QPainter painter;
    3. painter.begin(this);
    4. m_graphicsView->paint(&painter);
    5. painter.end();
    6. }
    To copy to clipboard, switch view to plain text mode 
    But now I get 2D on QGLWidget.
    Did you mean something like this. Or Is there something else.


    and yeah, is ur 3D view editable ??? i mean do u want items to be manipulated in 3D view, or just show a 3D view of the items ??
    [/QUOTE]
    Yes in some scenario. First I would be interested in representing a 2D object to 3D. If I succeed then move ahead for editing.

    Hope I made some sense now. PLease let me know if still there is any confusion.

    I'll go through the link you have given and see if I can get it.
    Thanks

    //I want through the link and could not find QTransform in qt4.2.2.
    Last edited by vermarajeev; 16th April 2007 at 05:42.

  12. #12
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 3 Dimensions using opengl

    i cudnt check the link example.... i dont have admin rights to install it in office.. will chk later when i get home..
    also QTransform is a feature of 4.3 I guess ??

    and i think u will have to implement a method for drawing ur 2D objects in 3D...but as ur future requirements, i wud say draw and edit in 3D ... u can get a 2D projection frm 3D...
    for eg, u draw the objects in 3D.... zoom out a little, and the drawing will appear 2D from far...

    rest i will say after seeing that example

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 3 Dimensions using opengl

    Quote Originally Posted by vermarajeev View Post
    I draw some objects on QFrame, now on click of a button say '3D Viewer', I want to display those objects on QGLWidget with 3D representation.
    Can't you simply draw them on QGLWidget once in 2D and once in 3D?

  14. #14
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 3 Dimensions using opengl

    Quote Originally Posted by jacek View Post
    Can't you simply draw them on QGLWidget once in 2D and once in 3D?
    No! Basically the user has an option to draw in 2D and view the result in 3D. If I need to use QGLWidget for both 2D and 3D then I'll have so much of rework to do. Is it not possible to use what is already existing?
    Something like Just send the graphicsView object to QGLwidget and let it draw using opengl APIs.

    Waiting eagerly.
    Thanks

  15. #15
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 3 Dimensions using opengl

    Quote Originally Posted by vermarajeev View Post
    Something like Just send the graphicsView object to QGLwidget and let it draw using opengl APIs.
    This is not a bad idea if you know how to handle the OpenGL-based 3D drawing but consider using the QGraphicsScene object instead.
    Current Qt projects : QCodeEdit, RotiDeCode

  16. #16
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 3 Dimensions using opengl

    Quote Originally Posted by fullmetalcoder View Post
    This is not a bad idea if you know how to handle the OpenGL-based 3D drawing but consider using the QGraphicsScene object instead.
    Jacek--> Can you please tell me how do I use QTranform as specified in the link above in qt4.2.2. I think QTranform should make work easier.

    fullmetalcoder--> I have written a sample program can you please help me to represent a 2D object to 3D.

    //GraphicsView
    Qt Code:
    1. graphicsView::graphicsView(QGraphicsScene* s, QWidget *parent)
    2. : QGraphicsView(s, parent), m_scene(s){
    3. s->setItemIndexMethod(QGraphicsScene::NoIndex);
    4. s->setSceneRect(-200, -200, 400, 400);
    5.  
    6. QPushButton* btnOPenGl = new QPushButton("OpenGL", this);
    7. btnOPenGl->setGeometry( 300, 300, 50, 30 );
    8. connect( btnOPenGl, SIGNAL( clicked() ), this, SLOT( onbtnOPenGlClick() ) );
    9. }
    10.  
    11. graphicsView::~graphicsView()
    12. {}
    13.  
    14. void graphicsView::onbtnOPenGlClick(){
    15. GLWidget* GLwid = new GLWidget(this);
    16. GLwid->show();
    17. }
    18.  
    19. void graphicsView::drawBackground(QPainter *painter, const QRectF &rect){
    20. Q_UNUSED(rect);
    21. paint(painter);
    22. }
    23. void graphicsView::paint(QPainter* painter){
    24. static const QPoint points[6] = {
    25. QPoint(-30, -20),
    26. QPoint(-70, 30),
    27. QPoint(-30, 80),
    28. QPoint(30, 80),
    29. QPoint(70, 30),
    30. QPoint(30, -20)
    31. };
    32. painter->drawPolygon(points, 6);
    33. }
    To copy to clipboard, switch view to plain text mode 

    //QGLWidget
    Qt Code:
    1. #include <QGLWidget>
    2. #include "graphicsview.h"
    3.  
    4. class graphicsView;
    5. class GLWidget : public QGLWidget
    6. {
    7. public:
    8. GLWidget(graphicsView* gv, QWidget *parent=0 )
    9. : QGLWidget( parent ), m_graphicsView(gv)
    10. {}
    11.  
    12. protected:
    13. void paintEvent( QPaintEvent* event ){
    14. QPainter paint;
    15. paint.begin(this);
    16. m_graphicsView->paint(&paint);
    17. paint.end();
    18. }
    19. /*void initializeGL()
    20. {
    21. glShadeModel(GL_SMOOTH);
    22.  
    23. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    24. glClearDepth(1.0f);
    25.  
    26. glEnable(GL_DEPTH_TEST);
    27. glDepthFunc(GL_LEQUAL);
    28.  
    29. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    30. }
    31. void resizeGL( int width, int height )
    32. {
    33.   height = height?height:1;
    34.  
    35. glViewport( 0, 0, (GLint)width, (GLint)height );
    36.  
    37. glMatrixMode(GL_PROJECTION);
    38. glLoadIdentity();
    39. gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
    40.  
    41. glMatrixMode(GL_MODELVIEW);
    42. glLoadIdentity();
    43. }
    44. void paintGL()
    45. {
    46. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    47. glLoadIdentity();
    48.  
    49. glTranslatef(-1.5f,0.0f,-6.0f);
    50.  
    51. glBegin(GL_TRIANGLES);
    52. glVertex3f( 0.0f, 1.0f, 0.0f);
    53. glVertex3f(-1.0f,-1.0f, 0.0f);
    54. glVertex3f( 1.0f,-1.0f, 0.0f);
    55. glEnd();
    56. }*/
    57. private:
    58. graphicsView* m_graphicsView;
    59. };
    To copy to clipboard, switch view to plain text mode 

    Thanks

  17. #17
    Join Date
    Feb 2007
    Posts
    49
    Thanks
    4
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 3 Dimensions using opengl

    What kind of 2D objects do you want to represent with 3D version?

  18. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 3 Dimensions using opengl

    Quote Originally Posted by vermarajeev View Post
    Can you please tell me how do I use QTranform as specified in the link above in qt4.2.2. I think QTranform should make work easier.
    If I understand your problem correctly, QTransform won't help you --- after you apply QTransform your object still will be flat.

  19. #19
    Join Date
    Feb 2007
    Posts
    49
    Thanks
    4
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 3 Dimensions using opengl

    Lines in 2D can be represented in 3D with simple lines or cylinder.

    For simple lines you can use glBegin(GL_LINES), glBegin(GL_LINESTRIP) or glBegin(GL_LINE_LOOP).

    Qt Code:
    1. glBegin(GL_LINE_STRIP);
    2. glVertex2f(from_x, from_y);
    3. glVertex2f(to1_x, to1_y);
    4. glVertex2f(to2_x, to2_y);
    5. ....
    6. glEnd();
    To copy to clipboard, switch view to plain text mode 

    This will draw simple connected line, which can be easily rotated.

  20. #20
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 3 Dimensions using opengl

    Quote Originally Posted by minimoog View Post
    Lines in 2D can be represented in 3D with simple lines or cylinder.

    For simple lines you can use glBegin(GL_LINES), glBegin(GL_LINESTRIP) or glBegin(GL_LINE_LOOP).

    Qt Code:
    1. glBegin(GL_LINE_STRIP);
    2. glVertex2f(from_x, from_y);
    3. glVertex2f(to1_x, to1_y);
    4. glVertex2f(to2_x, to2_y);
    5. ....
    6. glEnd();
    To copy to clipboard, switch view to plain text mode 

    This will draw simple connected line, which can be easily rotated.
    So you mean to say, I need to redraw the objects again in 3D.
    Actually I dont want to redraw the things again.
    Anyway since I'm not able to get the what I want I should atleast see how your logic works. Can you please help me to achieve what you have suggested in the above sample program that I have posted.

    Thanks

Similar Threads

  1. opengl help!!
    By rachana in forum Qt Programming
    Replies: 1
    Last Post: 19th February 2007, 08:52
  2. Qt and OpenGL
    By ToddAtWSU in forum Qt Programming
    Replies: 8
    Last Post: 15th June 2006, 21:37
  3. Does OpenGL be supported in opensource of Qt-4.1.2?
    By showhand in forum Qt Programming
    Replies: 1
    Last Post: 15th May 2006, 10:46
  4. Qt's optimized OpenGL context switching
    By sverhoff in forum Qt Programming
    Replies: 0
    Last Post: 28th March 2006, 16:40
  5. Example OpenGL project?
    By brcain in forum Qt Tools
    Replies: 9
    Last Post: 23rd February 2006, 21:17

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.