我正在实现 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 轴上递减: