OOP: C++/OpenGl. Creating multiple bezier curves

Hi, I'm relatively new to OOP/C++/Opengl.  Thanks in advance.

I want to create multiple Bezier curves in a OOP class structure.  Eventually I would like to create multiple bezier curves with different attributes like x, y position, line thickness, etc.

I'm having difficulty with pointer/array portion.  I want to create an array of 100 bezier curves.  These individual curves are set up in an 6x3 array.  I'm getting 2 errors.  

My code:
#include <windows.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <iostream.h>
#include <time.h>

//function prototypes

//Define bezier class for animation

class BezierClass {

     public:
     float x1,y1;
     float x2,y2;
     float x_cp1,y_cp1;
     float x_cp2,y_cp2;
     float x_cp3,y_cp3;
     float x_cp4,y_cp4;
     float xpos,ypos;

     BezierClass() {
     float bezierArray[6][3] =
               { {x1, y1, 0.0},     /* 1st End Point */
                {x_cp1, y_cp1, 0.0},     /* 1st Control P. */
                {x_cp2, y_cp2, 0.0},     /* 2nd Control P. */
                {x_cp3, y_cp3, 0.0},     /* 3rd Control P. */
                {x_cp4, y_cp4, 0.0},     /* 3th Control P. */
                {x2, y2, 0.0} };          /* 2nd End Point */;

     }

};


/* Create an array of type bezierClass that may hold 100 beziers */
BezierClass bezierArray[100];
int beziernum=0;
int totalbezier=5;
float zoom = -7;

void InitGL(int Width, int Height)
{
     glClearDepth(1.0);
     glClearColor(0.0, 0.0, 0.0, 0.0);     /* bg color = black */
     glDepthFunc(GL_LESS);               /* nearer appears nearer */
     glEnable(GL_DEPTH_TEST);          /* enable depth testing */
     glShadeModel(GL_SMOOTH);          /* shade colors smooth */
     glMatrixMode(GL_PROJECTION);          /* switch the matrix */
     glLoadIdentity();               /* reset the cur. matrix */
     gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
     glMatrixMode(GL_MODELVIEW);          /* switch matrix back */
}

void DrawGLScene(void)
{
     int i;                         /* temp var */

     /* Clear the screen and the buffers */
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glLoadIdentity();               /* reset cur. matrix */
     glTranslatef(0.0f, 0.0f, -10.0f);     /* move the cam */

     /* ok, this is the main point of our bezier curve
      * void glMap1f(GLenum target, float u1, float u2, int stride
      *         int order, const float *points);
      * target is one of these:
      * GL_MAP1_VERTEX_3     (Vertex Coordinates (xyz))
      * GL_MAP1_VERTEX_4     (Vertex Coordinates (xyzw))
      * GL_MAP1_INDEX     (Color Index)
      * GL_MAP1_COLOR_4     (Color Values (rgba))
      * GL_MAP1_NORMAL     (Normal Coordinates)
      * GL_MAP1_TEXTURE_COORD_1/2/3/4 (Texture Coordinates(s/st/str/strq)
      *
      * no idea what u1 and u2 these are, but it works with 0.0 and 1.0
      * stride is the distance between each point on the curve
      * order should always just fit the number of the points
      * points is just a pointer to the pointdata(array)*/
////********error here: says "pointer/array required"//////////////

                glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 6, &bezierArray[0][0]);
               

     glEnable(GL_MAP1_VERTEX_3);          /* enable the mode */

     glColor3f(1.0, 1.0, 1.0);          /* set color to white */

     glBegin(GL_LINE_STRIP);               /* start drawing */
          for(i = 0; i <= 60; i++)
          {
               /* start evaluating the points
                * void glEvalCoord1f(float u);
                * u is the current point we evaluate */
               glEvalCoord1f((float)i/60.0f);
          }
     glEnd();                    /* stop drawing */

     glPointSize(3.0);               /* set point size to 3px */
     glColor3f(1.0f, 1.0f, 0.0f);          /* set color to yellow */
     glBegin(GL_POINTS);               /* start drawing again */
          for(i = 0; i < 6; i++)
          {
               /* draw all control/end points */
////********error here: says "pointer/array required"//////////////
               glVertex3fv(&bezierArray[i][0]);
          }
     glEnd();

     glFlush();                    /* Flush all buffers */
     glutSwapBuffers();               /* Sawp buffers */
     return;
}

void reshape(int w, int h)
{
     glViewport(0, 0, (GLint) w, (GLint) h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();

     if ( h==0)
          gluPerspective(45, (GLdouble)w, 1.0, 100.0);
     else
          gluPerspective(45, (GLdouble)w/(GLdouble)h,1.0, 100.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}

//test-trying to make the bezier curve move in y direction.

void idle_func (void)
{
for (beziernum=0 ;beziernum < 100; beziernum++) {
   //bezierArray[beziernum].ypos+=1;

   }
   glutPostRedisplay();
}

/*  Main Loop
 *  Open window with initial window size, title bar,
 *  RGBA display mode, and handle input events.
 */
int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
   glutInitWindowSize (800, 600);
   glutCreateWindow (argv[0]);
   glutReshapeFunc (reshape);
   glutDisplayFunc (DrawGLScene);
   glutIdleFunc (idle_func);
   glutMainLoop();
   return 0;
}
//end of code

eye30Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gkatzCommented:
ok, i think you're a little confused with how classes and arrays work.
What you wrote was &bezierArray[i][0].  You think that you are referencing an array here.  You created beizerArray as an array of instances of a class called BezierClass.  Inside of that class is an array called beizerArray.
To actually access the array inside the instances of the class you would write something like:
beizerArray[x].beizerArray[i][0]

I think that you didn't mean to name your instances of your class the same as the array.  You could have written
BeizerClass beizer[100]; //this would create 100 instances of the class  
                                  // beizerClass

now there are 100 instances of the class, each instance of the class contains an array called beizerArray.


to access an element of one of these arrays you would type
beizer[classNumber].beizerArray[xElement][yElement];

The classNumber would refer to one of the 100 different instances of beizerClass and the xElement and yElement would refer to the individual element.

This would now work execpt for one reason.  You didn't make the array beizerArray global inside of your class.  What happens is that your constructor creates the array beizerArray, but since it's local to the constructor, once the constructor is done executing it deletes it.  You want to make your array public also. You would need to change your class to:

class BezierClass {

     public:
     float x1,y1;
     float x2,y2;
     float x_cp1,y_cp1;
     float x_cp2,y_cp2;
     float x_cp3,y_cp3;
     float x_cp4,y_cp4;
     float xpos,ypos;
     float bezierArray[6][3];
     BezierClass() {
          bezierArray[6][3] =
               { {x1, y1, 0.0},     /* 1st End Point */
                {x_cp1, y_cp1, 0.0},     /* 1st Control P. */
                {x_cp2, y_cp2, 0.0},     /* 2nd Control P. */
                {x_cp3, y_cp3, 0.0},     /* 3rd Control P. */
                {x_cp4, y_cp4, 0.0},     /* 3th Control P. */
                {x2, y2, 0.0} };          /* 2nd End Point */;

     }

};

Ok now we're starting to get somewhere.  The next problem is that

     float x1,y1;
     float x2,y2;
     float x_cp1,y_cp1;
     float x_cp2,y_cp2;
     float x_cp3,y_cp3;
     float x_cp4,y_cp4;
are never initialized before you use them inside of your constructor.  These would have to be initialized inside the constructor before you initialize beizerArray.  This could either be done at the begining of the function or in an initializer list.  For right now i'd just do it at the beginning of the constructor.

Ok to recap:
1 you need to call the arrays correctly. beizer[].beizerArray[][]
2. you need to make the array public inside of the class instead of a local variable inside of the constructor
3. you need to initialize the variables that are used to initialize beizerArray before initializing beizerArray.

I didn't take a look at the rest of your code but that should fix the errors that you're getting right now.  The code as a whole looks pretty good.  I think you just got a little confused with how classes work.  

Good luck
gkatz

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
eye30Author Commented:
Thanks for your reply.  Yeah, I after I posted that I named my instance the same as my array.  I've made the changes but still getting three errors.

Something seems to be wrong with the initialization of the constructor and the call to the array again.

Code:
-------------------------------------------------

#include <windows.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <iostream.h>
#include <time.h>

//function prototypes

//Define bezier class for animation



class BezierClass {

      public:
      float x1,y1;
      float x2,y2;
      float x_cp1,y_cp1;
      float x_cp2,y_cp2;
      float x_cp3,y_cp3;
      float x_cp4,y_cp4;
      float xpos,ypos;
      float bezierArray[6][3];
      
      BezierClass() {
            float x1,y1;  
            float x2,y2;
            float x_cp1,y_cp1;
            float x_cp2,y_cp2;
            float x_cp3,y_cp3;
            float x_cp4,y_cp4;
            float xpos,ypos;
             bezierArray[6][3]=
                                                                                //****Error 1: Expression syntax error
                    {x1, y1, 0.0},      /* 1st End Point */
                  {x_cp1, y_cp1, 0.0},      /* 1st Control P. */
                  {x_cp2, y_cp2, 0.0},      /* 2nd Control P. */
                  {x_cp3, y_cp3, 0.0},      /* 3rd Control P. */
                  {x_cp4, y_cp4, 0.0},      /* 3th Control P. */
                  {x2, y2, 0.0};            /* 2nd End Point */

      }

};

/* Create an array of type bezierClass that may hold 100 beziers */
BezierClass bezier[100];
int beziernum=0;
int totalbezier=5;
float zoom = -7;

void InitGL(int Width, int Height)
{
      glClearDepth(1.0);
      glClearColor(0.0, 0.0, 0.0, 0.0);      /* bg color = black */
      glDepthFunc(GL_LESS);                  /* nearer appears nearer */
      glEnable(GL_DEPTH_TEST);            /* enable depth testing */
      glShadeModel(GL_SMOOTH);            /* shade colors smooth */
      glMatrixMode(GL_PROJECTION);            /* switch the matrix */
      glLoadIdentity();                  /* reset the cur. matrix */
      gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
      glMatrixMode(GL_MODELVIEW);            /* switch matrix back */
}

void DrawGLScene(void)
{
      int i;                              /* temp var */

      /* Clear the screen and the buffers */
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glLoadIdentity();                  /* reset cur. matrix */
      glTranslatef(0.0f, 0.0f, -10.0f);      /* move the cam */

      /* ok, this is the main point of our bezier curve
       * void glMap1f(GLenum target, float u1, float u2, int stride
       *          int order, const float *points);
       * target is one of these:
       * GL_MAP1_VERTEX_3      (Vertex Coordinates (xyz))
       * GL_MAP1_VERTEX_4      (Vertex Coordinates (xyzw))
       * GL_MAP1_INDEX      (Color Index)
       * GL_MAP1_COLOR_4      (Color Values (rgba))
       * GL_MAP1_NORMAL      (Normal Coordinates)
       * GL_MAP1_TEXTURE_COORD_1/2/3/4 (Texture Coordinates(s/st/str/strq)
       *
       * no idea what u1 and u2 these are, but it works with 0.0 and 1.0
       * stride is the distance between each point on the curve
       * order should always just fit the number of the points
       * points is just a pointer to the pointdata(array)*/
      glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 6, bezier[0].bezierArray[0][0]); **//Error #2: Function Call does not match.
      glEnable(GL_MAP1_VERTEX_3);            /* enable the mode */

      glColor3f(1.0, 1.0, 1.0);            /* set color to white */

      glBegin(GL_LINE_STRIP);                  /* start drawing */
            for(i = 0; i <= 60; i++)
            {
                  /* start evaluating the points
                   * void glEvalCoord1f(float u);
                   * u is the current point we evaluate */
                  glEvalCoord1f((float)i/60.0f);
            }
      glEnd();                        /* stop drawing */

      glPointSize(3.0);                  /* set point size to 3px */
      glColor3f(1.0f, 1.0f, 0.0f);            /* set color to yellow */
      glBegin(GL_POINTS);                  /* start drawing again */
            for(i = 0; i < 6; i++)
            {
                  /* draw all control/end points */
                  glVertex3fv(bezier[0].bezierArray[i][0]);    ////Error #3: Function Call does not match.
            }
      glEnd();

      glFlush();                        /* Flush all buffers */
      glutSwapBuffers();                  /* Sawp buffers */
      return;
}

void reshape(int w, int h)
{
      glViewport(0, 0, (GLint) w, (GLint) h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();

      if ( h==0)
            gluPerspective(45, (GLdouble)w, 1.0, 100.0);
      else
            gluPerspective(45, (GLdouble)w/(GLdouble)h,1.0, 100.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}

void idle_func (void)
{
for (beziernum=0 ;beziernum < 100; beziernum++) {
   //bezierArray[beziernum].ypos+=1;

   }
   glutPostRedisplay();
}

/*  Main Loop
 *  Open window with initial window size, title bar,
 *  RGBA display mode, and handle input events.
 */
int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
   glutInitWindowSize (800, 600);
   glutCreateWindow (argv[0]);
   glutReshapeFunc (reshape);
   glutDisplayFunc (DrawGLScene);
   glutIdleFunc (idle_func);
   glutMainLoop();
   return 0;
}
_ys_Commented:
As for error 1:
You had it right in a previous post.

AbezierArray[6][3]=
        { // *** missing opening and closing braces
                 {x1, y1, 0.0},     /* 1st End Point */
                {x_cp1, y_cp1, 0.0},     /* 1st Control P. */
                {x_cp2, y_cp2, 0.0},     /* 2nd Control P. */
                {x_cp3, y_cp3, 0.0},     /* 3rd Control P. */
                {x_cp4, y_cp4, 0.0},     /* 3th Control P. */
                {x2, y2, 0.0}
        };          /* 2nd End Point */

As for errors 2 & 3, if it's a pointer it wants, simply give it one:

& bezier[0].bezierArray[0][0] //*** take the address of it.

[I don't know the GL library at all, so I can't validate the above code against the function's signature/purpose]
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.