2020年4月28日 星期二

#include <GL/glut.h>
#include <stdio.h>
///打光0 : 宣告陣列,裡面是光的屬性值
const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 1.0f, 3.0f, 5.0f, 3.0f };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
void arm()
{
    glPushMatrix(); ///備份
        glScalef(1, 0.3 , 0.3); ///y,z變1/3
        glutSolidCube(0.5);
    glPopMatrix();   ///還原
}
float CubeX=0, CubeY=0;
float CubeRotY=0;
float angle=0;
int oldX=0, oldY=0;
int type=2; /// type1轉動 type2移動
void mouse(int button, int state, int x , int y)
{
    oldX=x;
    oldY=y;
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
    glTranslatef(((CubeX/150)+0.2), CubeY/150, 0);
    glTranslatef(((CubeX/150)+0.25), (CubeY)/150, 0);
    glTranslatef(-((CubeX/150)+0.2), (CubeY)/150, 0);
    glTranslatef(-((CubeX/150)+0.25), (CubeY)/150, 0);
    glPopMatrix();

    glPushMatrix();  ///右手
        glTranslatef(((CubeX/150)+0.2), CubeY/150, 0);
        glRotatef(CubeRotY, 0, 0, 1);
        glTranslatef(((CubeX/150)+0.25),(CubeY)/150,0);
        glColor3f(1,0,0);
        arm();

        glPushMatrix();  ///備份
            glTranslatef(((CubeX/150)+0.25), (CubeY)/150, 0);
            glRotatef(CubeRotY, 0, 0, 1);
            glTranslatef(((CubeX/150)+0.25),(CubeY)/150,0);
            glColor3f(1,1,0);
            arm();
        glPopMatrix();
    glPopMatrix();   ///還原
    glPushMatrix();  ///左手
        glTranslatef(-((CubeX/150)+0.2), (CubeY)/150, 0);
        glRotatef(-CubeRotY, 0, 0, 1);
        glTranslatef(-((CubeX/150)+0.25),(CubeY)/150,0);
        glColor3f(1,0,0);
        arm();

        glPushMatrix();  ///備份
            glTranslatef(-((CubeX/150)+0.25), (CubeY)/150, 0);
            glRotatef(-CubeRotY, 0, 0, 1);
            glTranslatef(-((CubeX/150)+0.25),(CubeY)/150,0);
            glColor3f(1,1,0);
            arm();
        glPopMatrix();
    glPopMatrix();   ///還原
    glutSwapBuffers();
}
void keyboard(unsigned char key, int x , int y)
{
    if(key=='e' || key=='E') type=1 ;
    if(key=='w' || key=='W') type=2 ;
    printf("now: %c\n", key); ///印出現在的字母確定沒錯
}
void motion(int x, int y)
{
    if(type==1)
        {
        CubeRotY += (x-oldX);
        }
    if(type==2)
        {
        CubeX += x-oldX;
        CubeY += -(y-oldY);
        }
    oldX=x;
    oldY=y;
    glutPostRedisplay();
}
static void resize(int width, int height)///可改視窗大小
{///保持物體的aspect ratio長寬比,要是正確的
    const float ar=(float) width/(float)height;
    glViewport(0,0,width,height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-ar,ar,-1,+1,-1,+1);///保持物體的aspect ratio長寬比,要是正確的
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week08");
    glutMotionFunc(motion);///加motion函式
    glutMouseFunc(mouse);///記得增加mouse函式
    glutReshapeFunc(resize);///記得在main函式增加
    glutKeyboardFunc(keyboard); ///註冊Keyboard函式
    glutIdleFunc(display);
    glutDisplayFunc(display);
    ///打光需要的3D depth test
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    ///打光1:開啟光線
    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    ///打光2:設定light的設定值
    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    ///打光2.2:要把物體也設定好她的material
    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
    glutMainLoop();
}

沒有留言:

張貼留言