reworked lighting uniforms
[carveJwlIkooP6JGAAIwe30JlM.git] / lighting.h
1 #ifndef LIGHTING_H
2 #define LIGHTING_H
3
4 #include "common.h"
5 #include "scene.h"
6
7 static int ray_world( v3f pos, v3f dir, ray_hit *hit );
8
9 typedef struct voxel_gi voxel_gi;
10
11 struct voxel_gi
12 {
13 GLuint hw_texture,
14 pt_texture;
15
16 v3i pt_dims; /* Page table dimentions */
17 i32 page_size;
18
19 v3f origin;
20 };
21
22 static void voxel_gi_setup( voxel_gi *gi, scene *sc,
23 i32 page_size, float voxel_res )
24 {
25 v3_copy( sc->bbx[0], gi->origin );
26 gi->page_size = page_size;
27
28 v3f extent;
29 v3_sub( sc->bbx[1], sc->bbx[0], extent );
30
31 float fpage_size = voxel_res * (float)page_size;
32
33 for( int i=0; i<3; i++ )
34 {
35 i32 voxel_count = extent[i] / voxel_res;
36 gi->pt_dims[i] = (voxel_count+page_size-1) / page_size;
37 }
38
39 i32 pt_capacity = gi->pt_dims[0]*gi->pt_dims[1]*gi->pt_dims[2];
40 vg_info( "Page table size: %dkb\n", (pt_capacity*2*2)/1024 );
41
42 u16 *page_table = malloc( pt_capacity*sizeof(u16)*3 );
43
44
45 u32 active_count = 0;
46
47 for( int z=0; z<gi->pt_dims[2]; z++ )
48 {
49 for( int y=0; y<gi->pt_dims[1]; y++ )
50 {
51 for( int x=0; x<gi->pt_dims[0]; x++ )
52 {
53 v3f base = {x,y,z},
54 end = {x+1,y+1,z+1};
55
56 boxf page_region;
57
58 v3_muladds( sc->bbx[0], base, ((float)page_size)*voxel_res, base );
59 v3_muladds( sc->bbx[0], end, ((float)page_size)*voxel_res, end );
60 v3_copy( base, page_region[0] );
61 v3_copy( end, page_region[1] );
62
63 u32 nothing[2];
64 if( bh_select( &sc->bhtris, page_region, nothing, 2 ))
65 {
66 active_count ++;
67
68 /* Calculate lighting */
69 }
70 }
71 }
72 }
73
74 /* Calculate physical storage size */
75 vg_info( "Hardware texture required size: %dmb\n",
76 ((active_count*page_size*page_size*page_size*1)/1024)/1024 );
77 vg_info( "Required physical blocks: %d\n", active_count );
78
79 free( page_table );
80 }
81
82 static void compute_lighting_vertex( scene *sc )
83 {
84 v3f light_dir = { 0.2f, 1.0f, 1.0f };
85 v3_normalize( light_dir );
86
87 for( int i=0; i<sc->vertex_count; i++ )
88 {
89 model_vert *mv = &sc->verts[i];
90
91 ray_hit hit;
92 hit.dist = 100.0f;
93
94 ray_world( mv->co, light_dir, &hit );
95 float amt = hit.dist / 100.0f;
96
97 mv->colour[0] = amt;
98 mv->colour[1] = amt;
99 mv->colour[2] = amt;
100 mv->colour[3] = 1.0f;
101 }
102 }
103
104 #endif /* LIGHTING_H */