我如何在此代码中使用键盘交互
How I can use Keyboard interaction in this code
我想使用键盘键移动路径上的对象,但在使用键盘功能后对象不在 moving.Please 帮助我 this.after 应用键盘功能对象没有从其移动地方。
i want to add 3 different function for wave/path so keyboard interaction is necessary for me, so that i may able to use different keys for different scenes.
void object()
{
glPushMatrix();
glTranslatef(x, y, 0);
glBegin(GL_LINES);
glColor3f(0, 0, 0);
glVertex2f(-0.3, 0.1);
glVertex2f(0.3, 0.1);
glEnd();
glPopMatrix();
glFlush();
}
void drawsine()
{ glBegin(GL_LINE_STRIP);//Primitive
glColor3f(255, 0, 0);//Set drawing color
int i = 0;
float x = 0, y = 0;
for (x = -5; x < 6; x = x + 0.1)
{ y = (sin(3.142*x)) / 3.142*x;
glVertex2f(x, y);
sinex[i] = x;
siney[i] = y;
i++;}
glEnd();
glFlush();
}
void doFrame(int v) {
//x = sinex[i];
//y = siney[i];
if (x < 5.9)
{
x += 0.1;
y = (sin(3.142*x)) / 3.142*x;
}
glutPostRedisplay();
//glutTimerFunc(x,doFrame,0);
}
void scene1()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
drawsine();
glPopMatrix();
//glScaled(0.3, 0.3, 1);
object();
//glutTimerFunc(30,doFrame,0);
glutSwapBuffers();
}
void exit(void)
{
exit(-1);
}
void myKeyboard(unsigned char key, int x, int y)
{
switch (key) {
case 'h':
{
scene1(); }
break;
case 'e':
{
exit(); }
break;
}
}
void display() {
//glClear(GL_COLOR_BUFFER_BIT);
}
void init() {
glClearColor(1, 1, 1, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5, 7, -5, 5, -1, 1);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowSize(700, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Roller Coaster Simulation");
init();
glutDisplayFunc(display);
glutTimerFunc(200, doFrame, 0);
glutKeyboardFunc(myKeyboard);
glutMainLoop();
return 0;
}
您必须在显示回调中绘制场景 glutDisplayFunc
。
添加场景状态(current_scene
),按键切换状态:
例如
int current_scene = 1;
void myKeyboard(unsigned char key, int x, int y)
{
switch (key) {
case 'h': current_scene = 1; break;
case 'i': current_scene = 2; break;
// [...]
}
}
根据显示回调中的状态绘制场景:
void display() {
glClear(GL_COLOR_BUFFER_BIT);
switch(current_scene) {
case 1: scene1(); break;
case 2: scene2(); break;
// [...]
}
glutSwapBuffers();
glutPostRedisplay();
}
我建议仅在 disaply
中进行缓冲区交换 (glutSwapBuffers
)。从其他函数中删除它。
使用 glutPostRedisplay
将当前 window 标记为需要连续重新显示。
我想使用键盘键移动路径上的对象,但在使用键盘功能后对象不在 moving.Please 帮助我 this.after 应用键盘功能对象没有从其移动地方。
i want to add 3 different function for wave/path so keyboard interaction is necessary for me, so that i may able to use different keys for different scenes.
void object()
{
glPushMatrix();
glTranslatef(x, y, 0);
glBegin(GL_LINES);
glColor3f(0, 0, 0);
glVertex2f(-0.3, 0.1);
glVertex2f(0.3, 0.1);
glEnd();
glPopMatrix();
glFlush();
}
void drawsine()
{ glBegin(GL_LINE_STRIP);//Primitive
glColor3f(255, 0, 0);//Set drawing color
int i = 0;
float x = 0, y = 0;
for (x = -5; x < 6; x = x + 0.1)
{ y = (sin(3.142*x)) / 3.142*x;
glVertex2f(x, y);
sinex[i] = x;
siney[i] = y;
i++;}
glEnd();
glFlush();
}
void doFrame(int v) {
//x = sinex[i];
//y = siney[i];
if (x < 5.9)
{
x += 0.1;
y = (sin(3.142*x)) / 3.142*x;
}
glutPostRedisplay();
//glutTimerFunc(x,doFrame,0);
}
void scene1()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
drawsine();
glPopMatrix();
//glScaled(0.3, 0.3, 1);
object();
//glutTimerFunc(30,doFrame,0);
glutSwapBuffers();
}
void exit(void)
{
exit(-1);
}
void myKeyboard(unsigned char key, int x, int y)
{
switch (key) {
case 'h':
{
scene1(); }
break;
case 'e':
{
exit(); }
break;
}
}
void display() {
//glClear(GL_COLOR_BUFFER_BIT);
}
void init() {
glClearColor(1, 1, 1, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5, 7, -5, 5, -1, 1);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowSize(700, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Roller Coaster Simulation");
init();
glutDisplayFunc(display);
glutTimerFunc(200, doFrame, 0);
glutKeyboardFunc(myKeyboard);
glutMainLoop();
return 0;
}
您必须在显示回调中绘制场景 glutDisplayFunc
。
添加场景状态(current_scene
),按键切换状态:
例如
int current_scene = 1;
void myKeyboard(unsigned char key, int x, int y)
{
switch (key) {
case 'h': current_scene = 1; break;
case 'i': current_scene = 2; break;
// [...]
}
}
根据显示回调中的状态绘制场景:
void display() {
glClear(GL_COLOR_BUFFER_BIT);
switch(current_scene) {
case 1: scene1(); break;
case 2: scene2(); break;
// [...]
}
glutSwapBuffers();
glutPostRedisplay();
}
我建议仅在 disaply
中进行缓冲区交换 (glutSwapBuffers
)。从其他函数中删除它。
使用 glutPostRedisplay
将当前 window 标记为需要连续重新显示。