前提
c++ のopenGLを用いて物体をディスプレイ上に表示させ、クリックにより回転を、キーボード入力により視点の移動を行うプログラムです。エラーは出ないのですがキーボード入力が反映されず困っています。ご教授いただければ幸いです。
実現したいこと
wasdの入力により視点の移動を実現したい。
発生している問題・エラーメッセージ
キーボード入力をしても視点が動かず反応がない。
該当のソースコード
C++
1#include <gl/glut.h> 2#include <stdio.h> 3 4int mouse_rightclick_state = 0; 5int mouse_leftclick_state = 0; 6int up = 0, down = 0, right = 0, left = 0; 7 8void display(void) 9{ 10 int angle_left = mouse_leftclick_state * 30; 11 int angle_right = mouse_rightclick_state * 30; 12 13 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 14 15 if (mouse_leftclick_state >= 1) 16 { 17 glPushMatrix(); 18 glRotatef(angle_left, 0.f, 1.f, 0.f); 19 glColor3f(1.f, 0.f, 0.f); 20 glutWireTeapot(2); 21 glPopMatrix(); 22 } 23 if (mouse_rightclick_state >= 1) 24 { 25 glPushMatrix(); 26 glRotatef(-angle_right, 0.f, 1.f, 0.f); 27 glColor3f(0.f, 0.f, 1.f); 28 glutWireTetrahedron(); 29 glPopMatrix(); 30 } 31 32 glFinish(); 33} 34 35void reshape(int w, int h) 36{ 37 double a = 5 + 0.1 * (right - left), b = 5 + 0.1 * (up - down); 38 glViewport(0, 0, w, h); 39 40 glMatrixMode(GL_PROJECTION); 41 glLoadIdentity(); 42 gluPerspective(30.0, double(w) / h, 0.1, 200.0); 43 44 glMatrixMode(GL_MODELVIEW); 45 glLoadIdentity(); 46 gluLookAt(a, 5.0, b, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); 47} 48 49void mouse(int button, int state, int x, int y) { 50 if (button == GLUT_LEFT_BUTTON) { 51 printf("(%d, %d) で左クリックしました\n", x, y); 52 mouse_leftclick_state++; 53 } 54 else if (button == GLUT_RIGHT_BUTTON) { 55 printf("(%d, %d) で右クリックしました\n", x, y); 56 mouse_rightclick_state++; 57 } 58 display(); 59} 60 61void keyboard(unsigned char key, int x, int y) { 62 switch (key) 63 { 64 case 'w': 65 up++; 66 break; 67 case 's': 68 down++; 69 break; 70 case 'a': 71 left++; 72 break; 73 case 'd': 74 right++; 75 break; 76 77 default: 78 break; 79 } 80 display(); 81} 82 83 84void otherInit(void) 85{ 86 glClearColor(1.f, 1.f, 1.f, 1.f); 87 glClearDepth(1.f); 88 glEnable(GL_DEPTH_TEST); 89} 90 91int main(int argc, char* argv[]) 92{ 93 glutInit(&argc, argv); 94 glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH); 95 glutInitWindowSize(640, 480); 96 glutCreateWindow("Hello!"); 97 98 glutDisplayFunc(display); 99 glutReshapeFunc(reshape); 100 glutMouseFunc(mouse); 101 glutKeyboardFunc(keyboard); 102 103 otherInit(); 104 105 glutMainLoop(); 106 107 return 0; 108}
試したこと
変数名を変更した。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/05/25 08:56
2022/05/25 08:59
2022/05/25 09:07 編集
2022/05/25 09:09