OpenGL图形编程(free glut库) 机械臂的实现 - 黑色spider

时间:2024-03-12 16:54:51

1
#include"windows.h" 2 #include "GL/freeglut.h" 3 4 5 6 void ReShape(GLsizei w, GLsizei h); 7 void display(); 8 void KeyAction(unsigned char key, int x, int y); 9 void ChangeData(int id); 10 11 bool isshoudlerUP = true; 12 bool iselbowUP = true; 13 bool isotherUP = true; 14 15 16 17 GLfloat shoudler = 20; // 第一个关节的旋转角 18 GLfloat elbow = -10; // 第2个关节的旋转角 19 GLfloat other = -50; 20 21 22 int main(int argc, char * argv[]) 23 { 24 glutInit(&argc,argv); 25 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); 26 27 glutInitWindowPosition(50, 50); 28 glutInitWindowSize(700, 700); 29 30 glutCreateWindow("机械手臂"); 31 32 glEnable(GL_DEPTH_TEST); // 打开深度测试 33 34 glutReshapeFunc(ReShape); 35 glutDisplayFunc(display); 36 glutKeyboardFunc(KeyAction); 37 38 glutIdleFunc(display); 39 40 glutTimerFunc(50, ChangeData,1); 41 42 glutMainLoop(); 43 return 0; 44 } 45 46 47 void ReShape(GLsizei w, GLsizei h) 48 { 49 glViewport(0,0,w,h); 50 glMatrixMode(GL_PROJECTION); 51 glLoadIdentity(); 52 gluPerspective(60, 1*(GLfloat)w / (GLfloat)h, 1, 30); 53 gluLookAt(6, 6, 6, 1, 0, 0, 0, 1, 0); 54 } 55 56 57 void display() 58 { 59 glColor4f(0, 0, 0, 0); 60 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 61 62 glMatrixMode(GL_MODELVIEW); 63 //glutInitDisplayMode(); 坑 ******* 64 65 glPushMatrix(); 66 // 绘制第一节关节 67 glTranslatef(-1, 0, 0); 68 glRotatef(shoudler, 0, 0,1); 69 glTranslatef(1, 0, 0); 70 71 glPushMatrix(); 72 glColor4f(0, 1, 1, 0); 73 glScalef(2, 0.4, 1); 74 glutWireCube(1); 75 glPopMatrix(); 76 77 // 绘制第二个关节 78 glTranslatef(1, 0, 0); 79 glRotatef(elbow, 0, 0, 1); 80 glTranslatef(1, 0, 0); 81 glPushMatrix(); 82 glColor4f(1, 0, 1, 0); 83 glScalef(2, 0.4, 1); 84 glutWireCube(1); 85 glPopMatrix(); 86 87 // 绘制第三个关节 88 glPushMatrix(); 89 glTranslatef(1, 0, 0); 90 glRotatef(other, 0, 0, 1); 91 glTranslatef(0.5, 0, 0.5); 92 93 glPushMatrix(); 94 glColor4f(1, 0.5, 0.5, 0); 95 glScalef(1, 0.2, 0.5); 96 glutWireCube(1); 97 glPopMatrix(); 98 // (2) 99 glTranslatef(0.5, 0, 0); 100 glRotatef(other, 0, 0, 1); 101 glTranslatef(0.4, 0, 0); 102 103 glPushMatrix(); 104 glColor4f(0, 0, 1, 0); 105 glScalef(0.8, 0.2, 0.3); 106 glutWireCube(1); 107 glPopMatrix(); 108 109 glPopMatrix(); 110 111 // 绘制第四个关节 112 glPushMatrix(); 113 glTranslatef(1, 0, 0); 114 glRotatef(other, 0, 0, 1); 115 glTranslatef(0.5, 0, -0.5); 116 117 glPushMatrix(); 118 glColor4f(1, 0.5, 0.5, 0); 119 glScalef(1, 0.2, 0.5); 120 glutWireCube(1); 121 glPopMatrix(); 122 123 // (2) 124 glTranslatef(0.5, 0, 0); 125 glRotatef(other, 0, 0, 1); 126 glTranslatef(0.4, 0, 0); 127 128 glPushMatrix(); 129 glColor4f(0, 0, 1, 0); 130 glScalef(0.8, 0.2, 0.3); 131 glutWireCube(1); 132 glPopMatrix(); 133 glPopMatrix(); 134 135 136 glPopMatrix(); 137 138 139 140 /**************************坐标**************************************/ 141 { 142 glPushMatrix(); 143 glLoadIdentity(); 144 glColor4f(1, 1, 1, 0); 145 glTranslatef(0, 0, 0); 146 glutWireCube(0.1f); 147 glPopMatrix(); 148 glPushMatrix(); 149 glBegin(GL_LINE_STRIP); 150 glColor4f(1, 0, 0, 0); 151 glTranslatef(0, 0, 0); 152 for (int i = -10; i < 10; i++) 153 { 154 glVertex3f(i, 0, 0); 155 } 156 glEnd(); 157 glFlush(); 158 glPopMatrix(); 159 160 glPushMatrix(); 161 glBegin(GL_LINE_STRIP); 162 glColor4f(0, 1, 0, 0); 163 glTranslatef(0, 0, 0); 164 for (int i = -10; i < 10; i++) 165 { 166 glVertex3f(0, i, 0); 167 } 168 glEnd(); 169 glFlush(); 170 glPopMatrix(); 171 172 glPushMatrix(); 173 glBegin(GL_LINE_STRIP); 174 glColor4f(0, 0, 1, 0); 175 glTranslatef(0, 0, 0); 176 for (int i = -10; i < 10; i++) 177 { 178 glVertex3f(0, 0, i); 179 } 180 glEnd(); 181 glFlush(); 182 glPopMatrix(); 183 /****************************************************************/ 184 } 185 186 glutSwapBuffers(); 187 } 188 189 190 191 void KeyAction(unsigned char key, int x, int y) 192 { 193 switch (key) 194 { 195 case \'a\': 196 if (shoudler < 50) shoudler += 5; break; 197 case \'A\': 198 if(shoudler>-50) shoudler -= 5; break; 199 case \'b\': 200 if (elbow < 40) elbow += 5; break; 201 case \'B\': 202 if (elbow > -50) elbow -= 5; break; 203 case \'c\': 204 if (other < 10) other += 5; break; 205 case \'C\': 206 if (other > -120) other -= 5; break; 207 default: 208 break; 209 } 210 } 211 212 213 void ChangeData(int id) 214 { 215 216 if (isshoudlerUP) 217 { 218 shoudler += 1; 219 if (shoudler > 50) isshoudlerUP = false; 220 }else 221 { 222 shoudler -= 1; 223 if (shoudler < -50) isshoudlerUP = true; 224 } 225 if (iselbowUP) 226 { 227 elbow += 3; 228 if (elbow > 30) iselbowUP = false; 229 } 230 else 231 { 232 elbow -= 3; 233 if (elbow < -60) iselbowUP = true; 234 } 235 236 if (isotherUP) 237 { 238 other += 5; 239 if (other > 10) isotherUP = false; 240 } 241 else 242 { 243 other -= 5; 244 if (other < -120) isotherUP = true; 245 } 246 247 glutTimerFunc(50, ChangeData, 1); 248 }

 

posted on 2020-04-18 16:54  黑色spider  阅读(359)  评论(0编辑  收藏  举报