2015年5月27日 星期三

#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("WTF");
    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()
{
    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("WTF");
    glutDisplayFunc(display);
    glutTimerFunc(20,timer,0);
    glutMainLoop();
}
方塊像機械手臂一樣旋轉!!
#include <GL/glut.h>
float angle=0, oldX=0, oldY;

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();
        //glutSolidTeapot(0.3);
    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("WTF");
    glutDisplayFunc(display);
    glutTimerFunc(20, timer, 0);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutMainLoop();
}
類似展現肌肉一樣動作,用方塊來表示。

沒有留言:

張貼留言