GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: No Line Numbers


3d bezier curve by lew26
1
 
# include "glut.h"
# include <iostream>
# include <list>

using namespace std;

//##################################################################
class vertex
{
private:
  GLdouble x;
  GLdouble y;
  GLint delta;
public:

    vertex ()
  {
    x = 0.0;
    y = 0.0;
    delta = 5;
  };

  vertex (const vertex & v)
  {
    x = v.x;
    y = v.y;
    delta = v.delta;
  }

  void operator= (const vertex & v)
  {
    x = v.x;
    y = v.y;
    delta = v.delta;
  }

  bool nearvertex (GLint xscreen, GLint yscreen)
  {
    if ((xscreen >= x - delta) && (xscreen <= x + delta)
    && (yscreen >= y - delta) && (yscreen <= y + delta))
      return true;
    else
      return false;
  }

  void set (GLdouble x, GLdouble y)
  {
    this->x = x;
    this->y = y;
  }

  void update (GLdouble dy)
  {
    y += dy;
  }

  void print ()
  {
    cout << "(x,y)=( " << x << " , " << y << " )" << endl;
  }

  GLdouble getx ()
  {
    return x;
  }

  GLdouble gety ()
  {
    return y;
  }
};

//##################################################################
GLuint xsize, ysize;
GLint d = 5;

GLdouble dt = 0.001;

// added for 3d
GLfloat            rotz = 0.0f;        // Rotation about the Z axis
GLfloat         rotx = 0.0f;
GLfloat            roty = 0.0f;
GLfloat            rota = 0.0f;

list < vertex > points;
list < vertex >::iterator iter;
list < vertex >::iterator move;
bool found = false;
GLint max_order;        // maximale Anzahl an Punkten pro Kurve
//##################################################################
void
main_display ()
{
  GLdouble t;
  GLdouble *plist;
  GLint i;

  plist = new GLdouble[3 * points.size ()];

  for (iter = points.begin (), i = 0; iter != points.end (); iter++, i++)
    {
      plist[3 * i] = iter->getx ();
      plist[3 * i + 1] = iter->gety ();
      plist[3 * i + 2] = 0.0;
    }

  glLoadIdentity ();
  glClear (GL_COLOR_BUFFER_BIT);
  
  // for 3d support
  glLoadIdentity();
  glTranslatef(0.0f, 0.0f, 0.0f);    
  glRotatef(0.0f, 0.0f, 0.0f, 1.0f); 
  glRotatef(rota,rotx,roty,rotz;
    

  glBegin (GL_QUADS);
  glColor3d (1.0, 0.0, 0.0);
  for (iter = points.begin (); iter != points.end (); iter++)
    {
      if ((found) && (iter == move))
    glColor3d (0.0, 1.0, 0.0);
      else
    glColor3d (1.0, 0.0, 0.0);

      glVertex2d (iter->getx () - d, iter->gety () + d);
      glVertex2d (iter->getx () - d, iter->gety () - d);
      glVertex2d (iter->getx () + d, iter->gety () - d);
      glVertex2d (iter->getx () + d, iter->gety () + d);
    }
  glEnd ();

  if (points.size () > 1)
    {
      glColor3f (1.0, 0.0, 0.0);

      
      glMap1d (GL_MAP1_VERTEX_3, 0.0, 1.0, 3, points.size (), plist);
      /*
         
       */

      
      glEnable (GL_MAP1_VERTEX_3);

      
      glBegin (GL_LINE_STRIP);
      for (t = 0.0; t < 1.0; t += dt)
    {
      glColor3f (0.5, 0.5 * t, t);
      
      glEvalCoord1d (t);
    }
      glEnd ();
    }

  glutSwapBuffers ();

  delete plist;
}

//##################################################################
void
main_reshape (GLint w, GLint h)
{
  GLint dy = h - ysize;
  GLint dx = w - xsize;
  
  for (iter = points.begin (); iter != points.end (); iter++)
    {
      iter->update ((GLdouble) dy);
    }

  glMatrixMode (GL_PROJECTION);
  glLoadIdentity ();
  glOrtho (0, xsize, 0, ysize, -1000, 1000);
  glTranslatef(1.0f,1.0f,1.0f);    

   glMatrixMode (GL_MODELVIEW);
  /*glLoadIdentity();*/

  xsize = w;
  ysize = h;
}

//##################################################################
void
main_key (unsigned char key, GLint x, GLint y)
{
  switch (key)
    {
    case 'r':
    case 'R':
      points.clear ();

      break;
    case 'd':
      if (found)
    {
      points.erase (move);
      found = false;
    }
      break;
    }
  glutPostRedisplay ();
}

//##################################################################
void spec_Key (GLint key, GLint x, GLint y)
{
    switch (key)
    {
    case GLUT_KEY_LEFT:        // Left directional key.
        rota -= 5.8f;
        roty = 1.0f;
        rotx = 1.0f;
        break;
    case GLUT_KEY_RIGHT:        // Up directional key.
        rota += 5.8f;
        roty = 1.0f;
        rotx = 1.0f;
        break;
    case GLUT_KEY_UP:    // Right directional key.
        rota += 5.8f;
        rotx = 1.0f;
        roty = 1.0f;
        break;
    case GLUT_KEY_DOWN:        // Down directional
        rota -= 5.8f;
        rotx = 1.0f;
        roty = 1.0f;
        break;

    } // switch

    glutPostRedisplay ();

} // spec_Key

//##################################################################
void
main_mouse (GLint button, GLint state, GLint x, GLint y)
{
  vertex v;

  // if right mouse button down display point at coordinates
  if ((button == GLUT_RIGHT_BUTTON) && (state == GLUT_DOWN)
      && (points.size () < max_order))
    {
      v.set ((GLdouble) x, (GLdouble) ysize - y);
      points.push_back (v);
    }

  // if left mouse button down display point at coordinates
  if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN))
    {
      found = false;

      for (move = points.begin (); move != points.end (); move++)
    {
      if (move->nearvertex ((GLint) x, (GLint) ysize - y))
        {
          dt = 0.1;
          found = true;
          break;
        }
    }
    }

  if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_UP))
    {
      found = false;
      dt = 0.001;
    }
  glutPostRedisplay ();
}

//##################################################################
void
main_mousemove (GLint x, GLint y)
{
  if (found)
    {
      move->set ((GLdouble) x, (GLdouble) ysize - y);
    }

  glutPostRedisplay ();
}

//####################################################################
int
main (int argc, char **argv)
{
  using namespace std;
  xsize = ysize = 1000;

  
  glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE);
  glClearColor (0.0, 0.0, 0.0, 1.0);

  glutInitWindowSize (xsize, ysize);
  glutInit (&argc, argv);
  glutCreateWindow (argv[0]);
  glutDisplayFunc (main_display);
  glutReshapeFunc (main_reshape);
  glutKeyboardFunc (main_key); // ansi keys
  glutSpecialFunc (spec_Key); // special keys
  glutMouseFunc (main_mouse);
  glutMotionFunc (main_mousemove);

  /*glMatrixMode (GL_PROJECTION);
  glLoadIdentity ();
  gluOrtho2D (0, xsize, ysize, 0.0);
  glMatrixMode (GL_MODELVIEW);*/

  
  glGetIntegerv (GL_MAX_EVAL_ORDER, &max_order);

  glutMainLoop ();
  return 0;
}