S.A.Parkhid
S.A.Parkhid

Reputation: 2868

OpenGL evaluating a plane

I want to evaluate a plane using 4 control points which they are at four corner of the plane. so I have declared them like this :

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

Then I had used this three functions to do evaluation process :

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);

And I have wrote loop below which draws the control points as red dots :

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

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

And output is in image below :

enter image description here

why this occurs , how can i fix that ?

Full source code is listed below :

#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;
}

Upvotes: 0

Views: 624

Answers (1)

S.A.Parkhid
S.A.Parkhid

Reputation: 2868

Finally I had corrected the issue and the result has converted to the image below :

enter image description here

the code has changed to this :

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

And the the paramters are updated like below:

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

And I had called the eval functions like this :

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);

And every thing has been corrected.

Upvotes: 1

Related Questions