qt-interest@trolltech.com
[Top] [All Lists]

Re: Cylinder draw using QT opengl functions

Subject: Re: Cylinder draw using QT opengl functions
From: Enrico Ros
Date: Fri, 13 Jun 2008 13:00:49 +0200
On Friday 13 June 2008 10:32:30 Yuriy Rusinov wrote:
> Dear colleagues !
>
> I want to draw cylinder on QGLWidget. I do
> void GLWidget :: initializeGL (void)
> {
>     qDebug () << __FUNCTION__;
>     glClearColor (1.0, 1.0, 1.0, 0.0);
> }
>
> void GLWidget :: resizeGL (int width, int height)
> {
>     qDebug () << __FUNCTION__;
>     glMatrixMode (GL_PROJECTION);
>     glLoadIdentity ();
>     glViewport ((GLsizei)width/2, (GLsizei)height/2, (GLsizei)width,
> (GLsizei)height);
>     glOrtho (0, this->QGLWidget::width (), 0, this->QGLWidget::height (),
> -1,1);
>     glMatrixMode (GL_MODELVIEW);
> }
>
> void GLWidget :: paintGL (void)
> {
>     glBegin (GL_POLYGON);
>     glColor3d (1, 0, 0);
>     int w=this->QGLWidget::width();
>     int h=this->QGLWidget::height();
>     glMatrixMode (GL_PROJECTION);
>     glLoadIdentity();
>     GLUquadricObj* q = gluNewQuadric ();
>     GLUnurbsObj* nobs = gluNewNurbsRenderer ();
>     gluBeginSurface (nobs);
>     gluCylinder (q, (GLdouble)cyl_radius, (GLdouble)cyl_radius,
> (GLdouble)cyl_height, 32, 32);
>     gluEndSurface (nobs);
>     gluDeleteNurbsRenderer (nobs);
>     gluDeleteQuadric (q);
>     glEnd ();
>     glMatrixMode (GL_MODELVIEW);
> }
>
> But except cylinder I receive sector of circle as presented in attachment.
> Where is error ?

Let's start by changing the paintGL function to this:
 void GLWidget :: paintGL (void)
 {
     glClear( GL_COLOR_BUFFER_BIT );
     glMatrixMode (GL_MODELVIEW); // should be already in GL_MODELVIEW..
     glColor3d (1, 0, 0);
     GLUquadricObj* q = gluNewQuadric ();
     GLUnurbsObj* nobs = gluNewNurbsRenderer ();
     gluBeginSurface (nobs);
     gluCylinder (q, (GLdouble)cyl_radius, (GLdouble)cyl_radius, 
(GLdouble)cyl_height, 32, 32);
     gluEndSurface (nobs);
     gluDeleteNurbsRenderer (nobs);
     gluDeleteQuadric (q);
 }

Changes:
 - removal of the invalid glBegin(...) ... glEnd() calls: they are used only
when specifying the vertex/color/texture data to be rendered and they
can't enclose other operations. see the OpenGL reference about vertex
data
 - since you setup the VIEWPORT matrix in the resizeGL, don't reset it
to zero on the paintGL function! if you do it, you don't get the perspective
transformation anymore, but a cartesian -1 ... -1 for x and y plane.
 - hint: you can change "this->QGLWidget::width();" and similar to "width()"
because you aready sit on 'this', and 'with()' is of yours.
 - I added glClear( GL_COLOR_BUFFER_BIT ); oherwise rendered data will
leave a trace on the framebuffer (this clears the framebuffer on each
frame) 

NOW, looking at the resizeGL function:
 - it's correct to setup the VIEWPORT matrix here, however you have to
decide here how 'big' is the space you view and how it is projected.. let's
take a look at this:

>     glMatrixMode (GL_PROJECTION);
>     glLoadIdentity ();
>     glViewport ((GLsizei)width/2, (GLsizei)height/2, (GLsizei)width,
> (GLsizei)height);
>     glOrtho (0, this->QGLWidget::width (), 0, this->QGLWidget::height (),
> -1,1);

the glViewport sets a 'clip rectangle' on your window, that's why you see
only a quarter of your cylinder! change it to:
   glViewport(0, 0, (GLint)width, (GLint)height);

the glOrtho call may be good, but note that 'bottom' and 'top' parameters
may be swapped. if you want to do perspective transformations, also take
a look at gluPerspective.

Good luck with your GFX programs, and enjoy OpenGL programming ;-)
Also take the time, after experimenting a bit more, to read the first
pages of the OpenGL specifications that will teach you how the graphics
pipeline is modeled in OpenGL.

Enrico Ros

 
 
 --
 Email.it, the professional e-mail, gratis per te: http://www.email.it/f
 
 Sponsor:
 Scopri le tue passioni con Leonardo.it!
* 
 Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid=7614&d=13-6

--
To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
"unsubscribe" in the subject or the body.
List archive and information: http://lists.trolltech.com/qt-interest/

<Prev in Thread] Current Thread [Next in Thread>