PDA

View Full Version : opengl bezier surface



zorro68
15th February 2007, 03:25
Im trying to make a bezier surface. I read points from a file and then use and evaluator to print the surface. I have read some samples (red book or blue book and some links - all use the same sample) that works fine if the points definition is into the main program. But if i want to programm a class with this phylosopy, dont work:



class GLWidget : public QGLWidget
{
...

private:
GLuint makeSurf(const GLfloat *reflectance, GLfloat ***points);
GLfloat ***ctrlpoints;
...





GLWidget::GLWidget(QWidget *parent)
: QGLWidget(parent)
{
...
//ctrlpoints = new (GLfloat **);
GLfloat ctrlpoints[4][4][3]= <- this crash
{
{
{-1.5, -1.5, 4.0},
{-0.5, -1.5, 2.0},
{0.5, -1.5, -1.0},
{1.5, -1.5, 2.0}},
{
{-1.5, -0.5, 1.0},
{-0.5, -0.5, 3.0},
{0.5, -0.5, 0.0},
{1.5, -0.5, -1.0}},
{
{-1.5, 0.5, 4.0},
{-0.5, 0.5, 0.0},
{0.5, 0.5, 3.0},
{1.5, 0.5, 4.0}},
{
{-1.5, 1.5, -2.0},
{-0.5, 1.5, -2.0},
{0.5, 1.5, 0.0},
{1.5, 1.5, -1.0}}
};
...




void GLWidget::initializeGL()
{
....
surf1=makeSurf(reflectance3,&ctrlpoints[0][0][0]); <- this crash too
....




GLuint GLWidget::makeSurf(const GLfloat *reflectance, GLfloat ***points)
{
...
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &points[0][0][0]);
...


The problem is how to define a pointer dinamically with 3 indexes (¿[][][]->***?) in a class...
I hope you understand me.
Some help please :confused:

ToddAtWSU
16th February 2007, 19:52
I think what you want to do is create the array like this:


int ***array;

array = (int***) malloc( sizeof( int** ) * X_SIZE );
for( int i = 0 ; i < X_SIZE ; i++ )
{
array[i] = (int**) malloc( sizeof( int* ) * Y_SIZE );

for( int j = 0 ; j < Y_SIZE ; j++ )
{
array[i][j] = (int*) malloc( sizeof( int ) * Z_SIZE );
}
}

This should create your 3-D array in whatever sizes you need assuming the array is:
array[X_SIZE][Y_SIZE][Z_SIZE]

Hope this helps!

wysota
17th February 2007, 00:13
Remember that you can always emulate a multidimensional array using a single dimensional array just by manipulating indexes.

First Z_SIZE indexes will contain values for x=0 && y=0, next Z_SIZE indexes will contain values for x=0 && y=1 and so on...

First Z_SIZE*Y_SIZE indexes will contain values for x=0, next will contain values for x=1 and so on...

Methedrine
17th February 2007, 02:22
Additionally, use new instead of malloc.

ToddAtWSU
20th February 2007, 22:17
Additionally, use new instead of malloc.

The big thing is to use new with delete or malloc with free. As long as you use one pair you should be fine. What makes new better than malloc besides it is C++ where malloc is more of a C function? I always use new for Qt objects and malloc when dynamically creating arrays.

If you use the 3-dimensional array make sure to free/delete every pointer and not just the overlying pointer.

jacek
20th February 2007, 22:59
What makes new better than malloc besides it is C++ where malloc is more of a C function?
The main difference is that new operator invokes objects' constructors, while malloc() doesn't (same for free() vs. delete).

Also it can throw an exception when allocation fails and you don't need any sizeofs or casts, so the code is cleaner.

ToddAtWSU
22nd February 2007, 18:44
The main difference is that new operator invokes objects' constructors, while malloc() doesn't (same for free() vs. delete).

Also it can throw an exception when allocation fails and you don't need any sizeofs or casts, so the code is cleaner.

I always wondered what the difference was but never had an opportunity to look it up when I was thinking about it. Thanks!