改變點座標(0,1,0)改(0,0,1)-->對著z軸轉動(本來對著瘠髓--->對鈕扣)
交換Rotatef和Translatef結果轉動點是對著手旋轉
打光#include <GL/glut.h> ///GL最好用大寫比較不會出bug
void display()
{
GLfloat pos[]={0,0,-200,0}; ///打光座標
glLightfv(GL_LIGHT0,GL_POSITION,pos);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING); ///打光
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); ///打畫面清乾淨
glutSolidTeapot(0.5);
glFlush();
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutCreateWindow("TRT");
glutDisplayFunc(display);
glutMainLoop();
}
#include <GL/glut.h>
float angle=0;///預設角度
void display()
{
GLfloat pos[]={0,0,-200,0};
glLightfv(GL_LIGHT0,GL_POSITION,pos);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
////////////////關節
glTranslatef(0.6,0.2,0);///4.將會轉動的茶壺往右上,掛上去
glRotatef(angle,0,0,1);///3.固定然後以點為基準轉動
glTranslatef(0.3,0,0);///2.把會茶向右移
glutSolidTeapot(0.3);///1.畫出茶壺
glPopMatrix();
glutSolidTeapot(0.5);
glFlush();
}
void timer(int t)///以時間為基準對角度做迴圈
{
glutTimerFunc(20,timer,t+1);
angle++;
glutPostRedisplay();
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutCreateWindow("TRT");
glutDisplayFunc(display);
glutTimerFunc(20,timer,0);
glutMainLoop();
}
#include <GL/glut.h>
float angle=0;
void drawBody()///身體方塊大小設定
{
glPushMatrix();
glScalef(1,0.5,0.5);
glutSolidCube(1);
glPopMatrix();
}
void drawArm()///手臂方塊大小設定
{
glPushMatrix();
glScalef(0.8,0.3,0.3);
glutSolidCube(1);
glPopMatrix();
}
void display()
{
GLfloat pos[]={0,0,-200,0};
glLightfv(GL_LIGHT0,GL_POSITION,pos);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.5,0.0,0);
glRotatef(angle,0,0,1);
glTranslatef(0.3,0,0);
drawArm();
glPopMatrix();
drawBody();
glFlush();
}
void timer(int t)
{
glutTimerFunc(20,timer,t+1);
angle++;
glutPostRedisplay();
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutCreateWindow("TRT");
glutDisplayFunc(display);
glutTimerFunc(20,timer,0);
glutMainLoop();
}

float oldX=0;oldY=y;
void motion(int x,int y)
{
angel -=Y-oldY;
oldY=y;oldX=x;
}
void mouse(int botton,int state,int x,int y)
{
if(state==GLUT_DOWN){oldX=x;oldY=y;}
下面多設定
glutMouseFunc(mouse);
glutMotionFunc(motion);
做右邊的上手臂和下手臂
glPushMatrix();///下
glTranslatef(0.5,0,0);
glRotatef(angle,0,0,1);
glTranslatef(0.25,0,0);
drawArm();
glPopMatrix();
glPushMatrix();///上
glTranslatef(0.25,0.2,0);
glRotatef(angle,0,0,1);
glTranslatef(0.25,0,0);
drawArm();
glPopMatrix();
坐左邊的上手臂和下手臂
glPushMatrix();///上
glTranslatef(-0.5,0,0);
glRotatef(angle,0,0,1);
glTranslatef(-0.25,0,0);
drawArm();
glPopMatrix();
glPushMatrix();///下
glTranslatef(-0.25,0.2,0);
glRotatef(angle,0,0,1);
glTranslatef(-0.25,0,0);
drawArm();
glPopMatrix();
沒有留言:
張貼留言