2022年3月29日 星期二

一定是大拇指的啦

 step 0 :下載模型

0.0  jsyeh.org/3dcg10 下載  data.zip  , windows.zip

0.1 data.zip =>下載/windows/data

     windows.zip => 下載/windows/Transformation.exe

step 1 : 打光

1.1 開啟 window/Light & Material

1.2 程式碼介紹 :
        1.glLightfv(...)中fv是float vector(陣列)
        2.GLfloat light_pos[]={....}; 陣列
        3.glLightfv(GL_LIGHT0  ,   GL_POSITION  ,  陣列 )
                             第幾個燈           設定它的位置
        4.光的性質 ( 位置  ,   Ambient  ,      Diffuse     ,       Specular ) 大概翻譯
                                          無處不在的光                           特別的光
                                                                     有角度的光


step 2 : 實作

2.1 開新專案 , 命名為week06_light

2.2 Ctrl_F,搜尋light

2.3 留下開頭陣列及main函式

2.4 貼上程式碼

#include <GL/glut.h>
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 }; /// -5.0f是亮的 , 5.0是暗的

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 };
void display()///茶壺
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
        glColor3f(1,1,0);
        glutSolidTeapot(0.3);
    glutSwapBuffers();
}


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

    glutDisplayFunc(display);
    ///留下的程式碼 , 要放在glutCreateWindow後才會有效
    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()之前
    glutMainLoop();///否則卡在這裡,之後的程式,都不會執行到

}


Step 3 : 加入mouse , motion , rotate函式
 3.1 開新專案 , 命名為week06_light_mouse_motion

 3.2 貼上上週程式碼( mouse , motion , 初始值 )

#include <GL/glut.h>
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 x=150 , y=150 , z=0 ,scale=0.1;
float oldX=0 , oldY=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glPushMatrix();
        glTranslatef( (x-150)/150.0, -(y-150)/150.0, z);
        glScalef(scale , scale , scale);///x, y, z 等比例放大
        glColor3f(1,1,0);
        glutSolidTeapot( 0.8 );
    glPopMatrix();
    glutSwapBuffers();
}
void mouse(int button , int state , int mouseX , int mouseY)
{
    oldX=mouseX;
    oldY=mouseY;
}
void motion (int mouseX ,int mouseY)
{
    if ( mouseX-oldX > 0 ) scale *= 1.01; ///滑鼠往右放大1.01倍
    if ( mouseX-oldX < 0 ) scale *= 0.99;///滑鼠往左縮小0.99倍
    /// x+=(mouseX-oldX);  y+=(mouseY-oldY); ///移動
    oldX=mouseX;            oldY=mouseY;
    display();

}

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

    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
    glutMainLoop();

}

3.3 新增角度程式碼

#include <GL/glut.h>
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 x=150 , y=150 , z=0 ,scale=0.5 , angle=0.1; ///新增angle
float oldX=0 , oldY=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glPushMatrix();
        glTranslatef( (x-150)/150.0, -(y-150)/150.0, z);
        glRotatef(angle , x, y ,z);///轉angle角度
        glScalef(scale , scale , scale);
        glColor3f(1,1,0);
        glutSolidTeapot( 0.8 );
    glPopMatrix();
    glutSwapBuffers();
}
void mouse(int button , int state , int mouseX , int mouseY)
{
    oldX=mouseX;
    oldY=mouseY;
}
void motion (int mouseX ,int mouseY)
{
    angle += (mouseX - oldX);///angle角度大小
    ///if ( mouseX-oldX > 0 ) scale *= 1.01; ///縮放
    ///if ( mouseX-oldX < 0 ) scale *= 0.99;
    /// x+=(mouseX-oldX);  y+=(mouseY-oldY); ///移動
    oldX=mouseX;
    oldY=mouseY;
    display();

}

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

    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);
    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();

}
step 4 : 整合light , mouse  , motion , rotate函式
4.1開新專案 命名為week06_light_mouse_motion_rotate

4.2新增keyboard函式 (按鍵盤旋轉用)

4.3 貼上程式碼

#include <GL/glut.h>
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 x=150 , y=150 , z=0 ,scale=0.5 , angle=0.1;
float oldX=0 , oldY=0 ,now=1; ///now : 1.移動 , 2.轉動 , 3.縮放
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glPushMatrix();
        glTranslatef( (x-150)/150.0, -(y-150)/150.0, z);
        glRotatef(angle , x, y ,z);
        glScalef(scale , scale , scale);
        glColor3f(1,1,0);
        glutSolidTeapot( 0.8 );
    glPopMatrix();
    glutSwapBuffers();
}
void keyboard(unsigned char key , int mouseX , int mouseY)
{
    if ( key=='1' || key=='w' || key=='W')  now=1;  ///移動
    if ( key=='2' || key=='e' || key=='E')    now=2;  ///轉動
    if ( key=='3' || key=='r' || key=='R')   now=3;  ///縮放
}
void mouse(int button , int state , int mouseX , int mouseY)
{
    oldX=mouseX;
    oldY=mouseY;
}
void motion (int mouseX ,int mouseY)
{
    if ( now == 1 ){ ///移動

        x+=(mouseX-oldX);  y+=(mouseY-oldY);    }
    else if ( now == 2 ){///轉動
        angle += (mouseX - oldX);
    }
    else if ( now == 3 ){///縮放
        if ( mouseX-oldX > 0 ) scale *= 1.01;
        if ( mouseX-oldX < 0 ) scale *= 0.99;

    }
    oldX=mouseX;
    oldY=mouseY;
    display();

}

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

    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);
     glutKeyboardFunc(keyboard);///keyboard用
    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();

}

沒有留言:

張貼留言

VERY BEAUTIFUL, VERY POWERFUL

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