1 / 55

บทที่ 5 Texture Mapping

บทที่ 5 Texture Mapping. Texture Mapping. ทำได้ 2 วิธี - Surface detail polygons: กำหนด Polygon แต่ละตัวตามสีที่ต้องการ - ถ้าภาพที่ต้องการซับซ้อน  Speed down! - รูปบางอย่างสร้างยาก(หาโมเดลไม่ได้) Map a texture to the surface ( ใช้ทั่วไป ).

Download Presentation

บทที่ 5 Texture Mapping

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. บทที่ 5 Texture Mapping

  2. Texture Mapping • ทำได้ 2 วิธี - Surface detail polygons: กำหนด Polygon แต่ละตัวตามสีที่ต้องการ - ถ้าภาพที่ต้องการซับซ้อน Speed down! - รูปบางอย่างสร้างยาก(หาโมเดลไม่ได้) • Map a texture to the surface (ใช้ทั่วไป) ความซับซ้อนของภาพไม่มีผลต่อการจัดการทางเรขาคณิต (transformation, clipping…)

  3. Texture Mapping • เทคโนโลยีที่เอาภาพ(image)ไปแปะบนพื้นผิวของวัตถุ • ไม่ต้องทำModelingให้ซับซ้อนแต่ก็สร้างภาพที่ดูแล้วเหมือนจริงได้

  4. 3. patch texel 2. texture lookup t S Texture Mapping 1. projection 3D geometry 2D projection of 3D geometry 2D image

  5. Bump Mapping

  6. Mapping

  7. Environment Mapping

  8. Texture Mapping + =

  9. = +

  10. t s ตัวอย่าง • เอาภาพมาแปะที่ตัวคน

  11. (1,0) (1,1) (1,0) (0,0) Map textures to surfaces

  12. Texture • Texture = ข้อมูลของ pixelที่บันทึกไว้ในรูปแบบของ array • Pixelของ textureเราเรียกว่า “textel” • Texture coordinate: (s,t) • (s,t):s และ t จะมีค่า 0~1

  13. (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]

  14. 600 400 Texture Mapping t s texel[j][k]: เก็บข้อมูลสีของ texture j:0~599, k:0~399

  15. 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]

  16. ys t y xs s x z Texture Mapping • การเอาภาพ(image)ไปแปะบน polygon

  17. ys t y xs s x z Texture Mapping

  18. 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

  19. Texture Component • 1 component texture: จะแสดงความสว่างของแสงเท่านั้น เช่น ไม้ สนามหญ้า หาดทรายขาว

  20. Texture Component • 2 component texture: จะแสดงความสว่างของแสงและความโปร่งใสเช่น เมฆ

  21. Texture Component • 3 component texture: จะแสดงสีของแสง(RGB)

  22. Texture Component • 4component texture: จะแสดงสีของแสงและค่าความโปร่งใส(RGBA)

  23. การกำหนดค่าต่างๆของ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

  24. การกำหนดค่าต่างๆของ 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)

  25. การกำหนดค่าต่างๆของ 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

  26. 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;} } } }

  27. Generate Texture 3 component texture 4 component texture

  28. การกำหนดค่าต่างๆของ Texture glTexImage2D( GL_ TEXTURE_ 2D, 0, 4, Width, Height,0, GL_ RGBA, GL_ UNSIGNED_ BYTE,textureImage1 );

  29. จำนวน 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);

  30. 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

  31. texel Texture Parameter Magnification Minification pixel Polygon texture texture Polygon

  32. Magnification Parameter GL_NEAREST: เลือก texelที่ใกล้ที่สุด GL_LINEAR: หาค่าเฉลี่ยของ texel รอบๆ 4 texel

  33. Minification

  34. Mipmap • การขยายหรือย่อขนาดของ texture ทำให้เกิดการกระพริบเวลา reshape • สร้าง texture ที่มีขนาดหลายๆขนาดเตรียมเอาไว้ • OpenGL จะเลิกใช้ mipmap ที่เหมาะสมโดยอัตโนมัติ

  35. 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() { .... }

  36. 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); .... }

  37. (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

  38. 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);

  39. Texture Parameter

  40. การกำหนดตำแหน่งของ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();

  41. TextureVertex 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();

  42. TextureVertex 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();

  43. Enable (Disable) Textures • Enable texture – glEnable(GL_TEXTURE_2D) • Disable texture – glDisable(GL_TEXTURE_2D)

  44. 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 ….

  45. Display List • ก้อนคำสั่งทางกราฟฟิก • เก็บเอาไว้ใน memory • จะเสียเวลานานนิดนึงในการสร้าง display list • การเรนเดอร์ภาพจะเร็วขึ้นมาก

  46. Display List เรียกใช้คำสั้งวาดภาพที่ เก็บไว้ใน display list Display List Model glBegin(GL_LINES); glVertex(…); …. glEnd();

  47. คุณสมบัติของ display list • เร็วเพราะไม่ต้องเสียเวลาคำนวณใหม่ • สับเปลี่ยน texture หลายๆแบบได้ • ออกแบบโมเดลที่มีความสัมพันธ์แบบ Tree ได้

  48. วิธีการสร้าง Display List Gluint glGenLists(int count); • สร้างและกำหนดชื่อ display list • คืน ID(ชื่อ) • count คือจำนวนของ display list • ที่ต้องการสร้าง • Glboolean glIslist(Gluint list); • ตรวจสอบว่าชื่อ list ถูกใช้แล้วหรือยัง

  49. วิธีการใช้ 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();

More Related