calling the same calss/method 2 times with different parameters

Write your question here.

All,

sorry for asking basic question, couldn't able to figure out how to do it. I am calling OpenGL class GlobJ to paint lines,circles..etc. I need to call the same class again to paint Sine wave. here is my code.

Code to draw Lines/circles..etc. is as below.

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  #include <QtGui/QtGui>
#include <QtOpenGL/QtOpenGL>
#include <math.h>
#include "globj.h"

GLobj::GLobj(QWidget *parent)
    : QGLWidget(parent)
{

}

GLobj::~GLobj()
{

}

//Initialize the GL settings
void GLobj::initializeGL()
{
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClearDepth(5.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glPointSize(2.0);
    glLineWidth(4.0);
    
}

//Set up the viewport based on the screen dimentions
//Function is called implicitly by initializeGL and when screen is resized
void GLobj::resizeGL( int w, int h )
{
    //algorithm to keep scene "square" (preserve aspect ratio)
    //even if screen is streached
    if(w>h)
        glViewport((w-h)/2, 0, h, h);
    else
        glViewport(0, (h-w)/2, w, w);

    //setup the projection and switch to model view for transformations
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1, 1, -1, 1, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    

    //implicit call to paintGL after resized
}

//Paints the GL scene
void GLobj::paintGL()
{
    glClear (GL_COLOR_BUFFER_BIT);
    glClear(GL_DEPTH_BUFFER_BIT);
    
    glBegin(GL_POINTS);
    
    glColor3f (1.0, 1.0, 1.0);
     glVertex2f(-0.95,0.85);
    glEnd();

   glBegin(GL_LINES);
   glColor3f (1.0, 0.0, 0.0);
   glVertex2f(-0.75,0.65);
   glVertex2f(-0.75,0.25);
    glEnd();

   glBegin(GL_TRIANGLES);
   glColor3f (1.0, 0.0,1.0);
   glVertex2f(-0.65,0.0);
   glVertex2f(-0.45,0.0);
   glVertex2f(-0.55,0.5);
    glEnd();

 glBegin(GL_QUADS);
 glColor3f (0.0, 1.0,1.0);
 glVertex2f(0.1, 0.1);
 glVertex2f(0.3,0.1);
 glVertex2f(0.3,0.3);
 glVertex2f(0.1,0.3);
 glEnd();


 glBegin(GL_QUADS);
 glColor3f (0.0, 0.0,1.0);
 glVertex2f(0.4, 0.1);
 glVertex2f(0.9, 0.1);
 glVertex2f(0.9, 0.3);
 glVertex2f(0.4, 0.3);
 glEnd();

 glBegin(GL_POLYGON);
 glColor3f (1.0, 1.0,0.0);
 glVertex2f(0.1, 0.6);
 glVertex2f(0.2, 0.4);
 glVertex2f(0.4, 0.4);
 glVertex2f(0.5, 0.6);
 glVertex2f(0.3, 0.8);
 glEnd();

 glBegin(GL_POLYGON);
 glColor3f (0.5, 1.0,1.0);
 glVertex2f(0.6, 0.6);
 glVertex2f(0.7, 0.5);
 glVertex2f(0.8, 0.5);
 glVertex2f(0.9, 0.6);
 glVertex2f(0.8, 0.7);
 glVertex2f(0.7, 0.7);
 glEnd();

 glBegin(GL_POLYGON);
 glColor3f (0.5, 0.5,1.0);
 glVertex2f(-0.1, -0.2);
 glVertex2f(-0.2, -0.1);
 glVertex2f(-0.3, -0.1);
 glVertex2f(-0.4, -0.2);
 glVertex2f(-0.4, -0.3);
 glVertex2f(-0.25, -0.45);
 glVertex2f(-0.1, -0.3);
 glEnd();

 glBegin(GL_QUADS);
 glColor3f (0.5, 0.5,0.5);
 glVertex2f(0.1, -0.2);
 glVertex2f(0.3, -0.2);
 glVertex2f(0.3, -0.4);
 glVertex2f(0.1, -0.4);
 glEnd();

 glBegin(GL_QUADS);
 glColor3f (1.0, 0.5,1.0);
 glVertex2f(0.3, -0.2);
 glVertex2f(0.35, -0.3);
 glVertex2f(0.35, -0.5);
 glVertex2f(0.3, -0.4);
 glEnd();

 glBegin(GL_QUADS);
 glColor3f (1.0, 1.0,0.5);
 glVertex2f(0.1, -0.4);
 glVertex2f(0.3, -0.4);
 glVertex2f(0.35, -0.5);
 glVertex2f(0.15, -0.5);
 glEnd();

    glFlush ();
}


I am calling above program in my add widget program as below:

1
2
3
4
5
6
7
8
9
10
11
12
  
GLobj *globj = new GLobj(this);
QHBoxLayout *mainLayout3 = new QHBoxLayout(this);
    mainLayout3->addWidget(globj);

      QWidget *newWidget3 = new QWidget(this);
      newWidget3->setLayout(mainLayout3);

      dwidget3->setWidget(newWidget3);

      addDockWidget(Qt::RightDockWidgetArea, dwidget3);


until here everything is working fine. Now i wrote another program to draw Sine wave.

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
74
75
76
77
78
79
80
81
82
83
84
85
86
  #include <QtGui/QtGui>
#include <QtOpenGL/QtOpenGL>
#include <math.h>
#include "globj.h"
#include <iostream>

using namespace std;

GLobj::GLobj(QWidget *parent)
    : QGLWidget(parent)
{

}

GLobj::~GLobj()
{

}

//Initialize the GL settings
void GLobj::initializeGL()
{
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glPointSize(2.0);
    glLineWidth(1.0);
    
}

//Set up the viewport based on the screen dimentions
//Function is called implicitly by initializeGL and when screen is resized
void GLobj::resizeGL( int w, int h )
{
    //algorithm to keep scene "square" (preserve aspect ratio)
    //even if screen is streached
    if(w>h)
        glViewport((w-h)/2, 0, h, h);
    else
        glViewport(0, (h-w)/2, w, w);

    //setup the projection and switch to model view for transformations
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1, 1, -1, 1, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    

    //implicit call to paintGL after resized
}

//Paints the GL scene
void GLobj::paintGL()
{
    glClear (GL_COLOR_BUFFER_BIT);
    glClear(GL_DEPTH_BUFFER_BIT);

glBegin(GL_LINES);
double amp=1;
double freq = 4;
double t=0.0;
for (double x=-1.0; x<=1; x+=0.001)
{
double y = amp * cos(2 * 3.14 * freq * x );
glVertex2f(x, y);
t+=10.0;
}
glEnd();

glBegin(GL_LINES);
double amp2=1;
double freq2 = 4;
double t2=0.0;
for (double x2=-0.999; x2<=1; x2+=0.001)
{
double y2 = amp2 * cos(2 * 3.14 * freq2 * x2 );
glVertex2f(x2, y2);
t+=10.0;
}
glEnd();

    glFlush ();
}


I need to call this sine wave in my add widget program, this one is different from lines/circles widget above. this is where i am getting trouble.


1
2
3
4
5
6
7
8
9
10
11
12
13
  dwidget3 = new QDockWidget(tr("Sine Wave"), this);
    
    GLobj2 *globj2 = new GLobj2(this);
    QHBoxLayout *mainLayout4 = new QHBoxLayout(this);
    mainLayout3->addWidget(globj2);

      QWidget *newWidget4 = new QWidget(this);
      newWidget4->setLayout(mainLayout4);

      dwidget3->setWidget(newWidget4);

      dwidget3->show();
      addDockWidget(Qt::RightDockWidgetArea, dwidget3);


Thanks,
Ravi
What trouble, apart from using class GLobj2 instead of class GLobj?


You could have single GLobj type, if it had a functor that you change as needed.
keskiverto,

Thank you very much for your help. That worked. I think somewhere i missed to change it to GlobJ2 in my GlobJ2 file.

Ravi Bavanari
Topic archived. No new replies allowed.