OpenGL评估一架飞机

时间:2013-12-29 12:09:59

标签: opengl

我想用4个控制点评估一个平面,它们位于飞机的四个角落。所以我已经宣布他们是这样的:

GLfloat ctrlpoints[4][3] = {
    { -3, 0, 3 }, { 3, 0, 3 }, { 3, 0, -3 }, { -3, 0, -3 }
};

然后我用这三个函数来做评估过程:

glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 3, 4, &ctrlpoints[0][0]);

glMapGrid2f(5, 0.0, 1.0, 5, 0.0, 1.0);
glEvalMesh2(GL_LINE, 0.0, 5.0, 0.0, 5.0);

我在下面写了一个循环,将控制点绘制为红点:

glPointSize(5.0f);
glColor3f(1, 0, 0);

glBegin(GL_POINTS);
for (i = 0; i < 4; i++)
    glVertex3fv(ctrlpoints[i]);
glEnd();

输出如下图所示:

enter image description here

为什么会发生这种情况,我该如何解决?

完整源代码如下:

#include <glut.h>
#include <stdio.h>

GLfloat ctrlpoints[4][3] = {
    { -3, 0, 3 }, { 3, 0, 3 }, { 3, 0, -3 }, { -3, 0, -3 }
};
void display(void)
{
    int i, j;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1.0, 1.0, 1.0);

    //glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &ctrlpoints[0][0][0]);
    glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 3, 4, &ctrlpoints[0][0]);

    glMapGrid2f(5, 0.0, 1.0, 5, 0.0, 1.0);
    glEvalMesh2(GL_LINE, 0.0, 5.0, 0.0, 5.0);

    glPointSize(5.0f);
    glColor3f(1, 0, 0);

    glBegin(GL_POINTS);
    for (i = 0; i < 4; i++)
        glVertex3fv(ctrlpoints[i]);
    glEnd();

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


    glRotatef(90.0, 1.0, 0.0, 0.0);

}
void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);

    glEnable(GL_MAP2_VERTEX_3);

    glEnable(GL_DEPTH_TEST);
    glShadeModel(GL_FLAT);
}
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(keyInput);
    //glutSpecialFunc(specialKeyInput);
    glutMainLoop();
    return 0;
}

1 个答案:

答案 0 :(得分:1)

最后我更正了问题,结果已转换为下图:

enter image description here

代码已更改为:

GLfloat ctrlpoints[2][2][3] = {
    { { -3, 0, 3 }, { 3, 0, 3 } },
    { { -3, 0, -3 }, { 3, 0, -3 } }
}

并且参数更新如下:

int vstride = 6;
int ustride = 3;
int uorder = 2;
int vorder = 2;
int max1 = 2;
int max2 = 2;

我已经调用了这样的eval函数:

glMap2f(GL_MAP2_VERTEX_3, 0, 1,ustride,uorder,0, 1,
    vstride, vorder, ctrlpoints[0][0]);

glMapGrid2f(un, 0.0, 1.0,vn, 0.0, 1.0);
glEvalMesh2(GL_LINE, 0.0, un, 0.0, vn);

所有事情都得到了纠正。