C++ opengl动态贝塞尔曲线有间隙-glEvalCoord1f();

C++ opengl动态贝塞尔曲线有间隙-glEvalCoord1f();,c++,opengl,glut,freeglut,C++,Opengl,Glut,Freeglut,我想有一个程序,你可以选择10个点,然后计算出一条贝塞尔曲线。它似乎工作得非常完美,但显示的曲线有一些间隙。我使用了GL\u LINE\u STRIP这些点怎么可能没有连接 我发现,glEvalCoord1f(u)中有一个非常小的u使间隙变小。 u如何取决于glEvalCoord1f(u)中的控制点和窗口属性 [编辑]添加了线条较粗的屏幕: #包括 #包括 #包括 #包括 #包括 #包括 #include//为Ubuntu兼容性添加了GL前缀 #定义maxp10 闪烁nNumPoints=0

我想有一个程序,你可以选择10个点,然后计算出一条贝塞尔曲线。它似乎工作得非常完美,但显示的曲线有一些间隙。我使用了
GL\u LINE\u STRIP
这些点怎么可能没有连接

我发现,
glEvalCoord1f(u)中有一个非常小的
u
使间隙变小。
u
如何取决于
glEvalCoord1f(u)中的控制点和窗口属性

[编辑]添加了线条较粗的屏幕:

#包括
#包括
#包括
#包括
#包括
#包括
#include//为Ubuntu兼容性添加了GL前缀
#定义maxp10
闪烁nNumPoints=0;
GLfloat ctrlpoints[10][3];
GLdouble mouseOgl[3]={0.0,0.0,0.0};
闪烁的鼠标,鼠标;
bool mousePressed=false;
void GetOGLPos(int x,int y);
void init(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glShadeModel(GLU平面);
//启用评估器
glEnable(GL_地图1_顶点_3);
峡谷(GL_深度);
}
作废显示(作废)
{
int i;
glClear(GL_颜色_缓冲_位| GL_深度_缓冲_位);
GL3F(0.0,0.0,1.0);
如果(鼠标按下(&N点数<最大值){
GetOGLPos(mouseX,mouseY);
标准::cout

尝试将控制点Z坐标归零:

#include <GL/glut.h>
#include <cmath>

const unsigned int MAXP = 10;

GLint nNumPoints = 0;

GLfloat ctrlpoints[10][3];
GLdouble mouseOgl[3] = {0.0,0.0,0.0};
GLint mouseX, mouseY;
bool mousePressed = false;
void GetOGLPos(int x, int y);

void display(void)
{
    glClearColor(1.0, 1.0, 1.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    int w = glutGet( GLUT_WINDOW_WIDTH );
    int h = glutGet( GLUT_WINDOW_HEIGHT );
    glViewport(0, 0, w, h);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    double ar = w / (double)h;
    glOrtho(-2.0*ar, 2.0*ar, -2.0, 2.0, -2.0, 2.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    if(mousePressed && nNumPoints < MAXP)
    {
        GetOGLPos(mouseX, mouseY);
        nNumPoints++;
        ctrlpoints[nNumPoints-1][0] = mouseOgl[0];
        ctrlpoints[nNumPoints-1][1] = mouseOgl[1];
        // this is where the magic happens:
        ctrlpoints[nNumPoints-1][2] = 0;
    }

    //Curves
    glColor3f(0.0, 0.0, 1.0);
    if( nNumPoints == MAXP )
    {
        glMap1f(GL_MAP1_VERTEX_3,   // Type of data generated
            0.0f,                       // Lower u range
            1.0f,                       // Upper u range
            3,                          // Distance between points in the data 3: ...Z-X-Y-Z...
            nNumPoints,                 // number of control points
            &ctrlpoints[0][0]);         // start point

        glBegin(GL_LINE_STRIP);
        float max = pow(MAXP,4.0)*2;  //accuracy of pint calulation?
        for (int i = 0; i <= max; i++) 
            glEvalCoord1f((GLfloat) (i/max));   //high value to avoid gaps?!?
        glEnd();
    }

    //Controllpoints:
    glPointSize(5.0);
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_POINTS);
    for (int i = 0; i < nNumPoints; i++) 
        glVertex3fv(&ctrlpoints[i][0]);
    glEnd();
    //Lines
    glColor3f(0.0, 1.0, 0.0);
    glBegin(GL_LINE_STRIP);
    for (int i = 0; i < nNumPoints; i++) 
        glVertex3fv(&ctrlpoints[i][0]);
    glEnd();
    if( nNumPoints == MAXP ){nNumPoints = 0;}
    glFlush();
}

//handle click events of the mouse
void myMouse(int button, int state, int x, int y)
{
    //mouse coords to gl coords

    mouseX = x;
    mouseY = y;
    switch (button)
    {
    case GLUT_LEFT_BUTTON:
        if(state == GLUT_UP){   //on release left mouse button
            mousePressed = true;
            glutPostRedisplay(); //redisplay and calculate gl coords
        } else {    
            mousePressed = false;
        } 
        break;
    }
}

// detailed information: 
// http://nehe.gamedev.net/article/using_gluunproject/16013/
void GetOGLPos(int x, int y)
{
    //init vars:
    GLint viewport[4];          
    GLdouble modelview[16];     
    GLdouble projection[16];    
    GLfloat winX, winY, winZ;   
    GLdouble posX, posY, posZ;
    //get gl specs
    glGetDoublev( GL_MODELVIEW_MATRIX, modelview );     //get Modelmatrix   
    glGetDoublev( GL_PROJECTION_MATRIX, projection );   //get projection matrix
    glGetIntegerv( GL_VIEWPORT, viewport );             //get viewport values
    //calculate the gl mouseposition
    winX = (float)x;
    winY = (float)viewport[3] - (float)y;
    glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

    gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
    mouseOgl[0] = posX;
    mouseOgl[1] = posY;
    mouseOgl[2] = posZ;
}

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

    glShadeModel(GL_FLAT);
    // Enable the evaluator
    glEnable(GL_MAP1_VERTEX_3);
    glEnable(GL_DEPTH);

    glutDisplayFunc(display);
    glutMouseFunc(myMouse);
    glutMainLoop();
    return 0;
}
#包括
#包括
常量无符号整数MAXP=10;
闪烁nNumPoints=0;
GLfloat ctrlpoints[10][3];
GLdouble mouseOgl[3]={0.0,0.0,0.0};
闪烁的鼠标,鼠标;
bool mousePressed=false;
void GetOGLPos(int x,int y);
作废显示(作废)
{
glClearColor(1.0,1.0,1.0,0.0);
glClear(GL_颜色_缓冲_位| GL_深度_缓冲_位);
int w=glutGet(GLUT\u窗口\u宽度);
int h=glutGet(GLUT\U窗口\U高度);
glViewport(0,0,w,h);
glMatrixMode(GL_投影);
glLoadIdentity();
双ar=w/(双)h;
格洛托(-2.0*ar,2.0*ar,-2.0,2.0,-2.0,2.0);
glMatrixMode(GLU模型视图);
glLoadIdentity();
如果(鼠标按下(&N点数<最大值)
{
GetOGLPos(mouseX,mouseY);
nNumPoints++;
ctrlpoints[nNumPoints-1][0]=鼠标指针[0];
ctrlpoints[nNumPoints-1][1]=鼠标指针[1];
//这就是神奇发生的地方:
ctrlpoints[nNumPoints-1][2]=0;
}
//曲线
GL3F(0.0,0.0,1.0);
如果(nNumPoints==MAXP)
{
glMap1f(GL_MAP1_VERTEX_3,//生成的数据类型
0.0f,//较低的u范围
1.0f,//上限u范围
3,//数据3中点之间的距离:…Z-X-Y-Z。。。
nNumPoints,//控制点的数量
&ctrlpoints[0][0]);//起点
glBegin(GL_线_带);
浮点最大值=功率(最大值,4.0)*2;//品脱计算的准确性?

对于(int i=0;提供了i屏幕,为了在sceen上更清晰,我将“浮点最大值=功率(最大值,4)*2;”更改为“浮点最大值=功率(最大值,2)*2;”你能增加蓝线的大小吗?我能,但它不能解决问题。或者你需要它作为屏幕吗?我添加了第二个屏幕,显示了粗线的问题。仍然一样…是的,就是这样。谢谢你的提示。Z值始终是2,这正是剪裁值。我刚刚从Z中减去了0.1f。
#include <GL/glut.h>
#include <cmath>

const unsigned int MAXP = 10;

GLint nNumPoints = 0;

GLfloat ctrlpoints[10][3];
GLdouble mouseOgl[3] = {0.0,0.0,0.0};
GLint mouseX, mouseY;
bool mousePressed = false;
void GetOGLPos(int x, int y);

void display(void)
{
    glClearColor(1.0, 1.0, 1.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    int w = glutGet( GLUT_WINDOW_WIDTH );
    int h = glutGet( GLUT_WINDOW_HEIGHT );
    glViewport(0, 0, w, h);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    double ar = w / (double)h;
    glOrtho(-2.0*ar, 2.0*ar, -2.0, 2.0, -2.0, 2.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    if(mousePressed && nNumPoints < MAXP)
    {
        GetOGLPos(mouseX, mouseY);
        nNumPoints++;
        ctrlpoints[nNumPoints-1][0] = mouseOgl[0];
        ctrlpoints[nNumPoints-1][1] = mouseOgl[1];
        // this is where the magic happens:
        ctrlpoints[nNumPoints-1][2] = 0;
    }

    //Curves
    glColor3f(0.0, 0.0, 1.0);
    if( nNumPoints == MAXP )
    {
        glMap1f(GL_MAP1_VERTEX_3,   // Type of data generated
            0.0f,                       // Lower u range
            1.0f,                       // Upper u range
            3,                          // Distance between points in the data 3: ...Z-X-Y-Z...
            nNumPoints,                 // number of control points
            &ctrlpoints[0][0]);         // start point

        glBegin(GL_LINE_STRIP);
        float max = pow(MAXP,4.0)*2;  //accuracy of pint calulation?
        for (int i = 0; i <= max; i++) 
            glEvalCoord1f((GLfloat) (i/max));   //high value to avoid gaps?!?
        glEnd();
    }

    //Controllpoints:
    glPointSize(5.0);
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_POINTS);
    for (int i = 0; i < nNumPoints; i++) 
        glVertex3fv(&ctrlpoints[i][0]);
    glEnd();
    //Lines
    glColor3f(0.0, 1.0, 0.0);
    glBegin(GL_LINE_STRIP);
    for (int i = 0; i < nNumPoints; i++) 
        glVertex3fv(&ctrlpoints[i][0]);
    glEnd();
    if( nNumPoints == MAXP ){nNumPoints = 0;}
    glFlush();
}

//handle click events of the mouse
void myMouse(int button, int state, int x, int y)
{
    //mouse coords to gl coords

    mouseX = x;
    mouseY = y;
    switch (button)
    {
    case GLUT_LEFT_BUTTON:
        if(state == GLUT_UP){   //on release left mouse button
            mousePressed = true;
            glutPostRedisplay(); //redisplay and calculate gl coords
        } else {    
            mousePressed = false;
        } 
        break;
    }
}

// detailed information: 
// http://nehe.gamedev.net/article/using_gluunproject/16013/
void GetOGLPos(int x, int y)
{
    //init vars:
    GLint viewport[4];          
    GLdouble modelview[16];     
    GLdouble projection[16];    
    GLfloat winX, winY, winZ;   
    GLdouble posX, posY, posZ;
    //get gl specs
    glGetDoublev( GL_MODELVIEW_MATRIX, modelview );     //get Modelmatrix   
    glGetDoublev( GL_PROJECTION_MATRIX, projection );   //get projection matrix
    glGetIntegerv( GL_VIEWPORT, viewport );             //get viewport values
    //calculate the gl mouseposition
    winX = (float)x;
    winY = (float)viewport[3] - (float)y;
    glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

    gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
    mouseOgl[0] = posX;
    mouseOgl[1] = posY;
    mouseOgl[2] = posZ;
}

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

    glShadeModel(GL_FLAT);
    // Enable the evaluator
    glEnable(GL_MAP1_VERTEX_3);
    glEnable(GL_DEPTH);

    glutDisplayFunc(display);
    glutMouseFunc(myMouse);
    glutMainLoop();
    return 0;
}