templeos-info/public/Wb/Demo/Graphics/Shading.HC.HTML

209 lines
18 KiB
Plaintext
Raw Normal View History

2024-03-24 21:24:44 +00:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="generator" content="TempleOS V5.03">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="/style/templeos.css">
<script src="/script/templeos.js"></script>
<style type="text/css">
.cF0{color:#000000;background-color:#ffffff;}
.cF1{color:#0000aa;background-color:#ffffff;}
.cF2{color:#00aa00;background-color:#ffffff;}
.cF3{color:#00aaaa;background-color:#ffffff;}
.cF4{color:#aa0000;background-color:#ffffff;}
.cF5{color:#aa00aa;background-color:#ffffff;}
.cF6{color:#aa5500;background-color:#ffffff;}
.cF7{color:#aaaaaa;background-color:#ffffff;}
.cF8{color:#555555;background-color:#ffffff;}
.cF9{color:#5555ff;background-color:#ffffff;}
.cFA{color:#55ff55;background-color:#ffffff;}
.cFB{color:#55ffff;background-color:#ffffff;}
.cFC{color:#ff5555;background-color:#ffffff;}
.cFD{color:#ff55ff;background-color:#ffffff;}
.cFE{color:#ffff55;background-color:#ffffff;}
.cFF{color:#ffffff;background-color:#ffffff;}
</style>
</head>
<body>
<pre id="content">
<a name="l1"></a><span class=cF2>//&quot;ls&quot; is light source.</span><span class=cF0>
<a name="l2"></a>
<a name="l3"></a></span><span class=cF1>U0</span><span class=cF0> Lighting(</span><span class=cF9>CDC</span><span class=cF0> *dc,</span><span class=cF9>CD3I32</span><span class=cF0> *ls,</span><span class=cF9>CD3I32</span><span class=cF0> *poly,</span><span class=cF9>I64</span><span class=cF0> color)
<a name="l4"></a>{</span><span class=cF2>//color is a color from 0-7</span><span class=cF0>
<a name="l5"></a> </span><span class=cF9>CD3I32</span><span class=cF0> v1,v2;
<a name="l6"></a> </span><span class=cF9>I64</span><span class=cF0> *r=dc-&gt;r,i,vn_x,vn_y,vn_z;
<a name="l7"></a> </span><span class=cF1>F64</span><span class=cF0> d;
<a name="l8"></a>
<a name="l9"></a> v1.x=poly[0].x-poly[1].x;
<a name="l10"></a> v1.y=poly[0].y-poly[1].y;
<a name="l11"></a> v1.z=poly[0].z-poly[1].z;
<a name="l12"></a>
<a name="l13"></a> v2.x=poly[2].x-poly[1].x;
<a name="l14"></a> v2.y=poly[2].y-poly[1].y;
<a name="l15"></a> v2.z=poly[2].z-poly[1].z;
<a name="l16"></a>
<a name="l17"></a> </span><span class=cF2>//V1 and V2 are vects along two sides</span><span class=cF0>
<a name="l18"></a> </span><span class=cF2>//of the polygon joined at point[1].</span><span class=cF0>
<a name="l19"></a>
<a name="l20"></a> vn_x=v1.y*v2.z-v1.z*v2.y;
<a name="l21"></a> vn_y=v1.z*v2.x-v1.x*v2.z;
<a name="l22"></a> vn_z=v1.x*v2.y-v1.y*v2.x;
<a name="l23"></a>
<a name="l24"></a> </span><span class=cF1>if</span><span class=cF0> (d=</span><span class=cF5>Sqrt</span><span class=cF7>(</span><span class=cF5>SqrI64</span><span class=cF0>(vn_x)+</span><span class=cF5>SqrI64</span><span class=cF0>(vn_y)+</span><span class=cF5>SqrI64</span><span class=cF0>(vn_z)</span><span class=cF7>)</span><span class=cF0>)
<a name="l25"></a> d=1&lt;&lt;16/d;
<a name="l26"></a> vn_x*=d;
<a name="l27"></a> vn_y*=d;
<a name="l28"></a> vn_z*=d;
<a name="l29"></a></span><span class=cF2>//Vn is the cross product of V1 and V3</span><span class=cF0>
<a name="l30"></a> </span><span class=cF2>//which means it is perpendicular. It</span><span class=cF0>
<a name="l31"></a> </span><span class=cF2>//is the normal vect to the surface.</span><span class=cF0>
<a name="l32"></a> </span><span class=cF2>//It has been scaled to length 65536.</span><span class=cF0>
<a name="l33"></a> </span><span class=cF5>Mat4x4MulXYZ</span><span class=cF0>(r,&amp;vn_x,&amp;vn_y,&amp;vn_z);
<a name="l34"></a> i=(vn_x*ls-&gt;x+vn_y*ls-&gt;y+vn_z*ls-&gt;z)&gt;&gt;16;
<a name="l35"></a></span><span class=cF2>//The dot product of the light source</span><span class=cF0>
<a name="l36"></a> </span><span class=cF2>//vect and the surface normal</span><span class=cF0>
<a name="l37"></a> </span><span class=cF2>//gives an illumination number.</span><span class=cF0>
<a name="l38"></a>
<a name="l39"></a> </span><span class=cF2>//TempleOS will generate a random U16</span><span class=cF0>
<a name="l40"></a> </span><span class=cF2>//and compare to dither_probability_u16 and</span><span class=cF0>
<a name="l41"></a> </span><span class=cF2>//will pick from two colors.</span><span class=cF0>
<a name="l42"></a> </span><span class=cF2>//Probability dithering does not work with thick&gt;1 at this time.</span><span class=cF0>
<a name="l43"></a> </span><span class=cF1>if</span><span class=cF0> (i&lt;0) </span><span class=cF7>{</span><span class=cF0>
<a name="l44"></a> dc-&gt;color=</span><span class=cF3>ROPF_PROBABILITY_DITHER</span><span class=cF0>+</span><span class=cF3>BLACK</span><span class=cF0>&lt;&lt;16+color;
<a name="l45"></a> dc-&gt;dither_probability_u16=-i;
<a name="l46"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l47"></a> dc-&gt;color=</span><span class=cF3>ROPF_PROBABILITY_DITHER</span><span class=cF0>+(color^8)&lt;&lt;16+color;
<a name="l48"></a> dc-&gt;dither_probability_u16=i;
<a name="l49"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l50"></a>}
<a name="l51"></a>
<a name="l52"></a>#</span><span class=cF1>define</span><span class=cF0> RINGS 8
<a name="l53"></a>#</span><span class=cF1>define</span><span class=cF0> FACES 32
<a name="l54"></a>#</span><span class=cF1>define</span><span class=cF0> SLOP 0.03 </span><span class=cF2>//Gaps appear without this.</span><span class=cF0>
<a name="l55"></a>
<a name="l56"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>DrawIt</span><span class=cF0>(</span><span class=cF9>CTask</span><span class=cF0> *task,</span><span class=cF9>CDC</span><span class=cF0> *dc)
<a name="l57"></a>{
<a name="l58"></a> </span><span class=cF9>CCtrl</span><span class=cF0> *c=</span><span class=cF5>CtrlFindUnique</span><span class=cF0>(task,</span><span class=cF3>CTRLT_VIEWING_ANGLES</span><span class=cF0>);
<a name="l59"></a> </span><span class=cF9>CViewAngles</span><span class=cF0> *s=c-&gt;state;
<a name="l60"></a> </span><span class=cF1>F64</span><span class=cF0> tt=0.5*(</span><span class=cF5>Sin</span><span class=cF7>(</span><span class=cF3>pi</span><span class=cF0>*2*(</span><span class=cF5>tS</span><span class=cF0>%10.0)/10.0</span><span class=cF7>)</span><span class=cF0>+2.0),
<a name="l61"></a> theta,theta2,phi,phi2,radius,d;
<a name="l62"></a> </span><span class=cF9>I64</span><span class=cF0> i,j,cx=task-&gt;pix_width/2,cy=task-&gt;pix_height/2;
<a name="l63"></a> </span><span class=cF9>CD3I32</span><span class=cF0> poly[3],ls;
<a name="l64"></a>
<a name="l65"></a> dc-&gt;flags|=</span><span class=cF3>DCF_TRANSFORMATION</span><span class=cF0>;
<a name="l66"></a> </span><span class=cF5>DCDepthBufAlloc</span><span class=cF0>(dc);
<a name="l67"></a>
<a name="l68"></a> </span><span class=cF5>Mat4x4IdentEqu</span><span class=cF0>(dc-&gt;r);
<a name="l69"></a> </span><span class=cF5>Mat4x4RotZ</span><span class=cF0>(dc-&gt;r,s-&gt;az);
<a name="l70"></a> </span><span class=cF5>Mat4x4RotY</span><span class=cF0>(dc-&gt;r,s-&gt;ay);
<a name="l71"></a> </span><span class=cF5>Mat4x4RotX</span><span class=cF0>(dc-&gt;r,s-&gt;ax+</span><span class=cF3>pi</span><span class=cF0>);
<a name="l72"></a> </span><span class=cF5>Mat4x4Scale</span><span class=cF0>(dc-&gt;r,tt);
<a name="l73"></a> </span><span class=cF5>DCMat4x4Set</span><span class=cF0>(dc,dc-&gt;r);
<a name="l74"></a>
<a name="l75"></a> ls.x=-(</span><span class=cFB>ms</span><span class=cF0>.pos.x-task-&gt;pix_left-task-&gt;scroll_x-cx);
<a name="l76"></a> ls.y=-(</span><span class=cFB>ms</span><span class=cF0>.pos.y-task-&gt;pix_top-task-&gt;scroll_y-cy);
<a name="l77"></a> ls.z=</span><span class=cF3>GR_WIDTH</span><span class=cF0>/8;
<a name="l78"></a> d=1&lt;&lt;16/</span><span class=cF5>D3I32Norm</span><span class=cF0>(&amp;ls);
<a name="l79"></a> ls.x*=d;
<a name="l80"></a> ls.y*=d;
<a name="l81"></a> ls.z*=d;
<a name="l82"></a>
<a name="l83"></a> dc-&gt;x=cx;
<a name="l84"></a> dc-&gt;y=cy;
<a name="l85"></a> dc-&gt;z=</span><span class=cF5>MaxI64</span><span class=cF0>(cx,cy);
<a name="l86"></a> radius =</span><span class=cF5>MinI64</span><span class=cF0>(cx,cy)/2;
<a name="l87"></a>
<a name="l88"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;RINGS;i++) </span><span class=cF7>{</span><span class=cF0>
<a name="l89"></a> phi = i*</span><span class=cF3>pi</span><span class=cF0>/2/RINGS;
<a name="l90"></a> phi2=(i+1)*</span><span class=cF3>pi</span><span class=cF0>/2/RINGS+SLOP;
<a name="l91"></a> </span><span class=cF1>for</span><span class=cF0> (j=0;j&lt;FACES;j++) {
<a name="l92"></a> theta =j*2*</span><span class=cF3>pi</span><span class=cF0>/FACES;
<a name="l93"></a> theta2=(j+1)*2*</span><span class=cF3>pi</span><span class=cF0>/FACES+SLOP;
<a name="l94"></a>
<a name="l95"></a> </span><span class=cF2>//Upper half</span><span class=cF0>
<a name="l96"></a> poly[0].x=radius*</span><span class=cF5>Cos</span><span class=cF0>(phi)*</span><span class=cF5>Cos</span><span class=cF0>(theta);
<a name="l97"></a> poly[0].y=radius*</span><span class=cF5>Cos</span><span class=cF0>(phi)*</span><span class=cF5>Sin</span><span class=cF0>(theta);
<a name="l98"></a> poly[0].z=radius*</span><span class=cF5>Sin</span><span class=cF0>(phi);
<a name="l99"></a> poly[1].x=radius*</span><span class=cF5>Cos</span><span class=cF0>(phi)*</span><span class=cF5>Cos</span><span class=cF0>(theta2);
<a name="l100"></a> poly[1].y=radius*</span><span class=cF5>Cos</span><span class=cF0>(phi)*</span><span class=cF5>Sin</span><span class=cF0>(theta2);
<a name="l101"></a> poly[1].z=radius*</span><span class=cF5>Sin</span><span class=cF0>(phi);
<a name="l102"></a> poly[2].x=radius*</span><span class=cF5>Cos</span><span class=cF0>(phi2)*</span><span class=cF5>Cos</span><span class=cF0>(theta+2*</span><span class=cF3>pi</span><span class=cF0>/FACES/2);
<a name="l103"></a> poly[2].y=radius*</span><span class=cF5>Cos</span><span class=cF0>(phi2)*</span><span class=cF5>Sin</span><span class=cF0>(theta+2*</span><span class=cF3>pi</span><span class=cF0>/FACES/2);
<a name="l104"></a> poly[2].z=radius*</span><span class=cF5>Sin</span><span class=cF0>(phi2);
<a name="l105"></a> Lighting(dc,&amp;ls,poly,</span><span class=cF3>BLUE</span><span class=cF0>);
<a name="l106"></a> </span><span class=cF5>GrFillPoly3</span><span class=cF0>(dc,3,poly);
<a name="l107"></a>
<a name="l108"></a> poly[2].x=radius*</span><span class=cF5>Cos</span><span class=cF0>(phi2)*</span><span class=cF5>Cos</span><span class=cF0>(theta +2*</span><span class=cF3>pi</span><span class=cF0>/FACES/2);
<a name="l109"></a> poly[2].y=radius*</span><span class=cF5>Cos</span><span class=cF0>(phi2)*</span><span class=cF5>Sin</span><span class=cF0>(theta +2*</span><span class=cF3>pi</span><span class=cF0>/FACES/2);
<a name="l110"></a> poly[2].z=radius*</span><span class=cF5>Sin</span><span class=cF0>(phi2);
<a name="l111"></a> poly[1].x=radius*</span><span class=cF5>Cos</span><span class=cF0>(phi2)*</span><span class=cF5>Cos</span><span class=cF0>(theta2+2*</span><span class=cF3>pi</span><span class=cF0>/FACES/2);
<a name="l112"></a> poly[1].y=radius*</span><span class=cF5>Cos</span><span class=cF0>(phi2)*</span><span class=cF5>Sin</span><span class=cF0>(theta2+2*</span><span class=cF3>pi</span><span class=cF0>/FACES/2);
<a name="l113"></a> poly[1].z=radius*</span><span class=cF5>Sin</span><span class=cF0>(phi2);
<a name="l114"></a> poly[0].x=radius*</span><span class=cF5>Cos</span><span class=cF0>(phi)*</span><span class=cF5>Cos</span><span class=cF0>(theta2);
<a name="l115"></a> poly[0].y=radius*</span><span class=cF5>Cos</span><span class=cF0>(phi)*</span><span class=cF5>Sin</span><span class=cF0>(theta2);
<a name="l116"></a> poly[0].z=radius*</span><span class=cF5>Sin</span><span class=cF0>(phi);
<a name="l117"></a> Lighting(dc,&amp;ls,poly,</span><span class=cF3>BLUE</span><span class=cF0>);
<a name="l118"></a> </span><span class=cF5>GrFillPoly3</span><span class=cF0>(dc,3,poly);
<a name="l119"></a>
<a name="l120"></a> </span><span class=cF2>//Lower half</span><span class=cF0>
<a name="l121"></a> poly[2].x=radius*</span><span class=cF5>Cos</span><span class=cF0>(phi)*</span><span class=cF5>Cos</span><span class=cF0>(theta);
<a name="l122"></a> poly[2].y=radius*</span><span class=cF5>Cos</span><span class=cF0>(phi)*</span><span class=cF5>Sin</span><span class=cF0>(theta);
<a name="l123"></a> poly[2].z=-radius*</span><span class=cF5>Sin</span><span class=cF0>(phi);
<a name="l124"></a> poly[1].x=radius*</span><span class=cF5>Cos</span><span class=cF0>(phi)*</span><span class=cF5>Cos</span><span class=cF0>(theta2);
<a name="l125"></a> poly[1].y=radius*</span><span class=cF5>Cos</span><span class=cF0>(phi)*</span><span class=cF5>Sin</span><span class=cF0>(theta2);
<a name="l126"></a> poly[1].z=-radius*</span><span class=cF5>Sin</span><span class=cF0>(phi);
<a name="l127"></a> poly[0].x=radius*</span><span class=cF5>Cos</span><span class=cF0>(phi2)*</span><span class=cF5>Cos</span><span class=cF0>(theta+2*</span><span class=cF3>pi</span><span class=cF0>/FACES/2);
<a name="l128"></a> poly[0].y=radius*</span><span class=cF5>Cos</span><span class=cF0>(phi2)*</span><span class=cF5>Sin</span><span class=cF0>(theta+2*</span><span class=cF3>pi</span><span class=cF0>/FACES/2);
<a name="l129"></a> poly[0].z=-radius*</span><span class=cF5>Sin</span><span class=cF0>(phi2);
<a name="l130"></a> Lighting(dc,&amp;ls,poly,</span><span class=cF3>RED</span><span class=cF0>);
<a name="l131"></a> </span><span class=cF5>GrFillPoly3</span><span class=cF0>(dc,3,poly);
<a name="l132"></a>
<a name="l133"></a> poly[0].x=radius*</span><span class=cF5>Cos</span><span class=cF0>(phi2)*</span><span class=cF5>Cos</span><span class=cF0>(theta +2*</span><span class=cF3>pi</span><span class=cF0>/FACES/2);
<a name="l134"></a> poly[0].y=radius*</span><span class=cF5>Cos</span><span class=cF0>(phi2)*</span><span class=cF5>Sin</span><span class=cF0>(theta +2*</span><span class=cF3>pi</span><span class=cF0>/FACES/2);
<a name="l135"></a> poly[0].z=-radius*</span><span class=cF5>Sin</span><span class=cF0>(phi2);
<a name="l136"></a> poly[1].x=radius*</span><span class=cF5>Cos</span><span class=cF0>(phi2)*</span><span class=cF5>Cos</span><span class=cF0>(theta2+2*</span><span class=cF3>pi</span><span class=cF0>/FACES/2);
<a name="l137"></a> poly[1].y=radius*</span><span class=cF5>Cos</span><span class=cF0>(phi2)*</span><span class=cF5>Sin</span><span class=cF0>(theta2+2*</span><span class=cF3>pi</span><span class=cF0>/FACES/2);
<a name="l138"></a> poly[1].z=-radius*</span><span class=cF5>Sin</span><span class=cF0>(phi2);
<a name="l139"></a> poly[2].x=radius*</span><span class=cF5>Cos</span><span class=cF0>(phi)*</span><span class=cF5>Cos</span><span class=cF0>(theta2);
<a name="l140"></a> poly[2].y=radius*</span><span class=cF5>Cos</span><span class=cF0>(phi)*</span><span class=cF5>Sin</span><span class=cF0>(theta2);
<a name="l141"></a> poly[2].z=-radius*</span><span class=cF5>Sin</span><span class=cF0>(phi);
<a name="l142"></a> Lighting(dc,&amp;ls,poly,</span><span class=cF3>RED</span><span class=cF0>);
<a name="l143"></a> </span><span class=cF5>GrFillPoly3</span><span class=cF0>(dc,3,poly);
<a name="l144"></a> }
<a name="l145"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l146"></a>}
<a name="l147"></a>
<a name="l148"></a></span><span class=cF2>//See </span><a href="/Wb/Demo/Graphics/SpritePlot3D.HC.HTML#l1"><span class=cF4>::/Demo/Graphics/SpritePlot3D.HC</span></a><span class=cF2>.</span><span class=cF0>
<a name="l149"></a></span><span class=cF2>//for a CSprite example.</span><span class=cF0>
<a name="l150"></a>
<a name="l151"></a></span><span class=cF2>//See </span><a href="/Wb/Adam/Gr/SpriteMesh.HC.HTML#l957"><span class=cF4>SpriteMeshEd</span></a><span class=cF2>() for a fancy example.</span><span class=cF0>
<a name="l152"></a>
<a name="l153"></a></span><span class=cF1>U0</span><span class=cF0> Main()
<a name="l154"></a>{
<a name="l155"></a> </span><span class=cF9>CCtrl</span><span class=cF0> *c=</span><span class=cF5>ViewAnglesNew</span><span class=cF0>;
<a name="l156"></a> </span><span class=cF9>CViewAngles</span><span class=cF0> *s=c-&gt;state;
<a name="l157"></a> s-&gt;sx=2*</span><span class=cF3>VIEWANGLES_SNAP</span><span class=cF0>;
<a name="l158"></a> s-&gt;sy=7*</span><span class=cF3>VIEWANGLES_SNAP</span><span class=cF0>;
<a name="l159"></a> s-&gt;sz=6*</span><span class=cF3>VIEWANGLES_SNAP</span><span class=cF0>;
<a name="l160"></a>
<a name="l161"></a> </span><span class=cF5>SettingsPush</span><span class=cF0>; </span><span class=cF2>//See </span><a href="/Wb/Adam/TaskSettings.HC.HTML#l3"><span class=cF4>SettingsPush</span></a><span class=cF0>
<a name="l162"></a> </span><span class=cF5>AutoComplete</span><span class=cF0>;
<a name="l163"></a> </span><span class=cF5>WinBorder</span><span class=cF0>;
<a name="l164"></a> </span><span class=cF5>WinMax</span><span class=cF0>;
<a name="l165"></a> </span><span class=cF5>DocClear</span><span class=cF0>;
<a name="l166"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;draw_it=&amp;</span><span class=cF5>DrawIt</span><span class=cF0>;
<a name="l167"></a> </span><span class=cF1>try</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l168"></a> </span><span class=cF6>&quot;\n\nMove mouse to change light source.\n\n&quot;</span><span class=cF0>;
<a name="l169"></a> </span><span class=cF5>PressAKey</span><span class=cF0>;
<a name="l170"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>catch</span><span class=cF0>
<a name="l171"></a> </span><span class=cF5>PutExcept</span><span class=cF0>;
<a name="l172"></a> </span><span class=cF5>DocClear</span><span class=cF0>;
<a name="l173"></a> </span><span class=cF5>SettingsPop</span><span class=cF0>;
<a name="l174"></a> </span><span class=cF5>ViewAnglesDel</span><span class=cF0>;
<a name="l175"></a>}
<a name="l176"></a>
<a name="l177"></a>Main;
</span></pre></body>
</html>