1.小考:T-R-T 轉動
2. 主題:階層轉動骨架
3.主題:鍵盤按鍵
4.#include <GL/glut.h>
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(255,255,255);
glPushMatrix();
glRotatef(angle,0,0,1);
glColor3f(255,0,0);
glutSolidTeapot(0.2);
glPopMatrix();
glutSwapBuffers();
angle++;
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("06160396-week11");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
在codeblocks打開會出現下圖
5.在CodeBlocks打上
#include <GL/glut.h>
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(255,255,255);
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(0.2,0,0);
glRotatef(angle,0,0,1);
glTranslatef(0.28,0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(0.2,0,0);
glRotatef(angle,0,0,1);
glTranslatef(0.28,0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2,0,0);
glRotatef(angle,0,0,1);
glTranslatef(-0.28,0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(-0.2,0,0);
glRotatef(angle,0,0,1);
glTranslatef(-0.28,0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
angle++;
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("06160396-week11");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
會出現以下如圖
6.用鍵盤來控制手臂
#include <GL/glut.h>
float angle1,angle2,angle3,angle4;
int joinID=1;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(255,255,255);///白色
glutSolidTeapot( 0.3 );///身體
///右邊的手臂
glPushMatrix();
glTranslatef(0.2, 0,0 );///(3)往右移動, 掛在身體的右肩
glRotatef(angle1, 0,0,1);///(2)旋轉
glTranslatef(0.28, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glutSolidTeapot( 0.2 );///手臂
glPushMatrix();
glTranslatef(0.2, 0,0 );
glRotatef(angle2, 0,0,1);
glTranslatef(0.28, 0,0);
glColor3f(255,0,0);///紅色
glutSolidTeapot( 0.2 );///手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2, 0,0 );///(3)往右移動, 掛在身體的右肩
glRotatef(angle3, 0,0,1);///(2)旋轉
glTranslatef(-0.28, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glutSolidTeapot( 0.2 );///手臂
glPushMatrix();
glTranslatef(-0.2, 0,0 );
glRotatef(angle4, 0,0,1);
glTranslatef(-0.28, 0,0);
glColor3f(255,0,0);///紅色
glutSolidTeapot( 0.2 );///手臂
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
///angle++;
}
void motion(int x,int y)
{
if(joinID==1) angle1=x;
if(joinID==2) angle2=x;
if(joinID==3) angle3=x;
if(joinID==4) angle4=x;
display();
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='1') joinID=1;
if(key=='2') joinID=2;
if(key=='3') joinID=3;
if(key=='4') joinID=4;
display();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11 06160396");
glutIdleFunc(display);///Now!!!!
glutDisplayFunc(display);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(255,255,255);
glPushMatrix();
glRotatef(angle,0,0,1);
glColor3f(255,0,0);
glutSolidTeapot(0.2);
glPopMatrix();
glutSwapBuffers();
angle++;
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("06160396-week11");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
在codeblocks打開會出現下圖
5.在CodeBlocks打上
#include <GL/glut.h>
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(255,255,255);
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(0.2,0,0);
glRotatef(angle,0,0,1);
glTranslatef(0.28,0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(0.2,0,0);
glRotatef(angle,0,0,1);
glTranslatef(0.28,0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2,0,0);
glRotatef(angle,0,0,1);
glTranslatef(-0.28,0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(-0.2,0,0);
glRotatef(angle,0,0,1);
glTranslatef(-0.28,0,0);
glColor3f(255,0,0);
glutSolidTeapot(0.2);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
angle++;
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("06160396-week11");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
會出現以下如圖
6.用鍵盤來控制手臂
#include <GL/glut.h>
float angle1,angle2,angle3,angle4;
int joinID=1;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(255,255,255);///白色
glutSolidTeapot( 0.3 );///身體
///右邊的手臂
glPushMatrix();
glTranslatef(0.2, 0,0 );///(3)往右移動, 掛在身體的右肩
glRotatef(angle1, 0,0,1);///(2)旋轉
glTranslatef(0.28, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glutSolidTeapot( 0.2 );///手臂
glPushMatrix();
glTranslatef(0.2, 0,0 );
glRotatef(angle2, 0,0,1);
glTranslatef(0.28, 0,0);
glColor3f(255,0,0);///紅色
glutSolidTeapot( 0.2 );///手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2, 0,0 );///(3)往右移動, 掛在身體的右肩
glRotatef(angle3, 0,0,1);///(2)旋轉
glTranslatef(-0.28, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glutSolidTeapot( 0.2 );///手臂
glPushMatrix();
glTranslatef(-0.2, 0,0 );
glRotatef(angle4, 0,0,1);
glTranslatef(-0.28, 0,0);
glColor3f(255,0,0);///紅色
glutSolidTeapot( 0.2 );///手臂
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
///angle++;
}
void motion(int x,int y)
{
if(joinID==1) angle1=x;
if(joinID==2) angle2=x;
if(joinID==3) angle3=x;
if(joinID==4) angle4=x;
display();
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='1') joinID=1;
if(key=='2') joinID=2;
if(key=='3') joinID=3;
if(key=='4') joinID=4;
display();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11 06160396");
glutIdleFunc(display);///Now!!!!
glutDisplayFunc(display);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
會出現以下圖片
7.把地6個程式碼變成更乾淨
#include <GL/glut.h>
float angle[20];
int joinID=1;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(255,255,255);///白色
glutSolidTeapot( 0.3 );///身體
///右邊的手臂
glPushMatrix();
glTranslatef(0.2, 0,0 );///(3)往右移動, 掛在身體的右肩
glRotatef(angle[1], 0,0,1);///(2)旋轉
glTranslatef(0.28, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glutSolidTeapot( 0.2 );///手臂
glPushMatrix();
glTranslatef(0.2, 0,0 );
glRotatef(angle[2], 0,0,1);
glTranslatef(0.28, 0,0);
glColor3f(255,0,0);///紅色
glutSolidTeapot( 0.2 );///手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2, 0,0 );///(3)往右移動, 掛在身體的右肩
glRotatef(angle[3], 0,0,1);///(2)旋轉
glTranslatef(-0.28, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glutSolidTeapot( 0.2 );///手臂
glPushMatrix();
glTranslatef(-0.2, 0,0 );
glRotatef(angle[4], 0,0,1);
glTranslatef(-0.28, 0,0);
glColor3f(255,0,0);///紅色
glutSolidTeapot( 0.2 );///手臂
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
///angle++;
}
void motion(int x,int y)
{
angle[joinID]=x;
///if(joinID==1) angle1=x;
///if(joinID==2) angle2=x;
///if(joinID==3) angle3=x;
///if(joinID==4) angle4=x;
display();
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='1') joinID=1;
if(key=='2') joinID=2;
if(key=='3') joinID=3;
if(key=='4') joinID=4;
display();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11 06160396");
glutIdleFunc(display);///Now!!!!
glutDisplayFunc(display);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
#include <GL/glut.h>
float angle[20];
int joinID=1;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(255,255,255);///白色
glutSolidTeapot( 0.3 );///身體
///右邊的手臂
glPushMatrix();
glTranslatef(0.2, 0,0 );///(3)往右移動, 掛在身體的右肩
glRotatef(angle[1], 0,0,1);///(2)旋轉
glTranslatef(0.28, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glutSolidTeapot( 0.2 );///手臂
glPushMatrix();
glTranslatef(0.2, 0,0 );
glRotatef(angle[2], 0,0,1);
glTranslatef(0.28, 0,0);
glColor3f(255,0,0);///紅色
glutSolidTeapot( 0.2 );///手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2, 0,0 );///(3)往右移動, 掛在身體的右肩
glRotatef(angle[3], 0,0,1);///(2)旋轉
glTranslatef(-0.28, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸
glColor3f(255,0,0);///紅色
glutSolidTeapot( 0.2 );///手臂
glPushMatrix();
glTranslatef(-0.2, 0,0 );
glRotatef(angle[4], 0,0,1);
glTranslatef(-0.28, 0,0);
glColor3f(255,0,0);///紅色
glutSolidTeapot( 0.2 );///手臂
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
///angle++;
}
void motion(int x,int y)
{
angle[joinID]=x;
///if(joinID==1) angle1=x;
///if(joinID==2) angle2=x;
///if(joinID==3) angle3=x;
///if(joinID==4) angle4=x;
display();
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='1') joinID=1;
if(key=='2') joinID=2;
if(key=='3') joinID=3;
if(key=='4') joinID=4;
display();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11 06160396");
glutIdleFunc(display);///Now!!!!
glutDisplayFunc(display);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
沒有留言:
張貼留言