Skip to content

Commit 0324cd9

Browse files
committed
Lighting working!
1 parent 1c48364 commit 0324cd9

4 files changed

Lines changed: 41 additions & 7 deletions

File tree

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<center>
1111
<table border="1" >
1212
<tr>
13-
<td colspan="2">
13+
<td colspan="2" bgcolor="black">
1414
<svg
1515
width="400px"
1616
height="300px"

src/main.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ export function main_3d() {
2121

2222
const scene:Scene = new Scene([sun_mesh,planet_mesh],[vec3(1,0,0),vec3(0,0,1)]);
2323

24+
2425
console.log(scene.draw_end)
2526

27+
2628
const y = vec3(0,1,0);
2729
const view:mat4 = look_at(vec3(0,2,6.5),vec3(0,0,0),y);
2830
const projection = perspective(60 * Math.PI / 180, 400/300, 0.1, 100);
@@ -33,11 +35,26 @@ export function main_3d() {
3335
const sun_light = new Light(
3436
vec3(10,10,10),
3537
vec3(1.0,0.95,0.9),
36-
8.0,
38+
6.0,
39+
200.0
40+
);
41+
const second_sun = new Light(
42+
vec3(10,-10,-10),
43+
vec3(0.2,0.95,0.2),
44+
6.0,
3745
200.0
3846
);
47+
const third_sun = new Light(
48+
vec3(10,10,10),
49+
vec3(0.3,0.6,0.9),
50+
6.0,
51+
200.0
52+
);
53+
3954

4055
scene.add_light(sun_light);
56+
scene.add_light(second_sun);
57+
scene.add_light(third_sun);
4158

4259
const loop = () => {
4360
time += 0.01;

src/rendering/render.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,22 @@ export function render_scene(scene:Scene, mvp:mat4[], invert_y:boolean = true){
2828
}
2929
let index = 0;
3030
let offset = 0;
31+
let color_offset = 0;
3132
for(const mesh of scene.meshes){
32-
for(let i = 0; i < mesh.raster_end; i+=9){
33-
scene.draw_order[index] = i + offset;
33+
const raster_size = mesh.raster_end;
34+
const raster_color = raster_size / 3;
35+
const pos_view = mesh.raster_buffer.subarray(0, raster_size);
36+
const col_view = mesh.raster_color.subarray(0, raster_color);
37+
38+
scene.scene_buffer.set(pos_view, offset);
39+
scene.raster_color.set(col_view, color_offset);
40+
41+
for(let i = 0; i < raster_size; i+=9){
42+
scene.draw_order[index] = offset + i;
3443
index++;
3544
}
36-
offset += mesh.raster_buffer.length;
45+
offset += raster_size;
46+
color_offset += raster_color;
3747
}
3848
scene.draw_order.subarray(0,index).sort((a,b)=>get_z_value(scene.scene_buffer,b) - get_z_value(scene.scene_buffer,a));
3949
scene.draw_end = index;

src/rendering/types/scene.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import type { Geometry } from "./geometry";
33
import type { Light } from "./light";
44
import { Mesh } from "./mesh";
55

6+
7+
const HUGE = 1024*1024 * 10;
8+
69
export class Scene{
710
projected_buffer!:ArrayType;
811
scene_buffer!:ArrayType;
@@ -72,14 +75,18 @@ export class Scene{
7275
add_mesh(geometry:Geometry, color:vec3 = vec3(0.2,0.2,0.2)):Mesh{
7376
const scene_size = geometry.indices.length * 4;
7477
const proj_size = geometry.vertices.length/3*4;
75-
const color_size = geometry.vertices.length * 3;
76-
const raster_color_size = geometry.indices.length * 3;
78+
const color_size = geometry.vertices.length;
79+
const raster_color_size = geometry.indices.length*3;
7780

7881
const scene_view = this.scene_buffer.subarray(this.scene_cursor, this.scene_cursor + scene_size);
7982
const proj_view = this.projected_buffer.subarray(this.projected_cursor,this.projected_cursor + proj_size);
8083
const color_view = this.color_buffer.subarray(this.color_cursor,this.color_cursor + color_size);
8184
const raster_color = this.raster_color.subarray(this.raster_color_cursor,this.raster_color_cursor + raster_color_size);
8285

86+
console.log(`Cursors: ${[this.scene_cursor,this.projected_cursor,this.color_cursor,this.raster_color_cursor]}`);
87+
console.log(`Scene_size for mesh: ${scene_size}, proj = ${proj_size}, color_size = ${color_size}, raster_color_size = ${raster_color_size}`)
88+
console.log(`Full sizes: ${[this.scene_buffer.length,this.projected_buffer.length,this.color_buffer.length,this.raster_color.length]}`)
89+
8390
this.scene_cursor += scene_size;
8491
this.projected_cursor += proj_size;
8592
this.color_cursor += color_size;

0 commit comments

Comments
 (0)