2018年5月28日 星期一

Week 14 X+7 的筆記(攝影機,投影)

1.今天只要下載

1.[data][win32glut32.dll
2.將[win32]解壓縮
3.將glut32.dll [data放入window資料夾       ........才不會閃退






















第一行說明盒子(在盒子裡頭才能顯示)

左, 右, 上, 下, 近, 遠 ;

第二行說明各個點座標

攝影機座標 ;物體座標 ;拍攝角度 ;







2.了解攝影機重點











3.開一個專案了解realize程式碼(移動視窗物體不會變形)



4.更改相機鏡頭



#include <Gl/glut.h>
#include <mmsystem.h>
#include "CMP3_MCI.h"
#include <stdio.h>
FILE * fout=NULL;
FILE * fin=NULL;
CMP3_MCI myMP3;

float angle[20]={0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0};///Now4: 用陣列
int oldX=0, now=0;
float alpha=0;

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() ;
    gluLookAt(0,1,3 , 0,0,0 , 0,1,0);
}

void keyboard(unsigned char key ,int x,int y)
{///Now3: 用keyboard來讀入動畫資料
    if(key=='1') now=1;///按1就會改變1號關節
    if(key=='2') now=2;///按2就會改變3號關節
    if(key=='3') now=3;
    if(key=='4') now=4;
    if(key=='5') now=5;
    if(key=='6') now=6;
    if(key=='7') now=7;
    if(key=='8') now=8;
    if(key=='9') now=9;
    if(key=='r' || key=='R'){///Now4: 按'r'才會讀入關節的角度,一次讀20個
        if(fin==NULL){///Now3: TODO:一開始是空指標,所以會進來if(..)
            fin=fopen("output.txt", "r");///Now4: r表示讀入read
        }
        for(int i=0;i<20;i++) fscanf(fin, "%f", &angle[i]);///Now4: 用陣列從檔案fin讀進來
    }
    if(key=='w' || key=='W' || key=='s' || key=='S'){///Now4: 按's'才會寫關節的角度,一次20個
        if(fout==NULL){///TODO:一開始是空指標,所以會進來if(..)
            fout = fopen("output.txt", "w+");///第一節教的開檔
        }///TODO:之後就不再是空指標了
        for(int i=0;i<20;i++) fprintf(fout, "%.3f\n", angle[i]);///TODO:把角度印到檔案裡
    }
//    fscanf(fin, "%f %f %f %f %f %f %f %f %f %f  %f %f %f %f %f %f %f %f %f %f",
//           &angle[0], &angle[1], &angle[2], &angle[3], &angle[4], &angle[5],
//           &angle[6], &angle[7], &angle[8], &angle[9], &angle[10], &angle[11],
//           &angle[12], &angle[13], &angle[14], &angle[15], &angle[16], &angle[17],
//           &angle[18], &angle[19]  );
    glutPostRedisplay();///Now3: 讀入資料,馬上重畫
}

void mouse(int button,int state,int x,int y)
{
    oldX=x;
}

void motion(int x,int y)
{
    angle[now] += x-oldX; ///角度稍微增減, 以前教過把大象放到冰箱去的程式碼
    oldX=x;
    glutPostRedisplay();
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glutSolidTeapot(0.3);///正中間,當成身體的茶壼
        glPushMatrix();
            glTranslatef(0.4, 0,0);///掛上去
            glRotatef(angle[0], 0,0,1);///
            glTranslatef(0.4, 0,0);///把柄放中間
            glutSolidTeapot(0.3);///右邊,當成手臂的茶壼
            glPushMatrix();
                glTranslatef(0.4, 0,0);///掛上去
                glRotatef(angle[1], 0,0,1);///
                glTranslatef(0.4, 0,0);///把柄放中間
                glutSolidTeapot(0.3);///右邊,當成手臂的茶壼
            glPopMatrix();

        glPopMatrix();
        glPushMatrix();
            glTranslatef(-0.4, 0,0);///掛上去
            glRotatef(angle[2], 0,0,1);///
            glTranslatef(-0.4, 0,0);///把柄放中間
            glutSolidTeapot(0.3);///右邊,當成手臂的茶壼
            glPushMatrix();
                glTranslatef(-0.4, 0,0);///掛上去
                glRotatef(angle[3], 0,0,1);///
                glTranslatef(-0.4, 0,0);///把柄放中間
                glutSolidTeapot(0.3);///右邊,當成手臂的茶壼
            glPopMatrix();

        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}

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


/*void timer(int t)
{
    glutTimerFunc(50,timer,t+1);
    alpha = t/100.0;
    angle = alpha*90 + (1-alpha)*0;
    glutPostRedisplay();
    ///PlaySound("Shot.wav",NULL,SND_ASYNC);
}*/

int main(int argc, char *argv[])
{
    myMP3.Load("MJThriller.mp3");
    myMP3.Play();
    ///PlaySound("BigDog.mp3", NULL, SND_ASYNC);

    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT Shapes");

    glutReshapeFunc(resize);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    /*glutTimerFunc(1000,timer,0);*/


    glClearColor(1,1,1,1);

    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;

}

5.增加Timer



























#include <Gl/glut.h>
#include <mmsystem.h>
#include "CMP3_MCI.h"
#include <stdio.h>
FILE * fout=NULL;
FILE * fin=NULL;
CMP3_MCI myMP3;

float angle[20]={0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0};///Now4: 用陣列
float oldAngle[20]={0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0};
float newAngle[20]={0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0};
int oldX=0, now=0 ;
float alpha=0;

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() ;
    gluLookAt(0,1,3 , 0,0,0 , 0,1,0);
}

void keyboard(unsigned char key ,int x,int y)
{///Now3: 用keyboard來讀入動畫資料
    if(key=='1') now=1;///按1就會改變1號關節
    if(key=='2') now=2;///按2就會改變3號關節
    if(key=='3') now=3;
    if(key=='4') now=4;
    if(key=='5') now=5;
    if(key=='6') now=6;
    if(key=='7') now=7;
    if(key=='8') now=8;
    if(key=='9') now=9;

    if(key=='w' || key=='W' || key=='s' || key=='S'){///Now4: 按's'才會寫關節的角度,一次20個
        if(fout==NULL) fout = fopen("output.txt", "w+");///第一節教的開檔

        for(int i=0;i<20;i++) {
        printf("%.3f\n",angle[i]);
        fprintf(fout, "%.3f\n", angle[i]);///TODO:把角度印到檔案裡
    }
}
        if(key=='r' || key=='R'){///Now4: 按'r'才會讀入關節的角度,一次讀20個
            if(fin==NULL)fin=fopen("output.txt", "r");///Now4: r表示讀入read
            for(int i=0;i<20;i++) {
            fscanf(fin, "%f", &angle[i]);///Now4: 用陣列從檔案fin讀進來
            printf("%.3f\n",angle[i]);
            }
            glutPostRedisplay();
        }
        if(key=='p'){
        glutTimerFunc(0,timer,0);
        if(fin==NULL)fin = fopen("output.txt", "r");
        for(int i=0;i<20;i++){
        fscanf(fin, "%f", &newAngle[i]);
        }
    }
}




void timer(int t)
{
    printf("%d\n",t);
    glutTimerFunc(30, timer,t+1);

    if(t%100==0)
    {
        if(fin==NULL)fin=fopen("output.txt","r");
        for(int i=0;i<20;i++)
        {
            oldAngle[i] = newAngle[i];
            fscanf(fin,"%f", &newAngle[i]);
        }
    }
    float alpha = (t%100)/100.0;
    for(int i=0;i<20;i++)
    {
        angle[i] = newAngle[i]*alpha +  oldAngle[i]*(1-alpha);
    }
    glutPostRedisplay();
}

void mouse(int button,int state,int x,int y)
{
    oldX=x;
}

void motion(int x,int y)
{
    angle[now] += x-oldX; ///角度稍微增減, 以前教過把大象放到冰箱去的程式碼
    oldX=x;
    glutPostRedisplay();
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glutSolidTeapot(0.3);///正中間,當成身體的茶壼
        glPushMatrix();
            glTranslatef(0.4, 0,0);///掛上去
            glRotatef(angle[0], 0,0,1);///
            glTranslatef(0.4, 0,0);///把柄放中間
            glutSolidTeapot(0.3);///右邊,當成手臂的茶壼
            glPushMatrix();
                glTranslatef(0.4, 0,0);///掛上去
                glRotatef(angle[1], 0,0,1);///
                glTranslatef(0.4, 0,0);///把柄放中間
                glutSolidTeapot(0.3);///右邊,當成手臂的茶壼
            glPopMatrix();

        glPopMatrix();
        glPushMatrix();
            glTranslatef(-0.4, 0,0);///掛上去
            glRotatef(angle[2], 0,0,1);///
            glTranslatef(-0.4, 0,0);///把柄放中間
            glutSolidTeapot(0.3);///右邊,當成手臂的茶壼
            glPushMatrix();
                glTranslatef(-0.4, 0,0);///掛上去
                glRotatef(angle[3], 0,0,1);///
                glTranslatef(-0.4, 0,0);///把柄放中間
                glutSolidTeapot(0.3);///右邊,當成手臂的茶壼
            glPopMatrix();

        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}

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


/*void timer(int t)
{
    glutTimerFunc(50,timer,t+1);
    alpha = t/100.0;
    angle = alpha*90 + (1-alpha)*0;
    glutPostRedisplay();
    ///PlaySound("Shot.wav",NULL,SND_ASYNC);
}*/

int main(int argc, char *argv[])
{
    myMP3.Load("MJThriller.mp3");
    myMP3.Play();
    ///PlaySound("BigDog.mp3", NULL, SND_ASYNC);

    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT Shapes");

    glutReshapeFunc(resize);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    /*glutTimerFunc(1000,timer,0);*/


    glClearColor(1,1,1,1);

    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;

}






















沒有留言:

張貼留言