22< html > < head >
33 < script src ="lightgl.js "> </ script >
44 < script src ="../csg.js "> </ script >
5+ < script src ="viewer.js "> </ script >
56 < style >
67
78body {
2425h1 , h2 { font : bold 50 px/50px 'Helvetica Neue' , Helvetica, Arial; }
2526h2 { font-size : 30px ; margin : 100px 0 0 0 ; }
2627a { color : inherit; }
27- .viewer { width : 200 px ; height : 200 px ; background : # EEE ; }
28+ .viewer { width : 250 px ; height : 250 px ; background : # EEE ; }
2829table { border-collapse : collapse; margin : 0 auto; }
2930td { padding : 5px ; text-align : center; }
3031td code { background : none; border : none; color : inherit; }
@@ -37,7 +38,7 @@ <h1>csg.js</h1>
3738 < b > Documentation:</ b > < a href ="http://evanw.github.com/csg.js/docs/ "> http://evanw.github.com/csg.js/docs/</ a > </ p >
3839
3940 < h2 > Coplanar Test Cases</ h2 >
40- < p > These tests cover all the cases of overlapping coplanar polygons in both solids. To avoid duplicate polygons, the overlapping polygons are kept in the first solid and removed from the second solid.</ p >
41+ < p > These tests cover all the cases of a pair of overlapping coplanar polygons in both solids. To avoid duplicate polygons, the overlapping polygons are kept in the first solid and removed from the second solid.</ p >
4142 < table >
4243 < tr >
4344 < td > < div id ="0 " class ="viewer "> </ div > < code > a</ code > </ td >
@@ -85,137 +86,6 @@ <h2>Coplanar Test Cases</h2>
8586
8687 < script >
8788
88- // Set the color of all polygons in this solid
89- CSG . prototype . setColor = function ( r , g , b ) {
90- this . toPolygons ( ) . map ( function ( polygon ) {
91- polygon . shared = [ r , g , b ] ;
92- } ) ;
93- } ;
94-
95- // Convert from CSG solid to GL.Mesh object
96- CSG . prototype . toMesh = function ( ) {
97- var mesh = new GL . Mesh ( { normals : true , colors : true } ) ;
98- var indexer = new GL . Indexer ( ) ;
99- this . toPolygons ( ) . map ( function ( polygon ) {
100- var indices = polygon . vertices . map ( function ( vertex ) {
101- vertex . color = polygon . shared ;
102- return indexer . add ( vertex ) ;
103- } ) ;
104- for ( var i = 2 ; i < indices . length ; i ++ ) {
105- mesh . triangles . push ( [ indices [ 0 ] , indices [ i - 1 ] , indices [ i ] ] ) ;
106- }
107- } ) ;
108- mesh . vertices = indexer . unique . map ( function ( v ) { return [ v . pos . x , v . pos . y , v . pos . z ] ; } ) ;
109- mesh . normals = indexer . unique . map ( function ( v ) { return [ v . normal . x , v . normal . y , v . normal . z ] ; } ) ;
110- mesh . colors = indexer . unique . map ( function ( v ) { return v . color ; } ) ;
111- mesh . computeWireframe ( ) ;
112- return mesh ;
113- } ;
114-
115- var angleX = 20 ;
116- var angleY = 20 ;
117- var viewers = [ ] ;
118-
119- // A viewer is a WebGL canvas that lets the user view a mesh. The user can
120- // tumble it around by dragging the mouse.
121- function Viewer ( csg , width , height , depth ) {
122- viewers . push ( this ) ;
123-
124- // Get a new WebGL canvas
125- var gl = GL . create ( ) ;
126- this . gl = gl ;
127- this . mesh = csg . toMesh ( ) ;
128-
129- // Set up the viewport
130- gl . canvas . width = width ;
131- gl . canvas . height = height ;
132- gl . viewport ( 0 , 0 , width , height ) ;
133- gl . matrixMode ( gl . PROJECTION ) ;
134- gl . loadIdentity ( ) ;
135- gl . perspective ( 45 , width / height , 0.1 , 100 ) ;
136- gl . matrixMode ( gl . MODELVIEW ) ;
137-
138- // Set up WebGL state
139- gl . blendFunc ( gl . SRC_ALPHA , gl . ONE_MINUS_SRC_ALPHA ) ;
140- gl . clearColor ( 0.93 , 0.93 , 0.93 , 1 ) ;
141- gl . enable ( gl . DEPTH_TEST ) ;
142- gl . enable ( gl . CULL_FACE ) ;
143- gl . polygonOffset ( 1 , 1 ) ;
144-
145- // Black shader for wireframe
146- this . blackShader = new GL . Shader ( '\
147- void main() {\
148- gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\
149- }\
150- ' , '\
151- void main() {\
152- gl_FragColor = vec4(0.0, 0.0, 0.0, 0.3);\
153- }\
154- ' ) ;
155-
156- // Shader with diffuse and specular lighting
157- this . lightingShader = new GL . Shader ( '\
158- varying vec3 color;\
159- varying vec3 normal;\
160- varying vec3 light;\
161- void main() {\
162- const vec3 lightDir = vec3(1.0, 2.0, 3.0) / 3.741657386773941;\
163- light = (gl_ModelViewMatrix * vec4(lightDir, 0.0)).xyz;\
164- color = gl_Color.rgb;\
165- normal = gl_NormalMatrix * gl_Normal;\
166- gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\
167- }\
168- ' , '\
169- varying vec3 color;\
170- varying vec3 normal;\
171- varying vec3 light;\
172- void main() {\
173- vec3 n = normalize(normal);\
174- float diffuse = max(0.0, dot(light, n));\
175- float specular = pow(max(0.0, -reflect(light, n).z), 32.0) * sqrt(diffuse);\
176- gl_FragColor = vec4(mix(color * (0.3 + 0.7 * diffuse), vec3(1.0), specular), 1.0);\
177- }\
178- ' ) ;
179-
180- gl . onmousemove = function ( e ) {
181- if ( e . dragging ) {
182- angleY += e . deltaX * 2 ;
183- angleX += e . deltaY * 2 ;
184- angleX = Math . max ( - 90 , Math . min ( 90 , angleX ) ) ;
185-
186- viewers . map ( function ( viewer ) {
187- viewer . gl . ondraw ( ) ;
188- } ) ;
189- }
190- } ;
191-
192- var that = this ;
193- gl . ondraw = function ( ) {
194- gl . makeCurrent ( ) ;
195-
196- gl . clear ( gl . COLOR_BUFFER_BIT | gl . DEPTH_BUFFER_BIT ) ;
197- gl . loadIdentity ( ) ;
198- gl . translate ( 0 , 0 , - depth ) ;
199- gl . rotate ( angleX , 1 , 0 , 0 ) ;
200- gl . rotate ( angleY , 0 , 1 , 0 ) ;
201-
202- gl . enable ( gl . POLYGON_OFFSET_FILL ) ;
203- that . lightingShader . draw ( that . mesh , gl . TRIANGLES ) ;
204- gl . disable ( gl . POLYGON_OFFSET_FILL ) ;
205-
206- gl . enable ( gl . BLEND ) ;
207- that . blackShader . draw ( that . mesh , gl . LINES ) ;
208- gl . disable ( gl . BLEND ) ;
209- } ;
210-
211- gl . ondraw ( ) ;
212- }
213-
214- var nextID = 0 ;
215- function addViewer ( viewer ) {
216- document . getElementById ( nextID ++ ) . appendChild ( viewer . gl . canvas ) ;
217- }
218-
21989// Test all coplanar cases
22090var a = CSG . cube ( ) ;
22191var b = CSG . cylinder ( { slices : 8 , start : new CSG . Vector ( - 1 , 0 , 0 ) , end : new CSG . Vector ( 1 , 0 , 0 ) } ) ;
@@ -254,8 +124,9 @@ <h2>Coplanar Test Cases</h2>
254124 c . intersect ( d ) ,
255125 d . intersect ( c )
256126] ;
127+ Viewer . lineOverlay = true ;
257128for ( var i = 0 ; i < operations . length ; i ++ ) {
258- addViewer ( new Viewer ( operations [ i ] , 200 , 200 , 6 ) ) ;
129+ addViewer ( new Viewer ( operations [ i ] , 250 , 250 , 5 ) ) ;
259130}
260131
261132 </ script >
0 commit comments