point maps (wip)
[carveJwlIkooP6JGAAIwe30JlM.git] / pointcloud.h
1 #ifndef POINTCLOUD_H
2 #define POINTCLOUD_H
3
4 #include "common.h"
5 #include "world.h"
6 #include "shaders/point_map.h"
7
8 #define POINTCLOUD_POINTS 250000
9
10 struct pointcloud{
11 GLuint vao, vbo;
12 u32 count;
13
14 v4f control;
15 }
16 static pointcloud = { .control = {0.0f,0.0f,0.0f,1.0f} };
17
18 #pragma pack(push,1)
19 struct pointcloud_vert{
20 u16 pos[4]; /* float[ 0 -> 1 ] */
21 i8 norm[4]; /* float[ -1 -> 1 ] */
22 u8 colour[4]; /* float[ 0 -> 1 ] */
23 };
24 #pragma pack(pop)
25
26 static void async_pointcloud_sub( void *payload, u32 size )
27 {
28
29 glBindVertexArray( pointcloud.vao );
30 glBindBuffer( GL_ARRAY_BUFFER, pointcloud.vbo );
31 glBufferSubData( GL_ARRAY_BUFFER, 0,
32 sizeof(struct pointcloud_vert)*size, payload );
33 pointcloud.count = size;
34 }
35
36 static void async_pointcloud_alloc( void *payload, u32 size )
37 {
38 glGenVertexArrays( 1, &pointcloud.vao );
39 glGenBuffers( 1, &pointcloud.vbo );
40 glBindVertexArray( pointcloud.vao );
41
42 size_t stride = sizeof( struct pointcloud_vert );
43
44 glBindBuffer( GL_ARRAY_BUFFER, pointcloud.vbo );
45 glBufferData( GL_ARRAY_BUFFER, stride * POINTCLOUD_POINTS,
46 payload, GL_DYNAMIC_DRAW );
47
48 /* 0: coordinates */
49 glVertexAttribPointer( 0, 4, GL_UNSIGNED_SHORT, GL_TRUE, stride, (void*)0 );
50 glEnableVertexAttribArray( 0 );
51
52 /* 1: normal */
53 glVertexAttribPointer( 1, 4, GL_BYTE, GL_TRUE,
54 stride, (void *)offsetof(struct pointcloud_vert, norm) );
55 glEnableVertexAttribArray( 1 );
56
57 /* 2: colour */
58 glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE, GL_TRUE,
59 stride, (void *)offsetof(struct pointcloud_vert, colour) );
60 glEnableVertexAttribArray( 2 );
61 VG_CHECK_GL_ERR();
62 pointcloud.count = size;
63 }
64
65 static void pointcloud_init(void)
66 {
67 vg_info( "Generating random test point cloud\n" );
68
69 vg_rand_seed( 2000 );
70 vg_async_item *call =
71 vg_async_alloc( sizeof(struct pointcloud_vert)*POINTCLOUD_POINTS );
72 struct pointcloud_vert *test_data = call->payload;
73 call->size = POINTCLOUD_POINTS;
74
75 for( u32 i=0; i<POINTCLOUD_POINTS; i++ ){
76 test_data[i].pos[0] = vg_randf64() * 65535.0f;
77 test_data[i].pos[1] = vg_randf64() * 65535.0f;
78 test_data[i].pos[2] = vg_randf64() * 65535.0f;
79
80 v3f norm;
81 vg_rand_dir( norm );
82
83 test_data[i].norm[0] = norm[0] * 127.0f;
84 test_data[i].norm[1] = norm[1] * 127.0f;
85 test_data[i].norm[2] = norm[2] * 127.0f;
86
87 test_data[i].colour[0] = 90;
88 test_data[i].colour[1] = 90;
89 test_data[i].colour[2] = 90;
90 test_data[i].colour[3] = 255;
91 }
92
93 vg_async_dispatch( call, async_pointcloud_alloc );
94 shader_point_map_register();
95 }
96
97 static void pointcloud_render( world_instance *world, camera *cam, m4x3f model )
98 {
99 m4x4f upvmprev;
100 m4x3_expand( model, upvmprev );
101 m4x4_mul( cam->mtx_prev.pv, upvmprev, upvmprev );
102
103 shader_point_map_use();
104 shader_point_map_uPv( cam->mtx.pv );
105 shader_point_map_uPvmPrev( upvmprev );
106 shader_point_map_uMdl( model );
107 shader_point_map_uCamera( cam->pos );
108 shader_point_map_uTime( (v2f){ vg.time, sinf(vg.time) } );
109 shader_point_map_uTransform( pointcloud.control );
110
111 m3x3f mnorm;
112 m3x3_inv( model, mnorm );
113 m3x3_transpose( mnorm, mnorm );
114 shader_point_map_uNormMtx( mnorm );
115
116 glBindVertexArray( pointcloud.vao );
117 glEnable( GL_PROGRAM_POINT_SIZE );
118 glDrawArrays( GL_POINTS, 0, pointcloud.count );
119 }
120
121 #endif /* POINTCLOUD_H */