3

我有一个我正在处理的 OpenGL 项目,它涉及创建一个 2D 平面网格来处理。代码是这样的:

#include <stdlib.h>
#include <glut.h>

GLfloat ctrlpoints[9][9][3] = {
    {{0, 0, 0}, {1, 0, 0}, {2, 0, 0}, {3, 0, 0}, {4, 0, 0}, {5, 0, 0}, {6, 0, 0}, {7, 0, 0}, {8, 0, 0}},
    {{0, 1, 0}, {1, 1, 0}, {2, 1, 0}, {3, 1, 0}, {4, 1, 0}, {5, 1, 0}, {6, 1, 0}, {7, 1, 0}, {8, 1, 0}},
    {{0, 2, 0}, {1, 2, 0}, {2, 2, 0}, {3, 2, 0}, {4, 2, 0}, {5, 2, 0}, {6, 2, 0}, {7, 2, 0}, {8, 2, 0}},
    {{0, 3, 0}, {1, 3, 0}, {2, 3, 0}, {3, 3, 0}, {4, 3, 0}, {5, 3, 0}, {6, 3, 0}, {7, 3, 0}, {8, 3, 0}},
    {{0, 4, 0}, {1, 4, 0}, {2, 4, 0}, {3, 4, 0}, {4, 4, 0}, {5, 4, 0}, {6, 4, 0}, {7, 4, 0}, {8, 4, 0}},
    {{0, 5, 0}, {1, 5, 0}, {2, 5, 0}, {3, 5, 0}, {4, 5, 0}, {5, 5, 0}, {6, 5, 0}, {7, 5, 0}, {8, 5, 0}},
    {{0, 6, 0}, {1, 6, 0}, {2, 6, 0}, {3, 6, 0}, {4, 6, 0}, {5, 6, 0}, {6, 6, 0}, {7, 6, 0}, {8, 6, 0}},
    {{0, 7, 0}, {1, 7, 0}, {2, 7, 0}, {3, 7, 0}, {4, 7, 0}, {5, 7, 0}, {6, 7, 0}, {7, 7, 0}, {8, 7, 0}},
    {{0, 8, 0}, {1, 8, 0}, {2, 8, 0}, {3, 8, 0}, {4, 8, 0}, {5, 8, 0}, {6, 8, 0}, {7, 8, 0}, {8, 8, 0}}
};

void init(void)
{
   glClearColor(0.0, 0.0, 0.0, 0.0);
   glShadeModel(GL_FLAT);
   glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 9, 0, 1, 27, 9, &ctrlpoints[0][0][0]);
   glEnable(GL_MAP2_VERTEX_3);
   glEnable(GL_DEPTH_TEST);
}

void display(void)
{
    int i, j;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity();

    glColor3f(1.0, 1.0, 1.0);
    glPushMatrix ();
    gluLookAt(10,10,10, 0,0,0, 0,0,1);

    /**** We have the option to use this method rather than dealilng with nasty loops *****/
    glMapGrid2f(  50, 0.0, 1.0,  50, 0.0, 1.0);
    glEnable(GL_AUTO_NORMAL);   //this line calculates all normal vectors for the mesh. It has to be commented in when lighting is enabled.

    //glEvalMesh2(GL_LINE, 0, 50, 0, 50);  //This defines a mesh that 5 by 6 vetrices defined by our evaluator defined above
    glEvalMesh2(GL_FILL, 0, 50, 0, 50); //This one fills the the mesh
    /******************************************************/
    glPopMatrix ();
    glutSwapBuffers();
    glutPostRedisplay();
}

void reshape(int w, int h)
{
   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(-10, 10, -10, 10, 0.1, 30);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}

void initlights(void)
{
   GLfloat ambient[] = {0.0, 0.0, 1.0, 1.0};
   GLfloat position[] = {1.0, 1.0, 2.0, 1.0};
   GLfloat mat_diffuse[] = {1.0, 0.0, 0.0, 1.0};
   GLfloat mat_specular[] = {0.0, 1.0, 0.0, 1.0};
   GLfloat mat_shininess[] = {50.0};

   glEnable(GL_LIGHTING);
   glEnable(GL_LIGHT0);

   glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
   glLightfv(GL_LIGHT0, GL_POSITION, position);

   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
   glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
}

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

GLfloat ctrlpoints是一个数组,它决定了网格的大小和每个点的坐标。必须更改glMap2f以适应数组,我相信我使用正确。现在是9x9。但是,当我尝试运行该程序时,什么都没有出现。然而真正奇怪的是,如果我让 ctrlpoints 更小,比如 8x8:

GLfloat ctrlpoints[8][8][3] = {
    {{0, 0, 0}, {1, 0, 0}, {2, 0, 0}, {3, 0, 0}, {4, 0, 0}, {5, 0, 0}, {6, 0, 0}, {7, 0, 0}},
    {{0, 1, 0}, {1, 1, 0}, {2, 1, 0}, {3, 1, 0}, {4, 1, 0}, {5, 1, 0}, {6, 1, 0}, {7, 1, 0}},
    {{0, 2, 0}, {1, 2, 0}, {2, 2, 0}, {3, 2, 0}, {4, 2, 0}, {5, 2, 0}, {6, 2, 0}, {7, 2, 0}},
    {{0, 3, 0}, {1, 3, 0}, {2, 3, 0}, {3, 3, 0}, {4, 3, 0}, {5, 3, 0}, {6, 3, 0}, {7, 3, 0}},
    {{0, 4, 0}, {1, 4, 0}, {2, 4, 0}, {3, 4, 0}, {4, 4, 0}, {5, 4, 0}, {6, 4, 0}, {7, 4, 0}},
    {{0, 5, 0}, {1, 5, 0}, {2, 5, 0}, {3, 5, 0}, {4, 5, 0}, {5, 5, 0}, {6, 5, 0}, {7, 5, 0}},
    {{0, 6, 0}, {1, 6, 0}, {2, 6, 0}, {3, 6, 0}, {4, 6, 0}, {5, 6, 0}, {6, 6, 0}, {7, 6, 0}},
    {{0, 7, 0}, {1, 7, 0}, {2, 7, 0}, {3, 7, 0}, {4, 7, 0}, {5, 7, 0}, {6, 7, 0}, {7, 7, 0}}
};

并像这样修改 glMap2f

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

网格突然出现在屏幕上!如果我也降低,它也会起作用,比如 7x7。

这里到底发生了什么?如果我尝试使网格大于 8x8,它不会出现怎么办?

4

1 回答 1

0

确保GL_MAX_EVAL_ORDER大于 8。评估器又老又笨,所以如果您的 OpenGL 实现仅提供所需的最小值 (8),我不会感到惊讶。

您可以检索GL_MAX_EVAL_ORDERvia的值glGet()

您也可以在通话后检查glGetError()GL_INVALID_VALUEglMap2f()

于 2012-11-27T23:09:35.423 回答