2012年6月17日 星期日

作業5 3D動畫程式-My Room

a)     作業要求:
這次作業老師希望盡我們所學時做出Lignting, Texture Mapping, Object Loading, Transformation and Viewing,因為我必較沒有創意,也想不出
什麼有趣的題目,毅然決然地依照作業要求實作出我的房間。
b)     實作過程:
            1.     Lighting :
在這一次作業中,我終於知道原來是我沒有做貼圖導致我作業四的燈光總是那麼暗。至於實作lighting的部分主要是利用以下function來實作。
            float light_diffuse[4] = {1.0, 1.0, 1.0, 1.0};  //散射光
            float light_ambient[4] = {1.0, 1.0, 1.0, 1.0};  //環境光
            float light_specular[4] = {0.5, 0.5, 0.5, 1.0}; //全反射光
float light_position[4] = { 0 , 100, 0, 1.0 };  //位置
2. Texture Mapping:
這是我作業四沒實做出來的部分。這次的牆壁、天花板與地板都是用texture mapping實作,主要還是藉由老師的範例程式慢慢修改而來。
先利用老師給的function讀入 .bmp檔:
bool Load_Texture(unsigned int & aTexture, char * fileName){
if(!fileName) return false; //if no file name is given return

FILE *aFile = NULL;
AUX_RGBImageRec *image_record = NULL;

if((aFile = fopen(fileName, "rb")) == NULL) return false;

image_record = auxDIBImageLoad(fileName); //Function to load BMP

if(!image_record) return false; //check that BMP loaded

glGenTextures(1, &aTexture); //generates 1 texture name

glPixelStorei (GL_UNPACK_ALIGNMENT, 1); //sets how the pixels of the texture are stored

glBindTexture(GL_TEXTURE_2D, aTexture); //binds the name to the target


//build prefiltered textures at different resolutions
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image_record->sizeX,
image_record->sizeY, GL_RGB, GL_UNSIGNED_BYTE, image_record->data);

//give the texture its attributes
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

// Now we need to free the image data that we loaded since openGL stored it as a texture
if (image_record) { //if we stored data then free the memory
if (image_record->data) { //check if there is any data
free(image_record->data); //free the memory
}
free(image_record); //free the image record
}

return true; //everything should have worked so return true
}


Load_Texture(texture[0],"floor_texture.bmp");

再將圖貼上去
glPushMatrix();

glColor3f(1.0f,1.0f,1.0f); //Set colour to White
glEnable(GL_TEXTURE_2D); //Enable texture mapping
glDisable (GL_DEPTH_TEST); //Disable depth testing
glDisable (GL_LIGHTING);
//before you can use a texture you have to bind it
glBindTexture(GL_TEXTURE_2D, texture[0]);

glBegin(GL_POLYGON);
glTexCoord2f(0.0, 1.0);
glVertex3f(-10,0,10);

glTexCoord2f(1.0, 1.0);
glVertex3f( 10,0,10);

glTexCoord2f(1.0, 0.0);
glVertex3f(10,0,-10);

glTexCoord2f(0.0, 0.0);
glVertex3f(-10,0,-10);
glEnd();

//Re-Enable the depth test
glEnable(GL_DEPTH_TEST);
//disable texture mapping
glDisable(GL_TEXTURE_2D);
glEnable (GL_LIGHTING);
glPopMatrix();

     3.Obj Loading :
               這次作業裡所有的模型都是利用obj loading讀進來的。載入obj檔方法如下:
               要貼物件必須先include"glm.h",然後

GLMmodel *glm_model;
glm_model= glmReadOBJ( "tree.obj" );//讀該物件
glmUnitize( glm_model );
glmScale( glm_model, .2 );//控制大小
glmFacetNormals( glm_model );
glmVertexNormals( glm_model, 90 );
list_id[0] = glmList( glm_model, GLM_MATERIAL | GLM_SMOOTH );
glmDelete( glm_model );



再利用glCallList( list_id[0]);就能載入物件了。


        4.Transformation and Viewing :
           遊走的部分是利用gluLookAt()來實作完成。
           glTranslatef()來將物件translate到你要的座標軸位置。


c)    心得:
       這次作業把自己這學期的所有topic都實作過了一次 ,雖然作品很陽春,但感覺還蠻扎實的      
           
     

2012年6月11日 星期一

HW4

a)實作與過程
   這次作業主要是延續上一次作業而來,首先我先去下載了一棵樹的.obj檔,範例圖片這棵樹是  彩色的但是載入後卻只有灰階。載入obj檔方法如下:
要貼物件必須先include"glm.h",然後

GLMmodel *glm_model;
     glm_model= glmReadOBJ( "tree.obj" );//讀該物件
     glmUnitize( glm_model );
     glmScale( glm_model, .2 );//控制大小
     glmFacetNormals( glm_model );
     glmVertexNormals( glm_model, 90 );
     list_id[0] = glmList( glm_model, GLM_MATERIAL | GLM_SMOOTH );
     glmDelete( glm_model );   

 再利用glCallList( list_id[0]);就能載入物件了了。

接下來就是實作打光,方法如下:


  float light_diffuse[4] = {1.0, 1.0, 1.0, 1.0};  //散射光 
  float light_ambient[4] = {1.0, 1.0, 1.0, 1.0};  //環境光 
  float light_specular[4] = {0.5, 0.5, 0.5, 1.0}; //全反射光 
  float light_position[4] = { 0 , 100, 0, 1.0 };  //位置


但是打光後,所有的建築都變成了灰黑,查完資料後發現原來是沒有貼材質的原因。


這次因為時間不夠,沒有把貼材質實作出來。




b)心得


這次貼圖沒實作出來實在很可惜,希望下次十座可以完美的呈現出材質。