commit 50e1563b007de63aabe332b712bf407be26f72b0
parent 743121ccc6c2ae3b15bd5383a2a2ba3fcf89f1ba
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Fri, 17 Apr 2015 14:37:23 +0200
Finalize the implementation of the s3d_sampler_get function
Diffstat:
2 files changed, 26 insertions(+), 4 deletions(-)
diff --git a/src/s3d_sampler.c b/src/s3d_sampler.c
@@ -481,6 +481,7 @@ s3d_sampler_get
const float* cdf_start = NULL;
const float* cdf_stop = NULL;
const float* cdf_found = NULL;
+ double sqrt_v;
struct mesh* mesh = NULL;
size_t imesh;
size_t imesh2tri;
@@ -522,12 +523,15 @@ s3d_sampler_get
cdf_found = std::lower_bound(cdf_start, cdf_stop, u);
ASSERT(cdf_found != cdf_stop);
+ /* Setup the sampled primitive */
primitive->mesh__ = mesh;
primitive->inst__ = sampler->cached_instance;
primitive->iprim__ = (unsigned)(cdf_found - cdf_start);
- /* TODO sample the triangle */
- (void)v, (void)w;
+ /* Compute the sampled barycentric coordinate of the sampled primitive */
+ sqrt_v = sqrt(v);
+ uv[0] = (float)(1.0 - sqrt_v);
+ uv[1] = (float)(w * sqrt_v);
exit:
return res;
diff --git a/src/test_s3d_sampler.c b/src/test_s3d_sampler.c
@@ -34,6 +34,8 @@
#include "test_s3d_cbox.h"
#include "test_s3d_utils.h"
+#include <rsys/float3.h>
+
int
main(int argc, char** argv)
{
@@ -47,6 +49,7 @@ main(int argc, char** argv)
struct s3d_vertex_data attribs[2];
struct s3d_sampler* sampler;
struct s3d_primitive prim;
+ struct s3d_attrib attr0, attr1;
struct cbox_desc desc;
float uv[2];
unsigned ntris, nverts;
@@ -98,7 +101,19 @@ main(int argc, char** argv)
CHECK(s3d_sampler_get(sampler, 1, 0, 0, &prim, uv), RES_BAD_ARG);
CHECK(s3d_sampler_get(sampler, 0, 1, 0, &prim, uv), RES_BAD_ARG);
CHECK(s3d_sampler_get(sampler, 0, 0, 1, &prim, uv), RES_BAD_ARG);
+
+ CHECK(s3d_sampler_get(sampler, 0.5f, 0.5f, 0.5f, &prim, uv), RES_OK);
+ CHECK(s3d_primitive_get_attrib(&prim, S3D_POSITION, uv, &attr0), RES_OK);
CHECK(s3d_sampler_get(sampler, 0.5f, 0.5f, 0.5f, &prim, uv), RES_OK);
+ CHECK(s3d_primitive_get_attrib(&prim, S3D_POSITION, uv, &attr1), RES_OK);
+
+ CHECK(attr0.type, S3D_FLOAT3);
+ CHECK(attr1.type, S3D_FLOAT3);
+ CHECK(f3_eq_eps(attr0.value, attr1.value, 1.e-6f), 1);
+
+ CHECK(s3d_sampler_get(sampler, 0.3f, 0.1f, 0.2f, &prim, uv), RES_OK);
+ CHECK(s3d_primitive_get_attrib(&prim, S3D_POSITION, uv, &attr1), RES_OK);
+ NCHECK(f3_eq_eps(attr0.value, attr1.value, 1.e-6f), 1);
CHECK(s3d_sampler_end_sampling(NULL), RES_BAD_ARG);
CHECK(s3d_sampler_end_sampling(sampler), RES_OK);
@@ -127,16 +142,19 @@ main(int argc, char** argv)
CHECK(s3d_sampler_begin_sampling(sampler), RES_OK);
CHECK(s3d_sampler_get(sampler, 0.5f, 0.5f, 0.5f, &prim, uv), RES_OK);
+ CHECK(s3d_primitive_get_attrib(&prim, S3D_POSITION, uv, &attr0), RES_OK);
desc.vertices = cbox_tall_block;
CHECK(s3d_mesh_setup_indexed_vertices
(tall_block, ntris, cbox_get_ids, nverts, attribs, &desc), RES_OK);
CHECK(s3d_sampler_get(sampler, 0.5f, 0.5f, 0.5f, &prim, uv), RES_OK);
- /* TODO Check that the sample is the same */
+ CHECK(s3d_primitive_get_attrib(&prim, S3D_POSITION, uv, &attr1), RES_OK);
+ CHECK(f3_eq_eps(attr0.value, attr1.value, 1.e-6f), 1);
CHECK(s3d_sampler_end_sampling(sampler), RES_OK);
CHECK(s3d_sampler_begin_sampling(sampler), RES_OK);
CHECK(s3d_sampler_get(sampler, 0.5f, 0.5f, 0.5f, &prim, uv), RES_OK);
- /* TODO Check that the sample is *not* the same */
+ CHECK(s3d_primitive_get_attrib(&prim, S3D_POSITION, uv, &attr1), RES_OK);
+ NCHECK(f3_eq_eps(attr0.value, attr1.value, 1.e-6f), 1);
CHECK(s3d_sampler_end_sampling(sampler), RES_OK);
CHECK(s3d_sampler_ref_get(NULL), RES_BAD_ARG);