TRT! 移動!旋轉!移動!
T - 移中心點
R - 旋轉
T - 移物體
#include <GL/glut.h>
#include <stdio.h>
void display();
void timer(int);
void mouse(int, int, int, int);
void motion(int, int);
void drawArm();
void drawBody();
void drawhead();
float anglex=0, angley=0, oldX=0, oldY=0;
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutCreateWindow("TRT 3D");
glutDisplayFunc(display);
glutTimerFunc(20, timer, 0);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
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.1, 0, 0);
glRotatef(anglex, 0, 0, 1); ///version3
glTranslatef(0.25, 0, 0); ///version2
drawArm();
glPushMatrix();
glTranslatef(0.25, 0, 0);
glRotatef(anglex, 0, 0, 1); ///version3
glTranslatef(0.25, 0, 0); ///version2
drawArm();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.1, 0, 0);
glRotatef(-anglex, 0, 0, 1); ///version3
glTranslatef(-0.25, 0, 0); ///version2
drawArm();
glPushMatrix();
glTranslatef(-0.25, 0, 0);
glRotatef(-anglex, 0, 0, 1); ///version3
glTranslatef(-0.25, 0, 0); ///version2
drawArm();
glPopMatrix();
glPopMatrix();
drawBody();
drawhead();
glFlush();
}
void timer(int t)
{
glutTimerFunc(20, timer, 0);
//anglex++;
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
if(state==GLUT_DOWN)
{
oldX=x; oldY=y;
}
}
void motion(int x, int y)
{
anglex+=(x-oldX);
oldX = x;
//angley+=(y-oldY);
//oldY = y;
printf("anglex=%f\n",anglex);
}
void drawBody()
{
glPushMatrix();
glTranslatef(0,-0.5,0);
glRotatef(-90,1,0,0);
glutSolidCone(0.3,1,30,30);
glPopMatrix();
}
void drawArm()
{
glPushMatrix();
glScalef(0.5,0.2,0.2);
glutSolidCube(1);
glPopMatrix();
}
void drawhead()
{
glPushMatrix();
glTranslatef(0,0.4,0);
glScalef(0.5,0.5,0.5);
glutSolidCube(1);
glPopMatrix();
}




沒有留言:
張貼留言