c++openglglutnurbs

OpenGL NURBS surfaces


I am implementing NURBS surfaces. All I want is on each mouse click there is decrements on Y-axis, so it looks like there is a weight of Sun or other planets.

#include <stdlib.h>
#include <glut.h>
int  PI = 3.145;
int y = 0;

here i am trying to do the same thing but there is no change in output

void myMouse(int button, int state, int x, int y) {
    if (state == GLUT_LEFT_BUTTON && GLUT_DOWN) {
        y++;
    }
    glutPostRedisplay();
}

Code continue here

GLfloat ctrlpoints[4][4][3] = {
    {{-3.5, 1.0, -4.5},  {-0.5, 1.0,-4.5 }, {0.5, 1.0, -4.5 },   {3.5, 1.0,-4.5}},
    {{-3.5, 1.0, -0.5},  {-0.5, -2.0 - y,-0.5 }, {0.5, -2.0 - y, -0.5 }, {3.5, 1.0,-0.5}},
    {{-3.5, 1.0,  0.5},  {-0.5, 1.0, 0.5 }, {0.5, 1.0,  0.5 },   {3.5, 1.0, 0.5}},
    {{-3.5, 1.0,  4.5},  {-0.5, 1.0, 4.5 }, {0.5, 1.0,  4.5 },   {3.5, 1.0, 4.5}} 
};

void Sun() {
    glPushMatrix();
    glTranslatef(0, 1, 0);
    glRotatef(2*PI, 1, 0, 0);
    glutSolidSphere(0.5, 20, 20);
    glPopMatrix();
}

void display(void)
{
    int i, j;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(0.0, 1.0, 1.0);
    glPushMatrix();
    glRotatef(45.0, 1.0, 1.0, 1.0);
    for (j = 0; j <= 30; j++) {
        glBegin(GL_LINE_STRIP);
        for (i = 0; i <= 100; i++)
            glEvalCoord2f((GLfloat)i / 100.0, (GLfloat)j / 30.0);
        glEnd();
        glBegin(GL_LINE_STRIP);
        for (i = 0; i <= 100; i++)
            glEvalCoord2f((GLfloat)j / 30.0, (GLfloat)i / 100.0);
        glEnd();
    }
    glPopMatrix();

    glPushMatrix();
    glColor3f(1, 1, 0);
    Sun();
    glPopMatrix();
    glFlush();
}

void init(void)
{
    glClearColor(1.0, 1.0, 1.0, 0.0);
    glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &ctrlpoints[0][0][0]);
    glEnable(GL_MAP2_VERTEX_3);
    glMapGrid2f(20, 0.0, 1.0, 20, 0.0, 1.0);
    glEnable(GL_DEPTH_TEST);
    glShadeModel(GL_FLAT);
}

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

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);
    glutMouseFunc(myMouse);
    Sun();
    glutMainLoop();
    return 0;
}

This is my current hardcoded output of this code i want this to decrements on y axis every time i click mouse button:

current output


Solution

  • ctrlpoints is a global variable. Once it is initialized it can be changed only by an assignment. There is no magic dynamic dependency on the variable y.

    You have to change the filed of ctrlpoints by an assignment. After changing the content of ctrlpoints, the two-dimensional evaluator has to be redefined.

    The correct condition which is true, when the left mouse button is presse is button == GLUT_LEFT_BUTTON && state == GLUT_DOWN.

    Note since y is a name of a formal parameter in the function signature of myMouse(int button, int state, int x, int y), y ++ (inside myMouse) won't change the the vlaue of the globale variable y, but it will change the value of the parameter y.

    Add a function initMap and change the functions myMouse and init as folows:

    void initMap(void)
    {
        glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &ctrlpoints[0][0][0]);
        glEnable(GL_MAP2_VERTEX_3);
        glMapGrid2f(20, 0.0, 1.0, 20, 0.0, 1.0);
    }
    
    void myMouse(int button, int state, int px, int py)
    {
        if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
        {
            ctrlpoints[1][1][1] -= 1.0f;
            ctrlpoints[1][2][1] -= 1.0f;
        }
        initMap();
        glutPostRedisplay();
    }
    
    void init(void)
    { 
        //glClearColor(1.0, 1.0, 1.0, 0.0);
        glEnable(GL_DEPTH_TEST);
        glShadeModel(GL_FLAT);
        initMap();
    } 
    

    Preview: