b303973c6e6c46697ccdcc186b0c7a609e7fb456
6 #include "shaders/point_map.h"
8 #define POINTCLOUD_POINTS 250000
16 static pointcloud
= { .control
= {0.0f
,0.0f
,0.0f
,1.0f
} };
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 ] */
26 static void async_pointcloud_sub( void *payload
, u32 size
)
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
;
36 static void async_pointcloud_alloc( void *payload
, u32 size
)
38 glGenVertexArrays( 1, &pointcloud
.vao
);
39 glGenBuffers( 1, &pointcloud
.vbo
);
40 glBindVertexArray( pointcloud
.vao
);
42 size_t stride
= sizeof( struct pointcloud_vert
);
44 glBindBuffer( GL_ARRAY_BUFFER
, pointcloud
.vbo
);
45 glBufferData( GL_ARRAY_BUFFER
, stride
* POINTCLOUD_POINTS
,
46 payload
, GL_DYNAMIC_DRAW
);
49 glVertexAttribPointer( 0, 4, GL_UNSIGNED_SHORT
, GL_TRUE
, stride
, (void*)0 );
50 glEnableVertexAttribArray( 0 );
53 glVertexAttribPointer( 1, 4, GL_BYTE
, GL_TRUE
,
54 stride
, (void *)offsetof(struct pointcloud_vert
, norm
) );
55 glEnableVertexAttribArray( 1 );
58 glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE
, GL_TRUE
,
59 stride
, (void *)offsetof(struct pointcloud_vert
, colour
) );
60 glEnableVertexAttribArray( 2 );
62 pointcloud
.count
= size
;
65 static void pointcloud_init(void)
67 vg_info( "Generating random test point cloud\n" );
71 vg_async_alloc( sizeof(struct pointcloud_vert
)*POINTCLOUD_POINTS
);
72 struct pointcloud_vert
*test_data
= call
->payload
;
73 call
->size
= POINTCLOUD_POINTS
;
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
;
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
;
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;
93 vg_async_dispatch( call
, async_pointcloud_alloc
);
94 shader_point_map_register();
97 static void pointcloud_render( world_instance
*world
, camera
*cam
, m4x3f model
)
100 m4x3_expand( model
, upvmprev
);
101 m4x4_mul( cam
->mtx_prev
.pv
, upvmprev
, upvmprev
);
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
);
112 m3x3_inv( model
, mnorm
);
113 m3x3_transpose( mnorm
, mnorm
);
114 shader_point_map_uNormMtx( mnorm
);
116 glBindVertexArray( pointcloud
.vao
);
117 glEnable( GL_PROGRAM_POINT_SIZE
);
118 glDrawArrays( GL_POINTS
, 0, pointcloud
.count
);
121 #endif /* POINTCLOUD_H */