1 / 54

310482: Graphics Programming

310482: Graphics Programming. Windows ,Viewports and Clipping Lecture 8. World Coordinate. ระบบพิกัดโลก(World Coordinate) เป็นระบบที่ใช้อธิบายตำแหน่งของภาพวัตถุในธรรมชาติ

derry
Download Presentation

310482: Graphics Programming

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. 310482: Graphics Programming Windows ,Viewports and Clipping Lecture 8 110/15/2014

  2. World Coordinate • ระบบพิกัดโลก(World Coordinate) เป็นระบบที่ใช้อธิบายตำแหน่งของภาพวัตถุในธรรมชาติ • ไม่ขึ้นกับขนาดพิกัดของจอภาพ (Screen Coordinate)ช่วงตัวเลขจะเป็นเท่าไรก็ได้ (เป็นบวกหรือลบ หรือทศนิยมได้) ใช้อธิบายตำแหน่งภาพวัตถุได้ถูกต้อง • ถ้าภาพมีขนาดใหญ่ ซับซ้อน จนไม่สามารถแสดงให้เห็นได้ชัดเจนบนจอภาพ อาจแสดงได้เฉพาะบางส่วนภายในกรอบสี่เหลียมที่กำหนดให้เรียกว่าการทำช่องหน้าต่าง (windowing) และกรอบสี่เหลี่ยมที่ใช้แสดงภาพเรียกว่า หน้าต่าง (window) 210/15/2014

  3. Coordinate Systems • ส่วนต่างๆของภาพจะถูกนำแสดงบนจอภาพได้โดยการนำหน้าต่างไปวางไว้ที่ส่วนของภาพในพิกัดโลก • การปรับเปลี่ยนขนาดของหน้าต่างจะทำให้เกิดการย่อ ขยาย หรือการบิดเบี้ยวภาพที่อยู่ในหน้าต่างได้ • ส่วนของภาพที่อยู่นอกเหนือหน้าต่างจะไม่ถูกนำมาแสดงบนจอภาพ เรียกขั้นตอนการตัดส่วนเกินออกนี้ว่า การขริบภาพ(Clipping) 310/15/2014

  4. Viewports • A viewport is a rectangle on the display where the model is actually going to be drawn • viewport ใช้แสดงส่วนของภาพหลายๆภาพบนจอเดียวกันและในเวลาเดียวกัน ให้เห็นทุกส่วนพร้อมกัน โดยแบ่งจอภาพเป็นส่วนๆ • การเลือกหน้าต่างและช่องแสดงภาพ(viewport)แตกต่างกันทำให้เกิดการแสดงภาพบนจอภาพได้หลายแบบ 410/15/2014

  5. Window to viewport transformation • A viewing transformation using standard rectangles for the window and viewport 510/15/2014

  6. Windows and Viewports • กรณีของ window กรอบโต รูปที่เห็นจะเป็นรูปเล็ก • กรณีของ viewport ถ้ากำหนด window คงที่ • viewport ขนาดใหญ่ รูปที่เห็นจะมีขนาดใหญ่ • viewport ขนาดเล็ก รูปที่เห็นจะมีขนาดเล็ก • coordinates ของ window คือ World Coordinates • coordinates ของ viewportคือ Screen Coordinates 610/15/2014

  7. The two-dimensional viewing-transfomation pipeline 710/15/2014

  8. The viewing pipeline • Setting up a rotate world window in viewing coordinates and the corresponding normalized-coordinate viewport 810/15/2014

  9. Viewing coordinate reference frame • กำหนด P0 =(x0,y0) ต้องการแปลงสภาพของจุด • สร้างเวกเตอร์ V โดยกำหนด view เป็น Vy เวกเตอร์ V เรียกว่า view up vector • สามารถคำนวณ component ของunit vector v =(vx,vy)และ u =(ux,uy) สำหรับ view yvและ xv • unit vector ใช้เป็นแถวแรกและแถวที่สองของ Rotation matrix R ซึ่งดำเนินการกับแกน view xvyvและ xwyw • เราได้matrix ที่ convert WC position ไปเป็น VC position ดังนี้ a) translate the viewing origin to the world origin and b) rotate to align the two coordinate reference frame Mwc,vc = R .T 910/15/2014

  10. Viewing coordinate reference frame • A viewing-coordinate frame is moved into coincidence with the world frame in two steps:(a) translate the viewing origin to the world origin, then (b) rotate to align the axes of the two systems 1010/15/2014

  11. Window-to-viewport mapping • จุดมีพิกัดที่ (xw, yw) ใน windowถูกแมปไปที่ตำแหน่ง (xv, yv) ใน viewport ดังนั้นจะได้ว่า 1110/15/2014

  12. Window-to-viewport mapping • แก้สมการหาค่า(xv, yv) ได้ดังนี้ • เมื่อ 1210/15/2014

  13. Window-to-viewport mapping • จากสมการสามารถที่ใช้เซตของการแปลงเพื่อ converts window area เป็น viewports area โดยใช้การแปลงดังนี้ • ใช้ scaling transformationกับจุดของ(xwmin , ywmin) เพื่อ scale window area ไปเป็นviewport ที่มีขนาดที่ต้องการ • ย้าย scale window area ไปณ ตำแหน่งของ viewport 1310/15/2014

  14. workstation transfomations • Mapping selected parts of a scence in normalized coordinates to different video monitors with workstation transfomations 1410/15/2014

  15. Normallized Device Coordinates • เป็นระบบพิกัดซึ่งไม่ขึ้นอยู่กับขนาดของจอภาพ • ให้ค่าแกน x และแกน y มีค่าอยู่ระหว่าง 0 กับ 1 • มีจุดศูนย์กลางอยู่มุมล่างซ้าย • ระบบนี้ทำให้เขียนโปรแกรมกราฟฟิกได้โดยไม่ต้องคำนึงถึงความละเอียดของจอภาพ • การแสดงผลภาพ ระบบการแสดงผลจะแปลง NDC ให้ไปเป็นพิกัดของจอภาพขณะนั้น 1 0 1 0 1 1 NDC จอภาพ 1510/15/2014

  16. Viewing in 2D • Create your model in world coordinates • Map world coordinates to window coordinates(clipping when necessary) • Map window coordinates to viewport coordinates • Draw viewport on screen (usually handled by the display driver) 1610/15/2014

  17. 2D Clipping Algorithms • We could just throw away pixels that fall outside the window • Scan convert the primitive as usual • If the pixel to be drawn is outside the rectangle in question, don’t draw it • This isn’t the best idea in the world • Surely we can do better 1710/15/2014

  18. Line Clipping Example 1810/15/2014

  19. Clipping Points • ตรวจสอบว่าจุดอยู่ในกรอบของช่องหน้าต่างหรือช่องแสดงภาพหรือไม่ ทำได้โดยตรวจสอบว่าจุดนั้นมีค่าพิกัดอยู่ระหว่างขอบซ้ายและขอบด้านขวาของกรอบและอยู่ระหว่างขอบบนหรือขอบล่าง • ตรวจสอบ: xl <= x <= xr and yb <= y <= yt (xl,yt) (xr,yt) (xr,yb) (xl,yb) 1910/15/2014

  20. Clipping Points(cont.) • วิธีการขริบจุดนี้ใช้ได้แน่นอนเสมอ แต่ทางปฏิบัติจะไม่ใช้กันเนื่องจาก • ภาพที่ต้องผ่านการขริบมีขนาดใหญ่และมีความซับซ้อน ภาพนั้นจะประกอบด้วยจุดจำนวนมาก ทำให้เสียเวลาตรวจสอบ • ภาพอาจจะไม่ได้เก็บในลักษณะจุดทั้งหมดก็ได้ อาจจะแทนด้วยจุดสองจุดคือจุดเริ่มต้นกับจุดปลาย เช่นเส้นตรงเส้นหนึ่งอาจจะเก็บในรูปของจุดจำนวนมากที่มาต่อกันซึ่งเปลืองเนื้อที่ หรือเก็บเฉพาะจุดเริ่มต้นและจุดปลายแล้วมีตัวบอกเป็นเส้นตรง ใช้เนื้อที่น้อยแต่ใช้วิธีขริบภาพโดยการตรวจสอบจุดไม่ได้ 2010/15/2014

  21. Clipping Lines • การขริบเส้น จะพิจารณาเฉพาะจุดปลาย(endpoints) • ถ้าจุดปลายทั้งสองอยู่ในกรอบหน้าต่างก็ให้แสดงเส้นนั้น • ถ้าจุดปลายข้างหนึ่งอยู่ภายในช่องหน้าต่างและเส้นนั้นตัดกับช่องหน้าต่างโดยจุดปลายอีกข้างอยู่ข้างนอกจะต้องทำการขริบเส้นนั้น • ถ้าจุดปลายทั้งสองอยู่นอกกรอบหน้าต่าง เราไม่สามารถที่จะรู้ได้ว่าจะต้องวาดหรือแสดงเส้นหรือไม่ • เราจะต้องทำอะไร (What to do?) 2110/15/2014

  22. Clipping Lines • ภาพที่ซับซ้อนอาจประกอบด้วยเส้นจำนวนมากจึงจำเป็นที่ต้องมีวิธีการตรวจสอบ การขริบเส้นที่มีความเร็วสูง จึงจะสามารถแสดงภาพในช่องหน้าต่างอย่างรวดเร็ว • วิธีของ Cohen-Sutherland สามารถตรวจสอบว่าเส้นอยู่ภายในหรือภายนอกได้อย่างรวดเร็ว • นิยมใช้ในระบบกราฟฟิก 2210/15/2014

  23. Cohen-Sutherland Line Clipping • แบ่งระบบพิกัดโลกออกเป็นพื้นที่ 9 ส่วน โดยมีพื้นที่ตรงกลางเป็นช่องหน้าต่าง(viewport)และอีกแปดส่วนรอบช่องหน้าต่าง • แต่ละส่วนจะแทนด้วยตัวเลขฐานสอง 4-บิต:ดังรูป 2310/15/2014

  24. The Cohen-Sutherland Method • จุดพิกัดซึ่งตกอยู่ในพื้นที่ใดจะถูกกำหนดให้มีระหัสตามพื้นที่นั้น 1001:จุดอยู่ในตำแหน่งทางบน-ซ้าย ของช่องหน้าต่าง 1000:จุดอยู่ในตำแหน่งทางบน ของช่องหน้าต่าง 1010:จุดอยู่ในตำแหน่งทางบน-ขวา ของช่องหน้าต่าง 0001:จุดอยู่ในตำแหน่งทางซ้าย ของช่องหน้าต่าง 0000:จุดอยู่ในตำแหน่งภายใน ของช่องหน้าต่าง 0010:จุดอยู่ในตำแหน่งทางขวา ของช่องหน้าต่าง 0101:จุดอยู่ในตำแหน่งทางล่าง-ซ้าย ของช่องหน้าต่าง 0100:จุดอยู่ในตำแหน่งทางล่าง ของช่องหน้าต่าง 0110:จุดอยู่ในตำแหน่งทางล่างขวา ของช่องหน้าต่าง 2410/15/2014

  25. The Cohen-Sutherland Method • การอ้างถึงบิตในรหัส อ้างจากหลักหน่วยของรหัสไปข้างหน้า แต่ละบิตสัมพันธ์กับตำแหน่งในช่องหน้าต่าง บิต 1 - ซ้าย บิต 2 - ขวา บิต 3 - ล่าง บิต 4 - บน • ค่าของบิตในรหัสอาจเป็น 0 หรือ 1 ขึ้นกับผลของการเปรียบเทียบค่าพิกัดปลายของเส้นกับขอบเขตของช่องหน้าต่าง • บิต 1 (ซ้าย)จะถูกกำหนดให้เป็น1เมื่อค่า x ของพิกัดปลายน้อยกว่าค่า x ของช่องหน้าต่าง(x < xwmin) 2510/15/2014

  26. การกำหนดรหัสให้กับจุดการกำหนดรหัสให้กับจุด • คำนวณหาค่าผลต่างของพิกัดปลายกับขอบเขตของช่องหน้าต่าง • ใช้ผลลัพธ์ที่ได้(ค่าบวกหรือลบ)ไปกำหนดค่า 1หรือ 0 • ถ้า x - xwmin<0 แสดงว่าจุดพิกัดอยู่ทางซ้ายของช่องหน้าต่าง บิต 1 จะถูกกำหนดเป็น 1 • ถ้า x - xwmin>=0 แสดงว่าจุดพิกัดอยู่ภายในหรือบนขอบเขตของช่องหน้าต่าง บิต 1 จะถูกกำหนดเป็น 0 • ถ้า xwmax- x <0 แสดงว่าจุดพิกัดอยู่ทางขวาของช่องหน้าต่าง บิต 2 จะถูกกำหนดเป็น 1 • ถ้า xwmax- x >=0 แสดงว่าจุดพิกัดอยู่ภายในหรือบนขอบเขตของช่องหน้าต่าง บิต 2 จะถูกกำหนดเป็น 0 2610/15/2014

  27. การกำหนดรหัสให้กับจุดการกำหนดรหัสให้กับจุด • ถ้า ywmax - y <0แสดงว่าจุดพิกัดอยู่ข้างล่างของช่องหน้าต่าง บิต 3 จะถูกกำหนดเป็น 1 • ถ้า ywmax - y >=0แสดงว่าจุดพิกัดอยู่ภายในหรือบนขอบเขตของช่องหน้าต่าง บิต 3 จะถูกกำหนดเป็น 0 • ถ้า y - ywmin<0แสดงว่าจุดพิกัดอยู่ข้างบนของช่องหน้าต่าง บิต 4 จะถูกกำหนดเป็น 1 • ถ้า y - ywmin>=0 แสดงว่าจุดพิกัดอยู่ภายในหรือบนขอบเขตของช่องหน้าต่าง บิต 4 จะถูกกำหนดเป็น 0 2710/15/2014

  28. The Cohen-Sutherland Method การตรวจสอบเส้นว่าอยู่ในช่องหน้าต่าง • ถ้าจุดพิกัดปลายทั้งสองของเส้นมีรหัสเป็น 0000 ทั้งคู่แสดงว่าเส้นนั้นอยู่ในช่องหน้าต่าง • ถ้ารหัสของจุดพิกัดทั้งคู่มีค่าของบิตที่ตำแหน่งตรงกันเป็น 1 แสดงว่าเส้นนี้อยู่นอกช่องหน้าต่างให้ขริบออกได้เลย • ตรวจสอบโดยนำค่าพิกัดมา and กันถ้าผลที่ได้ไม่เป็น0000 สรุปได้ว่าเส้นนั้นอยู่ภายนอกช่องหน้าต่าง 2810/15/2014

  29. The Cohen-Sutherland Method • มีบางเส้นไม่สามารถบอกได้ทันทีว่าอยู่ภายในหรือภายนอกช่องหน้าต่าง ดังรูป 2910/15/2014

  30. The Cohen-Sutherland Method • P1P2 จะตรวจสอบว่าจุด P1อยู่สว่นใดโดยเทียบจากค่าพิกัดกับขอบเขตซึ่งได้ว่า P1 อยู่ข้างล่างช่องหน้าต่าง แล้วหาจุดตัดของเส้นกับขอบเขตล่างซึ่งได้เป็นจุด P’1 เส้น P1P’1 จะถูกตัดออกไป เหลือ P’1P2 • ตรวจสอบP2 พบว่าอยู่ข้างบนซ้ายช่องหน้าต่างและตัดที่จุด P’2ซึ่งอยู่บนช่องหน้าต่าง และคำนวณหาจุดตัดกับเส้นขอบเขตบนได้จุด P”2 เส้น P’1P”2 จะถูกเก็บไว้เป็นภาพ ส่วนอื่นจะถูกขริบ • P3P4 ตรวจสอบพบว่าจุด P3อยู่ทางซ้ายของช่องหน้าต่างหาจุดตัดกับเส้นขอบเขตล่างซ้ายได้จุด P’3 เส้น P3P’3 จะถูกตัดออกไป เหลือ P’3P4ซึ่งเมื่อนำไปตรวจสอบจะพบว่าอยู่ข้างล่างของช่องหน้าต่างจึงขริบออกได้ 3010/15/2014

  31. The Cohen-Sutherland Method • การคำนวณหาจุดตัดของเส้นกับเส้นขอบเขต หาได้จาก สมการเส้นตรงที่มีพิกัดจุดปลายเป็น(x1, y1) และ (x2, y2) • ค่า y ของจุดตัดหาได้จาก y = y1 + m(x - x1)โดยที่ x อาจกำหนดให้เป็นxwminหรือ xwmaxก็ได้ โดย m = (y2 -y1)/(x2 -x1) • หาค่าพิกัด x ของจุดตัด หาได้จากสมการ x = x1 + (y - y1)/m โดยที่ y อาจกำหนดให้เป็นywminหรือ ywmaxก็ได้ 3110/15/2014

  32. Clipping Polygons • การขริบรูปเหลี่ยมมีความซับซ้อนมากกว่าการขริบเส้นแต่ละเส้น • Input: polygon • Output: polygon, or nothing • When can we trivially accept/reject a polygon as opposed to the line segments that make up the polygon? 3210/15/2014

  33. Why Is Clipping Hard? • What happens to a triangle during clipping? • Possible outcomes: trianglequad triangletriangle triangle5-gon • How many sides can a clipped triangle have? 3310/15/2014

  34. Why Is Clipping Hard? • A really tough case: 3410/15/2014

  35. Why Is Clipping Hard? • A really tough case: concave polygonmultiple polygons 3510/15/2014

  36. Sutherland-Hodgman Clipping • Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation 3610/15/2014

  37. Sutherland-Hodgman Clipping • Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation • After doing all planes, the polygon is fully clipped 3710/15/2014

  38. Sutherland-Hodgman Clipping • Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation • After doing all planes, the polygon is fully clipped 3810/15/2014

  39. Sutherland-Hodgman Clipping • Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation • After doing all planes, the polygon is fully clipped 3910/15/2014

  40. Sutherland-Hodgman Clipping • Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation • After doing all planes, the polygon is fully clipped 4010/15/2014

  41. Sutherland-Hodgman Clipping • Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation • After doing all planes, the polygon is fully clipped 4110/15/2014

  42. Sutherland-Hodgman Clipping • Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation • After doing all planes, the polygon is fully clipped 4210/15/2014

  43. Sutherland-Hodgman Clipping • Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation • After doing all planes, the polygon is fully clipped 4310/15/2014

  44. Sutherland-Hodgman Clipping • Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation • After doing all planes, the polygon is fully clipped 4410/15/2014

  45. Sutherland-Hodgman Clipping:The Algorithm • Basic idea: • Consider each edge of the viewport individually • Clip the polygon against the edge equation • After doing all planes, the polygon is fully clipped • Will this work for non-rectangular clip regions? • What would 3-D clipping involve? 4510/15/2014

  46. Sutherland-Hodgman Clipping • Input/output for algorithm: • Input: list of polygon vertices in order • Output: list of clipped poygon vertices consisting of old vertices (maybe) and new vertices (maybe) • Note: this is exactly what we expect from the clipping operation against each edge 4610/15/2014

  47. Sutherland-Hodgman Clipping • Sutherland-Hodgman basic routine: • Go around polygon one vertex at a time • Current vertex has position p • Previous vertex had position s, and it has been added to the output if appropriate 4710/15/2014

  48. inside outside inside outside inside outside inside outside p s p s p s p s p output i output no output i outputp output Sutherland-Hodgman Clipping • Edge from s to ptakes one of four cases: (Purple line can be a line or a plane) 4810/15/2014

  49. Sutherland-Hodgman Clipping • Four cases: • s inside plane and p inside plane • Add p to output • Note: s has already been added • s inside plane and p outside plane • Find intersection point i • Add i to output • s outside plane and poutside plane • Add nothing • soutside plane and p inside plane • Find intersection point i • Add i to output, followed by p 4910/15/2014

  50. q n p P Point-to-Plane test • A very general test to determine if a point p is “inside” a plane P, defined by q and n: (p - q) • n < 0: p inside P (p - q) • n = 0: p on P (p - q) • n > 0: p outside P q q n n p p P P 5010/15/2014

More Related