Advertisement

11.26.2003 at 02:31PM PST, ID: 20809629
[x]
Attachment Details

C++/Opengl: Object Oriented Bezier Curves

Asked by eye30 in OpenGL Graphics & Game Programming

Tags: opengl, bezier, curve

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



Start Free Trial
[+][-]11.26.2003 at 09:33PM PST, ID: 9829715

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.26.2003 at 09:51PM PST, ID: 9829774

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.26.2003 at 09:58PM PST, ID: 9829798

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: OpenGL Graphics & Game Programming
Tags: opengl, bezier, curve
Sign Up Now!
Solution Provided By: sunnycoder
Participating Experts: 2
Solution Grade: A
 
 
[+][-]11.26.2003 at 10:21PM PST, ID: 9829865

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.12.2005 at 11:08PM PST, ID: 13527595

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • Automotive
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Displays / Monitors
  • Handhelds / PDAs
  • Components
  • Peripherals
  • Laptops/Notebooks
  • Servers
  • Misc
  • Apple
  • Embedded Hardware
  • Networking Hardware
  • Storage
  • Desktops
  • New Users
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMware
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Virtualization
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • Web Computing
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Consulting
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMware
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Automation
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Web Services
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Web Computing
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Lounge
  • Business Travel
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
  • Automotive
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
11.26.2003 at 09:33PM PST, ID: 9829715
>glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 6, &bezierArray[0][0]);

from the man page::::
points is the location of  the first control point, which occupies one,  two,  three, or  four  contiguous  memory locations, depending on which map is being defined.

so you really should be passing &bezierArray[1][0] ... but anyway, it should not have complained as the data types look right ...

try casting to GLfloat *

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

If that does not work, tell us the following:
what is the platform? ... also what is the exact error message (along with the error number)
 
11.26.2003 at 09:51PM PST, ID: 9829774
I tried the cast as you suggested, still spits out 3 errors.  I wrote the errors in the previous code.  I'll rewrite them here to make them clearer and more obvious.  Thanks again.  

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

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]={ {x1, y1, 0.0},      ********error here:Expression syntax error****

                  {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 */

      glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 6, (GLfloat*) bezier[0].bezierArray[1][0]); ********error here********
                        (Error: Illegal explicit conversion from 'float' to 'float*')

      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++)
            {
                  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((GLfloat*)bezier[0].bezierArray[i][0]);********error here********
                                                (Error: Illegal explicit conversion from 'float' to 'float*')
            }
      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();
}

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;
}





 
11.26.2003 at 09:58PM PST, ID: 9829798
>    glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 6, (GLfloat*) bezier[0].bezierArray[1][0]); ********error here********
>                       (Error: Illegal explicit conversion from 'float' to 'float*')

glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 6, (GLfloat*) &(bezier[0].bezierArray[1][0]) );

>glVertex3fv((GLfloat*)bezier[0].bezierArray[i][0]);********error here********
>                                               (Error: Illegal explicit conversion from 'float' to 'float*')

glVertex3fv((GLfloat*) &(bezier[0].bezierArray[i][0]));
Accepted Solution
 
11.26.2003 at 10:21PM PST, ID: 9829865
just checked your class definition

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 */;

    }

};

bezierArray is not a member of your class ... rather it is a local variable for the constructor ... perhaps you would like to include it as a class member and initialize it in the constructor ...
 
03.12.2005 at 11:08PM PST, ID: 13527595
Error Free Solid Code

#include "stdafx.h"

/*  bezcurve.c                  
 *  This program uses evaluators to draw a Bezier curve.
 */
#include <stdlib.h>
#include <GL/glut.h>

GLfloat ctrlpoints[4][3] = {
      { -4.0, -4.0, 0.0}, { -2.0, 4.0, 0.0},
      {2.0, -4.0, 0.0}, {4.0, 4.0, 0.0}};

void init(void)
{
   glClearColor(0.0, 0.0, 0.0, 0.0);
   glShadeModel(GL_FLAT);
   glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &ctrlpoints[0][0]);
   glEnable(GL_MAP1_VERTEX_3);
}

void display(void)
{
   int i;

   glClear(GL_COLOR_BUFFER_BIT);
   glColor3f(1.0, 1.0, 1.0);
   glBegin(GL_LINE_STRIP);
      for (i = 0; i <= 30; i++)
         glEvalCoord1f((GLfloat) i/30.0);
   glEnd();
   /* The following code displays the control points as dots. */
   glPointSize(5.0);
   glColor3f(1.0, 1.0, 0.0);
   glBegin(GL_POINTS);
      for (i = 0; i < 4; i++)
         glVertex3fv(&ctrlpoints[i][0]);
   glEnd();
   glFlush();
}

void reshape(int w, int h)
{
   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   if (w <= h)
      glOrtho(-5.0, 5.0, -5.0*(GLfloat)h/(GLfloat)w,
               5.0*(GLfloat)h/(GLfloat)w, -5.0, 5.0);
   else
      glOrtho(-5.0*(GLfloat)w/(GLfloat)h,
               5.0*(GLfloat)w/(GLfloat)h, -5.0, 5.0, -5.0, 5.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}

/* ARGSUSED1 */
void keyboard(unsigned char key, int x, int y)
{
   switch (key) {
      case 27:
         exit(0);
         break;
   }
}

int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
   glutInitWindowSize (500, 500);
   glutInitWindowPosition (100, 100);
   glutCreateWindow (argv[0]);
   init ();
   glutDisplayFunc(display);
   glutReshapeFunc(reshape);
   glutKeyboardFunc (keyboard);
   glutMainLoop();
   return 0;
}

Cheers,
       Tsiu
 
 
20080716-EE-VQP-32