updateGL in QT+VisualC++

updateGL in QT+VisualC++

Post by Katarzyna Uchnas » Sat, 07 Dec 2002 01:44:17



I've got some problems with updating OpenGL in QT. I want to rotate
the triangle by pressing the keys, but the moment I press them,
the triangle disappears and the only thing I see is black background.
Can anybody tell me what am I doing wrong?
Here's the code:

class MyOpenGLWidget : public QGLWidget
{
  public:
  void initializeGL();
  void resizeGL(int w, int h);
  void paintGL();

  int rotx, roty, rotz;
  MyOpenGLWidget(QGLWidget *parent=0, const char *name = 0);
  void keyPressEvent ( QKeyEvent * e );

Quote:};

void MyOpenGLWidget::keyPressEvent ( QKeyEvent * e )
{
  if(e->ascii()=='p')
  {
    rotx+=4;
    updateGL();
  }
  if(e->ascii()=='o')
  {
    roty+=4;
    updateGL();
  }
  if(e->ascii()=='i')
  {
    rotz+=4;
    updateGL();
  }

Quote:}

MyOpenGLWidget::MyOpenGLWidget(QGLWidget *parent, const char
*name):QGLWidget(parent,name),rotx(0),roty(0),rotz(0)
{}

void MyOpenGLWidget::initializeGL()
{
  qglClearColor(black);
  glShadeModel(GL_FLAT);

Quote:}

void MyOpenGLWidget::resizeGL( int w, int h )
{
  glViewport(0, 0, w, h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);

Quote:}

void MyOpenGLWidget::paintGL()
{
  glClear(GL_COLOR_BUFFER_BIT);
  glTranslatef(0.0, 0.0, -10.0);
  glShadeModel(GL_SMOOTH);

  glRotatef(rotx,1.0f,0.0f,0.0f);
  glRotatef(roty,0.0f,1.0f,0.0f);
  glRotatef(rotz,0.0f,0.0f,1.0f);

    glBegin(GL_TRIANGLES);
    glColor3f(1.0f, 0.0f, 0.0f);
     glVertex3f(0.5f, 0.0f, 0.0f);
     glColor3f(0.0f, 1.0f, 0.0f);
     glVertex3f(1.0f, -1.0f, 0.0f);
     glColor3f(0.0f, 0.0f, 1.0f);
     glVertex3f(0.0f, -1.0f, 0.0f);
  glEnd();

Quote:}

thanks in advance,
Katarzyna Uchnast
 
 
 

updateGL in QT+VisualC++

Post by Radek Karwowsk » Sat, 07 Dec 2002 03:45:27


Quote:> class MyOpenGLWidget : public QGLWidget
> {
>   public:
>   void initializeGL();
>   void resizeGL(int w, int h);
>   void paintGL();

>   int rotx, roty, rotz;
>   MyOpenGLWidget(QGLWidget *parent=0, const char *name = 0);
>   void keyPressEvent ( QKeyEvent * e );
> };

> void MyOpenGLWidget::keyPressEvent ( QKeyEvent * e )
> {
>   if(e->ascii()=='p')
>   {
>     rotx+=4;
>     updateGL();
>   }
>   if(e->ascii()=='o')
>   {
>     roty+=4;
>     updateGL();
>   }
>   if(e->ascii()=='i')
>   {
>     rotz+=4;
>     updateGL();
>   }
> }

> MyOpenGLWidget::MyOpenGLWidget(QGLWidget *parent, const char
> *name):QGLWidget(parent,name),rotx(0),roty(0),rotz(0)
> {}

> void MyOpenGLWidget::initializeGL()
> {
>   qglClearColor(black);
>   glShadeModel(GL_FLAT);
> }

> void MyOpenGLWidget::resizeGL( int w, int h )
> {
>   glViewport(0, 0, w, h);
>   glMatrixMode(GL_PROJECTION);
>   glLoadIdentity();
>   glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);
> }

> void MyOpenGLWidget::paintGL()
> {
>   glClear(GL_COLOR_BUFFER_BIT);
>   glTranslatef(0.0, 0.0, -10.0);
>   glShadeModel(GL_SMOOTH);

>   glRotatef(rotx,1.0f,0.0f,0.0f);
>   glRotatef(roty,0.0f,1.0f,0.0f);
>   glRotatef(rotz,0.0f,0.0f,1.0f);

>     glBegin(GL_TRIANGLES);
>     glColor3f(1.0f, 0.0f, 0.0f);
>      glVertex3f(0.5f, 0.0f, 0.0f);
>      glColor3f(0.0f, 1.0f, 0.0f);
>      glVertex3f(1.0f, -1.0f, 0.0f);
>      glColor3f(0.0f, 0.0f, 1.0f);
>      glVertex3f(0.0f, -1.0f, 0.0f);
>   glEnd();
> }

> thanks in advance,
> Katarzyna Uchnast


 
 
 

updateGL in QT+VisualC++

Post by Radek Karwowsk » Sat, 07 Dec 2002 04:00:52


It is usually a good idea to separate your projection setup stuff from
drawing. Do the former in resizeGL and the latter in paintGL. Your
particular problem is that you call glTranslate and glRotate in paintGL. The
effects are additive, which means that the second time you are drawing your
scene the triangle is translated by -20 in the z direction. Your triangle
should disappear the second time you draw your window. Just try to minimize
and restore your window and you'll see what I mean. More comments embedded
in your code.

HTH

Radek

Quote:> void MyOpenGLWidget::keyPressEvent ( QKeyEvent * e )
> {
>   if(e->ascii()=='p')
>   {
>     rotx+=4;

   makeCurrent();
   resizeGL(width(), height());

Quote:>     updateGL();
>   }
>   if(e->ascii()=='o')
>   {
>     roty+=4;

   makeCurrent();
   resizeGL(width(), height());

Quote:>     updateGL();
>   }
>   if(e->ascii()=='i')
>   {
>     rotz+=4;

   makeCurrent();
   resizeGL(width(), height());
Quote:>     updateGL();
>   }
> }

> void MyOpenGLWidget::initializeGL()
> {
>   qglClearColor(black);
>   glShadeModel(GL_FLAT);
> }

> void MyOpenGLWidget::resizeGL( int w, int h )
> {
>   glViewport(0, 0, w, h);
>   glMatrixMode(GL_PROJECTION);
>   glLoadIdentity();
>   glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 15.0);

add ModelView matrix manipulation here, in particular the calls to
glTranslate and glRotate

Quote:> }

> void MyOpenGLWidget::paintGL()
> {
>   glClear(GL_COLOR_BUFFER_BIT);
>   glTranslatef(0.0, 0.0, -10.0);

Move to resizeGL

Quote:>   glShadeModel(GL_SMOOTH);

So, which shade model do you want? In initializeGL you specified GL_FLAT.
Also note, that it is reduntant to specify it every time when drawing.

Quote:

>   glRotatef(rotx,1.0f,0.0f,0.0f);
>   glRotatef(roty,0.0f,1.0f,0.0f);
>   glRotatef(rotz,0.0f,0.0f,1.0f);

Move to resizeGl

Quote:

>     glBegin(GL_TRIANGLES);
>     glColor3f(1.0f, 0.0f, 0.0f);
>      glVertex3f(0.5f, 0.0f, 0.0f);
>      glColor3f(0.0f, 1.0f, 0.0f);
>      glVertex3f(1.0f, -1.0f, 0.0f);
>      glColor3f(0.0f, 0.0f, 1.0f);
>      glVertex3f(0.0f, -1.0f, 0.0f);
>   glEnd();

glFlush();

- Show quoted text -

Quote:> }

> thanks in advance,
> Katarzyna Uchnast

 
 
 

1. Help: Translating QT Mac to QT Windows

Please,

I need help translating a Quicktime movie I
made on a Macintosh so that it will be
viewable on QT for Windows.  Would someone
know how to do this?  I would prefer not to
use MPEG, as there is sound that I want to
preserve.


Thanks in advance,

--
Take an object                 Take a canvas
Do something to it             Put a mark on it        
Do something else to it        Put another mark on it
"      "       "   "  "        "      "      "   "  "
--Jasper Johns

2. Cheap CD-R's!! READ THIS!

3. openGL and VisualC++

4. fiber factory authorization #

5. GLUT installation with VisualC++6.0 and Windows98

6. glDrawPixel data arrangement...

7. Fullscreen using VisualC++ and CView

8. Fractals

9. openGL, VisualC++6, glut Problems- HELP!

10. Problems with VisualC++ 5.0. and OpenGL

11. compiling gl code with VisualC++ 4.0

12. VisualC++ viewing problems...

13. OpenGL+ MFC +VisualC++5 problem