1

我正在实现 NURBS 曲面。我想要的只是在每次鼠标点击时 Y 轴上都有减量,所以看起来有太阳或其他行星的重量。

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

在这里我正在尝试做同样的事情,但输出没有变化

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

代码在这里继续

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

这是此代码的当前硬编码输出,我希望每次单击鼠标按钮时都在 y 轴上递减:

电流输出

4

1 回答 1

2

ctrlpoints是一个全局变量。一旦它被初始化,它只能通过赋值来改变。变量没有神奇的动态依赖y

ctrlpoints您必须通过分配更改归档。改变 的内容后ctrlpoints,必须重新定义二维求值器。

当按下鼠标左键时,正确的条件是button == GLUT_LEFT_BUTTON && state == GLUT_DOWN.

请注意,由于y是函数签名中的形式参数的名称myMouse(int button, int state, int x, int y)y ++(内部myMouse)不会更改全局变量y的值,但会更改参数的值y

添加一个函数initMap并更改函数myMouseinit如下所示:

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

预习:

于 2018-12-26T16:58:06.853 回答