PDA

View Full Version : how to write QT openGL code in two classes



qtUse
30th August 2010, 19:20
I want to create large openGL models, so I want to divide it in multiple classes. For that, I wrote two classes,



/////////////////// glhandle.cpp ////////////////////////////////////////

#include "glhandle.h" //Contains defn of GLHandle class
#include "buildingpainter.h"

void GLHandle::paintGL() {
//SOME DRAWING CODE HERE
BuildingPainter p=BuildingPainter p=BuildingPainter(this->context());
p.draw();
}

//ResizeGL(), initializeGL() and other functions


and




////////////////// BuildingPainter.cpp ///////////////////////////////

#include "buildingpainter.h" //Defn of BuildingPainter class

BuildingPainter::BuildingPainter(QWidget *parent) :
QGLWidget(parent) {
}

// This constructor is called: So that openGL states should be copied
BuildingPainter::BuildingPainter(const QGLContext *refs): QGLWidget(refs) {
}

void BuildingPainter::draw() {
//More drawing Code here
}


However, I am getting compile error:
error: ‘QGLWidget::QGLWidget(const QGLWidget&)’ is private
in definition of BuildingPainter.

What is right way to do it so that I can render different parts of my model from different classes?

tbscope
30th August 2010, 19:38
The constructor you use is private, you need to use a public constructor.


QGLWidget(QGLFormat(QGL::SampleBuffers), parent)

May I ask what type of program you're making? I see a class BuildingPainter which interests me.

qtUse
30th August 2010, 20:16
Thanks for reply, tbscope

I tried function you mentioned above which threw an error as:
error: no matching function for call to ‘BuildingPainter::BuildingPainter(QGLFormat, GLHandle* const)’
Can you point me to sample use of function you mentioned??
Also will it be able to take OpenGL context (openGL states) from calling function?

BTW, I am trying to model real world scenes(Park) in openGL and "BuildingPainter" creates "Building" model (Basic modelling only, by cubes etc.)

tbscope
30th August 2010, 20:19
http://doc.qt.nokia.com/4.6/opengl-hellogl.html

qtUse
30th August 2010, 20:36
But HelloGL uses different GL interfacing class-'GLWidget' and I am using 'QGLWidget', so constructor is not found in my case.
Also helloGL handles all openGL calls from only one class.
Also in QGLWidget has constructors


QGLWidget ( QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WFlags f = 0 )
QGLWidget ( QGLContext * context, QWidget * parent, const char * name = 0, const QGLWidget * shareWidget = 0, WFlags f = 0 )

as public member (see this: http://doc.trolltech.com/3.3/qglwidget.html), so it should not cause problem.

Is there any alternate method to access and modify QGLWidget from different class?

tbscope
30th August 2010, 20:46
I suggest a good C++ book.

Are you using Qt3 or Qt4?
The hellogl example uses QGLWidget like you are. But it shows you how to use the QGLWidget constructor. It doesn't matter if you split your program in different parts.

C++ contains several ways to exchange information between different objects or different classes.

tbscope
30th August 2010, 20:49
By the way, I think you handle this a bit in the wrong way.

You should only have one GL Widget.
Then you can create different classes that do different things. Like a Building class or a Car class which each can serialise data and create the GL code. Then you can paint that GL code on the widget. There are several techniques and programming patterns you can use for this.

qtUse
30th August 2010, 21:03
Ohhhh .....I verified, HelloGL's using QGLWidget only as you said
I tried according to it and it's finely taking GL contexts from calling function

Thanks for your prompt help, tbscope :)

@second post:
But in order to draw, say car, I need to paint different sides in car drawing class only (to avoid passing all its vertices to single QGLWidget), so GL context must be available to my BuildingPainter class. So I am using multiple OGLWidgets

tbscope
31st August 2010, 05:41
I would probably go with the Model and View pattern. The view being the QGLWidget and the model a tree of objects.

Example:

+------------------+
| MyGlModel |
| |
| Rootitem | +-------------+
| | | | MyGlView |
| +- Item | | |
| | |----->| setModel() |
| +- Item | | |
| | | | |
| + Item | +-------------+
| |
+------------------+





+------------------+ +-------------------+
| MyGlView | | MyGlItem |
| | | |
| paintGl() | | drawItem(context) |
| | |----->| |
| +- iterate | | |
| over model | +-------------------+
| |
+------------------+



void MyGlView::drawGl()
{
// recursive call each item in the model tree

// For each item:
item->drawItem(context(), /*maybe other stuff*/);
}

void MyGlItem::drawItem(QGLContext *context, /*maybe other things*/)
{
// Do the GL Painting for this item based on the context.
}