顯示具有 04160116_許珏榆 標籤的文章。 顯示所有文章
顯示具有 04160116_許珏榆 標籤的文章。 顯示所有文章

2017年6月28日 星期三

Week18_魚筆記

期末作品展示

我們這一家-橘子愛跳舞



謝謝老師這學期的用心指導,老師辛苦了~~~

2017年6月18日 星期日

Week17_魚筆記

複習之所做過的機器人相關程式碼,好讓我們可以成功完成會動的機器人

作業一

貼上背景圖
1.下載openCV 2.1版本並安裝




2.在my earth檔案中修改程式碼讓自己的圖片成功跑出
#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
#include <stdio.h>
GLUquadric * quad;
GLuint id;
float angle=0;
void display()
{   glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
    glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///自動轉很帥
        glBegin(GL_POLYGON);
            glTexCoord2f(0,0); glVertex3f(-1,+1,0);
            glTexCoord2f(1,0); glVertex3f(+1,+1,0);
            glTexCoord2f(1,1); glVertex3f(+1,-1,0);
            glTexCoord2f(0,1); glVertex3f(-1,-1,0);
    glPopMatrix();///自動轉很帥
    glFlush();
}
void timer(int t)
{   glutTimerFunc(20, timer, 0);/// 1000 msec   50fps:20msec
    angle+=1;///自動轉很帥
    glutPostRedisplay();
}
int myTexture(char *filename)
{
    IplImage * img = cvLoadImage(filename); ///OpenCV讀圖
    cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
    glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
    GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
    glGenTextures(1, &id); /// 產生Generate 貼圖ID
    glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
    return id;
}

void myInit()
{   
    quad = gluNewQuadric();
    id = myTexture("we1.jpg");
}
int main(int argc, char**argv)
{   glutInit(&argc, argv);
    glutCreateWindow("3D");
    glutDisplayFunc(display); ///顯示
    glutTimerFunc(0, timer, 0);
    myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
    glutMainLoop();
}


3.將my earth和自己機器人的專案檔(.cbp)用notepad++開啟,並複製my earth部分程式碼進去自己專案內
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="04160116_FinalProject" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/04160011_hw1" prefix_auto="1" extension_auto="1" />
<Option working_dir="." />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/04160011_hw1" prefix_auto="1" extension_auto="1" />
<Option working_dir="." />
<Option object_output="obj/Release/" />
<Option type="0" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add directory="c:/opencv2.1/include" />
<Add directory="C:\Users\user\Desktop\freeglut\include" />
</Compiler>
<Linker>
<Add library="cv210" />
<Add library="cxcore210" />
<Add library="highgui210" />
<Add library="glut32" />
<Add library="opengl32" />
<Add library="glu32" />
<Add library="winmm" />
<Add library="gdi32" />
<Add directory="c:/opencv2.1/lib" />
<Add directory="C:\Users\user\Desktop\freeglut\lib" />
</Linker>
<Unit filename="glm.cpp">
<Option compilerVar="CC" />
</Unit>
<Unit filename="main.cpp" />
<Extensions>
<code_completion />
<envvars />
<debugger />
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>


4.將my earth讀程式碼加進去自己專案中,成功跑出背景圖來
int myTexture(char *filename)
{
    IplImage * img = cvLoadImage(filename); ///OpenCV讀圖
    cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
    glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
    GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
    glGenTextures(1, &id); /// 產生Generate 貼圖ID
    glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
    return id;
}


void myInit()
{   
    id = myTexture("we1.jpg");
}


void display()
{   glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
    glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///自動轉很帥
        glBegin(GL_POLYGON);
            glTexCoord2f(0,0); glVertex3f(-1,+1,0);
            glTexCoord2f(1,0); glVertex3f(+1,+1,0);
            glTexCoord2f(1,1); glVertex3f(+1,-1,0);
            glTexCoord2f(0,1); glVertex3f(-1,-1,0);
        glEnd();
    glPopMatrix();///自動轉很帥
    glFlush();
}


#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
GLuint id;


 myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行


5.打光,讓自己的模型不受圖檔點影響
glEnable(GL_TEXTURE_2D);///開2D貼圖
glDisable(GL_LIGHTING);///關掉打光
glDisable(GL_TEXTURE_2D);///關掉貼圖,才不因為最後貼圖點影響你的模型
glEnable(GL_LIGHTING);///打開打光


6.移3個檔案進自己的專案資料夾中



作業二

使用Timer播放
接續自己專案檔的程式碼,新增計時器的函式
void timer(int t)
{
    glutTimerFunc(100,timer,t+1);
    if(fin==NULL){
        fin=fopen("file.txt","r");   //開啟檔案,檔名準備好,而且是用read模式
        printf("Now open a file.txt for read Mode\n");
        fscanf(fin,"%f %f %f %f %f %f %f %f %f %f",&newAngle[0],&newAngle[1],&newAngle[2],&newAngle[3],&newAngle[4],&newAngle[5],&newAngle[6],&newAngle[7],&newAngle[8],&newAngle[9]
);
    }
    if(alpha>=1.0){
       for(int i=0;i<10;i++) oldAngle[i]=newAngle[i];
       fscanf(fin,"%f %f %f %f %f %f %f %f %f %f",&newAngle[0],&newAngle[1],&newAngle[2],&newAngle[3],&newAngle[4],&newAngle[5],&newAngle[6],&newAngle[7],&newAngle[8],&newAngle[9]
);
       printf("Now read angle[]\n");
       alpha=0.0;
    }
    for(int i=0;i<10;i++){
       angle[i]=alpha*newAngle[i]+(1-alpha)*oldAngle[i];
    }
    alpha+=0.1;
 
    glutPostRedisplay();
}
int main()
{
    ....
    glutTimerFunc(1000,timer,0);
    ....
}



2017年6月5日 星期一

Week16_魚筆記

作業一

內插公式
1.用Excel嘗試內插公式看是否可以成功  x*新+(1-x)*舊



2.把角度改成內插角度


3.內插公式的函式加入


4.Timer函式準備好

寫檔案
#include <stdio.h>
FILE* fout =NULL; ///檔案指標
. 在keyborad()中
.
if(key=='s' || key=='S')
    {
        if(fout==NULL)
        {
           fout=fopen("file.txt","w+");
            printf("現在我產生一個file.txt的檔案在目錄裡面哦\n");
        }
        fprintf(fout, "%.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f\n",       angle[0],angle[1],angle[2],angle[3],angle[4],angle[5],angle[6],angle[7]);///寫檔案的第5行
        printf("現在我把一些數字印到檔案裡面%.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f\n",angle[0],angle[1],angle[2],angle[3],angle[4],angle[5],angle[6],angle[7]);
    }
    if(key=='r' || key=='R')
    {
        if(fin==NULL){///read讀檔案的第2行
            fin=fopen("file.txt", "r");///read讀檔案的第3行
        }
        fscanf(fin, "%f %f %f %f %f %f %f %f", &angle[0], &angle[1], &angle[2], &angle[3], &angle[4], &angle[5], &angle[6], &angle[7]);///read讀檔案的第4行
        glutPostRedisplay();///read讀檔案後, 要重畫畫面
    }
}

5.成功執行
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
GLMmodel* pmodel1 = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel6 = NULL;
GLMmodel* pmodel7 = NULL;
GLMmodel* pmodel8 = NULL;
GLMmodel* pmodel9 = NULL;
float angle[9]={}, dir=1;
///利用角度處理
int now=0;
float alpha=0.0;
///16週課程的Alpha角度內插法
float oldAngle[9]={0, 0, 0, 0, 0, 0, 0, 0, 0};
///舊的角度
float newAngle[9]={40, 40, -40, -40, 0, 0, 0, 0, 0};
///新的角度
void interpolation()
{
    for(int i=0; i<9; i++){
        angle[i]=alpha*newAngle[i]+(1-alpha)*oldAngle[i];
        ///內插公式
    }
    ///更改角度的For迴圈
    alpha+=0.1;
    ///Alpha每次的微小更動
}
///16週課程的內插函式,每呼叫一次,換一格
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        //angle++;
        //glRotatef(180, 0,1,0);
        if (!pmodel) {
        pmodel = glmReadOBJ("data/head.obj");
        if (!pmodel) exit(0);
        glmUnitize(pmodel);
        glmFacetNormals(pmodel);
        glmVertexNormals(pmodel, 90.0);
        }
        if (!pmodel1) {
        pmodel1 = glmReadOBJ("data/body.obj");
        if (!pmodel1) exit(0);
        glmUnitize(pmodel1);
        glmFacetNormals(pmodel1);
        glmVertexNormals(pmodel1, 90.0);
        }
        if (!pmodel2) {
        pmodel2 = glmReadOBJ("data/larm1.obj");
        if (!pmodel2) exit(0);
        glmUnitize(pmodel2);
        glmFacetNormals(pmodel2);
        glmVertexNormals(pmodel2, 90.0);
        }
        if (!pmodel3) {
        pmodel3 = glmReadOBJ("data/larm2.obj");
        if (!pmodel3) exit(0);
        glmUnitize(pmodel3);
        glmFacetNormals(pmodel3);
        glmVertexNormals(pmodel3, 90.0);
        }
        if (!pmodel4) {
        pmodel4 = glmReadOBJ("data/lhand.obj");
        if (!pmodel4) exit(0);
        glmUnitize(pmodel4);
        glmFacetNormals(pmodel4);
        glmVertexNormals(pmodel4, 90.0);
        }
        if (!pmodel5) {
        pmodel5 = glmReadOBJ("data/rarm1.obj");
        if (!pmodel5) exit(0);
        glmUnitize(pmodel5);
        glmFacetNormals(pmodel5);
        glmVertexNormals(pmodel5, 90.0);
        }
        if (!pmodel6) {
        pmodel6 = glmReadOBJ("data/rarm2.obj");
        if (!pmodel6) exit(0);
        glmUnitize(pmodel6);
        glmFacetNormals(pmodel6);
        glmVertexNormals(pmodel6, 90.0);
        }
        if (!pmodel7) {
        pmodel7 = glmReadOBJ("data/rhand.obj");
        if (!pmodel7) exit(0);
        glmUnitize(pmodel7);
        glmFacetNormals(pmodel7);
        glmVertexNormals(pmodel7, 90.0);
        }
        if (!pmodel8) {
        pmodel8 = glmReadOBJ("data/rfoot.obj");
        if (!pmodel8) exit(0);
        glmUnitize(pmodel8);
        glmFacetNormals(pmodel8);
        glmVertexNormals(pmodel8, 90.0);
        }
        if (!pmodel9) {
        pmodel9 = glmReadOBJ("data/lfoot.obj");
        if (!pmodel9) exit(0);
        glmUnitize(pmodel9);
        glmFacetNormals(pmodel9);
        glmVertexNormals(pmodel9, 90.0);
        }

        //glPushMatrix();
            //glRotatef(angle, 0,1,0);

        glPushMatrix();
            glRotatef(180,0,1,0);
            glScalef(0.3,0.3,0.3);
            glTranslatef(0,2,0);
            glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);//hand
        glPopMatrix();
            glPushMatrix();
                glScalef(0.3,0.3,0.3);
                glTranslatef(0,0.5,0);
                glmDraw(pmodel1, GLM_SMOOTH | GLM_MATERIAL);//body
            glPopMatrix();
       glPushMatrix();
                glTranslated(-0.1,0.3,0);
                glRotatef(-angle[0]*1.5,0,0,1);
                glTranslated(0.1,-0.3,0);
                glScalef(0.3,0.3,0.3);
                glTranslatef(-0.4,0.8,0);
                glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);//larm1
            glPushMatrix();
                glTranslatef(-0.2,0,0);
                glRotatef(-angle[1], 0,0,1);
                glTranslatef(0.2,-0,0);
                glScalef(0.8,0.8,0.8);
                glTranslatef(-0.2,-0.35,0);
                glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);//larm2
            glPopMatrix();
            glPushMatrix();
                glTranslatef(-0.1,0.3,0);
                glRotatef(-angle[2]*2,0,0,1);
                glTranslatef(0.1,-0.3,0);
                glScalef(0.8,0.8,0.8);
                glTranslatef(-0.55,-0.7,0);
                glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);//lhand
            glPopMatrix();
        glPopMatrix();

        glPushMatrix();
                glTranslatef(0.1,0.3,0);
                glRotatef(angle[3]*1.5 , 0,0,1);
                glTranslatef(-0.1,-0.3,0);
                glScalef(0.35,0.3,0.3);
                glTranslatef(0.1,0.8,0);
                glmDraw(pmodel5, GLM_SMOOTH | GLM_MATERIAL);//rarm1
            glPushMatrix();
                glTranslatef(0.3,-0.3,0);
                glRotatef(angle[4]*2, 0,0,1);
                glTranslatef(-0.3,0.3,0);
                glScalef(0.8,0.8,0.8);
                glTranslatef(0.25,-0.3,0);
                glmDraw(pmodel6, GLM_SMOOTH | GLM_MATERIAL);//rarm2
             glPopMatrix();
             glPushMatrix();
                glTranslatef(0.15,0.35,0);
                glRotatef(angle[5] , 0,0,1);
                glTranslatef(-0.15,-0.35,0);
                glScalef(0.8,0.8,0.8);
                glTranslatef(0.65,-0.7,0);
                glmDraw(pmodel7, GLM_SMOOTH | GLM_MATERIAL);//rhand
            glPopMatrix();
        glPopMatrix();

        glPushMatrix();
            glRotatef(angle[6] , 0,0,1);
                glTranslatef(0,0,0);
            glPushMatrix();
                glScalef(0.3,0.3,0.3);
                glTranslatef(0.2,0,0);
                glmDraw(pmodel8, GLM_SMOOTH | GLM_MATERIAL);//rfoot
            glPopMatrix();
        glPopMatrix();

        glPushMatrix();
                glRotatef(-angle[7] , 0,0,1);
                glTranslatef(0,0,0);
            glPushMatrix();
                glScalef(0.3,0.3,0.3);
                glTranslatef(-0.2,0,0);
                glmDraw(pmodel9, GLM_SMOOTH | GLM_MATERIAL);//lfoot
            glPopMatrix();
        glPopMatrix();

        //glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
const GLfloat light_ambient[]  = { 10.0f, 10.0f, 10.0f, 10.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, 4.0f, -10.0f, 10.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 };

/*#include <stdio.h>
void timer(int t)///計時器叫的時候, 要做什麼呢?
{
    glutTimerFunc(100, timer, t+1);///先撥下一個鬧鐘
    angle++;///做了要做的事
    printf("現在時間是 %d, 我起床了, 等一下鬧鐘會再叫一下哦  ", t);
    glutPostRedisplay();///提醒電腦,有空要畫一下畫面哦
    printf("... 然後我就又睡了....\n");
}///再度沈睡*/

int oldX=0,oldY=0;
#include <stdio.h>///寫檔案的第1行
FILE * fout=NULL;///寫檔案的第2行, 空空的
FILE * fin=NULL;///read讀檔案的第1行
void keyboard(unsigned char key,int x,int y)
{
    printf("%c\n", key);
    if(key=='0') now=0;
    if(key=='1') now=1;
    if(key=='2') now=2;
    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=='s' || key=='S')
    {
        if(fout==NULL)
        {
           fout=fopen("file.txt","w+");
            printf("現在我產生一個file.txt的檔案在目錄裡面哦\n");
        }
        fprintf(fout, "%.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f\n", angle[0],angle[1],angle[2],angle[3],angle[4],angle[5],angle[6],angle[7]);///寫檔案的第5行
        printf("現在我把一些數字印到檔案裡面%.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f\n",angle[0],angle[1],angle[2],angle[3],angle[4],angle[5],angle[6],angle[7]);
    }
    if(key=='r' || key=='R')
    {
        if(fin==NULL){///read讀檔案的第2行
            fin=fopen("file.txt", "r");///read讀檔案的第3行
        }
        fscanf(fin, "%f %f %f %f %f %f %f %f", &angle[0], &angle[1], &angle[2], &angle[3], &angle[4], &angle[5], &angle[6], &angle[7]);///read讀檔案的第4行
        glutPostRedisplay();///read讀檔案後, 要重畫畫面
    }
}
void mouse(int button,int state,int x,int y)
{
    oldX=x;oldY=y;
    //glutPostRedisplay();
}
void motion(int x,int y)
{
    angle[now]+=(x-oldX);
    oldX=x;
    glutPostRedisplay();
}

int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
    glutCreateWindow("04160116");

    glutDisplayFunc(display);
    //glutIdleFunc(display);
    //glutTimerFunc(0, timer, 0);///(1) 第一次呼叫timer計時器
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

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


2017年5月22日 星期一

Week14_魚筆記

作業一

讓上週我們做的機器人動起來
1.解壓縮自己的作業並成功開起來(如果不行即調設定)


2.加入glRotatef(angle,0,1,0); 讓角色旋轉
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
GLMmodel* pmodel1 = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel6 = NULL;
GLMmodel* pmodel7 = NULL;
GLMmodel* pmodel8 = NULL;
GLMmodel* pmodel9 = NULL;
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        angle++;
        glRotatef(180, 0,1,0);
        if (!pmodel) {
        pmodel = glmReadOBJ("data/head.obj");
        if (!pmodel) exit(0);
        glmUnitize(pmodel);
        glmFacetNormals(pmodel);
        glmVertexNormals(pmodel, 90.0);
        }
        if (!pmodel1) {
        pmodel1 = glmReadOBJ("data/body.obj");
        if (!pmodel1) exit(0);
        glmUnitize(pmodel1);
        glmFacetNormals(pmodel1);
        glmVertexNormals(pmodel1, 90.0);
        }
        if (!pmodel2) {
        pmodel2 = glmReadOBJ("data/larm1.obj");
        if (!pmodel2) exit(0);
        glmUnitize(pmodel2);
        glmFacetNormals(pmodel2);
        glmVertexNormals(pmodel2, 90.0);
        }
        if (!pmodel3) {
        pmodel3 = glmReadOBJ("data/larm2.obj");
        if (!pmodel3) exit(0);
        glmUnitize(pmodel3);
        glmFacetNormals(pmodel3);
        glmVertexNormals(pmodel3, 90.0);
        }
        if (!pmodel4) {
        pmodel4 = glmReadOBJ("data/lhand.obj");
        if (!pmodel4) exit(0);
        glmUnitize(pmodel4);
        glmFacetNormals(pmodel4);
        glmVertexNormals(pmodel4, 90.0);
        }
        if (!pmodel5) {
        pmodel5 = glmReadOBJ("data/rarm1.obj");
        if (!pmodel5) exit(0);
        glmUnitize(pmodel5);
        glmFacetNormals(pmodel5);
        glmVertexNormals(pmodel5, 90.0);
        }
        if (!pmodel6) {
        pmodel6 = glmReadOBJ("data/rarm2.obj");
        if (!pmodel6) exit(0);
        glmUnitize(pmodel6);
        glmFacetNormals(pmodel6);
        glmVertexNormals(pmodel6, 90.0);
        }
        if (!pmodel7) {
        pmodel7 = glmReadOBJ("data/rhand.obj");
        if (!pmodel7) exit(0);
        glmUnitize(pmodel7);
        glmFacetNormals(pmodel7);
        glmVertexNormals(pmodel7, 90.0);
        }
        if (!pmodel8) {
        pmodel8 = glmReadOBJ("data/rfoot.obj");
        if (!pmodel8) exit(0);
        glmUnitize(pmodel8);
        glmFacetNormals(pmodel8);
        glmVertexNormals(pmodel8, 90.0);
        }
        if (!pmodel9) {
        pmodel9 = glmReadOBJ("data/lfoot.obj");
        if (!pmodel9) exit(0);
        glmUnitize(pmodel9);
        glmFacetNormals(pmodel9);
        glmVertexNormals(pmodel9, 90.0);
        }
        glPushMatrix();
            glRotatef(angle, 0,1,0);
        glPushMatrix();
            glScalef(0.3,0.3,0.3);
            glTranslatef(0,2,0);
            glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();
        glPushMatrix();
            glScalef(0.3,0.3,0.3);
            glTranslatef(0,0.5,0);
            glmDraw(pmodel1, GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();
        glPushMatrix();
            glScalef(0.3,0.3,0.3);
            glTranslatef(-0.4,0.8,0);
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();
        glPushMatrix();
            glScalef(0.3,0.3,0.3);
            glTranslatef(-0.55,0.55,0);
            glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();
        glPushMatrix();
            glScalef(0.3,0.3,0.3);
            glTranslatef(-0.85,0.2,0);
            glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();
        glPushMatrix();
            glScalef(0.3,0.3,0.3);
            glTranslatef(0.2,0.8,0);
            glmDraw(pmodel5, GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();
        glPushMatrix();
            glScalef(0.3,0.3,0.3);
            glTranslatef(0.38,0.5,0);
            glmDraw(pmodel6, GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();
        glPushMatrix();
            glScalef(0.3,0.3,0.3);
            glTranslatef(0.8,0.1,0);
            glmDraw(pmodel7, GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();
        glPushMatrix();
            glScalef(0.3,0.3,0.3);
            glTranslatef(0.2,0,0);
            glmDraw(pmodel8, GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();
        glPushMatrix();
            glScalef(0.3,0.3,0.3);
            glTranslatef(-0.2,0,0);
            glmDraw(pmodel9, GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
const GLfloat light_ambient[]  = { 10.0f, 10.0f, 10.0f, 10.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, 4.0f, -10.0f, 10.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 };

int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
    glutCreateWindow("Yes, 3D Model Here");

    glutDisplayFunc(display);
    glutIdleFunc(display);

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



3.計時器,在main() 
const GLfloat light_ambient[]  = { 10.0f, 10.0f, 10.0f, 10.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, 4.0f, -10.0f, 10.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 };

  #include <stdio.h>
void timer(int t)///計時器叫的時候, 要做什麼呢?
{
    glutTimerFunc(100, timer, t+1);///先撥下一個鬧鐘
    angle++;///做了要做的事
    printf("現在時間是 %d, 我起床了, 等一下鬧鐘會再叫一下哦  ", t);
    glutPostRedisplay();///提醒電腦,有空要畫一下畫面哦
    printf("... 然後我就又睡了....\n");
}///再度沈睡
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
    glutCreateWindow("Yes, 3D Model Here");

    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutTimerFunc(0, timer, 0);///(1) 第一次呼叫timer計時器 glutTimerFunc(等多久,叫誰,參數);



4.試著讓手旋轉




2017年5月15日 星期一

Week13_魚筆記

作業一

T-R-T觀念小考講解
移動手臂或小腿之中心點在旋轉其角度移至特定座標

glPushMatrix();
glTranslatef(0.4,-1.0,0);  ///4.再把樓下對旋轉的腿移至(0.4,-1.0,0)
glRotatef(angle,0,0,1);  ///3.對Z軸做旋轉angle角度
glTranslatef(0.2,-0.5,0);  ///2.把旋轉中心點放在畫面中間
drawLeg();  ///1.畫一個小腿 
glPopMatrix();

P.S移動角度的正負為 : 順時針為負,逆時針為正

作業二

1.先下載openCV2.1並安裝
 

其他的不用變


2.可在C槽中找到openCV的資料夾,其中必備3大重要bin、include、lib



3.開啟CodeBlocks新增貝殼專案(C++ 副檔名為.cpp)



4.先畫一個圖存成png檔再C槽


5.讀圖檔的程式撰寫
#include <opencv/highgui.h>
int main()
{
    IplImage * img = cvLoadImage("C:/star.png");
    cvShowImage("a",img);
    cvWaitKey(0);
}
執行時會有錯誤 因為還沒讀入opencv


6.讀入opencv使檔案可以執行






7.成功教出圖檔

作業三

轉動的地球
1.myEarth.zip解壓縮到桌面



2.freeglut.zip解壓縮到桌面



3.開啟myEarth專案檔
拉進去即可



4.執行時會有錯 因為還未設定opencv、freeglut  (開始設定)









5.成功後即可執行 轉動的地球
#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
#include <stdio.h>
GLUquadric * quad;
GLuint id;
float angle=0;
void display()
{   glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
    glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///自動轉很帥
        glRotatef(90, 1,0,0);
        glRotatef(angle, 0,0,1);///自動轉很帥
        gluQuadricTexture(quad, 1);
        gluSphere(quad, 1, 30, 30);///glutSolidTeapot(0.3);
    glPopMatrix();///自動轉很帥
    glFlush();
}
void timer(int t)
{   glutTimerFunc(20, timer, 0);/// 1000 msec   50fps:20msec
    angle+=1;///自動轉很帥
    glutPostRedisplay();
}
int myTexture(char *filename)
{
    IplImage * img = cvLoadImage(filename); ///OpenCV讀圖
    cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
    glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
    GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
    glGenTextures(1, &id); /// 產生Generate 貼圖ID
    glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
    return id;
}

void myInit()
{   quad = gluNewQuadric();
    id = myTexture("image.jpg");
}
int main(int argc, char**argv)
{   glutInit(&argc, argv);
    glutCreateWindow("3D");
    glutDisplayFunc(display); ///顯示
    glutTimerFunc(0, timer, 0);
    myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
    glutMainLoop();
}




6.讓地球印在茶壺上
#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
#include <stdio.h>
GLUquadric * quad;
GLuint id;
float angle=0;
void display()
{   glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
    glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///自動轉很帥
        glRotatef(90, 1,0,0);
        glRotatef(angle, 0,0,1);///自動轉很帥
        gluQuadricTexture(quad, 1);
        glutSolidTeapot(0.3);///gluSphere(quad, 1, 30, 30);
    glPopMatrix();///自動轉很帥
    glFlush();
}
void timer(int t)
{   glutTimerFunc(20, timer, 0);/// 1000 msec   50fps:20msec
    angle+=1;///自動轉很帥
    glutPostRedisplay();
}
int myTexture(char *filename)
{
    IplImage * img = cvLoadImage(filename); ///OpenCV讀圖
    cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
    glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
    GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
    glGenTextures(1, &id); /// 產生Generate 貼圖ID
    glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
    return id;
}

void myInit()
{   quad = gluNewQuadric();
    id = myTexture("image.jpg");
}
int main(int argc, char**argv)
{   glutInit(&argc, argv);
    glutCreateWindow("3D");
    glutDisplayFunc(display); ///顯示
    glutTimerFunc(0, timer, 0);
    myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
    glutMainLoop();
}




2017年5月8日 星期一

Week12_魚筆記

作業一

3D TRT 轉動
1.http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10下載data、windows、glut32.dll


2..解壓縮到windows資料夾(如下圖)
下載data.zip(解壓縮到桌面\移到window資料夾裡\data\mtlobj)
        window.zip(解壓縮到桌面並建一個新的資料夾window\window\Tranformation.exe)
        glut32.dll(解壓縮到桌面\移到window資料夾裡\glut32.dll)
        source.zip(解壓縮到桌面\移到window資料夾裡\source)


3.開啟Projection


4.試著調整gluLookAt(eyeX,eyeY,eyeZ,    ///攝影機
                                      centerX,centerY,centerZ, 
                                      upX,upY,upZ)  
 

作業二

矩陣 投影矩陣
攝影機運鏡
1.開啟04160011_hw1_project.zip 成功執行後如圖下
#include <GL/glut.h>
float eyeX=0, eyeY=0, eyeZ=1;
float centerX=0, centerY=0, centerZ=0;
float upX=0, upY=1, upZ=0;
static void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3d(1,0,0);
    glPushMatrix();
        gluLookAt(eyeX, eyeY, eyeZ,
                  centerX, centerY, centerZ,
                  upX, upY, upZ);
        glutSolidTeapot(0.3);
    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 };

/* Program entry point */

int main(int argc, char *argv[])
{
    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;
}

        


2.修改gluLookAt(eyeX,eyeY,eyeZ  看看茶壺的變化(更改紅色部分的數值)
#include <GL/glut.h>
float eyeX=0, eyeY=0, eyeZ=0.6;
float centerX=0, centerY=0.5, centerZ=0;
float upX=0, upY=1, upZ=0;
static void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3d(1,0,0);
    glPushMatrix();
        gluLookAt(eyeX, eyeY, eyeZ,
                  centerX, centerY, centerZ,
                  upX, upY, upZ);
        glutSolidTeapot(0.3);
    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 };

/* Program entry point */

int main(int argc, char *argv[])
{
    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;
}



3.改gluPerspective(fov , 1,  near, far) ///fov為視野角, 1為xy比例
不讓茶壺旋轉時破碎
#include <GL/glut.h>
#include <math.h>
float eyeX=1, eyeY= 0, eyeZ= 0;
float centerX=0, centerY=0, centerZ=0;
float upX=0, upY=1, upZ=0;
float angle=0;
static void display(void)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity(); ///備份矩陣
    gluPerspective(60, 1, 0.001, 10000);   ///讓茶壺不會因為旋轉而破碎  透視矩陣
    glMatrixMode(GL_MODELVIEW);


    angle+=0.01;
    eyeX=cos(angle); eyeZ=sin(angle);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3d(1,0,0);
    glPushMatrix();
        gluLookAt(eyeX, eyeY, eyeZ,
                  centerX, centerY, centerZ,
                  upX, upY, upZ);
        glutSolidTeapot(0.3);
    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 };

/* Program entry point */

int main(int argc, char *argv[])
{
    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);
    glutIdleFunc(display);

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

 

作業三

會動的機器人
1.複製茶壺更改其位置 再讓他旋轉
(茶壺的手舉放)
#include <GL/glut.h>
#include <math.h>
float eyeX=0, eyeY= 0, eyeZ= 2;
float centerX=0, centerY=0, centerZ=0;
float upX=0, upY=1, upZ=0;
float angle=0, dir=1;;
static void display(void)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60, 1, 0.001, 10000);
    glMatrixMode(GL_MODELVIEW);

    angle+=dir;
    if(angle>60) dir=-dir;
    if(angle<0) dir=-dir; ///讓手臂轉到一定角度後即迴轉

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3d(1,0,0);
    glPushMatrix();
        gluLookAt(eyeX, eyeY, eyeZ,
                  centerX, centerY, centerZ,
                  upX, upY, upZ);
        glPushMatrix();
            glColor3f(1,0,0);
            glutSolidTeapot(0.3);///head
            glPushMatrix();
                glTranslatef(0, -0.3, 0); 
                glColor3f(1,1,1);
                glutSolidTeapot(0.3);///body
                glPushMatrix();
                    ///glTranslatef(0.6,0,0);///old rotate
                    glTranslatef(0.3,0,0);/// T 掛上去
                    glRotatef(angle, 0,0,1);///R 旋轉
                    glTranslatef(0.3,0,0);/// T 釘旋轉中心
                    glutSolidTeapot(0.3);///right upper arm
                    glPushMatrix();
                        glTranslatef(0.3, 0,0);///T
                        glRotatef(angle, 0,0,1);///R
                        glTranslatef(0.3, 0,0);///T
                        glutSolidTeapot(0.3);///right lower arm
                    glPopMatrix();
                glPopMatrix();
                glPushMatrix();
                    ///glTranslatef(0.6,0,0);///old rotate
                    glTranslatef(-0.3,0,0);/// T
                    glRotatef(-angle, 0,0,1);///R
                    glTranslatef(-0.3,0,0);/// T
                    glutSolidTeapot(0.3);///left upper arm
                    glPushMatrix();
                        glTranslatef(-0.3, 0,0);///T
                        glRotatef(-angle, 0,0,1);///R
                        glTranslatef(-0.3, 0,0);///T
                        glutSolidTeapot(0.3);///left lower arm
                    glPopMatrix();
                glPopMatrix();
            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 };

/* Program entry point */

int main(int argc, char *argv[])
{
    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);
    glutIdleFunc(display);

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