銆愬崄澶╄嚜鍒惰蒋娓叉煋鍣ㄣ€慏AY 03锛氱敾涓€涓笁瑙掑舰锛堝悜閲忓弶涔樼畻娉?& 閲嶅績鍧愭爣绠楁硶锛?/a>

Posted 鍗や唬鐑?,

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了銆愬崄澶╄嚜鍒惰蒋娓叉煋鍣ㄣ€慏AY 03锛氱敾涓€涓笁瑙掑舰锛堝悜閲忓弶涔樼畻娉?& 閲嶅績鍧愭爣绠楁硶锛?/a>相关的知识,希望对你有一定的参考价值。

鍓嶉潰涓ゅぉ鐢讳簡鐐瑰拰绾匡紝浠婂ぉ鎴戜滑鏉ョ敾涓€涓渶绠€鍗曚篃鏄渶寮哄ぇ鐨勯潰鈥斺€?strong>涓夎褰?/strong>銆?/p>

鏈枃涓昏璁茶В涓夎褰㈢粯鍒剁畻娉曠殑鎺ㄥ鍜屾€濊矾锛堝彧娑夊強鍒颁竴鐐圭偣鐨勫悜閲忕煡璇嗭級锛屾渶鍚庝細缁欏嚭浠g爜瀹炵幇锛屽ぇ瀹舵斁蹇冪殑鐪嬩笅鍘诲氨濂姐€?/p>


鏈枃婧愮爜 馃憠锛?a href="https://link.segmentfault.com/?url=https%3A%2F%2Fgithub.com%2Fskychx%2FtoyRenderer%2Ftree%2Fday03-triangle-drawing-algorithm" rel="nofollow">toyRenderer-day3-draw-triangle

1.濡備綍鐢讳竴涓笁瑙掑舰锛?/h2>

鍦ㄦ寮忓紑濮嬭繖涓€灏忚妭鍓嶏紝鎴戜滑鍏堟兂涓€涓嬪浣曞埄鐢ㄤ笂涓€鑺傜殑鐢荤嚎绠楁硶缁樺埗涓€涓?strong>瀹炲績鐨?/strong>涓夎褰€?/p>

鍋囪鐜板湪骞抽潰鍐呮湁涓変釜涓嶅叡绾跨殑鐐圭粍鎴愪竴涓笁瑙掑舰锛屾垜浠彲浠ュ埄鐢ㄤ笂涓€鑺傜殑鐩寸嚎绠楁硶杞绘槗鐨勮繛鎺ヤ笁瑙掑舰鐨勪笁鏉¤竟锛岃繖鏃跺€欐垜浠細鐢熸垚涓€涓?strong>绌哄績鐨?/strong>銆?strong>灏侀棴鐨?/strong>涓夎褰€?/p>

閭d箞杩欐椂鍊欓棶棰樺氨杞崲涓猴紝濡備綍鎶婅繖涓┖蹇冪殑涓夎褰㈠彉涓轰竴涓?strong>瀹炲績鐨勪笁瑙掑舰锛?/p>

鎴戞兂澶у杩欐椂鍊欏凡缁忔湁鎬濊矾浜嗭紝灏辨槸涓€琛屼竴琛屽湴鎵弿鍍忕礌锛屾妸涓や釜杈圭晫鐐逛箣闂寸殑鍍忕礌鍏ㄩ儴娑傛弧涓婅壊灏卞彲浠ヤ簡銆?/p>

杩欎釜鏂规硶鑲畾鏄彲浠ョ殑锛屼絾鏄疄鐜颁笉鏄緢浼橀泤锛屼篃涓嶆槸涓氬唴鐨勪富娴佸疄鐜版柟寮忋€傚洜涓哄熀浜庤鎵弿鐨勭畻娉曚笉鏄湰鏂囩殑閲嶇偣锛屾墍浠ヨ缁嗙殑鎺ㄥ鍜屼唬鐮佸疄鐜板氨涓嶆彁渚涗簡锛屾劅鍏磋叮鐨勫悓瀛﹀彲浠ヨ嚜宸卞皾璇曞疄鐜颁竴涓嬨€?/p>

2.鍒╃敤鍚戦噺鍙変箻鐢讳笁瑙掑舰

寮€濮嬫湰鑺傚墠鍏堢畝鍗曞涔犱竴涓嬪悜閲忓弶涔樼殑鍑犱綍鎰忎箟銆?/p>

2.1 鏁板鎺ㄥ

鍦ㄤ笁缁寸┖闂翠腑锛屼袱涓笁缁村悜閲?$\\mathbf {a}$ 鍜?$\\mathbf {b}$ 鍋氬弶涔橈紝浼氬緱鍒颁竴涓拰宸茬煡涓や釜鍚戦噺鍨傜洿鐨勬柊鍚戦噺 $\\mathbf {a} \\times \\mathbf {b}$銆?/p>

鏃㈢劧鍙変箻浜х敓鐨勬槸涓€涓柊鍚戦噺锛岄偅涔堝畠鑲畾鏈変釜鏂瑰悜锛屾垜浠竴鑸敤鍙虫墜瀹氬垯鏉ュ垽鏂細灏嗗彸鎵嬮鎸囨寚鍚?$\\mathbf {a}$ 鐨勬柟鍚戙€佷腑鎸囨寚鍚?$\\mathbf {b}$ 鐨勬柟鍚戯紝鍒欐鏃舵媷鎸囩殑鏂瑰悜鍗充负 $\\mathbf {a} \\times \\mathbf {b}$ 鐨勬柟鍚戙€?/p>

缁间笂鎵€杩帮紝鎴戜滑鍙互瀵瑰悜閲忓弶涔樺仛涓€涓弗璋ㄧ殑瀹氫箟锛?/p>

$${\\displaystyle \\mathbf {a} \\times \\mathbf {b} =\\|\\mathbf {a} \\|\\|\\mathbf {b} \\|\\sin(\\theta )\\ \\mathbf {n} }$$

鍏朵腑 $\\theta$ 琛ㄧず $\\mathbf {a}$ 鍜?$\\mathbf {b}$ 鍦ㄥ畠浠墍瀹氫箟鐨勫钩闈笂鐨勫す瑙掞紙$0^{\\circ }\\leq \\theta \\leq 180^{\\circ}$锛夈€?{\\displaystyle \\|\\mathbf {a} \\|}$ 鍜?$\\displaystyle \\|\\mathbf {b} \\|$ 鏄悜閲?$\\mathbf {a}$ 鍜?$\\mathbf {b}$ 鐨勬ā闀匡紝鑰?$\\mathbf{n}$ 鍒欐槸涓€涓笌 $\\mathbf {a}$ 銆?\\mathbf {b}$ 鎵€鏋勬垚鐨勫钩闈㈠瀭鐩寸殑鍗曚綅鍚戦噺锛屾柟鍚戠敱鍙虫墜瀹氬垯鍐冲畾銆?/p>


鏈変笂闈㈢殑鐞嗚锛屾垜浠氨鍙互鍒ゆ柇涓や釜鍚戦噺鐨勭浉瀵逛綅缃細

  • $\\mathbf {a}$ 鍚戦噺鍙変箻 $\\mathbf {b}$ 鍚戦噺锛屽鏋滃€间负姝?/strong>锛屽垯琛ㄧず $\\mathbf {b}$ 鍚戦噺鍦?$\\mathbf {a}$ 鍚戦噺宸?/strong>渚?/li>
  • $\\mathbf {a}$ 鍚戦噺鍙変箻 $\\mathbf {b}$ 鍚戦噺锛屽鏋滃€间负璐?/strong>锛屽垯琛ㄧず $\\mathbf {b}$ 鍚戦噺鍦?$\\mathbf {a}$ 鍚戦噺鍙?/strong>渚?/li>
  • $\\mathbf {a}$ 鍚戦噺鍙変箻 $\\mathbf {b}$ 鍚戦噺锛屽鏋滃€间负闆?/strong>锛屽垯琛ㄧず $\\mathbf {b}$ 鍚戦噺涓?$\\mathbf {a}$ 鍚戦噺鍏辩嚎


浼氬垽鏂袱鏉$嚎鐨勭浉瀵逛綅缃簡锛屾垜浠彲浠ュ仛涓悊璁鸿縼绉伙紝鍒╃敤鍚戦噺鍙変箻鍒ゆ柇鐐瑰拰涓夎褰㈢殑浣嶇疆鍏崇郴銆?/p>

渚嬪涓嬮潰杩欓噷渚嬪瓙锛屽浜庝笁瑙掑舰 $\\Delta ABC$ 鏉ヨ锛屾妸涓夋潯杈圭湅浣?$\\overrightarrow{A B}$銆?$\\overrightarrow{B C}$銆?$\\overrightarrow{C A}$ 涓夋潯棣栧熬鐩歌繛鐨勫悜閲忥紝骞抽潰鍐呮湁涓€涓偣 $P$锛屾垜浠€氳繃鍚戦噺鍙変箻鏉ュ垽鏂浉瀵逛綅缃細

  • $\\overrightarrow{A B} \\times \\overrightarrow{A P}$锛屽€间负姝?/strong>锛屾晠 $P$ 鍦?$AB$ 宸︿晶
  • $\\overrightarrow{B C} \\times \\overrightarrow{B P}$锛屽€间负姝?/strong>锛屾晠 $P$ 鍦?$BC$ 宸︿晶
  • $\\overrightarrow{C A} \\times \\overrightarrow{C P}$锛屽€间负姝?/strong>锛屾晠 $P$ 鍦?$AC$ 宸︿晶


缁煎悎浠ヤ笂涓変釜闄愬埗鏉′欢锛屾垜浠彲浠ュ垽鏂?$P$ 鍦?$\\Delta ABC$ 鍐呫€?/p>

濡傛灉涓婇潰涓変釜璁$畻涓湁鍊间负璐熺殑鎯呭喌锛岃鏄?$P$ 鍦ㄤ笁瑙掑舰澶栵紱濡傛灉鏈夊€间负 0 鐨勬儏鍐碉紝璇存槑 $P$ 鍦ㄤ笁瑙掑舰鐨勮竟鎴栭《鐐逛笂銆?/p>

2.2 浠g爜瀹炵幇

鐞嗚鍩虹澶嶄範瀹屼簡锛屾垜浠氨鍙互鍐欎唬鐮佷簡銆備唬鐮佸疄鐜扮浉褰撶畝鍗曪紝鎴戜滑鏋勫缓涓€涓嚱鏁?crossProduct锛屼紶鍏ヤ笁瑙掑舰鐨勪笁涓《鐐瑰拰骞抽潰涓婄殑浠绘剰涓€鐐?$P$锛岀劧鍚庢牴鎹洓涓《鐐规瀯寤哄嚭鍚戦噺璁$畻鍙変箻灏卞彲浠ヤ簡锛?/p>

// 鍒╃敤鍙変箻鍒ゆ柇鏄惁鍦ㄤ笁瑙掑舰鍐呴儴
Vec3i crossProduct(Vec2i *pts, Vec2i P) {
    // 鏋勫缓鍑轰笁瑙掑舰 ABC 涓夋潯杈圭殑鍚戦噺
    Vec2i AB(pts[1].x - pts[0].x, pts[1].y - pts[0].y);
    Vec2i BC(pts[2].x - pts[1].x, pts[2].y - pts[1].y);
    Vec2i CA(pts[0].x - pts[2].x, pts[0].y - pts[2].y);
    
    // 涓夎褰笁涓《鐐瑰拰 P 閾炬帴褰㈡垚鐨勫悜閲?    Vec2i AP(P.x - pts[0].x, P.y - pts[0].y);
    Vec2i BP(P.x - pts[1].x, P.y - pts[1].y);
    Vec2i CP(P.x - pts[2].x, P.y - pts[2].y);
    
    return Vec3i(AB^AP, BC^BP, CA^CP);
}


浠g爜闈炲父鐨勭畝鍗曪紝鎴戜滑璺戜竴涓畝鍗曠殑渚嬪瓙楠岃瘉涓€涓嬶細

void drawSingleTriangle() {
    // 鍥剧墖鐨勫楂?    int width  = 200;
    int height = 200;

    TGAImage frame(width, height, TGAImage::RGB);
    Vec2i pts[3] = {Vec2i(10, 10), Vec2i(150, 30), Vec2i(70, 160)};

    Vec2i P;
    // 閬嶅巻鍥剧墖涓殑鎵€鏈夊儚绱?    for (P.x = 0; P.x <= width - 1; P.x++) {
        for (P.y = 0; P.y <= height - 1; P.y++) {
            Vec3i bc_screen  = crossProduct(pts, P);

            // bc_screen 鏌愪釜鍒嗛噺灏忎簬 0 鍒欒〃绀烘鐐瑰湪涓夎褰㈠锛堣涓鸿竟涔熸槸涓夎褰㈢殑涓€閮ㄥ垎锛?            if (bc_screen.x<0 || bc_screen.y<0 || bc_screen.z<0) {
                continue;
            }
            
            image.set(P.x, P.y, color);
        }
    }
    
    frame.flip_vertically();
    frame.write_tga_file("output/day03_cross_product_triangle.tga");
}


鐪嬭緭鍑哄浘鍍忥紝鎴戜滑宸茬粡鎴愬姛缁樺埗浜嗕竴涓笁瑙掑舰锛?/p>

瑙︿笉鍙婇槻鐨勫畨鍒?/strong>锛氬ぇ瀹跺彲浠ョ湅鎴戝ご鍍忓叧娉煕帮笍鍙枫€屽崵铔嬪疄楠屽銆嶏紝鍚庡彴鍥炲銆屽浘褰㈠銆嶈幏鍙栫粡鍏告暀鏉愩€婅檸涔?銆嬪拰銆奟eal Time Rendering 4銆?/blockquote>

3.鍒╃敤涓夎褰㈤噸蹇冨潗鏍囩敾涓夎褰?/h2>

鏈皬鑺備粙缁嶄竴涓洿閫氱敤鐨勫畾鐞嗏€斺€?strong>閲嶅績鍧愭爣锛圔arycentric Coordinate锛?/strong>銆傚叾瀹?a href="https://link.segmentfault.com/?url=https%3A%2F%2Fwww.wikiwand.com%2Fen%2FBarycentric_coordinate_system" rel="nofollow">閲嶅績鍧愭爣鐢ㄦ潵鐢讳笁瑙掑舰杩樻槸鏈変簺澶ф潗灏忕敤浜嗭紝浠栨渶閲嶈鐨勮繍鐢ㄥ叾瀹炴槸鐢ㄦ潵鍋?strong>鎻掑€?/strong>锛屼笉杩囨彃鍊肩殑鍏蜂綋杩愮敤鎴戜滑鍚庣画绔犺妭鍐嶆帰璁紝浠婂ぉ鎴戜滑鐪嬬湅閲嶅績鍧愭爣鐨勬帹瀵煎拰浠g爜瀹炵幇銆?/p>

3.1 鏁板鎺ㄥ

鎴戜滑鏆傛椂鍙€冭檻浜岀淮骞抽潰鐨勪笁瑙掑舰銆傚浜庝竴涓笁瑙掑舰 $\\Delta ABC$ 鏉ヨ锛屽亣璁惧钩闈㈠唴鏈変竴涓偣 $P$锛屽緢鏄剧劧锛?\\overrightarrow{A P}$锛?\\overrightarrow{A B}$锛?\\overrightarrow{A C}$ 鍚戦噺閮芥槸绾挎€х浉鍏崇殑锛屼篃灏辨槸璇达紝鍙互鐢ㄤ笅寮忚〃绀?$\\overrightarrow{A P}$锛?/p>

$$\\overrightarrow{A P}=u \\overrightarrow{A B}+v \\overrightarrow{A C}$$

鎴戜滑鎶婅繖涓笁瑙掑舰鏀惧湪涓€涓瑳鍗″皵鍧愭爣绯讳笅锛屾垜浠氨鍙互杩欐牱琛ㄧず锛?/p>

$$A-P=u(A-B)+v(A-C)$$

鎶婁綅缃尓涓€涓嬶紝鍚堝苟鍚岀被椤瑰悗锛?P$ 鐐圭殑浣嶇疆鍙互琛ㄧず涓轰笅寮忥細

$$P=(1-u-v) A+u B+v C$$


缁撳悎鍑犱綍鎰忎箟锛屾垜浠緢瀹规槗鎺ㄥ嚭锛?/p>

  • 褰?$(1 - u - v)$銆?u$ 鍜?$v$ 鍧?strong>澶т簬 0 灏忎簬 1 鏃讹紝P 浣嶄簬涓夎褰?strong>鍐呴儴
  • 鏈?strong>涓€涓?/strong>鍒嗛噺绛変簬 0 鏃讹紝P 鍦ㄤ笁瑙掑舰杈?/strong>涓?/li>
  • 鏈?strong>涓や釜鍙橀噺绛変簬 0 鏃讹紝P 鍦ㄦ煇涓?strong>椤剁偣涓?/li>


鍐嶅绗竴涓紡瀛愬仛涓€涓嬪彉褰紝鍙互寰楀埌涓嬪紡锛?/p>

$$u \\overrightarrow{A B}+v \\overrightarrow{A C}+\\overrightarrow{P A}=0$$


鍥犱负涓夎褰綅浜庣瑳鍗″皵鍧愭爣绯诲唴锛屾垜浠彲浠ユ妸涓婇潰鐨勫紡瀛愭部 $x$ 鍜?$y$ 杞存媶鍒嗕负涓や釜寮忓瓙锛屼粬浠拰涓婂紡鏄瓑浠风殑锛?/p>

$$\\left\\{\\begin{array}{l}u \\overrightarrow{A B}_{x}+v \\overrightarrow{A C}_{x}+\\overrightarrow{P A}_{x}=0 \\\\ u \\overrightarrow{A B}_{y}+v \\overrightarrow{A C}_{y}+\\overrightarrow{P A}_{y}=0\\end{array}\\right.$$


瑙傚療杩欎釜寮忓瓙锛屾垜浠彲浠ヨ浆鎹负鐭╅樀涔樻硶鐨勫舰寮忥細

$$\\left[\\begin{array}{lll}u & v & 1\\end{array}\\right]\\left[\\begin{array}{l}\\overrightarrow{A B}_{x} \\\\ \\overrightarrow{A C}_{x} \\\\ \\overrightarrow{P A}_{x}\\end{array}\\right]=0$$

$$\\left[\\begin{array}{lll}u & v & 1\\end{array}\\right]\\left[\\begin{array}{l}\\overrightarrow{A B}_{y} \\\\ \\overrightarrow{A C}_{y} \\\\ \\overrightarrow{P A}_{y}\\end{array}\\right]=0$$


瑙傚療涓婇潰鐨勫紡瀛愶紝鎴戜滑瑕佸鎵句竴涓悜閲?$[\\begin{array}{lll}u & v & 1\\end{array}]$锛屽畠瑕佷笌鍚戦噺 $[\\begin{array}{l}\\overrightarrow{A B}_{x} \\ \\overrightarrow{A C}_{x} \\ \\overrightarrow{P A}_{x}\\end{array}]$ 鍜?$[\\begin{array}{l}\\overrightarrow{A B}_{y} \\ \\overrightarrow{A C}_{y} \\ \\overrightarrow{P A}_{y}\\end{array}]$ 鍚屾椂鐐逛箻涓?0銆?/p>

绋嶅井鎬濊€冧竴涓嬶紝杩欎笉灏辨剰鍛崇潃鍚戦噺 $[\\begin{array}{lll}u & v & 1\\end{array}]$ 鍚屾椂鍨傜洿浜庡悜閲?$[\\begin{array}{l}\\overrightarrow{A B}_{x} \\ \\overrightarrow{A C}_{x} \\ \\overrightarrow{P A}_{x}\\end{array}]$ 鍜?$[\\begin{array}{l}\\overrightarrow{A B}_{y} \\ \\overrightarrow{A C}_{y} \\ \\overrightarrow{P A}_{y}\\end{array}]$ 鍚楋紒

鎵€浠ユ垜浠洿鎺ユ眰鍚庝袱涓悜閲忕殑鍙変箻灏卞彲浠ユ眰鍑哄悜閲?$[\\begin{array}{lll}u & v & 1\\end{array}]$ 浜嗐€?/p>


鍚庝袱涓悜閲忓仛鍙変箻鐨勬椂鍊欐湁涓皬缁嗚妭闇€瑕佹敞鎰忎竴涓嬶紝鍚戦噺鍙変箻鐨?strong>鐩存帴缁撴灉锛堝厛鍋囪缁撴灉涓?$[\\begin{array}{lll}e & f & g\\end{array}]$ 锛変竴鑸彧鏄拰 $[\\begin{array}{lll}u & v & 1\\end{array}]$ 骞宠锛屾兂瑕佹纭眰鍑?$u$ 鍜?$v$ 鐨勫€硷紝鎴戜滑闇€瑕佸鍚戦噺 $[\\begin{array}{lll}e & f & g\\end{array}]$ 闄や互 $g$锛屼篃灏辨槸璇?$u = e/g$锛?v = f/g$锛?1 = g/g$銆?/p>

杩欎釜鏃跺€欓棶棰樺氨鏉ヤ簡锛屼笂闈㈢殑闄ゆ硶鎴愮珛锛屽繀椤昏寤虹珛鍦?$g$ 涓嶄负 0 鐨勫熀纭€涓婏紝閭d箞鎴戜滑灏辫鐮旂┒涓€涓?$g$ 涓?0 鐨勬暟瀛﹀惈涔変簡銆?/p>

鏍规嵁鍚戦噺鐨勫弶涔樺叕寮忥細

$$\\mathbf{u} \\times \\mathbf{v}=\\left|\\begin{array}{cc}u_{2} & u_{3} \\\\ v_{2} & v_{3}\\end{array}\\right| \\mathbf{i}-\\left|\\begin{array}{cc}u_{1} & u_{3} \\\\ v_{1} & v_{3}\\end{array}\\right| \\mathbf{j}+\\left|\\begin{array}{cc}u_{1} & u_{2} \\\\ v_{1} & v_{2}\\end{array}\\right| \\mathbf{k}$$

$g$ 鍚戦噺鍙互琛ㄧず涓鸿繖鏍风殑琛屽垪寮忥細

$$\\left|\\begin{array}{ll}\\overrightarrow{A B}_{x} & \\overrightarrow{A C}_{x} \\\\ \\overrightarrow{A B}_{y} & \\overrightarrow{A C}_{y}\\end{array}\\right|$$

濡傛灉杩欐椂鍊欑殑鍙変箻缁撴灉涓?0锛屾妸杩欎釜琛屽垪寮?strong>浠庡垪鍚戦噺鐨勮瑙掔湅锛屽氨鐩稿綋浜?$\\overrightarrow{A B}$ 鍜?$\\overrightarrow{A C}$ 鍚戦噺鍙変箻缁撴灉涓?0锛屼篃灏辨槸璇?$\\overrightarrow{A B}$ 鍜?$\\overrightarrow{A C}$ 鍚戦噺鍏辩嚎銆傝繖鏃跺€欎笁瑙掑舰 $\\Delta ABC$ 灏?strong>閫€鍖栦负涓€鏉$嚎娈?/strong>銆?/p>

瀵逛簬鎴戜滑鐜板湪搴旂敤鍦烘櫙鏉ヨ锛屽彧瑕佹娴嬪埌 $g$ 涓?0锛屽氨鎰忓懗鐫€杩欎釜涓夎褰㈤€€鍖栦负涓€鏉$嚎娈典簡锛屾垜浠洿鎺ヨ垗寮冩帀瀹冨氨濂戒簡锛屽鏈€鍚庣殑缁撴灉涔熸病鏈夊奖鍝嶃€?/p>

3.2 浠g爜瀹炵幇

鏍规嵁涓婇潰鐨勫叕寮忔帹瀵硷紝鎴戜滑鍙互鐩存帴鍐欏嚭鍩轰簬涓夎褰㈤噸蹇冨潗鏍囩殑缁樺埗绠楁硶锛屾€濊矾鐞嗘竻浜嗭紝浠g爜瀹炵幇灏遍潪甯哥殑绠€鍗曪細

// 鍒╃敤閲嶅績鍧愭爣鍒ゆ柇鐐规槸鍚﹀湪涓夎褰㈠唴閮?Vec3f barycentric(Vec2i *pts, Vec2i P) {
    Vec3i x(pts[1].x - pts[0].x, pts[2].x - pts[0].x, pts[0].x - P.x);
    Vec3i y(pts[1].y - pts[0].y, pts[2].y - pts[0].y, pts[0].y - P.y);
    
    // u 鍚戦噺鍜?x y 鍚戦噺鐨勭偣绉负 0锛屾墍浠?x y 鍚戦噺鍙変箻鍙互寰楀埌 u 鍚戦噺
    Vec3i u = x^y;
    
    // 鐢变簬 A, B, C, P 鐨勫潗鏍囬兘鏄?int 绫诲瀷锛屾墍浠?u.z 蹇呭畾鏄?int 绫诲瀷锛屽彇鍊艰寖鍥翠负 ..., -2, -1, 0, 1, 2, ...
    // 鎵€浠?u.z 缁濆鍊煎皬浜?1 鎰忓懗鐫€涓夎褰㈤€€鍖栦簡锛岀洿鎺ヨ垗寮?    if(std::abs(u.z) < 1) {
        return Vec3f(-1, 1, 1);
    }
    return Vec3f(1.f- (u.x+u.y) / (float)u.z, u.x / (float)u.z, u.y / (float)u.z);
}


鐢ㄤ笂闈㈢殑绠楁硶鐢讳釜涓夎褰㈤獙璇佷竴涓嬶紝寰堝畬缇庯細

4.缁樺埗妯″瀷

绠楁硶鍐欏ソ浜嗭紝鎴戜滑灏辫鎶曞叆鍒板疄闄呭簲鐢ㄤ腑浜嗭紝鏄ㄥぉ閲屾垜浠敾浜嗕釜涓夌淮妯″瀷鐨勭嚎妗嗗浘锛屼粖澶╂垜浠氨涓繖涓ā鍨嬩笂鑹层€?/p>

4.1 闅忔満鐫€鑹?/h3>

涓轰簡鍖哄垎姣忎釜涓夎褰紝鎴戜滑闅忔満缁欎笁瑙掑舰涓婁笉鍚岀殑鑹诧細

triangle(screen_coords, frame, TGAColor(rand() % 255, rand() % 255, rand() % 255, 255));


杩欓噷杩樻湁涓叧浜庢€ц兘鐨勫皬缁嗚妭銆傝繖娆℃垜浠粯鍒剁殑鍥惧儚澶у皬涓?800*800锛屽鏋滄寜鐓т箣鍓嶇殑绠楁硶锛屾瘡娆$敾涓夎褰紝閮借鎶婃墍鏈夊儚绱犻亶鍘嗕竴閬嶏紝杩欎釜妯″瀷澶ф鏈?2000 涓笁瑙掑舰锛岄偅灏辨槸瑕佸惊鐜?2000*800*800 鍗?12.8 浜?/code> 娆★紒

杩欎釜閲忕骇鏄緢鎭愭€栫殑锛屽叾涓緢澶氱殑杩愮畻閮芥槸涓嶅繀瑕佺殑锛屾瘮濡傝涓嬪浘锛屾垜浠叾瀹炲彧瑕佸惊鐜敱涓変釜椤剁偣璁$畻鍑虹殑绾㈣壊鍖呭洿鐩掗噷鐨勫儚绱?/strong>灏卞彲浠ヤ簡锛屼笉闇€瑕佽绠楀浘鐗囧唴鐨勬墍鏈夊儚绱狅細


鎵€浠ユ垜浠鍦ㄩ亶鍘嗗儚绱犲墠鍔犲厛姹備竴閬嶄笁瑙掑舰鍖呭洿鐩掔殑杈圭晫锛屾寮忕敾涓夎褰㈡椂鍙閬嶅巻鍖呭洿鐩掑唴鐨勫儚绱犲氨鍙互浜嗭細

// 瀹氫箟鍖呭洿鐩?Vec2i boxmin(image.get_width() - 1, image.get_height() - 1);
Vec2i boxmax(0, 0);
// 鍥剧墖鐨勮竟鐣?Vec2i clamp(image.get_width() - 1, image.get_height() - 1);

// 鏌ユ壘鍖呭洿鐩掕竟鐣?for (int i = 0; i < 3; i++) {
    // 绗竴灞傚惊鐜紝閬嶅巻涓夎褰㈢殑涓変釜椤剁偣
    for (int j = 0; j < 2; j++) {
        // 绗簩灞傚惊鐜紝鏍规嵁椤剁偣鏁板€肩缉灏忓寘鍥寸洅鐨勮寖鍥?        boxmin.x = std::max(0,        std::min(boxmin.x, pts[i].x));
        boxmin.y = std::max(0,        std::min(boxmin.y, pts[i].y));

        boxmax.x = std::min(clamp.x, std::max(boxmax.x, pts[i].x));
        boxmax.y = std::min(clamp.y, std::max(boxmax.y, pts[i].y));
    }
}


鏈€鍚庢垜浠粯鍒跺嚭鐨勭粨鏋滃氨鏄繖鏍风殑锛岀湅鐫€杩樻槸鏈変簺閰风偒鐨勶細

4.2 绠€鍗曠殑鍏夌収鐫€鑹?/h3>

闅忔満鐫€鑹茬殑濂藉鏄彲浠ュ緢娓呮鐨勮〃鐜板嚭妯″瀷鍚勪釜涓夎褰㈢殑杞粨锛屼絾鏄篃澶卞幓浜嗘ā鍨嬬殑杈ㄨ瘑搴︼紝寰堝缁嗚妭閮戒涪澶变簡銆?/p>

鎴戜滑鍦ㄨ繖閲屽紩鍏ヤ竴涓潪甯哥畝鍗曠殑鍏夌収妯″瀷锛岃涓?strong>鍗曚綅闈㈢Н涓婃帴鏀跺埌鐨勫厜锛屽拰骞抽潰娉曠嚎涓庡厜鐓ф柟鍚戠殑浣欏鸡鍊兼垚姝f瘮锛?/p>


鎵€浠ョ潃鑹茬殑鎬濊矾灏卞緢娓呮櫚浜嗭細

  • 鎴戜滑瑕佸厛瀹氫箟涓€涓笁缁寸┖闂撮噷鐨?strong>鍏夌収鏂瑰悜锛堝悜閲忥級锛岀劧鍚庤绠椾笁缁寸┖闂撮噷鍚勪釜涓夎褰㈢殑娉曠嚎锛堝悜閲忥級
  • 涓や釜鍚戦噺褰掍竴鍖?/strong>鍚庯紝鐒跺悗璁$畻杩欎袱涓悜閲忕殑鐐逛箻锛屼細寰楀埌涓€涓€?/li>
  • 杩欎釜鍊煎皬浜?0锛岃鏄庡厜鍦ㄤ笁瑙掑舰鐨勫彟涓€渚э紝浠庣墿鐞嗕笂鐪嬫槸鐓у皠涓嶅埌涓夎褰㈣〃闈㈢殑锛屾墍浠ョ洿鎺ヨ垗寮冩涓夎褰?/li>
  • 杩欎釜鍊煎ぇ浜?0锛屽€?strong>瓒婂ぇ锛岃鏄庡崟浣嶉潰绉笂鎺ユ敹鍒扮殑鍏?strong>瓒婂锛屼笁瑙掑舰瓒婁寒


鎶婁笂闈㈢殑鎬濊矾缈昏瘧鎴愪唬鐮佸氨鏄繖鏍风殑锛?/p>

// 杩欎釜鏄敤涓€涓ā鎷熷厜鐓у涓夎褰㈣繘琛岀潃鑹?
Vec3f light_dir(0, 0, -1); // 鍋囪鍏夋槸鍨傜洿灞忓箷鐨?
for (int i = 0; i < model->nfaces(); i++) {
    std::vector<int> face = model->face(i);
    Vec2i screen_coords[3];
    Vec3f world_coords[3];

    // 璁$畻涓栫晫鍧愭爣鍜屽睆骞曞潗鏍?    for (int j = 0; j < 3; j++) {
        Vec3f v = model->vert(face[j]);
        // 鎶曞奖涓烘浜ゆ姇褰憋紝鑰屼笖鍙仛浜嗕釜绠€鍗曠殑瑙嗗彛鍙樻崲
        screen_coords[j] = Vec2i((v.x + 1.) * width / 2., (v.y + 1.) * height / 2.);
        world_coords[j]  = v;
    }

    // 璁$畻涓栫晫鍧愭爣涓煇涓笁瑙掑舰鐨勬硶绾匡紙娉曠嚎 = 涓夎褰换鎰忎袱鏉¤竟鍋氬弶涔橈級
    Vec3f n = (world_coords[2] - world_coords[0]) ^ (world_coords[1] - world_coords[0]);
    n.normalize(); // 瀵?n 鍋氬綊涓€鍖栧鐞?
    // 涓夎褰㈡硶绾垮拰鍏夌収鏂瑰悜鍋氱偣涔橈紝鐐逛箻鍊煎ぇ浜?0锛岃鏄庢硶绾挎柟鍚戝拰鍏夌収鏂瑰悜鍦ㄥ悓涓€渚?    // 鍊艰秺澶э紝璇存槑瓒婂鐨勫厜鐓у皠鍒颁笁瑙掑舰涓婏紝棰滆壊瓒婄櫧
    float intensity = n * light_dir;
    if (intensity > 0) {
        triangle(screen_coords, frame, TGAColor(intensity * 255, intensity * 255, intensity * 255, 255));
    }
}


鏈€鍚庣敓鎴愮殑鍥剧墖灏辨槸杩欐牱鐨勶細


鍒拌繖閲屾覆鏌撳嚭鐨勫浘鍍忓氨鏈変簺浜烘牱浜嗭紝浣嗘槸澶у搴旇涔熷彂鐜颁簡锛屼笂鍥剧殑鍢村反鍜岀溂鐫涘鐪嬩笂鍘绘€€殑銆傝繖閲岀殑纭槸鏈夐棶棰橈紝鍥犱负瀹冩妸鑳屽悗鐨勪笁瑙掑舰娓叉煋鍑烘潵浜嗭紝瑕佹兂瑙e喅杩欎釜闂锛屽氨瑕佸紩鍏ヤ竴涓柊鐨勬蹇碘€斺€?a href="https://link.segmentfault.com/?url=https%3A%2F%2Fwww.wikiwand.com%2Fen%2FZ-buffering" rel="nofollow">z-buffer銆?/p>

鎺ㄨ崘鐩存帴闃呰鍘熸枃锛屾洿鏂版洿鍙婃椂锛岄槄璇讳綋楠屾洿浣?/p>


濡傛灉浣犲枩娆㈡垜鐨勬枃绔狅紝甯屾湜鐐硅禐馃憤 鏀惰棌 馃搧 鍦ㄧ湅 馃専 涓夎繛鏀寔涓€涓嬶紒锛侊紒璋㈣阿浣狅紝杩欏鎴戠湡鐨勫緢閲嶈锛?/blockquote>

娆㈣繋澶у鍏虫敞鎴戠殑寰俊鍏紬鍙凤細鍗よ泲瀹為獙瀹?/strong>锛岀洰鍓嶄笓娉ㄥ墠绔妧鏈紝瀵瑰浘褰㈠涔熸湁涓€浜涘井灏忕爺绌躲€?/p>

以上是关于銆愬崄澶╄嚜鍒惰蒋娓叉煋鍣ㄣ€慏AY 03锛氱敾涓€涓笁瑙掑舰锛堝悜閲忓弶涔樼畻娉?& 閲嶅績鍧愭爣绠楁硶锛?/a>的主要内容,如果未能解决你的问题,请参考以下文章

win10瀛椾綋娓叉煋

vue娓叉煋鑷畾涔塲son鏁版嵁

鎵嬪啓Vue2.0婧愮爜锛堝洓锛?娓叉煋鏇存柊鍘熺悊

绉诲姩绔竷灞€

銆愬睆骞曞姞宸ュ厬鎹?鍩硅銆戞洸灞?iPad 鎵嬭〃 浼樺娍鍔犲伐

cesium 娴佺嚎 椋炵嚎 杩佸緳鍥?鏀婚槻鍥?/a>