2018年6月26日 星期二

Week07 老王

(老師這週剛好我們家家族旅遊所以請假借了同學的當作參考)
(1)主題:3D模型
(2)轉檔,OBJ格式(v,vt,vnf)
(3)期中考題(10行OpenGL Code)
(4)小考T-R-T
(5)回家作業:骨架階層矩陣

1.
(1)安裝、執行
TODO:下載jsyeh.org/3dcg10>>data.zip

(2)data.zip→桌面 / data / 3D模型
用 3D Exploration 開 3D模


下載jsyeh.org/3dcg10>>Source.zip
(1)glm.h
(2)glm.c重新命名glm.cpp
(3)transformation.c重新命名transfarmation,cpp
上述3個程式放入桌面week07_glm資料夾

freeglut>>bin放入data/模型
ib的改libfreeglut.a複製貼上檔名改libglut32.a

2.


3.


打造 3D模型 程式
(1)把上節課transfarmation.cpp內容刪除
(2)main.cpp放程式
#include "glm.h"///1.外掛
GLMmodel * pmodel=NULL;//2.指標
void display()

2018年6月25日 星期一

Week16 老王

期末作業網址
youtube連結: https://youtu.be/kk6_AXDLs8Y


作業程式碼 :
#include "CMP3_MCI.h"
#include <GL/glut.h>
#include <stdio.h>
#include "glm.h"
GLMmodel * pmodel = NULL;
GLMmodel * pmodel1 = NULL;
GLMmodel * pmodel2 = NULL;
GLMmodel * pmodel3 = NULL;
GLMmodel * pmodel4 = NULL;
GLMmodel * pmodel5 = NULL;

FILE * fout=NULL;
FILE *fin=NULL;
CMP3_MCI myMP3;

float angle[20], angleNew[20], angleOld[20], oldX=0, oldY=0;
int ID=0;
void timer(int t)
{
    glutTimerFunc(33, timer,t+1);

    if(t%30==0)
    {
        for(int i=0;i<20;i++)
        {
            angleOld[i] = angleNew[i];
            fscanf(fin, "%f", &angleNew[i]);
        }
    }
    float alpha = (t%30)/30.0;
    for(int i=0;i<20;i++)
    {
        angle[i] = angleNew[i]*alpha +  angleOld[i]*(1-alpha);
    }
    glutPostRedisplay();
}



void keyboard(unsigned char key, int x, int y)
{
    if(key=='1') ID=1;
    if(key=='2') ID=2;
    if(key=='3') ID=3;
    if(key=='4') ID=4;
    if(key=='5') ID=5;
    if(key=='6') ID=6;

    if(key=='w' || key=='W' || key=='s' || key=='S'){
        if(fout==NULL) fout = fopen("output.txt", "w+");

        for(int i=0;i<20;i++) {
            fprintf(fout, "%.2f ", angle[i]);
        }
        fprintf(fout, "\n");
    }

    if(key=='r' || key=='R'){
        if(fin==NULL) fin=fopen("output.txt", "r");
        for(int i=0;i<20;i++) {
            fscanf(fin, "%f", &angle[i]);
        }
    }
      if(key=='p' || key=='P'){
            myMP3.Play();
        if(fin==NULL) fin = fopen("output.txt", "r");
        for(int i=0;i<20;i++){
            fscanf(fin, "%f", &angleNew[i]);
        }
        glutTimerFunc(33, timer, 0);
    }
    glutPostRedisplay();
}



void motion(int x,int y)
{
    angle[ID] += (x-oldX);
    oldX = x;
    glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
    oldX=x; oldY=y;
}

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

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotated(angle[1],0,1,0);
        glPushMatrix();
            glTranslated(-0.3,0.5,-4.5);
            glRotated(90,1,0,0);

            if (!pmodel) {
            pmodel = glmReadOBJ("data/body.obj");
            if (!pmodel) exit(0);
            glmUnitize(pmodel);
            glmFacetNormals(pmodel);
            glmVertexNormals(pmodel, 90.0);
            }
            glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);

    glPushMatrix();
        glRotated(angle[2],0,1,1);
        glPushMatrix();
             glTranslated(0,0,-1.1);
             glRotated(90,1,0,0);
            if (!pmodel1) {
            pmodel1 = glmReadOBJ("data/hair.obj");
            if (!pmodel1) exit(0);
            glmUnitize(pmodel1);
            glmFacetNormals(pmodel1);
            glmVertexNormals(pmodel1, 90.0);
            }
            glmDraw(pmodel1, GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();
    glPopMatrix();

    glPushMatrix();
        glRotated(angle[3],1,0,0);
        glPushMatrix();
             glTranslated(-1,0.3,0);
             glRotated(90,1,0,0);
            if (!pmodel2) {
            pmodel2 = glmReadOBJ("data/lefthand.obj");
            if (!pmodel2) exit(0);
            glmUnitize(pmodel2);
            glmFacetNormals(pmodel2);
            glmVertexNormals(pmodel2, 90.0);
            }
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();
    glPopMatrix();

    glPushMatrix();
        glRotated(angle[4],1,0,0);
        glPushMatrix();
             glTranslated(1,0,0.3);
             glRotated(180,0,1,0);
            if (!pmodel3) {
            pmodel3 = glmReadOBJ("data/righthand.obj");
            if (!pmodel3) exit(0);
            glmUnitize(pmodel3);
            glmFacetNormals(pmodel3);
            glmVertexNormals(pmodel3, 90.0);
            }
            glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();
    glPopMatrix();

    glPushMatrix();
        glTranslated(-0.5,0,1);
        glRotated(angle[5],1,0,0);
        glTranslated(0,0,0.2);
        glPushMatrix();
             glRotated(90,0,0,1);
            if (!pmodel4) {
            pmodel4 = glmReadOBJ("data/leftleg.obj");
            if (!pmodel4) exit(0);
            glmUnitize(pmodel4);
            glmFacetNormals(pmodel4);
            glmVertexNormals(pmodel4, 90.0);
            }
            glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();
    glPopMatrix();

    glPushMatrix();
        glTranslated(0.5,0,1);
        glRotated(angle[6],1,0,0);
        glTranslated(0,0,0.2);
        glPushMatrix();
             glRotated(90,0,0,1);
            if (!pmodel4) {
            pmodel4 = glmReadOBJ("data/rightleg.obj");
            if (!pmodel4) exit(0);
            glmUnitize(pmodel4);
            glmFacetNormals(pmodel4);
            glmVertexNormals(pmodel4, 90.0);
            }
            glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);
        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[])
{
    myMP3.Load("123.mp3");
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT Shapes");

    glutReshapeFunc(resize);
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
///    glutIdleFunc(idle);
    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();

    return EXIT_SUCCESS;
}

WEEK16 QAQ筆記

WEEK16:期末作品展示

1.螢幕錄影

網址:https://www.youtube.com/watch?v=23ESnwFDclc


2.模型 (但是放進去顏色有點跑掉,可能之後要在研究一下)















3.程式碼
#include "glm.h"
#include <GL/glut.h>
#include <stdio.h>
#include <mmsystem.h>
#include"CMP3_MCI.h"
CMP3_MCI myMP3;
FILE * fout=NULL;
FILE * fin=NULL;
float angle[300],angleNew[300], angleOld[300], oldX=0, oldY=0;
int ID=0;
void timer(int t)
{
    glutTimerFunc(33, timer, t+1);
    if(t%10==0){
        for(int i=0;i<20;i++){
            angleOld[i] = angleNew[i];
            fscanf(fin, "%f", &angleNew[i]);
        }
    }
    float alpha= (t%10)/10.0;
    for(int i=0;i<20;i++){
        angle[i] = alpha*angleNew[i] + (1-alpha)*angleOld[i];
    }
    glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
    if(key=='1') ID=1;
    if(key=='2') ID=2;
    if(key=='3') ID=3;
    if(key=='4') ID=4;
    if(key=='5') ID=5;
    if(key=='6') ID=6;
    if(key=='7') ID=7;
    if(key=='8') ID=8;
    if(key=='9') ID=9;
    if(key=='s' || key=='S' || key=='w' || key=='W')
    {
        if(fout==NULL) fout=fopen("output.txt", "w+");
        for(int i=0;i<20;i++)
        {
            fprintf(fout, "%.2f ", angle[i]);
        }
        fprintf(fout, "\n");
    }
    if(key=='r' || key=='R'){
        if(fin==NULL) fin=fopen("output.txt", "r");
        for(int i=0;i<20;i++){
            fscanf(fin, "%f", &angle[i]);
        }
    }
    if(key=='p' || key=='P'){
        if(fin==NULL) fin=fopen("output.txt", "r");
        for(int i=0;i<20;i++){
            fscanf(fin, "%f", &angleNew[i]);
        }
        glutTimerFunc(100, timer, 0);

    }
    glutPostRedisplay();
}
void motion(int x, int y)
{
    angle[ID] += (x-oldX);
    oldX = x;
    glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
    oldX=x; oldY=y;
}
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(3,1,8,   0,-1,0,  0,10,0);
}
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;

static void display(void)
{
    glClearColor(0.5,0.5,0.5,0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
        glTranslatef(0,0.12,0);
        glRotatef(angle[2], 0,0,1);
        glTranslatef(0,0.13,0);
        glScalef(5,5,5);
        if (!pmodel2)
        {
            pmodel2= glmReadOBJ("model/steppy_body.obj");
            if (!pmodel2) exit(0);
            glmUnitize(pmodel2);
            glmFacetNormals(pmodel2);
            glmVertexNormals(pmodel2, 90.0);
        }
        glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);

        glPushMatrix();
            glTranslatef(0,0.35,0);
            glRotatef(angle[1], 0,1,0);
            glScalef(1,1,1);
            if (!pmodel1)
            {
                pmodel1 = glmReadOBJ("model/steppy_head.obj");
                if (!pmodel1) exit(0);
                glmUnitize(pmodel1);
                glmFacetNormals(pmodel1);
                glmVertexNormals(pmodel1, 90.0);
            }
            glmDraw(pmodel1, GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();///頭

        glPushMatrix();
            glTranslatef(0.25,0.18,0);
            glRotatef(angle[3], 1,0,0);
            glTranslatef(0,-0.15,0);
            glPushMatrix();
                glScalef(1,1,1);
                if (!pmodel3)
                {
                    pmodel3 = glmReadOBJ("model/steppy_right_upper_arm.obj");
                    if (!pmodel3) exit(0);
                    glmUnitize(pmodel3);
                    glmFacetNormals(pmodel3);
                    glmVertexNormals(pmodel3, 90.0);
                }
                glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
                    glPushMatrix();
                        glTranslatef(0,-0.16,0);
                        glRotatef(angle[5], 1,0,0);
                        glTranslatef(0,-0.14,0);
                        glPushMatrix();
                            glScalef(0.6,0.6,0.6);
                            if (!pmodel4)
                            {
                                pmodel4 = glmReadOBJ("model/steppy_right_down_arm.obj");
                                if (!pmodel4) exit(0);
                                glmUnitize(pmodel4);
                                glmFacetNormals(pmodel4);
                                glmVertexNormals(pmodel4, 90.0);
                            }
                            glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);
                        glPopMatrix();///畫右下肢
                    glPopMatrix();///右下肢
            glPopMatrix();///畫右上肢
        glPopMatrix();///右上肢

        glPushMatrix();
            glTranslatef(-0.25,0.18,0);
            glRotatef(angle[4], 1,0,0);
            glTranslatef(0,-0.15,0);
            glPushMatrix();
                glScalef(1,1,1);
                if (!pmodel5) {
                pmodel5 = glmReadOBJ("model/steppy_left_upper_arm.obj");
                if (!pmodel5) exit(0);
                glmUnitize(pmodel5);
                glmFacetNormals(pmodel5);
                glmVertexNormals(pmodel5, 90.0);
                }
                glmDraw(pmodel5, GLM_SMOOTH | GLM_MATERIAL);

                glPushMatrix();
                    glTranslatef(0,-0.16,0);
                    glRotatef(angle[6], 1,0,0);
                    glTranslatef(0,-0.14,0);
                    glPushMatrix();
                        glScalef(0.6,0.6,0.6);
                        if (!pmodel6)
                        {
                            pmodel6 = glmReadOBJ("model/steppy_left_down_arm.obj");
                            if (!pmodel6) exit(0);
                            glmUnitize(pmodel6);
                            glmFacetNormals(pmodel6);
                            glmVertexNormals(pmodel6, 90.0);
                        }
                        glmDraw(pmodel6, GLM_SMOOTH | GLM_MATERIAL);
                    glPopMatrix();///畫左下肢
                glPopMatrix();///左下肢
            glPopMatrix();///畫左手上肢
        glPopMatrix();///左手上肢
        glPushMatrix();
            glTranslatef(0,-0.43,0);
            glRotatef(angle[7], 0,1,0);
            glScalef(0.58,0.58,0.58);
            if (!pmodel7) {
            pmodel7 = glmReadOBJ("model/steppy_leg.obj");
            if (!pmodel7) exit(0);
            glmUnitize(pmodel7);
            glmFacetNormals(pmodel7);
            glmVertexNormals(pmodel7, 90.0);
            }
            glmDraw(pmodel7, GLM_SMOOTH | GLM_MATERIAL);

            glPushMatrix();
                glTranslatef(-0.2,-0.15,0.1);
                glRotatef(angle[8], 1,0,0);
                glTranslatef(0,-0.5,0);
                glPushMatrix();
                    glScalef(0.35,0.35,0.35);
                    if (!pmodel8)
                    {
                        pmodel8 = glmReadOBJ("model/steppy_left_foot.obj");
                        if (!pmodel8) exit(0);
                        glmUnitize(pmodel8);
                        glmFacetNormals(pmodel8);
                        glmVertexNormals(pmodel8, 90.0);
                    }
                    glmDraw(pmodel8, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();///畫左腳
            glPopMatrix();///左腳

            glPushMatrix();
                glTranslatef(0.2,-0.15,0);
                glRotatef(angle[9], 1,0,0);
                glTranslatef(0,-0.5,0);
                glPushMatrix();
                    glScalef(0.35,0.35,0.35);
                    if (!pmodel9)
                    {
                        pmodel9 = glmReadOBJ("model/steppy_right_foot.obj");
                        if (!pmodel9) exit(0);
                        glmUnitize(pmodel9);
                        glmFacetNormals(pmodel9);
                        glmVertexNormals(pmodel9, 90.0);
                    }
                    glmDraw(pmodel9, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();///畫右腳
            glPopMatrix();///右腳
        glPopMatrix();///腳
    glPopMatrix();///身體

    glutSwapBuffers();
}
///設定陣列,要給OpenGL打光用的
const GLfloat light_ambient[]  = { 1.45f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 0.7f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_position[] = { .0f, 0.0f, 1.0f, 10.0f };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.0f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 0.5f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };




int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(500,500);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("05160806_FINAL!!!");

    glutReshapeFunc(resize);
    glutDisplayFunc(display);

    glutMotionFunc(motion);
    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);
    myMP3.Load("Rolex.mp3");
    myMP3.Play();
    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;
}

Week13_阿天筆記

一:



二:


三:


四:
TRT畫好茶壺,定好關節


五:


六:


七:
要先註冊好函式


八:
手移動滑鼠,茶壺轉動,可以記錄座標了!!



week15

複習前幾周的問題

week14

主題  攝影機投影矩陣

fovy : 張開的角度
aspect : 長寬比例
zNear : 近的裁一刀
zFar : 遠的裁一刀

#include <GL/glut.h>
#include <stdio.h>
FILE * fout=NULL;     ///(0)宣告檔案的指標,一開始是空的
float angle=60, angle2=90, angle3=60;      ///準備3個角度的值
void keyboard(unsigned char key, int x, int y)
{
    if(key=='s' || key=='S' || key=='w' || key=='W'){
        if(fout==NULL) fout=fopen("output.txt", "w+");
        ///上面這行,只有第一次按s鍵,才會還沒有值,才fopen
        ///因為第二次按s鍵,fout名花有主,不會再重覆開檔案
        printf("%.3f %.3f %.3f\n", angle,angle2,angle3);
        fprintf(fout, "%.3f %.3f %.3f\n", angle,angle2,angle3);
    }///3個角度的值,印到檔案去
}
///打光的陣列之前全部刪掉, 陣列不要刪
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 */
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    ///Now: 要畫東西
    glPushMatrix();///Now:像大括號一樣,把裡面的移動/旋轉,控制在裡面,不要影響到外面
        glutSolidTeapot(0.3);///Now:身體

        glPushMatrix();  ///Now:像大括號一樣,把裡面的移動/旋轉,控制在裡面,不要影響到外面
            glTranslatef(0.4, 0, 0);  ///Now:(3) 掛在右邊
            glRotatef(angle, 0,0,1);  ///Now:(2) angle的轉動角度,對Z軸
            glTranslatef(0.4, 0, 0);  ///Now:(1) 往右移,讓手把在中間
            glutSolidTeapot(0.3);  ///Now:右手臂
        glPopMatrix();  ///Now:像大括號一樣,把裡面的移動/旋轉,控制在裡面,不要影響到外面
    glPopMatrix();  ///Now:像大括號一樣,把裡面的移動/旋轉,控制在裡面,不要影響到外面
    glutSwapBuffers();
}
void motion(int x, int y)///Now:
{///Now:
    angle = x;///Now: 角度會隨mouse motion而改值
    glutPostRedisplay();///Now:
}///Now:

static void resize(int width, int height)
{
    const float ar = (float) width / (float) height;///aspect rotio 長寬比

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

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);
    glutMotionFunc(motion);///Now: 加上motion事件

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


week13

主題  讀檔寫檔
          控制關節
#include <GL/glut.h>
#include <stdio.h>
FILE * fout=NULL;
float angle=0;
int oldX=0;
void mouse(int button,int state,int x,int y)
{
    oldX=x;
}
void motion(int x,int y)
{
    angle+=x-oldX;
    oldX=x;
    if(fout==NULL)
    {
        fout=fopen("output.txt","w+");

    }
    fprintf(fout,"%.3f\n",angle);
    printf("%.3f\n",angle);
    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,1);
            glTranslatef(0.4, 0,0);
            glutSolidTeapot(0.3);
        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");


    glutMouseFunc(mouse);
    glutMotionFunc(motion);

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

2018年6月24日 星期日

week16 阮致峰

期末作品
https://www.youtube.com/watch?v=EEkIDfpf5GE

week12

TRT
主題
內插 線性內插

week11

題目
放入聲音檔
PlaySound ("ccheer.wav",NULL ,SND ,SYNC);

week10

讀圖秀圖片

myearth.exe

week08

主題  打燈


打光用的陣列
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};

week07

Maya 3D  模型轉 0bj 檔
期中考題
glPushMatrix();//被分矩陣
    glTranslatef(x,y,z);//移動
    glRotatef(angel,x,y,z);//旋轉
    glScalef(x,y,z);//放大縮小
    
    glBegin(GL_POLYGON);//開始畫
        glNormal3f(x,y,z);//法向量
        glTexCoord2f(tx,ty);//貼圖座標
        glColor3f(r,g,b);//顏色
        glVertex3f(x,y,z);//頂點
     glEnd();//結束畫
glPopMatrix();//還原矩陣

week05

複習移動旋轉
主題  T_R_T

#include <GL/glut.h>
float angle=0;
void motion(int x, int y)
{
    angle=x;
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle, 0,0,1);
        glTranslatef(0.45, -0.08,0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week05 TRT");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}
TRT手臂
#include <GL/glut.h>
float angle=0;
void motion(int x, int y)
{
    angle=x;
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glutSolidTeapot(0.3);

        glTranslatef(0.5, 0.15, 0);
        glRotatef(angle, 0,0,1);
        glTranslatef(0.45, -0.08,0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week05 TRT");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}

week04


glPushMatrix();
glTranslatef(x,y,z);移動
glRotatef(angle,x,y,z);旋轉
glScalef(x,y,z);放大縮小
glBegin(GL_POLOGEN);
          glColor3f(r,g,b);
          glVertex3f(x,y,z);
glEnd();
glPopMatrix();

week03

先畫茶壺



改顏色


加入Motion 滑動


Week 16 潘家智的筆記

期末作品

youtube連結:https://www.youtube.com/watch?v=p5VJyv1cHn0

Week 04 潘家智的筆記

從老師的網站http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/
下載data、win32、glut32.dll 這三個檔案
將win32解壓縮然後把glut32.dll 放入window資料夾
執行Transfromation


安培右手定則
glRotatef( 角度, X軸, Y軸, Z軸);
X軸:正值>右手大拇指向右   轉動向內
        負值>右手大拇指向左   轉動向外
Y軸:正值>右手大拇指向上   轉動向內
        負值>右手大拇指向下   轉動向外
Z軸:正值>右手大拇指向內   逆時針轉動
        負值>右手大拇指向外   順時針轉動



期中考考題


可以轉動的茶壺

#include <stdio.h>
#include <GL/glut.h>
float angle=0,teapotX=0,teapotY=0;
int oldX=0,oldY=0,nowRotate=0;///

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(teapotX,teapotY,0);///
        glRotatef(angle,0,0,1);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
    ///printf("%.1f\n",angle);
    ///angle++;
}
void mouse(int button,int state,int x,int y)
{
    if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)nowRotate=1;
    if(button==GLUT_RIGHT_BUTTON&&state==GLUT_DOWN)nowRotate=0;
    oldX=x;oldY=y;///

}

void motion(int x,int y)
{
    if(nowRotate==1)angle+=(x-oldX);
    else
    {
        teapotX += (x-oldX)/150.0;///
        teapotY += (oldY-y)/150.0;///
    }
    oldX=x; oldY=y;
    glutPostRedisplay();
}

int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("05160133_week04 transformation");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    ///glutIdleFunc(display);
    glutDisplayFunc(display);
    glutMainLoop();
}


Week 03 潘家智的筆記

(1)先從這個網址http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/
下載data.zip, windows.zip, glut32.dll 3個檔案


解壓縮windows.zip之後把glut32.dll複製進去
然後就可以執行shapes.exe

(2)再開啟codeblock glut專案並且畫出茶壺
 增加滑鼠按下去就能顯示x,y座標的程式


增加程式碼讓座標以程式碼呈現執行後點出每個點

#include <GL/glut.h>
/*float teapotX=0,teapotY=0;*/
void display()
{
    glClearColor(1,1,0,1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   /* glPushMatrix();
        glTranslatef(teapotX,teapotY,0);
        glutSolidTeapot(0.3);
    glPopMatrix();*/
    glutSwapBuffers();
}
#include <stdio.h>
void mouse(int button,int state,int x,int y)
{
    if(state==GLUT_DOWN)printf("glVertex2f(%f, %f);\n",(x-150)/150.0,-(y-150)/150.0);

}
/*void motion(int x,int y)
{
    teapotX=(x-150)/150.0;
    teapotY=-(y-150)/150.0;
    printf("%d %d\n",x,y);
    glutPostRedisplay();

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

    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}

在程式執行框案右鍵全選後再按一次選擇複製

再重新貼到程式碼內




week02


   改變顏色















畫出茶壺

week01




OpenGL

GLUT