顯示具有 08131604_陳思穎 標籤的文章。 顯示所有文章
顯示具有 08131604_陳思穎 標籤的文章。 顯示所有文章

2022年6月7日 星期二

鹿的電腦圖學筆記week16

 Step01:

1.alpha內插法

alpha:00.0~1.0

angle=alpha*new+(1-alpha)*old

                                                                                                                                                                                                                                                                                                                                                         
2.讀入上週程式
 #include <GL/glut.h>
#include <stdio.h>
float ang    le[20],oldX=0;
int angleID=0;
FILE * fout=NULL,*fin=NULL;
void myWrite(){
    if(fout==NULL) fout=fopen("file.txt","w");

    for(int i=0;i<20;i++){
        printf("%.1f ",angle[i]);
        fprintf(fout,"%.1f ",angle[i]);
    }
    printf("\n");//跳行
    fprintf(fout,"\n");
}
void myRead(){
    if(fout!=NULL){fclose(fout);fout=NULL;}
    if(fin==NULL) fin=fopen("file.txt","r");
    for(int i=0;i<20;i++){
        fscanf(fin, "%f", &angle[i]);
    }
    glutPostRedisplay();//重畫畫面
}
void keyboard(unsigned char key,int x,int y){
    if(key=='s')myWrite();
    if(key=='r')myRead();
    if(key=='0')angleID=0;
    if(key=='1')angleID=1;
    if(key=='2')angleID=2;
    if(key=='3')angleID=3;
}
void mouse(int button,int state,int x,int y){
    oldX=x;
}
void motion(int x,int y){
    angle[angleID]+=(x-oldX);
    ///myWrite();
    oldX=x;
    glutPostRedisplay();///重畫畫面
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);
    glRectf(0.3,0.5,-0.3,-0.5);///身體

    glPushMatrix();
        glTranslatef(0.3,0.4,0);///手臂掛到身體
        glRotatef(angle[0],0,0,1);///旋轉
        glTranslatef(-0.3,-0.4,0);///把手臂旋轉中心放到中心
        glColor3f(1,0,0);///紅
        glRectf(0.3,0.5,0.7,0.3);///上手臂

        glPushMatrix();
            glTranslatef(0.7,.4,0);
            glRotatef(angle[1],0,0,1);///旋轉
            glTranslatef(-0.7,-0.4,0);///把手臂旋轉中心放到中心
            glColor3f(0,1,0);///紅
            glRectf(0.7,0.5,1.0,0.3);///下手臂
        glPopMatrix();
    glPopMatrix();

    glPushMatrix();
        glTranslatef(-0.3,0.4,0);///手臂掛到身體
        glRotatef(angle[2],0,0,1);///旋轉
        glTranslatef(0.3,-0.4,0);///把手臂旋轉中心放到中心
        glColor3f(1,0,0);///紅
        glRectf(-0.3,0.5,-0.7,0.3);///上手臂

        glPushMatrix();
            glTranslatef(-0.7,.4,0);
            glRotatef(angle[3],0,0,1);///旋轉
            glTranslatef(0.7,-0.4,0);///把手臂旋轉中心放到中心
            glColor3f(0,1,0);///紅
            glRectf(-0.7,0.5,-1.0,0.3);///下手臂
        glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
}

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

    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}
3.加入
void myInterpolate(float alpha){///alpha內插法
    for(int i=0;i<20;i++){
        angle[i]=alpha*Newangle[i]+(1-alpha)+Oldangle[i];
    }
}
在keyboard()加

int t=0;

if(key=='p'){///按p逐步加入內插
        if(t%30==0)myRead();
        myInterpolate(t%30/30);
        glutPostRedisplay();
        t++;
    }
修改mtRead()
float Newangle[20],Oldangle[20];
void myRead(){
    if(fout!=NULL){fclose(fout);fout=NULL;}
    if(fin==NULL) fin=fopen("file.txt","r");
    for(int i=0;i<20;i++){
        Oldangle[i]=Newangle[i];///原來新->舊
        fscanf(fin, "%f", &Newangle[i]);///讀新的
    }
    glutPostRedisplay();//重畫畫面
}
4.加入timer()
void timer(int t){
    if(t%50==0)myRead();
    myInterpolate(t%50/50.0);
    glutTimerFunc(20,timer,t+1);
}
5.修改keboard()
if(key=='p'){
        myRead();
        glutTimerFunc(0,timer,0);
    }

Step02:
1.開啟projection.exe
類似從眼睛視角看出去
glutLookAt(0.00,0.00,2.00,0.00    眼睛
                    0.00,0.80,0.00    中心
                    0.00,1.00,0.00);   上方(向量)
aspect ratio 長寬比ex:16:9,4:3
3.寫入
#include <GL/glut.h>
void reshape(int w,int h){
    const float ar = (float) w / (float) h;

    glViewport(0, 0, w, h);///3D變2D
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60,ar,0.1,100);

    glMatrixMode(GL_MODELVIEW);///3D Model+View
    glLoadIdentity() ;
    gluLookAt(0,0,3,
              0,0,0,
              0,1,0
              );
}
void display(){
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(1);
    glutSwapBuffers();
}
int main(int argc,char** argv){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week16");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
}
5.加入motion()
void motion(int x,int y){
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt((x-150/150.0,(y-150)/150.0,3,
              0,0,0,
              0,1,0
              );
    glutPostRedisplay();

}
在main加入
glutMotionFunc(motion);









2022年5月31日 星期二

鹿的電腦圖學筆記week15

 Step01撥聲音:

1.file-new-empty file再改檔名.cpp

2.寫入

#include <windows.h>

#include <stdio.h>

int main(){

    printf("playsound()前\n");

    PlaySound("bb.wav",NULL,SND_SYNC);

    printf("playsound()後\n");

}

2.Settings-Compiler-Linker settings

加入winmm

3.去https://sound-effects.bbcrewind.co.uk/search尋找想要的wav檔

3.下載後移到執行資料夾



4.執行結果(wav先改成跟下載的檔案同名

Step02:
1.file-new-empty file再改檔名.cpp
2.寫入
#include <windows.h>
#include <stdio.h>
int main(){
    PlaySound("do.wav",NULL,SND_SYNC);///ASYNC不等待
    PlaySound("re.wav",NULL,SND_SYNC);
    PlaySound("mi.wav",NULL,SND_SYNC);
}
3.wav檔移到執行資料夾
4.要記得寫成SYNC(寫ASYNC不等待會出錯

4.改寫成
#include <windows.h>
#include <stdio.h>
int main(){
    PlaySound("NHU05082251.wav",NULL,SND_ASYNC);///不等待執行較快,適合互動
    while(1){
        printf("輸入數字: ");
        int N;
        scanf("%d",&N);
        if(N==1)PlaySound("do.wav",NULL,SND_SYNC);///ASYNC不等待
        if(N==2)PlaySound("re.wav",NULL,SND_SYNC);
        if(N==3)PlaySound("mi.wav",NULL,SND_SYNC);
    }
}
Step03 mp3:
1.下載CMP3_MCI.h再移到執行資料夾
2.file-new-empty file再改檔名.cpp
3.寫入
#include "CMP3_MCI.h"
#include <stdio.h>
CMP3_MCI mp3;
int main(){
    mp3.Load("NHU05082251.wav");
    mp3.Play();
    
    printf("輸入數字(wav停止) :");
    int N;
    scanf("%d",&N);
}
Step04 :
1.複製上週程式
#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX=0;
int angleID=0;
FILE * fout=NULL,*fin=NULL;
void myWrite(){
    if(fout==NULL) fout=fopen("file.txt","w");

    for(int i=0;i<20;i++){
        printf("%.1f ",angle[i]);
        fprintf(fout,"%.1f ",angle[i]);
    }
    printf("\n");//跳行
    fprintf(fout,"\n");
}
void myRead(){
    if(fout!=NULL){fclose(fout);fout=NULL;}
    if(fin==NULL) fin=fopen("file.txt","r");
    for(int i=0;i<20;i++){
        fscanf(fin, "%f", &angle[i]);
    }
    glutPostRedisplay();//重畫畫面
}
void keyboard(unsigned char key,int x,int y){
    if(key=='r')myRead();
    if(key=='0')angleID=0;
    if(key=='1')angleID=1;
    if(key=='2')angleID=2;
    if(key=='3')angleID=3;
}
void mouse(int button,int state,int x,int y){
    oldX=x;
}
void motion(int x,int y){
    angle[angleID]+=(x-oldX);
    myWrite();
    oldX=x;
    glutPostRedisplay();///重畫畫面
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);
    glRectf(0.3,0.5,-0.3,-0.5);///身體

    glPushMatrix();
        glTranslatef(0.3,0.4,0);///手臂掛到身體
        glRotatef(angle[0],0,0,1);///旋轉
        glTranslatef(-0.3,-0.4,0);///把手臂旋轉中心放到中心
        glColor3f(1,0,0);///紅
        glRectf(0.3,0.5,0.7,0.3);///上手臂

        glPushMatrix();
            glTranslatef(0.7,.4,0);
            glRotatef(angle[1],0,0,1);///旋轉
            glTranslatef(-0.7,-0.4,0);///把手臂旋轉中心放到中心
            glColor3f(0,1,0);///紅
            glRectf(0.7,0.5,1.0,0.3);///下手臂
        glPopMatrix();
    glPopMatrix();

    glPushMatrix();
        glTranslatef(-0.3,0.4,0);///手臂掛到身體
        glRotatef(angle[2],0,0,1);///旋轉
        glTranslatef(0.3,-0.4,0);///把手臂旋轉中心放到中心
        glColor3f(1,0,0);///紅
        glRectf(-0.3,0.5,-0.7,0.3);///上手臂

        glPushMatrix();
            glTranslatef(-0.7,.4,0);
            glRotatef(angle[3],0,0,1);///旋轉
            glTranslatef(0.7,-0.4,0);///把手臂旋轉中心放到中心
            glColor3f(0,1,0);///紅
            glRectf(-0.7,0.5,-1.0,0.3);///下手臂
        glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
}

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

    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}
2.在motion註解
///myWrite();
3.keyboard寫入
if(key=='s')myWrite();
s:寫好才存入一個動作
3.小技巧:可以複製紀錄後的動作重複執行,動作就會增加









2022年5月24日 星期二

鹿的電腦圖學筆記week14

 Step01:

1.File-New-Empty File 另存新檔改名week14-1.cpp

寫入

#include <stdio.h>

int main(){

    FILE *fout=fopen("file.txt","w+");

    printf("HELLO WORLD\n");

    fprintf(fout,"HELLO WORLD\n");

    fclose(fout);

}



2.File-New-Empty File 另存新檔改名week14-2.cpp
寫入
#include <stdio.h>
int main(){
    FILE *fout=fopen("file.txt","w+");
    fprintf(fout,"3.1415926\n");
    fclose(fout);

    float angle=0;
    FILE * fin=fopen("file.txt","r");
    fscanf(fin,"%f",&
angle);//沒加&會當掉
    printf("讀到角度:%f",angle);
    fclose(fin);
}

Step02:
1.載入上週程式
#include <GL/glut.h>
float angle[20],oldX=0;
int angleID=0;

void keyboard(unsigned char key,int x,int y){
    if(key=='0')angleID=0;
    if(key=='1')angleID=1;
    if(key=='2')angleID=2;
    if(key=='3')angleID=3;
}
void mouse(int button,int state,int x,int y){
    oldX=x;
}
void motion(int x,int y){
    angle[angleID]+=(x-oldX);
    oldX=x;
    glutPostRedisplay();///重畫畫面
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);
    glRectf(0.3,0.5,-0.3,-0.5);///身體

    glPushMatrix();
        glTranslatef(0.3,0.4,0);///手臂掛到身體
        glRotatef(angle[0],0,0,1);///旋轉
        glTranslatef(-0.3,-0.4,0);///把手臂旋轉中心放到中心
        glColor3f(1,0,0);///紅
        glRectf(0.3,0.5,0.7,0.3);///上手臂

        glPushMatrix();
            glTranslatef(0.7,.4,0);
            glRotatef(angle[1],0,0,1);///旋轉
            glTranslatef(-0.7,-0.4,0);///把手臂旋轉中心放到中心
            glColor3f(0,1,0);///紅
            glRectf(0.7,0.5,1.0,0.3);///下手臂
        glPopMatrix();
    glPopMatrix();

    glPushMatrix();
        glTranslatef(-0.3,0.4,0);///手臂掛到身體
        glRotatef(angle[2],0,0,1);///旋轉
        glTranslatef(0.3,-0.4,0);///把手臂旋轉中心放到中心
        glColor3f(1,0,0);///紅
        glRectf(-0.3,0.5,-0.7,0.3);///上手臂

        glPushMatrix();
            glTranslatef(-0.7,.4,0);
            glRotatef(angle[3],0,0,1);///旋轉
            glTranslatef(0.7,-0.4,0);///把手臂旋轉中心放到中心
            glColor3f(0,1,0);///紅
            glRectf(-0.7,0.5,-1.0,0.3);///下手臂
        glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
}

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

    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}
2.加入
#include <stdio.h>
FILE * fout=NULL;
void myWrite(){
    if(fout==NULL) fout=fopen("file.txt","w");

    for(int i=0;i<20;i++){
        printf("%.1f",angle[i]);
        fprintf(fout,"%.1f",angle[i]);
    }
}
motion()加入
myWrite();
3.跳行
printf("\n");//跳行
fprintf(fout,"\n);
4.開新檔加入
#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX=0;
int angleID=0;
FILE * fout=NULL,*fin=NULL;
void myWrite(){
    if(fout==NULL) fout=fopen("file.txt","w");

    for(int i=0;i<20;i++){
        printf("%.1f ",angle[i]);
        fprintf(fout,"%.1f ",angle[i]);
    }
    printf("\n");//跳行
    fprintf(fout,"\n");
}
void myRead(){
    if(fout!=NULL){fclose(fout);fout=NULL;}
    if(fin==NULL) fin=fopen("file.txt","r");
    for(int i=0;i<20;i++){
        fscanf(fin,"%.f",&angle[i]);
    }
    glutPostRedisplay();//重畫畫面
}
void keyboard(unsigned char key,int x,int y){
    if(key=='r')myRead();
    if(key=='0')angleID=0;
    if(key=='1')angleID=1;
    if(key=='2')angleID=2;
    if(key=='3')angleID=3;
}
void mouse(int button,int state,int x,int y){
    oldX=x;
}
void motion(int x,int y){
    angle[angleID]+=(x-oldX);
    myWrite();
    oldX=x;
    glutPostRedisplay();///重畫畫面
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);
    glRectf(0.3,0.5,-0.3,-0.5);///身體

    glPushMatrix();
        glTranslatef(0.3,0.4,0);///手臂掛到身體
        glRotatef(angle[0],0,0,1);///旋轉
        glTranslatef(-0.3,-0.4,0);///把手臂旋轉中心放到中心
        glColor3f(1,0,0);///紅
        glRectf(0.3,0.5,0.7,0.3);///上手臂

        glPushMatrix();
            glTranslatef(0.7,.4,0);
            glRotatef(angle[1],0,0,1);///旋轉
            glTranslatef(-0.7,-0.4,0);///把手臂旋轉中心放到中心
            glColor3f(0,1,0);///紅
            glRectf(0.7,0.5,1.0,0.3);///下手臂
        glPopMatrix();
    glPopMatrix();

    glPushMatrix();
        glTranslatef(-0.3,0.4,0);///手臂掛到身體
        glRotatef(angle[2],0,0,1);///旋轉
        glTranslatef(0.3,-0.4,0);///把手臂旋轉中心放到中心
        glColor3f(1,0,0);///紅
        glRectf(-0.3,0.5,-0.7,0.3);///上手臂

        glPushMatrix();
            glTranslatef(-0.7,.4,0);
            glRotatef(angle[3],0,0,1);///旋轉
            glTranslatef(0.7,-0.4,0);///把手臂旋轉中心放到中心
            glColor3f(0,1,0);///紅
            glRectf(-0.7,0.5,-1.0,0.3);///下手臂
        glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
}

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

    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}
Step03timer:
1.寫入
#include <GL/glut.h>
#include <stdio.h>
void timer(int t){
    printf("起床現在:%d\n",t);
}
void display(){

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

    glutTimerFunc(1000,timer,1);
    glutTimerFunc(2000,timer,2);
    glutTimerFunc(3000,timer,3);
    glutTimerFunc(4000,timer,4);
    glutTimerFunc(5000,timer,5);
    glutDisplayFunc(display);
    glutMainLoop();
}
2.自動
#include <GL/glut.h>
#include <stdio.h>
void timer(int t){
    printf("起床現在:%d\n",t);
    glutTimerFunc(1000,timer,t+1);
}
void display(){

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


    glutTimerFunc(5000,timer,0);
    glutDisplayFunc(display);
    glutMainLoop();
}

3.加入聲音在timer()
PlaySound("do.wav",NULL,SND_ASYNC);










2022年5月17日 星期二

鹿的電腦圖學筆記week13

 Step01:

1.先寫入

void display(){

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glutSwapBuffers();

}


int main(int argc,char** argv){

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutInitWindowSize(600,600);

    glutCreateWindow("week13_RECT_TRT");


    glutDisplayFunc(display);

    glutMainLoop();

}

2.在display()加    glRectf(0.3,0.5,-0.3,-0.5);//矩形

3.再加(畫手臂)
 glPushMatrix();
        glColor3f(1,0,0);///紅
        glRectf(0.3,0.5,0.7,0.3);///手臂
    glPopMatrix();
4.把手臂旋轉中心放到身體中心
glTranslatef(-0.3,-0.4,0);///把手臂旋轉中心放到中心
5.旋轉手臂角度
float angle=45;
glRotatef(angle,0,0,1);///旋轉
6.手臂掛回身體(右上)
glTranslatef(0.3,0.4,0);///手臂掛到身體
7.讓手臂可以轉動(滑鼠控制)
float 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;
    glutPostRedisplay();///重畫畫面
}
main加入
glutMouseFunc(mouse);
glutMotionFunc(motion);


Step02:再加上手臂
1.加在上個glutPushMatrix()裡
glPushMatrix();
            glRotatef(angle,0,0,1);///旋轉
            glTranslatef(-0.3,-0.4,0);///把手臂旋轉中心放到中心
            glColor3f(0,1,0);
            glRectf(0.7,0.5,1.0,0.3);///手臂
glPopMatrix();
(要先把上面Rotate註解)
2.加入glTranslatef(0.7,.4,0);(取消紅手臂Rotate註解)
Step03:加入左手
1.複製glPushMatrix()程式,把X值數值設為負數(因為對稱在反方向)
glPushMatrix();
        glTranslatef(-0.3,0.4,0);///手臂掛到身體
        glRotatef(angle,0,0,1);///旋轉
        glTranslatef(0.3,-0.4,0);///把手臂旋轉中心放到中心
        glColor3f(1,0,0);///紅
        glRectf(-0.3,0.5,-0.7,0.3);///上手臂

        glPushMatrix();
            glTranslatef(-0.7,.4,0);
            glRotatef(angle,0,0,1);///旋轉
            glTranslatef(0.7,-0.4,0);///把手臂旋轉中心放到中心
            glColor3f(0,1,0);///紅
            glRectf(-0.7,0.5,-1.0,0.3);///下手臂
        glPopMatrix();
    glPopMatrix();
2.利用鍵盤和滑鼠搭配指定關節
float angle[20];
int angleID=0;
void keyboard(unsigned char key,int x,int y){
    if(key=='0')angleID=0;
    if(key=='1')angleID=1;
    if(key=='2')angleID=2;
    if(key=='3')angleID=3;
}
0對應上右臂;1對應下右臂;2對應上左臂;3對應下左臂;
motion裡面angle+=(x-oldX);改成angle[angleID]+=(x-oldX);
glPushMatrix()的angle改成angle[0],angle[1],angle[2],angle[3]
main()加入glutKeyboardFunc(keyboard);








2022年5月10日 星期二

鹿的電腦圖學筆記week12

 Step01:

1.開啟Transformation.exe

2.Swap交換T,R

3.

glScalef(1.00,1.00,1.00);glBegin();->正常藍色車子


glRotate(100.0,0.00,1.00,0.00);轉動中

glTranslate(0.89,0.00,0.00);移到右邊

Step02:    

myDrawObject(0);畫身體

glPushMatrix();

    glTranslate(0.29,0.31,0);往右上移(身體右上角)

    glRotatef(angle,0,0,1);做旋轉(物件要在該行程式下才會旋轉)

    glTranslatef(-0.3,-0.19,0);往左下移動(讓軸心在中心)

    myDrawObject(1);畫手臂

glPopMatrix();

Step03:

期末考題

glPushMatrix();

    gltranslatef(-0.5,-0.9,0);把T掛到指定位置

    glRotatef(-45,0,0,1);轉動物件角度

    glTranslatef(-0.8,0.9,0);把下面T移到中心點

    drawHand();

glPopMatrix();

Step04:

1.寫入之前程式

#include <GL/glut.h>

float angle=0;

void display(){

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glPushMatrix();

        glRotatef(angle,0,0,1);

        glutSolidTeapot(0.2);

    glPopMatrix();

    glutSwapBuffers();

    angle++;

}

int main(int argc,char** argv){

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week12_TRT");


    glutIdleFunc(display);

    glutDisplayFunc(display);

    glutMainLoop();

}

3.
glColor3f(1,1,1);
glutSolidTeapot(0.3);//大茶壺(身體)
    glPushMatrix();
        glTranslatef(0.2,0,0);茶壺往右移
        glRotatef(angle,0,0,1);旋轉
        glTranslatef(0.2,0,0);移到中心點
        glColor3f(1,0,0);
        glutSolidTeapot(0.2);小茶壺(手)
    glPopMatrix();

4.複製

glPushMatrix();
        glTranslatef(0.2,0,0);
        glRotatef(angle,0,0,1);
        glTranslatef(0.2,0,0);
        glColor3f(1,0,0);
        glutSolidTeapot(0.2);
 glPopMatrix();
5.重新貼上如下:
glPushMatrix();
        glTranslatef(0.2,0,0);
        glRotatef(angle,0,0,1);
        glTranslatef(0.2,0,0);
        glColor3f(1,0,0);
        glutSolidTeapot(0.2);
        glPushMatrix();
            glTranslatef(0.2,0,0);
            glRotatef(angle,0,0,1);
            glTranslatef(0.2,0,0);
            glColor3f(1,0,0);
            glutSolidTeapot(0.2);
         glPopMatrix();
 glPopMatrix();
6.複製上方程式,把X軸和angle都改成負數(因為對稱,在反方向)
glPushMatrix();
        glTranslatef(-0.2,0,0);
        glRotatef(-angle,0,0,1);
        glTranslatef(-0.2,0,0);
        glColor3f(1,0,0);
        glutSolidTeapot(0.2);
        glPushMatrix();
            glTranslatef(-0.2,0,0);
            glRotatef(-angle,0,0,1);
            glTranslatef(-0.2,0,0);
            glColor3f(1,0,0);
            glutSolidTeapot(0.2);
        glPopMatrix();
glPopMatrix();








2022年5月3日 星期二

鹿的電腦圖學筆記week11

 Step01:

1.載入myGundam.zip,data(file)放到工作目錄freeglut\bin\data

2.程式寫入:

#include <opencv/highgui.h>

int main(){

    IplImage * img=cvLoadImage("data/Diffuse.jpg");

    cvShowImage("week11",img);

    cvWaitKey();

}

3.執行結果

Step02:
1.加入上週程式
#include <GL/glut.h>
#include <opencv/cv.h>
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 display(){
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
int main(int argc,char**argv){
   glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week11_gundam");

    glutDisplayFunc(display);
    myTexture("data/Diffuse.jpg");
    glutMainLoop();
}
2.執行結果

Step03:
1.加入
#include "glm.h"
GLMmodel*pmodel=NULL;
2.display()加入
if(pmodel==NULL){
        pmodel=glmReadOBJ("data/Gundam.obj");
        glmUnitize(pmodel);
        glmFacetNormals(pmodel);
        glmVertexNormals(pmodel, 90.0);
    }
glmDraw(pmodel,GLM_TEXTURE);

Step04:
1.寫入float angle=0;
2.display()
glPushMatrix();
        glRotatef(angle,0,1,0);
        glmDraw(pmodel,GLM_TEXTURE);
    glPopMatrix();
glutSwapBuffers();
    angle+=1;
3.main()
glutIdleFunc(display);






2022年4月26日 星期二

鹿的電腦圖學筆記week10

 Step01:

1.加入上週程式如下:

#include <GL/glut.h>

#include <opencv/highgui.h>

#include <opencv/cv.h>

    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 display(){

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

        glutSolidTeapot(0.3);

    glutSwapBuffers();

}

int main(int argc,char**argv){

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week10_texture");


    glutDisplayFunc(display);

    myTexture("earth1.jpg");

    glutMainLoop();

}

2.修改opencv設定

Settings->Compiler
在Search directions->Compiler加入C:\OpenCV2.1\Include
再到Search directions->Linker加入C:\OpenCV2.1\lib
到LinkerSettins加入cv210,cxcore210,highgui210
3.修改圖片位置(查看Build log裡面的workplace)
4.執行結果

Step02:
1.display()加入以下程式:
Begin(GL_POLYGON);
            glTexCoord2f(0,1);glVertex2f(-1,-1);
            glTexCoord2f(1,1);glVertex2f(+1,-1);
            glTexCoord2f(1,0);glVertex2f(+1,+1);
            glTexCoord2f(0,0);glVertex2f(-1,+1);
glEnd();
2.加入GLUquadric *sphere=NULL;///指標,只到二次曲面
3.在display()加入
gluQuadricTexture(sphere,1);
gluSphere(sphere,1,30,30);
4.在main()加入
sphere=gluNewQuadric();
5.執行結果

Step03:
1.寫入float angle=0;
2.在display()寫入
glPushMatrix();
    glRotatef(angle,0,0,1);
    gluSphere(sphere,1,30,30);
glPopMatrix();
angle+=0.1;
3.在main()加入
glutIdleFunc(display);
4.執行結果

5.在display()加入
glRotatef(90,1,0,0);
6.在main()加入
glEnable(GL_DEPTH_TEST);///
7.執行













VERY BEAUTIFUL, VERY POWERFUL

一.     一樣先安裝且設定好freeglut,OpecCV, 開啟CodeBlocks建立新專案 week11_gundam,                 把 MyGundam.zip下載解壓縮後的data資料夾放到freeglut/bin裡面 把week09_openc...