基于OpenGL实现的多段Bezier曲线拼接(2)

//////////////////////////////////////////////////////////////////////////
  //rendering moving tangent(切线)
  //(vertexNum-1, vertexNum-2)
  if ( vertexNum>=2 )
  {
   glEnable(GL_LINE_STIPPLE);
   {
    glLineStipple(1, 0x0101);
    glBegin(GL_LINES);
    {
     Vec3d v1 = vertexVector[vertexNum-1];
     Vec3d v2 = vertexVector[vertexNum-2];
     glVertex3dv(v1.getValue());
     glVertex3dv(v2.getValue());
    } glEnd();
   }glDisable(GL_LINE_STIPPLE);
  }

//////////////////////////////////////////////////////////////////////////
  //if ( !_check() )
  // return;

//rendering bezier cells...
  system("CLS");
  for (int i=0; i<cellNum; i++)
  {
   int pos = i * 3;
   if ( (pos+3) < vertexNum )
    renderBezierCell( BezierCell(pos, pos+1, pos+2, pos+3) );
  }
  //////////////////////////////////////////////////////////////////////////
 }

// 3次bezier曲线经过vetex0和vextex3
 void renderBezierCell(const BezierCell& cell)
 {
  double *pBuffer = new double[Bezier3CtrlPnt * 3];

cout << "----------------------------------------------------" << endl;

cout << "Vertex number : " << vertexNum << endl;
  cout << "Cell number : " << cellNum << endl;
  cout << "The render cell: " << cell[0] << " " << cell[1] << " " << cell[2] << " " << cell[3] << endl;

for (int i = 0, bg = 0; i<4; i++)
  {
   Vec3d v = vertexVector[ cell[i] ];
   pBuffer[bg++] = v.x();
   pBuffer[bg++] = v.y();
   pBuffer[bg++] = v.z();
   
   cout << v.x() << " " << v.y() << " " << v.z() << endl;
  }cout << "----------------------------------------------------" << endl;

glMap1d(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, Bezier3CtrlPnt, pBuffer);
  glBegin(GL_LINE_STRIP);
  {
   for (int i = 0; i <= 30; i++)
    glEvalCoord1f((GLfloat) i/30.0f);
  } glEnd();

delete pBuffer; pBuffer = 0;
 }

void clear()
 {
  cellNum = 0;
  vertexNum = 0;

isFirstRender = true;

vertexVector.clear();
 }
protected:
 bool _check() { vertexNum =vertexVector.size(); 
     return vertexNum == (cellNum - 1) * 3 + 4; }
 void _updateVertexNum() { vertexNum=vertexVector.size();}

int cellNum;      //单元个数
 int vertexNum;      //顶点个数

bool isFirstRender;     //首次标志
 std::vector<Vec3d> vertexVector; //顶点数组

class cellRenderImple
 {
 public:
  enum RenderStep
  {
   Push = 0,
   Change = 1
  };
  cellRenderImple(){ setPush(); }
  bool operator()(void) { return flag; }
  void setPush() { flag = Push; }
  void setChange() { flag = Change; }

RenderStep flag;    //cell的渲染状态
 } cellRenderState;
};

测试程序如下:

#include <iostream>
#include <vector>
#include <GL/glut.h>

#include "BezierCurve.h"

using namespace std;

enum WindowSize{
 WinWidth = 1024,
 WinHeight = 768
};

int   g_Viewport[4];
double  g_ModelMatrix[16];
double  g_ProjMatrix[16];
BezierCurve myBezier;

//////////////////////////////////////////////////////////////////////////
void init();
void display();
void reshape(int w, int h);
void keyboard(unsigned char key, int x, int y);
void mouse(int button, int state, int x, int y);
void motion(int x, int y);

int main(int argc, char** argv)
{
 glutInit(&argc, argv);
 glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
 glutInitWindowSize (WinWidth, WinHeight);
 glutInitWindowPosition (100, 100);
 glutCreateWindow (argv[0]);

init ();
 glutDisplayFunc(display);
 glutReshapeFunc(reshape);
 glutKeyboardFunc (keyboard);
 glutMouseFunc(mouse);
 glutMotionFunc(motion);

glutMainLoop();

return 0;
}

void init(void)
{
 glClearColor(0.0, 0.0, 0.0, 0.0);
 glShadeModel(GL_SMOOTH);

myBezier.begin();
}

void display(void)
{
 glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0, 1.0, 0.0);
 glPointSize(5.0);

glPushMatrix();
 {
  myBezier.renderCurve();
 }glPopMatrix();

glutSwapBuffers();
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/270729584dbf2311efb6e609bfc79018.html