550 likes | 815 Views
บทที่ 5 Texture Mapping. Texture Mapping. ทำได้ 2 วิธี - Surface detail polygons: กำหนด Polygon แต่ละตัวตามสีที่ต้องการ - ถ้าภาพที่ต้องการซับซ้อน Speed down! - รูปบางอย่างสร้างยาก(หาโมเดลไม่ได้) Map a texture to the surface ( ใช้ทั่วไป ).
E N D
Texture Mapping • ทำได้ 2 วิธี - Surface detail polygons: กำหนด Polygon แต่ละตัวตามสีที่ต้องการ - ถ้าภาพที่ต้องการซับซ้อน Speed down! - รูปบางอย่างสร้างยาก(หาโมเดลไม่ได้) • Map a texture to the surface (ใช้ทั่วไป) ความซับซ้อนของภาพไม่มีผลต่อการจัดการทางเรขาคณิต (transformation, clipping…)
Texture Mapping • เทคโนโลยีที่เอาภาพ(image)ไปแปะบนพื้นผิวของวัตถุ • ไม่ต้องทำModelingให้ซับซ้อนแต่ก็สร้างภาพที่ดูแล้วเหมือนจริงได้
3. patch texel 2. texture lookup t S Texture Mapping 1. projection 3D geometry 2D projection of 3D geometry 2D image
Texture Mapping + =
= +
t s ตัวอย่าง • เอาภาพมาแปะที่ตัวคน
(1,0) (1,1) (1,0) (0,0) Map textures to surfaces
Texture • Texture = ข้อมูลของ pixelที่บันทึกไว้ในรูปแบบของ array • Pixelของ textureเราเรียกว่า “textel” • Texture coordinate: (s,t) • (s,t):s และ t จะมีค่า 0~1
(1,1) t s (0,0) Texture Representation • Bitmap (pixel map) textures (supported by OpenGL) • Bitmap texture: • เป็นภาพ2มิติแสดงได้ด้วย 2D array texture[height][width] • แต่ละเซ็ล (texel ) จะมีคู่กับจุดtexture coordinate (s, t) • s=[0,1], t=[0,1]
600 400 Texture Mapping t s texel[j][k]: เก็บข้อมูลสีของ texture j:0~599, k:0~399
600 400 t s Texture Mapping Color3 texture(float s,float t){ return texel[(int)s*W][(int)t*H]; } texture(0,0)texel[0][0] texture(0,1)texel[0][399] texture(1,0)texel[599][0] texture(1,1)texel[599][399] texture(0.261,0.783)texel[156][313]
ys t y xs s x z Texture Mapping • การเอาภาพ(image)ไปแปะบน polygon
ys t y xs s x z Texture Mapping
t t s = xs (1,1) (1,1) t = ys s s ys (3/2,1) s = 2xs /3 t = ys xs Texture Mapping ys (1,1) xs
Texture Component • 1 component texture: จะแสดงความสว่างของแสงเท่านั้น เช่น ไม้ สนามหญ้า หาดทรายขาว
Texture Component • 2 component texture: จะแสดงความสว่างของแสงและความโปร่งใสเช่น เมฆ
Texture Component • 3 component texture: จะแสดงสีของแสง(RGB)
Texture Component • 4component texture: จะแสดงสีของแสงและค่าความโปร่งใส(RGBA)
การกำหนดค่าต่างๆของTextureTexture Setup GLvoid glTexImage2D(Glenum target,GLint level,GLint components, GLsizei width,GLsizei height,GLint border,GLenum format, GLenum type,const GLvoid *image); • target เป็น GL_TEXTURE_2D • level(mipmap level) 0: ภาพที่เป็น original i: ภาพ mipmap ลำดับที่ i
การกำหนดค่าต่างๆของ Texture GLvoid glTexImage2D(Glenum target,GLint level,GLint components, GLsizei width,GLsizei height,GLint border,GLenum format, GLenum type,const GLvoid *image); • Component(1~4) • Width(2^m) • Height(2^n) • Border(0 or 1)
การกำหนดค่าต่างๆของ Texture GLvoid glTexImage2D(Glenum target,GLint level,GLint components, GLsizei width,GLsizei height,GLint border,GLenum format, GLenum type,const GLvoid *image); • Format เช่น GL_RGB,GL_RGBA • type ประเภทของ texel เช่น GL_BYTE,GL_UNSIGNED_BYTE • Image ภาพที่จะนำมาทำเป็น texture มีขนาด 2^m x 2^n
Generate Texture #define ImageWidth 8 #define ImageHeight 8 static Glubyte textureImage1[ Width][ Height][ 4]; void generateTexture1() { int i, j, c; for (i= 0; i< ImageWidth; i++) for (j= 0; j< ImageHeight; j++) { c = (( i& 0x8)^( j& 0x8)) * 255; textureImage1[ i][ j][ 0] = (GLubyte) c; textureImage1[ i][ j][ 1] = (GLubyte) c; textureImage1[ i][ j][ 2] = (GLubyte) c; textureImage1[ i][ j][ 3] = 255;} } } }
Generate Texture 3 component texture 4 component texture
การกำหนดค่าต่างๆของ Texture glTexImage2D( GL_ TEXTURE_ 2D, 0, 4, Width, Height,0, GL_ RGBA, GL_ UNSIGNED_ BYTE,textureImage1 );
จำนวน texture object มี 1อัน มี ID คือ? สร้าง texture object กำหนดค่าต่างๆเช่น format … เรียกใช้ texture ชื่อ textureID Texture Object GLuint textureID; glGenTextures(1, &textureID); glBindTexture(GL_TEXTURE_2D, textureID); glTexImage2D(GL_TEXTURE_2D, …); glBindTexture(GL_TEXTURE_2D, textureID);
Texture Parameter void glTexParameteri( GLenum target,GLenum pname,TYPE param ); target : GL_ TEXTURE_ WRAP_ S GL_ TEXTURE_ WRAP_ T GL_ TEXTURE_ MAG_ FILTER GL_ TEXTURE_ MIN_ FILTER GL_ TEXTURE_ BORDER_ COLOR GL_ TEXTURE_ PRIORITY
texel Texture Parameter Magnification Minification pixel Polygon texture texture Polygon
Magnification Parameter GL_NEAREST: เลือก texelที่ใกล้ที่สุด GL_LINEAR: หาค่าเฉลี่ยของ texel รอบๆ 4 texel
Mipmap • การขยายหรือย่อขนาดของ texture ทำให้เกิดการกระพริบเวลา reshape • สร้าง texture ที่มีขนาดหลายๆขนาดเตรียมเอาไว้ • OpenGL จะเลิกใช้ mipmap ที่เหมาะสมโดยอัตโนมัติ
Mipmap GLubyte mipmapImage32[ 32][ 32][ 4]; GLubyte mipmapImage16[ 16][ 16][ 4]; GLubyte mipmapImage8[ 8][ 8][ 4]; GLubyte mipmapImage4[ 4][ 4][ 4]; GLubyte mipmapImage2[ 2][ 2][ 4]; GLubyte mipmapImage1[ 1][ 1][ 4]; void createMipMaps() { .... }
Mipmap void createMipMaps() { .... glTexImage2D( GL_ TEXTURE_ 2D, 0, GL_ RGBA, 32, 32, 0, GL_ RGBA, GL_ UNSIGNED_ BYTE,mipmapImage32); glTexImage2D( GL_ TEXTURE_ 2D, 1, GL_ RGBA, 16, 16, 0, GL_ RGBA, GL_ UNSIGNED_ BYTE,mipmapImage16); glTexImage2D( GL_ TEXTURE_ 2D, 2, GL_ RGBA, 8, 8, 0, GL_ RGBA, GL_ UNSIGNED_ BYTE,mipmapImage8); .... }
(2,2) (2,2) (1,1) (0,0) (0,0) (0,0) GL_Clamp If (s >1) s = 1 If (t >1) t = 1 texture GL_Repeat Texture Parameter
Texture Parameter 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_ WRAP_ S,GL_ CLAMP); glTexParameteri( GL_ TEXTURE_ 2D, GL_ TEXTURE_ WRAP_ T,GL_ CLAMP);
การกำหนดตำแหน่งของtexture บน polygon glBegin( GL_QUADS ); glTexCoord2fv( t0 ); glVertex3fv( v0 ); glTexCoord2fv( t1 ); glVertex3fv( v1 ); glTexCoord2fv( t2 ); glVertex3fv( v2 ); glTexCoord2fv( t3 ); glVertex3fv( v3 ); glEnd(); glBegin( GL_QUADS ); glTexCoord2fv(0.0,0.0 ); glVertex3fv(-2.0,0.0,0.0); glTexCoord2fv(1.0,0.0); glVertex3fv(0.0,-2.0,0.0); glTexCoord2fv(1.0,1.0); glVertex3fv(2.0,0.0,0.0); glTexCoord2fv(0.0,1.0); glVertex3fv(0.0,2.0,0.0); glEnd();
TextureVertex glBegin( GL_ QUADS); glTexCoord2d( 0. 0, 0.0 ); glVertex3d( x0, y0, z0 ); glTexCoord2d( 1. 0, 0.0 ); glVertex3d( x1, y1, z1 ); glTexCoord2d( 1. 0, 1.0 ); glVertex3d( x2, y2, z2 ); glTexCoord2d( 0. 0, 1.0 ); glVertex3d( x3, y3, z3 ); glEnd();
TextureVertex glBegin( GL_ QUADS); glTexCoord2d( 0. 0, 0. 0 ); glVertex3d( x0, y0, z0 ); glTexCoord2d( 1. 0, 0. 0 ); glVertex3d( x1, y1, z1 ); glTexCoord2d( 1. 0, 1. 0 ); glVertex3d( x2, y2, z2 ); glTexCoord2d( 0. 0, 1. 0 ); glVertex3d( x3, y3, z3 ); glEnd(); glBegin( GL_ QUADS); glTexCoord2d( 0. 0, 0. 0 ); glVertex3d( x0, y0, z0 ); glTexCoord2d( 4. 0, 0. 0 ); glVertex3d( x1, y1, z1 ); glTexCoord2d( 4. 0, 4. 5 ); glVertex3d( x2, y2, z2 ); glTexCoord2d( 0. 0, 4. 5 ); glVertex3d( x3, y3, z3 ); glEnd();
Enable (Disable) Textures • Enable texture – glEnable(GL_TEXTURE_2D) • Disable texture – glDisable(GL_TEXTURE_2D)
Put it all together … 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); … glEnable(GL_TEXTURE_2D); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, mytexture); Draw_picture1(); // define texture coordinates and vertices in the function ….
Display List • ก้อนคำสั่งทางกราฟฟิก • เก็บเอาไว้ใน memory • จะเสียเวลานานนิดนึงในการสร้าง display list • การเรนเดอร์ภาพจะเร็วขึ้นมาก
Display List เรียกใช้คำสั้งวาดภาพที่ เก็บไว้ใน display list Display List Model glBegin(GL_LINES); glVertex(…); …. glEnd();
คุณสมบัติของ display list • เร็วเพราะไม่ต้องเสียเวลาคำนวณใหม่ • สับเปลี่ยน texture หลายๆแบบได้ • ออกแบบโมเดลที่มีความสัมพันธ์แบบ Tree ได้
วิธีการสร้าง Display List Gluint glGenLists(int count); • สร้างและกำหนดชื่อ display list • คืน ID(ชื่อ) • count คือจำนวนของ display list • ที่ต้องการสร้าง • Glboolean glIslist(Gluint list); • ตรวจสอบว่าชื่อ list ถูกใช้แล้วหรือยัง
วิธีการใช้ Display List Glvoid glNewList(Gluinit Listindex,Glenum Mode); …… Glvoid glEndList(); Listindex: ชื่อ display list Mode: GLCOMPILE, GLCOMPILE_AND_EXECUTE glNewList( 1, GL_COMPILE ); glColor3fv( colorVector ); glTranslatef( 1.5, 0.0, 0.0 ); glBegin( GL_TRIANGLES ); glVertex2f( 0.0, 0.0 ); glVertex2f( 1.0, 0.0 ); glVertex2f( 0.0, 1.0 ); glEnd(); glEndList();