11import { mat4 , vec3 } from "./math/types" ;
22import { perspective , identity , look_at , rotate , translate } from "./math/transformations" ;
33import { create_sphere } from "./rendering/primitives" ;
4- import { build_mesh , build_meshes } from "./rendering/render" ;
4+ import { build_mesh , render_scene } from "./rendering/render" ;
55import { StringBuffer } from "./utils/string_buffer" ;
6+ import { Mesh } from "./rendering/mesh" ;
7+ import { Scene } from "./rendering/scene" ;
8+ import { mul_mat4 } from "./math/matrix_operators" ;
69
710
811export function main_3d ( ) {
@@ -12,23 +15,33 @@ export function main_3d() {
1215 const do_wireframe :boolean = wireframe_el ?. checked ;
1316 const sun_mesh = create_sphere ( 1.5 , 16 , 16 ) ;
1417 const planet_mesh = create_sphere ( 0.5 , 12 , 12 ) ;
18+
19+ const scene :Scene = new Scene ( [ sun_mesh . vertices , planet_mesh . vertices ] , [ sun_mesh . indices , planet_mesh . indices ] ) ;
20+
21+
22+
1523 const y = vec3 ( 0 , 1 , 0 ) ;
1624 const view :mat4 = look_at ( vec3 ( 0 , 2 , 6.5 ) , vec3 ( 0 , 0 , 0 ) , y ) ;
1725 const projection = perspective ( 60 * Math . PI / 180 , 400 / 300 , 0.1 , 100 ) ;
1826 let time = 0 ;
1927 const string_buffer = new StringBuffer ( 1024 * 1024 ) ;
28+ const vp = mul_mat4 ( projection , view ) ;
2029 const loop = ( ) => {
2130 time += 0.01 ;
2231
2332 let frame_html = "" ;
2433 let sun_model = identity ( ) ;
2534 sun_model = rotate ( sun_model , time * 0.5 , y ) ;
26- frame_html += build_mesh ( sun_mesh , sun_model , view , projection , string_buffer , do_wireframe , true ) ;
2735 let planet_model = identity ( ) ;
2836 planet_model = rotate ( planet_model , time , vec3 ( 0 , 1 , 0 ) ) ;
2937 planet_model = translate ( planet_model , vec3 ( 3.5 , 0 , 0 ) ) ;
3038 planet_model = rotate ( planet_model , time * 3 , vec3 ( 1 , 0 , 1 ) ) ;
31- frame_html += build_meshes ( [ sun_mesh , planet_mesh ] , [ sun_model , planet_model ] , [ view , view ] , [ projection , projection ] , string_buffer , do_wireframe , true ) ;
39+
40+ const planet_mvp = mul_mat4 ( planet_model , vp ) ;
41+ const sun_mvp = mul_mat4 ( sun_model , vp ) ;
42+
43+ render_scene ( scene , [ sun_mvp , planet_mvp ] , true ) ;
44+
3245 target ! . innerHTML = frame_html ;
3346 requestAnimationFrame ( loop ) ;
3447 }
0 commit comments