You are not logged in.

1

Sunday, December 18th 2011, 9:09pm

Problem with rendering OpenGL models one after the other, and deleting them

I made two pushbuttons to Draw and Delete OpenGL models (triangle & quadrilateral). When I click Draw, it renders both models at once. I want Draw pushbutton to render the models one after the other i.e. when I click, it renders triangle, when I click again, it renders a Quadrilateral.
When I click "Remove" pushbutton, it removes Quadrilateral, and when I click Remove again, it removes Triangle. Hope I'm not confusing anyone. Here are my

--MainWindow.cpp--

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "mainwindow.h" #include "ui_mainwindow.h"
    MainWindow::MainWindow(QWidget *parent) :
     QMainWindow(parent),
     ui(new Ui::MainWindow)
 {
     ui->setupUi(this);
     connect(ui->pushButton,SIGNAL(clicked()),ui->centralWidget,SLOT(glTriangle()));
     connect(ui->pushButton_2,SIGNAL(clicked()),ui->centralWidget,SLOT(delObject()));	//I have not developed the delObject()
     
 }
  MainWindow::~MainWindow()
 {
     delete ui;
 }


--glWidget.cpp
codes:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "glwidget.h" #include<GL/glut.h>
 #include <ui_mainwindow.h>
   GLWidget::GLWidget(QWidget *parent) :
     QGLWidget(parent)
 {
 }
   void GLWidget::initializeGL()
 {
   //glClearColor(0.2, 0.2, 0.2, 1);  // Background Color
     glShadeModel(GL_SMOOTH);// Enable Smooth Shading
        glClearColor(0.0f, 0.0f, 0.0f, 0.5f);// Black Background
        glClearDepth(1.0f);// Depth Buffer Setup
        glEnable(GL_DEPTH_TEST);// Enables Depth Testing
        glDepthFunc(GL_LEQUAL);// The Type Of Depth Testing To Do
        glEnable ( GL_COLOR_MATERIAL );
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
 }
  void GLWidget::resizeGL(int weight, int height)
 {
     glViewport     ( 0, 0, weight, height );
         glMatrixMode   ( GL_PROJECTION );  // Select The Projection Matrix
         glLoadIdentity ( );                // Reset The Projection Matrix
         if ( height==0 )  // Calculate The Aspect Ratio Of The Window
             gluPerspective ( 80, ( float ) weight, 1.0, 5000.0 );
         else
             gluPerspective ( 80, ( float ) weight / ( float ) height, 1.0, 5000.0 );
         glMatrixMode   ( GL_MODELVIEW );  // Select The Model View Matrix
         glLoadIdentity ( );    // Reset The Model View Matrix
  }
   void GLWidget::paintGL()
 {
    }
  void GLWidget::glTriangle(){
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// Clear Screen And Depth Buffer
         glLoadIdentity();// Reset The Current Modelview Matrix
         glTranslatef(-1.5f,0.0f,-6.0f);// Move Left 1.5 Units And Into The Screen 6.0
               glBegin(GL_TRIANGLES);// Drawing Using Triangles
                 glColor3f(1.0f,0.0f,0.0f);// Set The Color To Red
                 glVertex3f( 0.0f, 1.0f, 0.0f);// Top
                 glColor3f(0.0f,1.0f,0.0f);// Set The Color To Green
                 glVertex3f(-1.0f,-1.0f, 0.0f);// Bottom Left
                 glColor3f(0.0f,0.0f,1.0f);// Set The Color To Blue
                 glVertex3f( 1.0f,-1.0f, 0.0f);// Bottom Right
             glEnd();// Finished Drawing The Triangle
 }
  void GLWidget::glQuard(){
        glTranslatef(3.0f,0.0f,0.0f);// Move Right 3 Units
        glColor3f(0.5f,0.5f,1.0f);// Set The Color To Blue One Time Only
        glBegin(GL_QUADS);// Draw A Quad
            glVertex3f(-1.0f, 1.0f, 0.0f);// Top Left
            glVertex3f( 1.0f, 1.0f, 0.0f);// Top Right
            glVertex3f( 1.0f,-1.0f, 0.0f);// Bottom Right
            glVertex3f(-1.0f,-1.0f, 0.0f);// Bottom Left
        glEnd();// Done Drawing The Quad
  }

2

Monday, December 19th 2011, 6:55am

You can do it like this way...
(1) connect(ui->pushButton,SIGNAL(clicked()),ui->centralWidget,SLOT(DrawModel())); //DrawModel() can be any other name
(2) //Set a global
bool bFlag = false;
(3) void GLWidget::DrawModel()
{
if(bFlag == false)
{
glTriangle();
bFlag == true;
}
else
{
glQuard();
bFlag = false'
}
}


--MainWindow.cpp--

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "mainwindow.h" #include "ui_mainwindow.h"
    MainWindow::MainWindow(QWidget *parent) :
     QMainWindow(parent),
     ui(new Ui::MainWindow)
 {
     ui->setupUi(this);
     connect(ui->pushButton,SIGNAL(clicked()),ui->centralWidget,SLOT(DrawModel())); //DrawModel() can be any other name
     connect(ui->pushButton_2,SIGNAL(clicked()),ui->centralWidget,SLOT(delObject()));	//I have not developed the delObject()
     
 }
  MainWindow::~MainWindow()
 {
     delete ui;
 }


--glWidget.cpp

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "glwidget.h" #include<GL/glut.h>
 #include <ui_mainwindow.h>

//Set a global
 bool bFlag = false;

   GLWidget::GLWidget(QWidget *parent) :
     QGLWidget(parent)
 {
 }
   void GLWidget::initializeGL()
 {
   //glClearColor(0.2, 0.2, 0.2, 1);  // Background Color
     glShadeModel(GL_SMOOTH);// Enable Smooth Shading
        glClearColor(0.0f, 0.0f, 0.0f, 0.5f);// Black Background
        glClearDepth(1.0f);// Depth Buffer Setup
        glEnable(GL_DEPTH_TEST);// Enables Depth Testing
        glDepthFunc(GL_LEQUAL);// The Type Of Depth Testing To Do
        glEnable ( GL_COLOR_MATERIAL );
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
 }
  void GLWidget::resizeGL(int weight, int height)
 {
     glViewport     ( 0, 0, weight, height );
         glMatrixMode   ( GL_PROJECTION );  // Select The Projection Matrix
         glLoadIdentity ( );                // Reset The Projection Matrix
         if ( height==0 )  // Calculate The Aspect Ratio Of The Window
             gluPerspective ( 80, ( float ) weight, 1.0, 5000.0 );
         else
             gluPerspective ( 80, ( float ) weight / ( float ) height, 1.0, 5000.0 );
         glMatrixMode   ( GL_MODELVIEW );  // Select The Model View Matrix
         glLoadIdentity ( );    // Reset The Model View Matrix
  }
   void GLWidget::paintGL()
 {
    }
  void GLWidget::glTriangle(){
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// Clear Screen And Depth Buffer
         glLoadIdentity();// Reset The Current Modelview Matrix
         glTranslatef(-1.5f,0.0f,-6.0f);// Move Left 1.5 Units And Into The Screen 6.0
               glBegin(GL_TRIANGLES);// Drawing Using Triangles
                 glColor3f(1.0f,0.0f,0.0f);// Set The Color To Red
                 glVertex3f( 0.0f, 1.0f, 0.0f);// Top
                 glColor3f(0.0f,1.0f,0.0f);// Set The Color To Green
                 glVertex3f(-1.0f,-1.0f, 0.0f);// Bottom Left
                 glColor3f(0.0f,0.0f,1.0f);// Set The Color To Blue
                 glVertex3f( 1.0f,-1.0f, 0.0f);// Bottom Right
             glEnd();// Finished Drawing The Triangle
 }
  void GLWidget::glQuard(){
        glTranslatef(3.0f,0.0f,0.0f);// Move Right 3 Units
        glColor3f(0.5f,0.5f,1.0f);// Set The Color To Blue One Time Only
        glBegin(GL_QUADS);// Draw A Quad
            glVertex3f(-1.0f, 1.0f, 0.0f);// Top Left
            glVertex3f( 1.0f, 1.0f, 0.0f);// Top Right
            glVertex3f( 1.0f,-1.0f, 0.0f);// Bottom Right
            glVertex3f(-1.0f,-1.0f, 0.0f);// Bottom Left
        glEnd();// Done Drawing The Quad
  }

void GLWidget::DrawModel()
{
    if(bFlag == false)
    {
        glTriangle();
        bFlag == true;
    }
    else
    {
        glQuard();
        bFlag = false'
    }
}

3

Monday, December 19th 2011, 11:11am

Thanks, but I got these two errors:

undefined reference to GLWidget::DrawModel()
collect2:id returned 1 exit status

4

Monday, December 19th 2011, 12:37pm

When you were trying the connect on SLOT glTriangle(), was it working???

Source code

1
connect(ui->pushButton,SIGNAL(clicked()),ui->centralWidget,SLOT(glTriangle()));


if yes, then i didnot do anything other except to change the name of SLOT to DrawModel();

You do following things in your program...

(1) declare DrawModel() in SLOT section of your class

(2) make glTriangle() and glQuard() the public function of your class

And the remainig you do as suggested.

I hope problem will be sorted out.

5

Tuesday, December 20th 2011, 8:01am

I did made glTriangle() and glQuard() public functions. Its pointing the error to the delObject()

(Qt generated codes)
........
..........
int GLWidget::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
_id = QGLWidget::qt_metacall(_c, _id, _a);
if (_id < 0)
return _id;
if (_c == QMetaObject::InvokeMetaMethod) {
switch (_id) {
case 0: DrawModel(); break;
case 1: delObject(); break; <---------------------- it points here
default: ;
}
_id -= 2;
}
return _id;
}
QT_END_MOC_NAMESPACE

6

Tuesday, December 20th 2011, 9:57am

Ok, Have you written the definition of SLOT delObject() ???? I think no.
What you have done is first you tried the SLOT delObect() and later on for checking the code you deleted the definition of this SLOT in .cpp (if i m not wrong :))
But you forgot to delete the declartion of SLOT delObject() from .h file.

So, you do follwing...
(1) write definition of SLOT delObject() (and also definition of other SLOT ) in .cpp and if you have written delcaration in .h

or

(2) remove the declaration of SLOT from .h if you are nor using or not writing definition of that SLOT in .cpp

7

Thursday, December 29th 2011, 10:59am

Okay, I should write the definition in the .cpp as the exact opposite of the DrawModel() ?

This post has been edited 1 times, last edit by "doughng" (Dec 29th 2011, 11:09am)


Similar threads

Used tags

OpenGL, pushbutton