顯示具有 06160921_龔書霆 標籤的文章。 顯示所有文章
顯示具有 06160921_龔書霆 標籤的文章。 顯示所有文章

2019年5月24日 星期五

WEEK14

Timer

#include <GL/glut.h>
#include <mmsystem.h>///NOW (0)發聲音
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glutSwapBuffers();
}
void timer(int t)///NOW (2)
{
    glutTimerFunc(1000, timer, 0);///NOW2 重複播放
    PlaySoundA("fart.wav", NULL, SND_ASYNC);///NOW (2)
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(500,500);///大一點
    glutCreateWindow("week14 timer");

    glutDisplayFunc(display);
    glutTimerFunc(1000, timer, 0);///NOW (1)
                           //一秒
    glutMainLoop();
}

配合計數器來旋轉茶壺
#include <GL/glut.h>
#include <stdio.h>///NOW3 for printf()
float angle=0;///NOW4
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///NOW4
        glRotatef(angle, 0, 0, 1);///NOW4
        glutSolidTeapot( 0.3 );///NOW4
    glPopMatrix();///NOW4
    glutSwapBuffers();
}
void timer(int t)///NOW (2)
{
    glutTimerFunc(30, timer, t+1);///NOW4 ///NOW3 一起床,馬上撥下一個鬧鐘, 時間才會準
    printf("現在的 timer(t)的t是: %d\n", t);///NOW3
    angle = t;///NOW4
    glutPostRedisplay();///NOW4 要更新畫面
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(500,500);///大一點
    glutCreateWindow("week14 timer");

    glutDisplayFunc(display);
    glutTimerFunc(1000, timer, 0);///NOW (1)

    glutMainLoop();
}
\

再來是用內差公式
#include <GL/glut.h>
#include <mmsystem.h>///NOW (0)發聲音
#include <stdio.h>///NOW3 for printf()
float angle=0;///NOW4
float nowX=0;///NOW5
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///NOW4
        ///glRotatef(angle, 0, 0, 1);///NOW4
        glTranslatef(nowX, 0, 0);///NOW5
        glutSolidTeapot( 0.3 );///NOW4
    glPopMatrix();///NOW4
    glutSwapBuffers();
}
void timer(int t)///NOW (2)
{/// 1000 ms = 1秒,   33ms = 0.033秒 => 33*30=1000,  30fps
    glutTimerFunc(33, timer, t+1);///NOW4 ///NOW3 一起床,馬上撥下一個鬧鐘, 時間才會準
    printf("現在的 timer(t)的t是: %d\n", t);///NOW3
    float alpha = (t%30) / 30.0;///變成 alpha: 0.0 ~ 1.0  ///NOW5
    nowX = alpha*1.0 + (1-alpha)* -1.0;///NOW5

    glutPostRedisplay();///NOW4 要更新畫面
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(500,500);///大一點
    glutCreateWindow("week14 timer");

    glutDisplayFunc(display);
    glutTimerFunc(1000, timer, 0);///NOW (1)

    glutMainLoop();
}


作業的程式碼
#include <stdio.h>///NOW1 for fprintf()
FILE * fout=NULL;///NOW1 for fopen()

#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel21 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel31 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel41 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel51 = NULL;

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

float angle[20];///NOW1
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);

            if (!pmodel) {
                pmodel = glmReadOBJ("data/06160451.obj");
            if (!pmodel) exit(0);
                glmUnitize(pmodel);
                glmFacetNormals(pmodel);
                glmVertexNormals(pmodel, 90.0);
            }
        glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
        ///右上臂
        glPushMatrix();
            glTranslatef(0.45,0.3,0 );
            glScalef(0.5,0.5,0.5);
            glRotatef(angle[1], 0,0,1);
            glTranslatef(0,-0.7, 0);
            if (!pmodel2) {
                pmodel2 = glmReadOBJ("data/123.obj");
            if (!pmodel2) exit(0);
                glmUnitize(pmodel2);
                glmFacetNormals(pmodel2);
                glmVertexNormals(pmodel2, 90.0);
            }
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);

            glPushMatrix();
            glTranslatef(0,-1.1,0 );
            glScalef(1,1,1);
            glRotatef(angle[2], 0,0,1);
            glTranslatef(0,-0.7, 0);
            if (!pmodel2) {
                pmodel2 = glmReadOBJ("data/123.obj");
            if (!pmodel2) exit(0);
                glmUnitize(pmodel2);
                glmFacetNormals(pmodel2);
                glmVertexNormals(pmodel2, 90.0);
            }
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
        glPopMatrix();
        glPushMatrix();
            glTranslatef(-0.45,0.3,0 );
            glScalef(0.5,0.5,0.5);
            glRotatef(angle[3], 0,0,1);
            glTranslatef(0,-0.7, 0);
            if (!pmodel2) {
                pmodel2 = glmReadOBJ("data/123.obj");
            if (!pmodel2) exit(0);
                glmUnitize(pmodel2);
                glmFacetNormals(pmodel2);
                glmVertexNormals(pmodel2, 90.0);
            }
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);

            glPushMatrix();
            glTranslatef(0,-1.1,0 );
            glScalef(1,1,1);
            glRotatef(angle[4], 0,0,1);
            glTranslatef(0,-0.7, 0);
            if (!pmodel2) {
                pmodel2 = glmReadOBJ("data/123.obj");
            if (!pmodel2) exit(0);
                glmUnitize(pmodel2);
                glmFacetNormals(pmodel2);
                glmVertexNormals(pmodel2, 90.0);
            }
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
        glPopMatrix();

        glPushMatrix();
            glTranslatef(0.2,-1.05,0 );
            glScalef(0.5,0.5,0.5);
            glRotatef(angle[5], 0,0,1);
            glTranslatef(0,-0.7, 0);
            if (!pmodel2) {
                pmodel2 = glmReadOBJ("data/123.obj");
            if (!pmodel2) exit(0);
                glmUnitize(pmodel2);
                glmFacetNormals(pmodel2);
                glmVertexNormals(pmodel2, 90.0);
            }
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);


            glPushMatrix();
                glTranslatef(0,-1.2,0 );
                glScalef(1,1,1);
                glRotatef(angle[6], 0,0,1);
                glTranslatef(0,-0.7, 0);
                if (!pmodel2) {
                    pmodel2 = glmReadOBJ("data/123.obj");
                    if (!pmodel2) exit(0);
                    glmUnitize(pmodel2);
                    glmFacetNormals(pmodel2);
                    glmVertexNormals(pmodel2, 90.0);
                }
                glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
            glPopMatrix();

        glPushMatrix();
            glTranslatef(-0.2,-1.05,0 );
            glScalef(0.5,0.5,0.5);
            glRotatef(angle[7], 0,0,1);
            glTranslatef(0,-0.7, 0);
            if (!pmodel2) {
                pmodel2 = glmReadOBJ("data/123.obj");
            if (!pmodel2) exit(0);
                glmUnitize(pmodel2);
                glmFacetNormals(pmodel2);
                glmVertexNormals(pmodel2, 90.0);
            }
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);


            glPushMatrix();
                glTranslatef(0,-1.2,0 );
                glScalef(1,1,1);
                glRotatef(angle[8], 0,0,1);
                glTranslatef(0,-0.7, 0);
                if (!pmodel2) {
                    pmodel2 = glmReadOBJ("data/123.obj");
                    if (!pmodel2) exit(0);
                    glmUnitize(pmodel2);
                    glmFacetNormals(pmodel2);
                    glmVertexNormals(pmodel2, 90.0);
                }
                glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
            glPopMatrix();

    glPopMatrix();

    glutSwapBuffers();
}
int oldX=0;
void mouse(int button, int state, int x, int y)///NOW2
{
    oldX = x;
}
#include <stdio.h>
void motion(int x, int y)
{
    angle[angleID] += x-oldX;
    oldX = x;
    for(int i=0;i<20;i++){
    printf(" %.2f ", angle[i]);
    }
    printf("\n");
    display();
}


void keyboard(unsigned char key, int x, int y)
{
    if(key=='1') angleID=1;
    if(key=='2') angleID=2;
    if(key=='3') angleID=3;
    if(key=='4') angleID=4;
    if(key=='5') angleID=5;
    if(key=='6') angleID=6;
    if(key=='7') angleID=7;
    if(key=='8') angleID=8;
    if(key=='w'){///NOW1
        if(fout==NULL) fout=fopen("motion.txt", "w+");///NOW1
        for(int i=0; i<20; i++){///NOW1
            fprintf(fout, "%.1f ", angle[i]);///NOW1
        }///NOW1
        fprintf(fout, "\n");///NOW1
    }
}

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

    glutDisplayFunc(display);
    glutIdleFunc(display);
    glClearColor(1,1,1,1);
    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);
    glutKeyboardFunc(keyboard);///NOW3
    glutMouseFunc(mouse);///NOW2
    glutMotionFunc(motion);///NOW2
    glutDisplayFunc(display);

    glutMainLoop();
}

最後結合今天交的

#include <stdio.h>///NOW1 for fprintf()
FILE * fout=NULL;///NOW1 for fopen() + fprintf()
FILE * fin =NULL;///NOW2 for fopen() + fscanf()
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel21 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel31 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel41 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel51 = NULL;

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

float angle[20];///NOW1
float oldAngle[20];///NOW4
float newAngle[20];///NOW4
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);

            if (!pmodel) {
                pmodel = glmReadOBJ("data/06160451.obj");
            if (!pmodel) exit(0);
                glmUnitize(pmodel);
                glmFacetNormals(pmodel);
                glmVertexNormals(pmodel, 90.0);
            }
        glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
        ///右上臂
        glPushMatrix();
            glTranslatef(0.45,0.3,0 );
            glScalef(0.5,0.5,0.5);
            glRotatef(angle[1], 0,0,1);
            glTranslatef(0,-0.7, 0);
            if (!pmodel2) {
                pmodel2 = glmReadOBJ("data/123.obj");
            if (!pmodel2) exit(0);
                glmUnitize(pmodel2);
                glmFacetNormals(pmodel2);
                glmVertexNormals(pmodel2, 90.0);
            }
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);

            glPushMatrix();
            glTranslatef(0,-1.1,0 );
            glScalef(1,1,1);
            glRotatef(angle[2], 0,0,1);
            glTranslatef(0,-0.7, 0);
            if (!pmodel2) {
                pmodel2 = glmReadOBJ("data/123.obj");
            if (!pmodel2) exit(0);
                glmUnitize(pmodel2);
                glmFacetNormals(pmodel2);
                glmVertexNormals(pmodel2, 90.0);
            }
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
        glPopMatrix();
        glPushMatrix();
            glTranslatef(-0.45,0.3,0 );
            glScalef(0.5,0.5,0.5);
            glRotatef(angle[3], 0,0,1);
            glTranslatef(0,-0.7, 0);
            if (!pmodel2) {
                pmodel2 = glmReadOBJ("data/123.obj");
            if (!pmodel2) exit(0);
                glmUnitize(pmodel2);
                glmFacetNormals(pmodel2);
                glmVertexNormals(pmodel2, 90.0);
            }
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);

            glPushMatrix();
            glTranslatef(0,-1.1,0 );
            glScalef(1,1,1);
            glRotatef(angle[4], 0,0,1);
            glTranslatef(0,-0.7, 0);
            if (!pmodel2) {
                pmodel2 = glmReadOBJ("data/123.obj");
            if (!pmodel2) exit(0);
                glmUnitize(pmodel2);
                glmFacetNormals(pmodel2);
                glmVertexNormals(pmodel2, 90.0);
            }
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
        glPopMatrix();

        glPushMatrix();
            glTranslatef(0.2,-1.05,0 );
            glScalef(0.5,0.5,0.5);
            glRotatef(angle[5], 0,0,1);
            glTranslatef(0,-0.7, 0);
            if (!pmodel2) {
                pmodel2 = glmReadOBJ("data/123.obj");
            if (!pmodel2) exit(0);
                glmUnitize(pmodel2);
                glmFacetNormals(pmodel2);
                glmVertexNormals(pmodel2, 90.0);
            }
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);


            glPushMatrix();
                glTranslatef(0,-1.2,0 );
                glScalef(1,1,1);
                glRotatef(angle[6], 0,0,1);
                glTranslatef(0,-0.7, 0);
                if (!pmodel2) {
                    pmodel2 = glmReadOBJ("data/123.obj");
                    if (!pmodel2) exit(0);
                    glmUnitize(pmodel2);
                    glmFacetNormals(pmodel2);
                    glmVertexNormals(pmodel2, 90.0);
                }
                glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
            glPopMatrix();

        glPushMatrix();
            glTranslatef(-0.2,-1.05,0 );
            glScalef(0.5,0.5,0.5);
            glRotatef(angle[7], 0,0,1);
            glTranslatef(0,-0.7, 0);
            if (!pmodel2) {
                pmodel2 = glmReadOBJ("data/123.obj");
            if (!pmodel2) exit(0);
                glmUnitize(pmodel2);
                glmFacetNormals(pmodel2);
                glmVertexNormals(pmodel2, 90.0);
            }
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);


            glPushMatrix();
                glTranslatef(0,-1.2,0 );
                glScalef(1,1,1);
                glRotatef(angle[8], 0,0,1);
                glTranslatef(0,-0.7, 0);
                if (!pmodel2) {
                    pmodel2 = glmReadOBJ("data/123.obj");
                    if (!pmodel2) exit(0);
                    glmUnitize(pmodel2);
                    glmFacetNormals(pmodel2);
                    glmVertexNormals(pmodel2, 90.0);
                }
                glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
            glPopMatrix();

    glPopMatrix();

    glutSwapBuffers();
}
int oldX=0;
void mouse(int button, int state, int x, int y)///NOW2
{
    oldX = x;
}
#include <stdio.h>
void motion(int x, int y)
{
    angle[angleID] += x-oldX;
    oldX = x;
    for(int i=0;i<20;i++){
    printf(" %.2f ", angle[i]);
    }
    printf("\n");
    display();
}
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)
{
    if(key=='1') angleID=1;
    if(key=='2') angleID=2;
    if(key=='3') angleID=3;
    if(key=='4') angleID=4;
    if(key=='5') angleID=5;
    if(key=='6') angleID=6;
    if(key=='7') angleID=7;
    if(key=='8') angleID=8;
    if(key=='w'){///NOW1
        if(fout==NULL) fout=fopen("motion.txt", "w+");///NOW1
        for(int i=0; i<20; i++){///NOW1
            fprintf(fout, "%.1f ", angle[i]);///NOW1
        }///NOW1
        fprintf(fout, "\n");///NOW1
    }
    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();///NOW2 請讀到angle[i]之後, 重畫畫面
}

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

    glutDisplayFunc(display);
    glutIdleFunc(display);
    glClearColor(1,1,1,1);
    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);
    glutKeyboardFunc(keyboard);///NOW3
    glutMouseFunc(mouse);///NOW2
    glutMotionFunc(motion);///NOW2
    glutDisplayFunc(display);

    glutMainLoop();
}


2019年5月17日 星期五

WEEK13

檔案
開啟貝殼專案

#include <stdio.h>
FILE*fout=NULL;
int main(){
    fout=fopen("filename.txt","w+");
    for(int i=0;i<20;i++){
        fprintf(fout,"Hello World\n");
    }
}
這樣就會在資料夾裡面多一個文件檔


#include <stdio.h>
FILE*fout=NULL;
float angle[20];
int main(){
    fout=fopen("motion.txt","w+");
    for(int i=0;i<20;i++){
        fprintf(fout," %.1f",angle[i]);
    }
}

最後配合剛剛貝殼的程式碼

程式碼

#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日 星期五

Week12_06160921

聲音
開貝殼專案
播放打雷聲
#include <windows.h>
#include <mmsystem.h>
int main()
{
    PlaySoundA("C:\\Users\\user\\Downloads\\thunder_clap.wav",NULL,SND_SYNC);
    return 0;
}

在開glut

#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\\do.wav",NULL,SND_ASYNC);
    if(key=='2') PlaySoundA("C:\\Users\\user\\Downloads\\re.wav",NULL,SND_ASYNC);
    if(key=='3') PlaySoundA("C:\\Users\\user\\Downloads\\mi.wav",NULL,SND_ASYNC);
    if(key=='4') PlaySoundA("C:\\Users\\user\\Downloads\\fa.wav",NULL,SND_ASYNC);
    if(key=='5') PlaySoundA("C:\\Users\\user\\Downloads\\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[])
{
    PlaySoundA("C:\\Users\\user\\Downloads\\thunder_clap.wav",NULL,SND_ASYNC);
    printf("邊播聲音,繼續下面開視窗\n");
    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;
}

換成用CMP3_MCI.h

2019年5月3日 星期五

Week11aaaaaaa

轉動
今天交的是要轉動
#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(); }

出來的結果就是每個關節都可以自由的動

2019年4月26日 星期五

Week10

老師今天講解了程式碼前後調換會有什麼改變

轉動程式碼
#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(0,0.5,0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glPushMatrix();
        glTranslatef(0.51,0.63,0);
        glRotatef(angle,0,0,1);
        glTranslatef(0.6,0,0);

        glRotatef(-90,0,1,0);
        glutSolidCone(0.1,0.6,10,10);
    glPopMatrix();
    glutSwapBuffers();

}
void motion(int x,int y)
{
    angle=x;
    display();
}

int main(int argc, char ** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("06160921");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}

2019年4月12日 星期五

week08


貼圖


先用這個,下載opencv2.1
https://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.1/OpenCV-2.1.0-win32-vs2008.exe/download

建立一個新的project

程式碼

#include <opencv/highgui.h>
int main()
{
    IplImage * img=cvLoadImage("image.jpg");
    cvShowImage("opencv Window",img);
    cvWaitKey(0);
}





加入一張圖片

完美


在開一個新的project
然後把之前茶壺的程式碼拿出來,再加上剛剛的那些程式碼加在一起
這個一樣要做剛剛那些很麻煩的動作,做完就有了



2019年3月29日 星期五

week06

到這http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/多下載一個新的這個source
,然後跟之前的步驟都一樣再來把source裡面的glm.cpp,glm.h,lightmaterial.c拿出來
把上次的程式碼拿出來加上黃色的部分,再把freeglut的資料夾裡面的lib的資料夾打開,
把data的資料夾丟進去程式碼要讀到#include "glm.h"的話要把glm.h的檔案放到放到codeblock新增的資料夾,再來打開codeblock在這上面點右鍵,點這個然後把glm.c改名成glm.cpp加進去,這樣就好了。


#include <GL/glut.h> #include "glm.h" GLMmodel* pmodel = NULL; 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 }; float angle=0; void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glRotatef(angle,0,1,0); if (!pmodel) { pmodel = glmReadOBJ("data/flowers.obj"); if (!pmodel) exit(0); glmUnitize(pmodel); glmFacetNormals(pmodel); glmVertexNormals(pmodel, 90.0); } glmDraw(pmodel, GLM_SMOOTH); glPopMatrix(); angle++; glutSwapBuffers(); } int main(int argc, char ** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow("06160823"); glutDisplayFunc(display); glutIdleFunc(display); glClearColor(1,0,0,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(); }