顯示具有 09161070_呂羽倢 標籤的文章。 顯示所有文章
顯示具有 09161070_呂羽倢 標籤的文章。 顯示所有文章

2022年6月7日 星期二

ఠ_ఠ week16

 

week16

(1)

1-1.alpha內插公式 : alpha : `0.0~1.0`
       `angle = alpha * 新 + ( 1 - alpha ) * 舊`
      ex.
      - alpha : 0 => 舊
      - alpha : 0.5 => 半新半舊
      - alpha : 1 => 新
1-2.開glut ,用上週TRT_again程式改
#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");
}
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]);
        ///fscanf(fin," %f ",&angle[i]);
    }
    glutPostRedisplay();
}

void myInterpolate(float alpha)
{
    for(int i=0;i<20;i++){
        angle[i]=alpha * NewAngle[i] + (1-alpha) * OldAngle[i];
    }
}
int t=0;
void keyboard(unsigned char key,int x,int y)
{
    if(key=='p'){
        if(t%30==0) myRead();
        myInterpolate( (t%30)/30.0 );
        glutPostRedisplay();
        t++;
    }
    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,0.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,0.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);
    glutCreateWindow("week15 angle TRT again");
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}
長按P會有動畫
1-3.
#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");
}
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]);
        ///fscanf(fin," %f ",&angle[i]);
    }
    glutPostRedisplay();
}

void myInterpolate(float alpha)
{
    for(int i=0;i<20;i++){
        angle[i]=alpha * NewAngle[i] + (1-alpha) * OldAngle[i];
    }
}
///int t=0;
void timer(int t)
{
    myInterpolate((t%50)/50.0);
    glutPostRedisplay();
    glutTimerFunc(20,timer,t+1);
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='p'){
        myRead();
    glutTimerFunc(0,timer,0);
        ///if(t%30==0) myRead();
        ///myInterpolate( (t%30)/30.0 );
        ///glutPostRedisplay();
        ///t++;
    }
    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,0.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,0.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);
    glutCreateWindow("week15 angle TRT again");
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

(2)

2-1.開windows/Projection

觀察運鏡效果
2-2.aspect ratio 長寬比 ex.1920 x1080 , 1280 x 720, 16 : 9 , 4 : 3

(3)

3-1.用十行程式改
#include <GL/glut.h>

void reshape(int w/int h)
{
    float ar=(float)w/(float)h;
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);///3D變2D
    glLoadIdentity();
    gluPerspective(60,ar,0.1,100);

    glMatrixMode(GL_MODELVIEW);///3D Model+View
    glLoadIdentity();
    gluLookAt(0,0,3,///eye
                       0,0,0,///center看哪裡
                       0,1,0);///up向量
}
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 camara");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);///範例用resize
    glutMainLoop();
}
3-2.
#include <GL/glut.h>

void reshape(int w,int h)
{
    float ar=(float)w/(float)h;
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);///3D變2D
    glLoadIdentity();
    gluPerspective(60,ar,0.1,100);

    glMatrixMode(GL_MODELVIEW);///3D Model+View
    glLoadIdentity();
    gluLookAt(0,0,3,///eye
                      0,0,0,///center看哪裡
                      0,1,0);///up向量
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(1);
    glutSwapBuffers();
}
void motion (int x,int y)
{
    glMatrixMode(GL_MODELVIEW);///3D Model+View
    glLoadIdentity();
    gluLookAt((x-150)/150.0,(y-150)/150.0,3,///eye
              0,0,0,///center看哪裡
              0,1,0);///up向量
    glutPostRedisplay();///請重畫畫面
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Week16 camara");
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
}





2022年5月31日 星期二

ఠ_ఠ week15

 

week15

(1)

1-1.開 CodeBlocks/empty file-->week15_PlaySound.cpp

#include <windows.h>
#include <stdio.h>
int main()
{
    printf("PlaySound()\n");
    PlaySound("07042111.wav",NULL,SND_SYNC);
    printf("PlaySound()之後\n");
}

⃟   如果錯誤會有錯誤音效

1-2.開 CodeBlocks/empty file-->week15_SND_SNTC_ASNYC.cpp
⃤   直接播
#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);
}

⃤   等待
#include <windows.h>
#include <stdio.h>
int main()
{
    PlaySound("07042111.wav",NULL,SND_ASYNC);///ASYNC不等待
    while(1){
       printf("請輸入數字: ");
        int N;
        scanf("%d",&N);
        if (N==1) PlaySound("do.wav",NULL,SND_ASYNC);
        if (N==2) PlaySound("re.wav",NULL,SND_ASYNC);
        if (N==3) PlaySound("mi.wav",NULL,SND_ASYNC);
    }
}

(2)

2-1.開 CodeBlocks/empty file-->week15_MP3.cpp
2-2.moodle 下載CMP3_MCI.h
2-3.

#include <stdio.h>
#include "CMP3_MCI.h"
CMP3_MCI mp3;

int main()
{
    mp3.Load("07042111.mp3");
    mp3.Play();

    printf("隨便等你輸入數字,程式就卡住了 : ");
    int N;///為了程式不要結束
    scanf("%d",&N);
}

2-4.開新專案-->week15_angle_TRT_again
      可以讀動作,長按r可播錄的動作

2-5.改上週程式

#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=='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,0.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,0.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);
    glutCreateWindow("week15 angle TRT again");
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

⃤   按s可以存動作

⃤   找txt複製動作可以做很多次r


(3)HW

3-1.開新專案week15_HW
3-2.用notepad++改目錄-->"."
3-3.把freeglut/bin/freeglut.dll複製到cbp資料夾
3-4.把 glm.h & glm.cpp & gundam的data目錄





2022年5月24日 星期二

ఠ_ఠ week14

 

week14

(1)

1-1.開 CodeBlocks/empty file-->week14-1.cpp
1-2.寫程式
#include<stdio.h>
int main(){指標fout開檔案(檔名,write模式)
    FILE*fout=fopen("file.txt,","w+");
        printf("Hello World\n");
    fprintf(fout,"Hello World\n");
    fclose(fout);關檔案
}
1-3.
#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);
}
1-4.開上週程式改

#include <GL/glut.h>
#include <stdio.h>

float angle[20], oldX=0;
int angleID=0;
FILE * fout =NULL;
void myWrite(){
    if(fout==NULL)
        fout=fopen("file.txt,","w+");

    for(int i=0;i<20;i++){
        printf("%.1f",angle[i]);
    }
}
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);
    myWrite();
    oldX=x;
    glutPostRedisplay();
}



(2)

2-1.
#include <GL/glut.h>
#include <stdio.h>

float angle[20], oldX=0;
int angleID=0;
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]);
    }
    printf("\n");
    fprintf(fout,"\n");
}
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);
    myWrite();
    oldX=x;
    glutPostRedisplay();
}



2-2.
#include <GL/glut.h>
#include <stdio.h>

float angle[20], oldX=0;
int angleID=0;///關節ID
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]);
    }f後面要加空格
    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();
}
長按r就會顯示錄到的動作


2-3.

    (1)用notepad++ 開 cbp檔 ---> 把working_dir 裡的路徑改成"."
    (2)把freeglut裡的freeglut.dll複製到專案中

這樣txt就會放到程式專案目錄了

(3)

3-1.開新專案week14_timer
3-2.
#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();
}

3-3.
#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(1000,timer,1);
    glutTimerFunc(2000,timer,2);
    glutTimerFunc(3000,timer,3);
    glutTimerFunc(4000,timer,4);
    glutTimerFunc(5000,timer,5);

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

3-4.
#include <GL/glut.h>
#include <stdio.h>
#include <mmsystem.h>

void timer(int t){
    printf("起床,現在時間 : %d\n",t);
    PlaySound("do.wav",NULL,SND_ASYNC);
    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();
}
會有噹噹聲




2022年5月17日 星期二

ఠ_ఠ week13

 

week13

(1)

1-1.開 CodeBlocks/GLUT/GLUT
1-2.貼十行程式,把茶壺換Rect();  ->方形
#include <GL/glut.h>

void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glRectf(0.3,0.5,-0.3,-0.5);
    glutSwapBuffers();
}

int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutInitWindowSize(600,600);
    glutCreateWindow("Week13 rect");
    glutDisplayFunc(display);
    glutMainLoop();
}


1-3.刪註解 -->加顏色跟手臂 改軸心 加角度手掛回身體
#include <GL/glut.h>
float angle=45;
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,1);
        ///glTranslatef(-0.3,-0.4,0);把軸心放中心
        glColor3f(1,0,0);///紅
        glRectf(0.3,0.5,0.7,0.3);
    glPopMatrix();
    glutSwapBuffers();
}

int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    ///glutInitWindowSize(600,600);
    glutCreateWindow("Week13 rect");
    glutDisplayFunc(display);
    glutMainLoop();
}

1-4.按滑鼠可以旋轉手臂
#include <GL/glut.h>
float angle=45,oldX=0;
void mouse(int button,int state,int x,int y){
    oldX=x;
}
void motion(int x,int y){
    angle +=(x-oldX);
    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,1);
        glTranslatef(-0.3,-0.4,0);
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);
    glPopMatrix();
    glutSwapBuffers();
}

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

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

(2)

2-1.
#include <GL/glut.h>
float angle=0,oldX=0;
void mouse(int button,int state,int x,int y){
    oldX=x;
}
void motion(int x,int y){
    angle +=(x-oldX);
    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,1);
        glTranslatef(-0.3,-0.4,0);
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);

        glPushMatrix();
            ///glTranslatef(0.3,0.4,0);
            ///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();

    glPopMatrix();
    glutSwapBuffers();
}

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

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



2-2.
#include <GL/glut.h>
float angle=45,oldX=0;
void mouse(int button,int state,int x,int y){
    oldX=x;
}
void motion(int x,int y){
    angle +=(x-oldX);
    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,1);
        glTranslatef(-0.3,-0.4,0);
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);

        glPushMatrix();
            glTranslatef(0.7,0.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();
    glutSwapBuffers();
}

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

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


2-3.
#include <GL/glut.h>
float angle=45,oldX=0;
void mouse(int button,int state,int x,int y){
    oldX=x;
}
void motion(int x,int y){
    angle +=(x-oldX);
    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,1);
        glTranslatef(-0.3,-0.4,0);
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);

        glPushMatrix();
            glTranslatef(0.7,0.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();

    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,0.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();
    glutSwapBuffers();
}

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

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









2022年5月10日 星期二

ఠ_ఠ week12

 

week12

(1)

1-1. 在 https://jsyeh.org/3dcg10,下載data , win32

1-2. 解壓縮,把data放到windows裡

1-3.開Transformation.exe

1-4.程式碼順序不同移動位置不同



(2)

2-1.開 120.125.80.50/GL 期中考題、小考題

2-2.點 ToDraw,可在左邊黑色方塊畫圖

2-3.ctrl+R Reload可以清空

2-4.畫身體跟手臂,改程式碼順序,按angle=再按空白建,會自動旋轉


2-5.點 glTranslatef ,可以移動左邊的東西

(3)

3-1.開 CodeBlocks/GLUT/GLUT
3-2.貼十行程式

#include <GL/glut.h>

float angle=0;

void display()

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glPushMatrix();

        ///glTranslatef(0,0,0);

        glRotatef(angle,0,0,1);

        ///glTranslatef(0,0,0);

        glutSolidTeapot(0.3);

    glPopMatrix();

    glutSwapBuffers();

    angle+=0.2;

}

int main(int argc,char**argv)

{

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("Week12 TRT");

    glutIdleFunc(display);

    glutDisplayFunc(display);

    glutMainLoop();

}



3-3.

#include <GL/glut.h>

float angle=0;

void display()

{             -->上手臂               -->下手臂

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glColor3f(1,1,1);///白

    glutSolidTeapot(0.3);///身體茶壺

    glPushMatrix();

        glTranslatef(0.4,0,0);///把茶壺掛右邊

        glRotatef(angle,0,0,1);///旋轉

        glTranslatef(0.2,0,0);///軸心放中間

        glColor3f(1,0,0);///紅

        glutSolidTeapot(0.2);///手臂茶壺

        glPushMatrix();

            glTranslatef(0.4,0,0);///把茶壺掛右邊

            glRotatef(angle,0,0,1);///旋轉

            glTranslatef(0.2,0,0);///軸心放中間

            glColor3f(1,0,0);///紅

            glutSolidTeapot(0.2);///手臂茶壺

        glPopMatrix();

    glPopMatrix();

    glutSwapBuffers();

    angle+=0.1;

}

int main(int argc,char**argv)

{

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("Week12 TRT");


    glutIdleFunc(display);

    glutDisplayFunc(display);

    glutMainLoop();

}

3-4.加左手臂
#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glColor3f(1,1,1);///白
    glutSolidTeapot(0.3);///身體茶壺

    glPushMatrix();///右手臂
        glTranslatef(0.4,0,0);///把茶壺掛右邊
        glRotatef(angle,0,0,1);///旋轉
        glTranslatef(0.2,0,0);///軸心放中間
        glColor3f(1,0,0);///紅
        glutSolidTeapot(0.2);///手臂茶壺
        glPushMatrix();
            glTranslatef(0.4,0,0);///把茶壺掛右邊
            glRotatef(angle,0,0,1);///旋轉
            glTranslatef(0.2,0,0);///軸心放中間
            glColor3f(1,0,0);///紅
            glutSolidTeapot(0.2);///手臂茶壺
        glPopMatrix();
    glPopMatrix();

    glPushMatrix();///左手臂
        glTranslatef(-0.4,0,0);///把茶壺掛右邊
        glRotatef(-angle,0,0,1);///旋轉
        glTranslatef(-0.2,0,0);///軸心放中間
        glColor3f(1,0,0);///紅
        glutSolidTeapot(0.2);///手臂茶壺
        glPushMatrix();
            glTranslatef(-0.4,0,0);///把茶壺掛右邊
            glRotatef(-angle,0,0,1);///旋轉
            glTranslatef(-0.2,0,0);///軸心放中間
            glColor3f(1,0,0);///紅
            glutSolidTeapot(0.2);///手臂茶壺
        glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
    angle+=0.05;
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Week12 TRT");

    glutIdleFunc(display);
    glutDisplayFunc(display);
    glutMainLoop();
}








2022年5月3日 星期二

ఠ_ఠ week11

 week11

(1)

1-1.開CodeBlocks
1-2.File - New - Project ,開GLUT
1-3.設定settings/Compiler
         (1)Search diedctories
           <1>Compiler
                 Add - C: \ OpenCV2.1 \ include
           <2>Linker
                 Add - C: \ OpenCV2.1 \ lib
        (2)Linker settings
             Add - cv210 & cxcore210 & highgui210
1-4.把gundam/data放到工作執行目錄freeglut/bin
1-5.顯示圖
#include <opencv/highgui.h>

int main()
{
    IplImage*img =cvLoadImage("data/Diffuse.jpg");
    cvShowImage("week11",img);
    cvWaitKey(0);
}


(2)

2-1.貼上周程式(貼圖在茶壺上)
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <GL/glut.h>
int myTexture(char * filename)
{
    IplImage * img = cvLoadImage(filename);
    cvCvtColor(img,img, CV_BGR2RGB);
    glEnable(GL_TEXTURE_2D);
    GLuint id;
    glGenTextures(1, &id);
    glBindTexture(GL_TEXTURE_2D, id);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    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-2.把 glm.c & glm.h & transformation.c 加到 week11_gundam ,把glm.c改附檔名
       -->glm.cpp
2-3.加程式碼 codeblocks / week11_gundam 按右鍵 add file 加glm.cpp
2-4.寫程式碼
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <GL/glut.h>
#include "glm.h"///在專業目錄找
GLMmodel * pmodel = NULL ;///指標,指到模型,是空指標
int myTexture(char * filename)
{
    IplImage * img = cvLoadImage(filename);
    cvCvtColor(img,img, CV_BGR2RGB);
    glEnable(GL_TEXTURE_2D);
    GLuint id;
    glGenTextures(1, &id);
    glBindTexture(GL_TEXTURE_2D, id);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    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);
    if( pmodel == NULL){
        pmodel=glmReadOBJ("data/Gundam.obj") ; ///讀模型
        glmUnitize(pmodel) ; ///換成Unit單位大小
        glmFacetNormals(pmodel) ; ///重新計算法向量
        glmVertexNormals(pmodel,90) ; ///重新計算模型頂點的法向量
    }
    glmDraw(pmodel,GLM_TEXTURE) ; ///模型畫貼圖
    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-5.圖片方向不對,用小畫家把Diffuse.jpg垂直翻轉


2-6.加3D旋轉
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <GL/glut.h>
#include "glm.h"///在專業目錄找
GLMmodel * pmodel = NULL ;///指標,指到模型,是空指標
int myTexture(char * filename)
{
    IplImage * img = cvLoadImage(filename);
    cvCvtColor(img,img, CV_BGR2RGB);
    glEnable(GL_TEXTURE_2D);
    GLuint id;
    glGenTextures(1, &id);
    glBindTexture(GL_TEXTURE_2D, id);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    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;
}
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    if( pmodel == NULL){
        pmodel=glmReadOBJ("data/Gundam.obj");///讀模型
        glmUnitize(pmodel);///換成Unit單位大小
        glmFacetNormals(pmodel);///重新計算模型面的法向量
        glmVertexNormals(pmodel,90);///重新計算模型頂點的法向量
    }
    glPushMatrix();
        glRotatef(angle,0,1,0);
        glmDraw(pmodel,GLM_TEXTURE);///模型畫貼圖
    glPopMatrix();

    glutSwapBuffers();
    angle+=0.8;
}

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

    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week11 gundam");

    glutIdleFunc(display);
    glutDisplayFunc(display);
    myTexture("data/Diffuse.jpg");
    glEnable(GL_DEPTH_TEST);
    glutMainLoop();
}


(3)

3-1.開新的 GLUT/week11_TRT
3-2.寫程式 模擬手臂轉動
#include <GL/glut.h>
void hand(){
    glColor3f(0,1,0);
    glutSolidTeapot(0.2);
}
void body(){
    glColor3f(1,1,0);
    glutSolidTeapot(0.3);
}
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    body();
    glPushMatrix();
        glTranslated(0.5,0.2,0);
        glRotatef(angle,0,0,1);
        glTranslated(0.3,0,0);
        hand();
    glPopMatrix();
    glutSwapBuffers();
    angle+=0.3;
}
int main(int argc,char** argv)
{

    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week11 TRT");

    glutIdleFunc(display);
    glutDisplayFunc(display);
    glutMainLoop();
}





2022年4月26日 星期二

ఠ_ఠ week10

week10

(1)

1-1.下載Opencv 2.1.0 win32 vs2008
1-2.CodeBlocks重開
1-3.File - New - Project ,開GLUT
1-4.複製上週程式
1-5.設定settings/Compiler
         (1)Search diedctories
           <1>Compiler
                 Add - C: \ OpenCV2.1 \ include
           <2>Linker
                 Add - C: \ OpenCV2.1 \ lib
        (2)Linker settings
             Add - cv210 & cxcore210 & highgui210
1-6.檔案要放對地方!!

(2)

2-1.改程式(用begin&end)
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <GL/glut.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);
    glBegin(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();
    glutSwapBuffers();
}

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

    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week10 texture background");
    glutDisplayFunc(display);
    myTexture("1.jpg");

    glutMainLoop();
}


2-2.增加指標,把圖貼在球體上面
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <GL/glut.h>
GLUquadric * sphere =NULL;///指標,指到二次曲面
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);

    gluQuadricTexture(sphere,1);
    gluSphere(sphere,1,30,30);

    glutSwapBuffers();
}

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

    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week10 texture background");

    glutDisplayFunc(display);
    myTexture("1.jpg");

    sphere=gluNewQuadric();

    glutMainLoop();
}


2-3.讓地球旋轉
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <GL/glut.h>
GLUquadric * sphere =NULL;///指標,指到二次曲面
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;
}
float angle=0;///旋轉角度
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle,0,0,1);
        gluQuadricTexture(sphere,1);///設貼圖
        gluSphere(sphere,1,30,30);///畫圓球
    glPopMatrix();
    glutSwapBuffers();
    angle+=1;///每次display()加1度
}

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

    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week10 texture background");
    glutIdleFunc(display);///有空idle 呼叫display()
    glutDisplayFunc(display);
    myTexture("1.jpg");

    sphere=gluNewQuadric();

    glutMainLoop();
}

2-4.轉正地球

#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <GL/glut.h>
GLUquadric * sphere =NULL;///指標,指到二次曲面
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;
}
float angle=0;///旋轉角度
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(90, 1,0,0);///轉正
        glRotatef(angle, 0,0,1);
        gluQuadricTexture(sphere,1);///設貼圖
        gluSphere(sphere,1,30,30);///畫圓球
    glPopMatrix();
    glutSwapBuffers();
    angle+=0.1;///每次display()加0.1度
}

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

    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week10 texture background");

    glEnable(GL_DEPTH_TEST);///開3D測試
    glutIdleFunc(display);///有空idle 呼叫display()
    glutDisplayFunc(display);
    myTexture("1.jpg");

    sphere=gluNewQuadric();

    glutMainLoop();
}






VERY BEAUTIFUL, VERY POWERFUL

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