我正在实现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轴上递减:
答案 0 :(得分:1)
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
并将功能myMouse
和init
更改为以下内容:
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();
}
预览: