Results 1 to 20 of 31

Thread: 3 Dimensions using opengl

Hybrid View

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

    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

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

    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

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

    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

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

    Default Re: 3 Dimensions using opengl

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

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

    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.

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

    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.

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

    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

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

    Default Re: 3 Dimensions using opengl

    Quote Originally Posted by vermarajeev View Post
    Actually I dont want to redraw the things again.
    It won't happen all by itself. It's much easier to simply draw that molecule again in 3D than convert a 2D image to 3D one. First you need some kind of a representation, for example a set of segments. Then you can draw it in whatever form you like.

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

    Default Re: 3 Dimensions using opengl

    Well, you have to draw it again.

    QPainter when is using OpenGl is doing the similar thing like in my code example. It's just sends triangles to opengl using orthographic projection. But you cannot access QPainter OpenGL projection and modelview matrix (of course, you can change the source code if you want it), so you must redraw in 3D all that stuff. That's why I asked you what kind of 2D objects you want to represent in 3D.

    Qt Code:
    1. void paintGL()
    2. {
    3.  
    4. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    5.  
    6. static const QPoint points[6] = {
    7. QPointF(-3, -2),
    8. QPointF(-7, 3),
    9. QPointF(-3, 8),
    10. QPointF(3, 8),
    11. QPointF(7, 3),
    12. QPointF(3, -0) };
    13.  
    14. glMatrixMode(GL_MODELVIEW);
    15. glLoadIdentity();
    16.  
    17. glTranslatef(0.0, 0.0, -5.0);
    18.  
    19. glBegin(GL_LINE_LIST);
    20.  
    21. for(int i = 0 ; i < 6; i++){
    22. glVertex2f(points[i].x, points[i].y);
    23. }
    24.  
    25. glEnd();
    26. }
    To copy to clipboard, switch view to plain text mode 

    Maybe there are some errors...

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

    Default Re: 3 Dimensions using opengl

    Quote Originally Posted by jacek View Post
    It won't happen all by itself. It's much easier to simply draw that molecule again in 3D than convert a 2D image to 3D one. First you need some kind of a representation, for example a set of segments. Then you can draw it in whatever form you like.
    Ok Here is the class representation of 2D objects drawn on QGraphicsView.

    Qt Code:
    1. class DrawMolecule : public ChemObject
    2. {}
    3. class DrawAtom : public ChemObject
    4. {}//
    5. class DrawBond : public ChemObject
    6. {}//
    To copy to clipboard, switch view to plain text mode 
    Here ChemObject is a pure Virtual class inherited from QObject which provides an interface to draw an atom, bonds. The combination of atoms and bonds forms a molecule.
    Jacek I think you were talking about this. Please suggest.

    minimoog--> Thanks for your sample program but there are some errors as you mentioned. M trying to rectify. Thanks again

    Jacek, you are a multitasking human being (really good at chemistry too). Fabulous!!!
    Last edited by vermarajeev; 19th April 2007 at 04:35.

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

    Default Re: 3 Dimensions using opengl

    Quote Originally Posted by vermarajeev View Post
    Jacek I think you were talking about this.
    Yes, now you have to add routines that will draw each of those objects. Take a look at gluSphere() and gluCylinder() functions.

    You can add QGraphicsView and QGLWidget to QStackedWidget, so you can switch between the 2D and 3D view easily.

  12. The following user says thank you to jacek for this useful post:

    vermarajeev (20th April 2007)

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

    Default Re: 3 Dimensions using opengl

    Quote Originally Posted by jacek View Post
    Yes, now you have to add routines that will draw each of those objects. Take a look at gluSphere() and gluCylinder() functions.

    You can add QGraphicsView and QGLWidget to QStackedWidget, so you can switch between the 2D and 3D view easily.
    Hi jackek,
    Thanks for your suggestions. I'll take a look at those examples and see how I can solve the problem. Thanks a lot. Will keep you updating.

    Thanks

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

    Default Re: 3 Dimensions using opengl

    Hi Jacek,
    I did a sample program to convert 2D to 3D. I have not used any representation but just a sample. I'll make use of DrawAtom, DrawBond and DrawMolecule classes once I'm perfect with what I have to do. Anyway below is the code. Please tell me if I'm going anywhere wrong. Also when I draw two or more polygons, they are not displayed in exact positions as they appear in 2D.

    I have a zipped file.

    Steps to follow once the progrma is executed:
    1) Click on 'Draw Poly'.
    2) Release the mouse on GraphicsView. It will draw a polygon. If you want two or more
    polygon then again click 'Draw Poly' and then release mouse and so on.
    3) Once there are polygons on GraphicsView click 'opengl' to get the 3D View.

    Please help me to improve my code. Also the positions of polygons in 3D are not same as 2D
    Thanks
    Attached Files Attached Files

  15. #14
    Join Date
    Nov 2006
    Location
    Shrewsbury, UK
    Posts
    97
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 11 Times in 11 Posts

    Default Re: 3 Dimensions using opengl

    I've put the QGraphicsView/Scene into a consistent coordinate system with OpenGL i.e.

    Y
    ^
    |
    +---> X

    I've switched you to a orthographic projection - always better for scientific stuff. The OpenGL viewport coordinates are mapped to the scenegraph, and I've changed the graphics object to use a 2:1 ratio triangle - much easier to see the mappings with this.

    Seems to work.

    Pete
    Attached Files Attached Files
    Last edited by pdolbey; 20th April 2007 at 19:55.

  16. The following user says thank you to pdolbey for this useful post:

    vermarajeev (21st April 2007)

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
  •  
Qt is a trademark of The Qt Company.