顯示具有 06160866_杜嘉文 標籤的文章。 顯示所有文章
顯示具有 06160866_杜嘉文 標籤的文章。 顯示所有文章

2019年5月31日 星期五

World.Execute(Me); Week15

運鏡
1.下載
2.
3.下載


4.剛開始第一步,做出白白的人
5.打光
6.

7.

#include <GL/glut.h>
#include "glm.h" ///for glmReadOBJ(), glmDraw(), glmUnitized()....
GLMmodel * pmodel=NULL;///NOW
void drawmodel(void)
{
    if (!pmodel) {
pmodel = glmReadOBJ("data/al.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
    }
    glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
}
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    drawmodel();
    glutSwapBuffers();
}
#include <math.h>
float eyeX=1, eyeY=0, eyeZ=0;
void timer(int t)///NOW3
{
    glutTimerFunc(33, timer, t+1);///NOW3
    float angle=t/180.0*3.1415926;///NOW3 算出一個角度
    eyeX=cos(angle); eyeZ=sin(angle);///NOW3

    glMatrixMode(GL_PROJECTION);///NOW4
    glLoadIdentity();///NOW4
    gluPerspective(60, 1, 0.001, 1000);///NOW5
///glOrtho(-1,+1, -1,+1, -10,+10);///NOW4 可以看到範圍比較大的投影法

    glMatrixMode(GL_MODELVIEW);///NOW3
    glLoadIdentity();///NOW3
    gluLookAt(eyeX, eyeY, eyeZ, ///NOW3 LookAt的 eye
              0.0, 0.6, 0.0, ///NOW3 LookAt的 center
              0, 1, 0);///NOW3 LookAt的 up

    glutPostRedisplay();///NOW3 Re-display
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(500,500);
    glutCreateWindow("week15");

    glutDisplayFunc(display);
    glutTimerFunc(33, timer, 0);///NOW3

    GLfloat light_pos[] = { 0.0, 0.0, 1.0, 0.0 };///NOW2 打光的位置
    glEnable(GL_DEPTH_TEST);///NOW2 有立體的功能
    glEnable(GL_LIGHT0);    ///NOW2 打光後變立體的彩色的
    glEnable(GL_LIGHTING);  ///NOW2 打光後變立體的彩色的
    glLightfv(GL_LIGHT0, GL_POSITION, light_pos);///NOW2 打光的位置

    glutMainLoop();
}

2019年5月24日 星期五

World.Execute(Me); Week14

TIMER
1.
2.
3.
4.
5.
6.
7.
8.
#include <GL/glut.h>
#include "glm.h"
#include <stdio.h>
GLMmodel* pmodel = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel21 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel31 = NULL;
FILE * fout=NULL;///(1) ///NOW4
FILE * fin =NULL;///(1) ///NOW5


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[] = { 2.0f, 5.0f, -5.0f, 0.0f };

/*const GLfloat light2_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light2_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light2_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light2_position[] = { 0.0f, 0.0f, 5.0f, 0.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 };

float angle[20];
float oldAngle[20];///NOW4
float newAngle[20];
int angleID=1;

void display(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
        glTranslatef(0, 0.3,0 );
        glScalef(0.5,0.5,0.5);                         //備份矩陣
        glRotatef(1, 1,1,1);///NOW2 ///(2)旋轉關節1
        if (!pmodel) {                              ///now要有3D Model
        pmodel = glmReadOBJ("data/body.obj");    ///now要有3D Model
        if (!pmodel) exit(0);                       ///now要有3D Model
        glmUnitize(pmodel);                         ///now要有3D Model
        glmFacetNormals(pmodel);                    ///now要有3D Model
        glmVertexNormals(pmodel, 90.0);             ///now要有3D Model
        }                                           ///now要有3D Model
        glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL); ///now要有3D Model
    glPopMatrix();                          //還原矩陣

    glPushMatrix();
        glTranslatef(-0.35,0.38,0 );
        glScalef(0.25,0.25,0.25);                         //備份矩陣
        glRotatef(angle[1],1,0,0);///NOW2 ///(2)旋轉關節1
        glTranslatef(0.2, -0.5,0 );
        if (!pmodel2) {                              ///now要有3D Model
        pmodel2 = glmReadOBJ("data/lefthand.obj");    ///now要有3D Model
        if (!pmodel2) exit(0);                       ///now要有3D Model
        glmUnitize(pmodel2);                         ///now要有3D Model
        glmFacetNormals(pmodel2);                    ///now要有3D Model
        glmVertexNormals(pmodel2, 90.0);             ///now要有3D Model
        }                                           ///now要有3D Model
        glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL); ///now要有3D Model
    glPopMatrix();

    glPushMatrix();
        glTranslatef(0.35,0.38,0 );
        glScalef(0.25,0.25,0.25);                         //備份矩陣
        glRotatef(angle[2], 1,0,0);///NOW2 ///(2)旋轉關節1
        glTranslatef(-0.2, -0.5,0 );
        if (!pmodel21) {                              ///now要有3D Model
        pmodel21 = glmReadOBJ("data/righthand.obj");    ///now要有3D Model
        if (!pmodel21) exit(0);                       ///now要有3D Model
        glmUnitize(pmodel21);                         ///now要有3D Model
        glmFacetNormals(pmodel21);                    ///now要有3D Model
        glmVertexNormals(pmodel21, 90.0);             ///now要有3D Model
        }                                           ///now要有3D Model
        glmDraw(pmodel21, GLM_SMOOTH | GLM_MATERIAL); ///now要有3D Model
    glPopMatrix();

    glPushMatrix();
        glTranslatef(-0.15, -0.1,0 );
        glScalef(0.7,0.7,0.7);                         //備份矩陣
        glRotatef(angle[3], 1,0,0);///NOW2 ///(2)旋轉關節1
        glTranslatef(0, -0.25,0 );
        if (!pmodel3) {                              ///now要有3D Model
        pmodel3 = glmReadOBJ("data/leftleg.obj");    ///now要有3D Model
        if (!pmodel3) exit(0);                       ///now要有3D Model
        glmUnitize(pmodel3);                         ///now要有3D Model
        glmFacetNormals(pmodel3);                    ///now要有3D Model
        glmVertexNormals(pmodel3, 90.0);             ///now要有3D Model
        }                                           ///now要有3D Model
        glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL); ///now要有3D Model
    glPopMatrix();

    glPushMatrix();
        glTranslatef(0.15, -0.1,0 );
        glScalef(0.7  ,0.7,0.7);                         //備份矩陣
        glRotatef(angle[4], 1,0,0);///NOW2 ///(2)旋轉關節1
        glTranslatef(0, -0.25,0 );
        if (!pmodel31) {                              ///now要有3D Model
        pmodel31 = glmReadOBJ("data/rightleg.obj");    ///now要有3D Model
        if (!pmodel31) exit(0);                       ///now要有3D Model
        glmUnitize(pmodel31);                         ///now要有3D Model
        glmFacetNormals(pmodel31);                    ///now要有3D Model
        glmVertexNormals(pmodel31, 90.0);             ///now要有3D Model
        }                                           ///now要有3D Model
        glmDraw(pmodel31, GLM_SMOOTH | GLM_MATERIAL); ///now要有3D Model
    glPopMatrix();

    glutSwapBuffers();
}

int oldX;///NOW
void mouse(int button, int state, int x, int y)///NOW
{
    oldX = x;///NOW
}
void motion(int x, int y)///NOW
{
    angle[angleID] += (x-oldX);///NOW ///NOW3
    oldX = x;///NOW

    for(int i=0; i<20; i++){///NOW4
        printf( "%.1f ", angle[i]);///NOW4
    }
    printf("\n");///NOW4
    display();///NOW
}

void timer(int t)///NOW3
{
    glutTimerFunc(33, timer, t+1);///NOW3 ///NOW4 快一點
    if(t%30==0){///NOW4 每 30frame要讀新資料
        if(fin==NULL) fin=fopen("motion.txt", "r");///NOW3
        for(int i=0; i<20; i++){///NOW3
            oldAngle[i] = newAngle[i]; ///NOW4
            fscanf(fin, "%f", &newAngle[i]);///NOW4 新資料
        }
    }
    float alpha = (t%30)/30.0;///NOW4 今天第1節有寫過它,內插
    for(int i=0;i<20;i++){///NOW4
        angle[i] = alpha*newAngle[i] + (1-alpha)*oldAngle[i];///NOW4
    }
    glutPostRedisplay();///NOW4
}

void keyboard(unsigned char key, int x, int y)///NOW3
{
    if(key=='1') angleID=1;///NOW3
    if(key=='2') angleID=2;///NOW3
    if(key=='3') angleID=3;///NOW3
    if(key=='4') angleID=4;///NOW3
    if(key=='w' || key=='W'){///NOW4 小寫的 w
        if(fout==NULL) fout=fopen("motion.txt", "w+");///NOW4
        for(int i=0; i<20; i++){///NOW4
            fprintf( fout, "%.1f ", angle[i]);///NOW4
        }
        printf("\n");///NOW4
        fprintf(fout, "\n");///NOW4
    }
    if(key=='r'){///NOW2
        if(fin==NULL) fin=fopen("motion.txt", "r");///NOW2
        for(int i=0; i<20; i++){///NOW2
            fscanf(fin, "%f", &angle[i]);///NOW2
        }
    }
    if(key=='t' || key=='p'){///NOW3 ///啟動timer 或是 play
        glutTimerFunc(33, timer, 0);///NOW3 ///NOW4 變快了
        if(fin==NULL) fin=fopen("motion.txt", "r");///NOW3
        for(int i=0; i<20; i++){///NOW3
            fscanf(fin, "%f", &newAngle[i]);///NOW3 ///NOW4 新角度
        }
    }
    glutPostRedisplay();///和 display()很像,但更好 ///NOW5
}

int main(int argc,char**argv){
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("5/23");

    glutDisplayFunc(display);
    glutIdleFunc(display);
///OpenGL設定
    glClearColor(1,1,1,1);
    //glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glutKeyboardFunc(keyboard);///NOW3
    glutMouseFunc(mouse);///NOW
    glutMotionFunc(motion);

    glEnable(GL_LIGHT0);
    /*glEnable(GL_LIGHT1);*/
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    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);

   /* glLightfv(GL_LIGHT1, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT1, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT1, GL_POSITION, light_position);*/

    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();
}

2019年5月17日 星期五

World.Execute(Me); Week13

剛開始的學習

1.
2.

TRT
1.
2.右手
再加關節
3.

雙手一起來
4.

雙手關節旋轉
5.

雙手旋轉讀數
6.

雙手關節旋轉讀數
7.


#include <stdio.h> ///(0)  ///NOW4
#include <GL/glut.h>
FILE * fout=NULL;///(1) ///NOW4
FILE * fin =NULL;///(1) ///NOW5
float angle[20];///NOW
int angleID=1;///第幾個要轉動的關節 ///NOW3
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();///body
        glColor3f(1,1,1);///white
        glutSolidTeapot( 0.3 );///body

        glPushMatrix();///right Arm
            glTranslatef(0.18, 0, 0);///(3)掛到肩上
            glRotatef(angle[1], 0,0,1);///(2)轉動 ///NOW
            glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心

            glColor3f(1,0,0);///red
            glutSolidTeapot( 0.2 );///right upper arm

            glPushMatrix();///NOW2 長出下手臂
                glTranslatef(0.18, 0, 0);///(3)掛到肘上
                glRotatef(angle[2], 0,0,1);///(2)轉動 ///NOW
                glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心

                glColor3f(1,0,0);///red
                glutSolidTeapot( 0.2 );///right lower arm

            glPopMatrix();
        glPopMatrix();

        glPushMatrix();///left Arm
            glTranslatef(-0.18, 0, 0);///(3)掛到肩上
            glRotatef(angle[3], 0,0,1);///(2)轉動 ///NOW
            glTranslatef(-0.18, 0, 0);///(1)改變旋轉中心,放到中心

            glColor3f(1,0,0);///red
            glutSolidTeapot( 0.2 );///left upper arm

            glPushMatrix();///NOW2 長出下手臂
                glTranslatef(-0.18, 0, 0);///(3)掛到肘上
                glRotatef(angle[4], 0,0,1);///(2)轉動 ///NOW
                glTranslatef(-0.18, 0, 0);///(1)改變旋轉中心,放到中心

                glColor3f(1,0,0);///red
                glutSolidTeapot( 0.2 );///left lower arm

            glPopMatrix();
        glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
}
int oldX;///NOW
void mouse(int button, int state, int x, int y)///NOW
{
    oldX = x;///NOW
}
void motion(int x, int y)///NOW
{
    angle[angleID] += (x-oldX);///NOW ///NOW3
    oldX = x;///NOW
    if(fout==NULL) fout=fopen("motion.txt", "w+");///NOW4
    for(int i=0; i<20; i++){///NOW4
        printf( "%.1f ", angle[i]);///NOW4
        fprintf( fout, "%.1f ", angle[i]);///NOW4
    }
    printf("\n");///NOW4
    fprintf(fout, "\n");///NOW4
    display();///NOW
}
void keyboard(unsigned char key, int x, int y)///NOW3
{
    if(key=='1') angleID=1;///NOW3
    if(key=='2') angleID=2;///NOW3
    if(key=='3') angleID=3;///NOW3
    if(key=='4') angleID=4;///NOW3
    if(key=='w' || key=='W'){///NOW4 小寫的 w
        if(fout==NULL) fout=fopen("motion.txt", "w+");///NOW4
        for(int i=0; i<20; i++){///NOW4
            printf( "%.1f ", angle[i]);///NOW4
            fprintf( fout, "%.1f ", angle[i]);///NOW4
        }
        printf("\n");///NOW4
        fprintf(fout, "\n");///NOW4
    }
    if(key=='r'){///NOW5
        if(fin==NULL) fin = fopen("motion.txt", "r");///NOW5
        for(int i=0; i<20;i++){///NOW5
            fscanf(fin, "%f", &angle[i]);///NOW5
        }///NOW5
    }
    glutPostRedisplay();///和 display()很像,但更好 ///NOW5
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);///我們可以開很大的視窗
    glutCreateWindow("week 13 motion TRT angle file");
    glutKeyboardFunc(keyboard);///NOW3
    glutMouseFunc(mouse);///NOW
    glutMotionFunc(motion);///NOW
    glutDisplayFunc(display);

    glutMainLoop();
}

2019年5月10日 星期五

World.Execute(Me); Week12

1.
2.
3.
4.
5.
6.
7.
8.
9.


#include "CMP3_MCI.h"///fb下載放到程式專案目錄
CMP3_MCI myMP3;///宣告物件變數myMP3
int     a;
float   b;
char    c;
double  d;
/*
 * GLUT Shapes Demo
 *
 * Written by Nigel Stewart November 2003
 *
 * This program is test harness for the sphere, cone
 * and torus shapes in GLUT.
 *
 * Spinning wireframe and smooth shaded shapes are
 * displayed until the ESC or q key is pressed.  The
 * number of geometry stacks and slices can be adjusted
 * using the + and - keys.
 */

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include <stdlib.h>

static int slices = 16;
static int stacks = 16;

/* GLUT callback Handlers */

static void resize(int width, int height)
{
    const float ar = (float) width / (float) height;

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity() ;
}

static void display(void)
{
    const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
    const double a = t*90.0;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3d(1,0,0);

    glPushMatrix();
        glTranslated(-2.4,1.2,-6);
        glRotated(60,1,0,0);
        glRotated(a,0,0,1);
        glutSolidSphere(1,slices,stacks);
    glPopMatrix();

    glPushMatrix();
        glTranslated(0,1.2,-6);
        glRotated(60,1,0,0);
        glRotated(a,0,0,1);
        glutSolidCone(1,1,slices,stacks);
    glPopMatrix();

    glPushMatrix();
        glTranslated(2.4,1.2,-6);
        glRotated(60,1,0,0);
        glRotated(a,0,0,1);
        glutSolidTorus(0.2,0.8,slices,stacks);
    glPopMatrix();

    glPushMatrix();
        glTranslated(-2.4,-1.2,-6);
        glRotated(60,1,0,0);
        glRotated(a,0,0,1);
        glutWireSphere(1,slices,stacks);
    glPopMatrix();

    glPushMatrix();
        glTranslated(0,-1.2,-6);
        glRotated(60,1,0,0);
        glRotated(a,0,0,1);
        glutWireCone(1,1,slices,stacks);
    glPopMatrix();

    glPushMatrix();
        glTranslated(2.4,-1.2,-6);
        glRotated(60,1,0,0);
        glRotated(a,0,0,1);
        glutWireTorus(0.2,0.8,slices,stacks);
    glPopMatrix();

    glutSwapBuffers();
}

#include <mmsystem.h>///簡單鋼琴
static void key(unsigned char key, int x, int y)
{
    if(key=='1') PlaySoundA("C:\\Users\\user\\Downloads\\music\\do.wav",NULL,SND_ASYNC);///數字鍵發聲音
    if(key=='2') PlaySoundA("C:\\Users\\user\\Downloads\\music\\re.wav",NULL,SND_ASYNC);
    if(key=='3') PlaySoundA("C:\\Users\\user\\Downloads\\music\\mi.wav",NULL,SND_ASYNC);
    if(key=='4') PlaySoundA("C:\\Users\\user\\Downloads\\music\\fa.wav",NULL,SND_ASYNC);
    if(key=='5') PlaySoundA("C:\\Users\\user\\Downloads\\music\\so.wav",NULL,SND_ASYNC);
    switch (key)
    {
        case 27 :
        case 'q':
            exit(0);
            break;

        case '+':
            slices++;
            stacks++;
            break;

        case '-':
            if (slices>3 && stacks>3)
            {
                slices--;
                stacks--;
            }
            break;
    }

    glutPostRedisplay();
}

static void idle(void)
{
    glutPostRedisplay();
}

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[] = { 2.0f, 5.0f, 5.0f, 0.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 };

/* Program entry point */
#include<stdio.h>
#include <mmsystem.h>
int main(int argc, char *argv[])
{
    myMP3.Load("C:\\Users\\user\\Downloads\\3983582900.mp3");///Load讀入mp3檔
    myMP3.Play();///play播放
    ///PlaySoundA("C:\\Users\\user\\Downloads\\Wave.wav",NULL,SND_ASYNC);///不延遲播放
    printf("邊播聲音下面繼續開視窗");
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT Shapes");

    glutReshapeFunc(resize);
    glutDisplayFunc(display);
    glutKeyboardFunc(key);
    glutIdleFunc(idle);

    glClearColor(1,1,1,1);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    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);

    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();

    return EXIT_SUCCESS;
}

2019年5月3日 星期五

World.Execute(Me); Week11

旋轉 TRT

1.
2.
3.













4.
5.


#include <GL/glut.h>
float angle1=0, angle2=0, angle3=0, angle4=0;///NOW ///NOW6 配合鍵盤
int jointID=1;///NOW6 配合鍵盤
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(255,255,255);///白色的
    glutSolidTeapot( 0.3 );///身體

    glPushMatrix();///右邊手臂
        glTranslatef(0.2, 0, 0);///(3) 掛在身體的右肩
        glRotatef(angle1, 0,0,1);///(2) 旋轉 ///NOW6 配合鍵盤
        glTranslatef(0.15, 0,0);///(1) 把茶壼柄,移動到畫面的中心,當旋轉軸

        glColor3f(1,0,0);///紅色的
        glutSolidTeapot( 0.15 );///右手臂

        glPushMatrix();///NOW2
            glTranslatef(0.2, 0, 0);///(3) 掛在身體的右肘///NOW2
            glRotatef(angle2, 0,0,1);///(2) 旋轉///NOW2 ///NOW6 配合鍵盤
            glTranslatef(0.15, 0,0);///(1) 把茶壼柄,移動到畫面的中心,當旋轉軸///NOW2

            glColor3f(1,0,0);///紅色的///NOW2
            glutSolidTeapot( 0.15 );///右下手臂///NOW2
        glPopMatrix();///NOW2

    glPopMatrix();

    glPushMatrix();///左邊手臂  ///NOW4 複製左半邊
        glTranslatef(-0.2, 0, 0);///(3) 掛在身體的左肩 ///NOW4 複製左半邊
        glRotatef(-angle3, 0,0,1);///(2) 旋轉 ///NOW4 複製左半邊 ///NOW5 angle變負的
        glTranslatef(-0.15, 0,0);///(1) 把茶壼柄,移動到畫面的中心,當旋轉軸 ///NOW4 複製左半邊

        glColor3f(1,0,0);///紅色的 ///NOW4 複製左半邊
        glutSolidTeapot( 0.15 );///左手臂 ///NOW4 複製左半邊

        glPushMatrix();///NOW2 ///NOW4 複製左半邊
            glTranslatef(-0.2, 0, 0);///(3) 掛在身體的左肘///NOW2 ///NOW4 複製左半邊
            glRotatef(-angle4, 0,0,1);///(2) 旋轉///NOW2 ///NOW4 複製左半邊 ///NOW5 angle變負的
            glTranslatef(-0.15, 0,0);///(1) 把茶壼柄,移動到畫面的中心,當旋轉軸///NOW2 ///NOW4 複製左半邊

            glColor3f(1,0,0);///紅色的///NOW2 ///NOW4 複製左半邊
            glutSolidTeapot( 0.15 );///左下手臂///NOW2 ///NOW4 複製左半邊
        glPopMatrix();///NOW2

    glPopMatrix();

    glutSwapBuffers();
    ///angle++;///NOW ///NOW3
}
int oldX, oldY;///NOW5
void motion(int x, int y)///NOW3 用滑鼠mouse來控制angle
{
    if(jointID==1) angle1 += (x-oldX);///NOW3 用滑鼠mouse來控制angle ///NOW6 配合鍵盤
    if(jointID==2) angle2 += (x-oldX);///NOW3 用滑鼠mouse來控制angle ///NOW6 配合鍵盤
    if(jointID==3) angle3 += (x-oldX);///NOW3 用滑鼠mouse來控制angle ///NOW6 配合鍵盤
    if(jointID==4) angle4 += (x-oldX);///NOW3 用滑鼠mouse來控制angle ///NOW6 配合鍵盤
    oldX=x;///NOW5
    display();///NOW3 用滑鼠mouse來控制angle
}
void mouse(int button, int state, int x, int y)///NOW5 要知道mouse的按鍵狀況
{///NOW5 要知道mouse的按鍵狀況
    oldX = x; oldY = y;///NOW5 要知道mouse的按鍵狀況, 按下去時,就記得 oldX
}
void keyboard(unsigned char key, int x, int y)///NOW6 配合鍵盤
{
    if(key=='1') jointID=1;///NOW6 配合鍵盤
    if(key=='2') jointID=2;///NOW6 配合鍵盤
    if(key=='3') jointID=3;///NOW6 配合鍵盤
    if(key=='4') jointID=4;///NOW6 配合鍵盤
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH );
    glutCreateWindow("week11 TRT");

    glutIdleFunc(display);///NOW
    glutDisplayFunc(display);
    glutMotionFunc(motion);///NOW3 用滑鼠mouse來控制angle
    glutMouseFunc(mouse);///NOW5 要知道mouse的按鍵狀況
    glutKeyboardFunc(keyboard);///NOW6 配合鍵盤

    glutMainLoop();
}