2017年10月19日 星期四

周承翰_計圖學習筆記_Week06

Week06 打光lighting



一. 複習上週week05進度

step01 : 

至小葉老師網站 :http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/

下載以下三項

1. data.zip 
2. win32
3. glut32.dll


step02  :

建立一個新的glut專案檔
試圖將source.zip中的glm.c/glm.h/transformation.c複製到glut專案檔中
也把data.zip準備好

step03 :

到moodle下載week05.zip
對week05.cbp按右鍵用notepad編輯
更改freeglut路徑
並回到cbp 按下F9執行



二. 開始本周week06進度--打光

step01 : 

   source.zip 有 transformation.c 範例 (or直接用week05的程式碼)

    新增螢光筆部分 (Lighting) 程式碼到cpp中

     GLfloat pos[]={0.0,0.0,-1.0,0.0}; //偷來的打光(3)
 
     .......  

    glEnable(GL_LIGHTING);           //偷來的3D深度測試,打開Enable
    glEnable(GL_LIGHT0);                //偷來的打光(2) ,打開Enable
    glEnable(GL_DEPTH_TEST);      //偷來的打光(1) ,打開Enable
    glLightfv(GL_LIGHT0,GL_POSITION,pos);

    
    按下F9執行後結果
    
 

Step02 : 加入Mouse & Keyboard 函式

int rotateX=0,rotateY=0,rotateZ=0;
int oldx=0,oldy=0;

void keyboard (unsigned char key , int x , int y)
{
     printf("now : %c ( %d  %d)\n ",key , x , y);
     if(key=='x')rotateX++;
     if(key=='y')rotateY++;
     if(key=='z')rotateZ++;

     glutPostRedisplay();  //貼出公告,請電腦有空時,重新展示畫面
}

void mouse ( int button , int state , int x , int y)
{  
   printf("button : %d  state: %d ( %d %d )\n ",button,state,x,y);
      //mouse drag (1) down (2) drag (3) up
   if(state==GLUT_DOWN)
     {
        oldx=x;
        oldy=y;

     }
 }

void motion (int x ,int y)
{
    rotateX += -(y-oldy);
    rotateY += -(x-oldx);
    oldx=x; oldy=y;
    glutPostRedisplay();

}

.............

glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutMotionFunc(motion);



void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glRotatef(rotateX,1,0,0);
    glRotatef(rotateY,0,1,0);
    glRotatef(rotateZ,0,0,1);

    drawmodel();
    glPopMatrix();
    glutSwapBuffers();
}





Summarize : 

以下紅色部分為本周打光重點 藍色部分為回家作業

=====================程式碼部分===========================

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

GLMmodel* pmodel = NULL;
void
drawmodel(void)
{
    if (!pmodel) {
pmodel = glmReadOBJ("data/al.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
    }

    glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
}

int rotateX=0,rotateY=0,rotateZ=0;
int oldx=0,oldy=0;

void keyboard (unsigned char key , int x , int y)
{
     printf("now : %c ( %d  %d)\n " ,key , x , y );
     if(key=='x')rotateX++;
     if(key=='y')rotateY++;
     if(key=='z')rotateZ++;
     glutPostRedisplay();  //貼出公告,請電腦有空時,重新展示畫面
}

void mouse ( int button , int state , int x , int y)
{  
   printf("button : %d  state: %d ( %d %d )\n ",button,state,x,y);
      //mouse drag (1) down (2) drag (3) up
   if(state==GLUT_DOWN)
     {
        oldx=x;
        oldy=y;

     }
 }

void motion (int x ,int y)
{
    rotateX += -(y-oldy);
    rotateY += -(x-oldx);
    oldx=x; oldy=y;
    glutPostRedisplay();

}


void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    //3D深度測試,清 depth buffer,才能繪圖

    glPushMatrix();
    glRotatef(rotateX,1,0,0);
    glRotatef(rotateY,0,1,0);
    glRotatef(rotateZ,0,0,1);

    drawmodel();
    glPopMatrix();

    glutSwapBuffers();
}

GLfloat pos[]={0.0,0.0,-1.0,0.0};   //偷來的打光(3)

int main(int argc, char**argv) 
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); 
     //3D深度測試,有準備好的depth buffer,才能繪圖
    glutCreateWindow("week05");

    glutDisplayFunc(display);

    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);
    glutMotionFunc(motion);
    
    

    glEnable(GL_LIGHTING);              //偷來的3D深度測試,打開Enable
    glEnable(GL_LIGHT0);                   //偷來的打光(2) ,打開Enable
    glEnable(GL_DEPTH_TEST);        //偷來的打光(1) ,打開Enable
    glLightfv(GL_LIGHT0,GL_POSITION,pos);

    glutMainLoop();
}
========================程式碼結束========================

keyboard 執行結果


mouse&motion 執行結果






沒有留言:

張貼留言