打開老師給的windows的資料夾裡的transformation,模型換成Al Capon
當把改成glRotatef(角度,0,0,1),去移動角度時會發現轉動會以人物的鈕扣(中心點)旋轉
當把改成glRotatef(角度,0,0,1),然後按右鍵選swap transform/rotate改變順序,移動glTranslatef,再去移動角度時,會發現轉動會以整個視窗的中心點旋轉。
喔喔~~忘記貼上程式碼了....
#include <GL/glut.h>
float angle=0;
void display()
{
GLfloat pos[]={0 ,0 , -1, 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);
glRotatef(angle, 0, 0, 1);
glTranslatef(0.3, 0, 0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSolidTeapot(0.3); ///再畫一個原本的茶壺
glFlush();
}
void timer(int t)
{
glutTimerFunc(20, timer, t+1);
angle++;
glutPostRedisplay();
}
int main(int argc,char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("T-R-T test");
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.6, 0.3, 0.3);
glutSolidCube(1);
glPopMatrix();
}
void display()
{
GLfloat pos[]={0 ,0 , -1, 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); ///把手移到身體的右上
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("T-R-T test");
glutDisplayFunc(display);
glutTimerFunc(20, timer, 0);
glutMainLoop();
}
#include <GL/glut.h>
float angle=0, oldX=0,oldY=0;
void drawBody()
{
glPushMatrix();
glScalef(1, 0.5, 0.5);
glutSolidCube(1);
glPopMatrix();
}
void drawArm()
{
glPushMatrix();
glScalef(0.6, 0.3, 0.3);
glutSolidCube(1);
glPopMatrix();
}
void display()
{
GLfloat pos[]={0 ,0 , -1, 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);
glRotatef(angle, 0, 0, 1);
glTranslatef(0.25, 0, 0);
drawArm(); ///這是右上的手臂
glPushMatrix();
glTranslatef(0.25, 0, 0);
glRotatef(angle, 0, 0, 1);
glTranslatef(0.25, 0, 0);
drawArm(); ///這是右下的手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.5, 0, 0);
glRotatef(-angle, 0, 0, 1);
glTranslatef(-0.25, 0, 0);
drawArm(); ///這是左上的手臂
glPushMatrix();
glTranslatef(-0.25, 0, 0);
glRotatef(-angle, 0, 0, 1);
glTranslatef(-0.25, 0, 0);
drawArm(); ///這是左下的手臂
glPopMatrix();
glPopMatrix();
drawBody();
glFlush();
}
void motion(int x,int y) ///滑鼠互動
{
angle -= y - oldY;
oldY=y; oldX=x;
}
void mouse(int button,int state,int x, int y)
{
if(state==GLUT_DOWN){oldX=x; oldY=y;}
}
void timer(int t)
{
glutTimerFunc(20, timer, t+1);
//angle++;
glutPostRedisplay();
}
int main(int argc,char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("T-R-T test");
glutDisplayFunc(display);
glutTimerFunc(20, timer, 0);
glutMotionFunc(motion);
glutMainLoop();
}
沒有留言:
張貼留言