commit 8d93f389412ac688d106b20b9068902859c64cbd
parent 0e6261e6cff71b88f4d3a8f5962350bbde3de25b
Author: vaplv <vaplv@free.fr>
Date: Wed, 24 Sep 2014 18:07:24 +0200
Change the aw_mtl_material_get API
The returned material is self contained and does not rely on the aw_mtl
memory management
Diffstat:
| M | src/aw.h | | | 11 | ++++++++++- |
| M | src/aw_mtl.c | | | 243 | +++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------- |
| M | src/test_aw_mtl.c | | | 241 | +++++++++++++++++++++++++++++++++++++++++-------------------------------------- |
3 files changed, 300 insertions(+), 195 deletions(-)
diff --git a/src/aw.h b/src/aw.h
@@ -236,11 +236,20 @@ aw_mtl_materials_count_get
(struct aw_mtl* mtl,
size_t* materials_count);
+/* The filled material must be released by the AW_MATERIAL_RELEASE function */
AW_API res_T
aw_mtl_material_get
(struct aw_mtl* mtl,
const size_t imaterial,
- const struct aw_material** material);
+ struct aw_material* material);
+
+AW_API res_T
+aw_material_init
+ (struct aw_material* material);
+
+AW_API res_T
+aw_material_release
+ (struct aw_material* material);
END_DECLS
diff --git a/src/aw_mtl.c b/src/aw_mtl.c
@@ -22,12 +22,26 @@
#include <rsys/logger.h>
#include <rsys/mem_allocator.h>
#include <rsys/ref_count.h>
+#include <rsys/str.h>
#include <float.h>
/*******************************************************************************
* Map helper data structure
******************************************************************************/
+struct map {
+ struct str filename;
+ int options_mask;
+ float image_bias; /* Scalar to add to the image pixels */
+ float image_scale; /* Scalar to multiply to the image pixels */
+ float texcoord_bias[3];
+ float texcoord_scale[3];
+ float texcoord_turbulence[3];
+ size_t resolution; /* image size = resolution x resolution */
+ enum aw_map_channel scalar; /* Channel used to create a scalar texture */
+ float bump_multiplier; /* Only available on bump maps */
+};
+
enum map_type {
MAP_COMMON = BIT(0),
MAP_SCALAR = BIT(1),
@@ -35,11 +49,11 @@ enum map_type {
};
static FINLINE void
-map_init(struct mem_allocator* allocator, struct aw_map* map)
+map_init(struct mem_allocator* allocator, struct map* map)
{
(void)allocator;
ASSERT(map);
- map->filename = NULL;
+ str_init(allocator, &map->filename);
map->options_mask = 0;
map->image_bias = 0.f;
map->image_scale = 1.f;
@@ -52,15 +66,14 @@ map_init(struct mem_allocator* allocator, struct aw_map* map)
}
static FINLINE void
-map_release(struct aw_map* map)
+map_release(struct map* map)
{
ASSERT(map);
- if(map->filename)
- mem_free(map->filename);
+ str_release(&map->filename);
}
static FINLINE void
-map_copy_pod__(struct aw_map* dst, const struct aw_map* src)
+map_copy_pod__(struct map* dst, const struct map* src)
{
ASSERT(dst && src);
dst->options_mask = src->options_mask;
@@ -75,32 +88,71 @@ map_copy_pod__(struct aw_map* dst, const struct aw_map* src)
}
static FINLINE res_T
-map_copy(struct aw_map* dst, const struct aw_map* src)
+map_copy(struct map* dst, const struct map* src)
{
ASSERT(dst && src);
- if(dst == src)
- return R_OK;
+ if(dst == src) return R_OK;
+ map_copy_pod__(dst, src);
+ return str_copy(&dst->filename, &src->filename);
+}
+static FINLINE res_T
+map_copy_and_release(struct map* dst, struct map* src)
+{
+ ASSERT(dst && src);
+ if(dst == src) return R_OK;
map_copy_pod__(dst, src);
- if(dst->filename)
- mem_free(dst->filename);
- if(src->filename) {
- dst->filename = mem_alloc(strlen(src->filename)+1);
- if(!dst->filename)
- return R_MEM_ERR;
- strcpy(dst->filename, src->filename);
+ return str_copy_and_release(&dst->filename, &src->filename);
+}
+
+static FINLINE res_T
+map_to_aw_map(struct aw_map* dst, const struct map* src)
+{
+ ASSERT(dst && src);
+ if(!str_len(&src->filename)) {
+ dst->filename = NULL;
+ } else {
+ dst->filename = mem_alloc(str_len(&src->filename) + 1);
+ if(!dst->filename) return R_MEM_ERR;
+ strcpy(dst->filename, str_cget(&src->filename));
}
+ dst->options_mask = src->options_mask;
+ dst->image_bias = src->image_bias;
+ dst->image_scale = src->image_scale;
+ f3_set(dst->texcoord_bias, src->texcoord_bias);
+ f3_set(dst->texcoord_scale, src->texcoord_scale);
+ f3_set(dst->texcoord_turbulence, src->texcoord_turbulence);
+ dst->resolution = src->resolution;
+ dst->scalar = src->scalar;
+ dst->bump_multiplier = src->bump_multiplier;
return R_OK;
}
/*******************************************************************************
* Material helper functions
******************************************************************************/
+struct material {
+ struct str name;
+ struct aw_color ambient;
+ struct aw_color diffuse;
+ struct aw_color specular;
+ struct aw_color transmission;
+ float specular_exponent;
+ float refraction_index;
+ size_t illumination_model; /* In [0, 10] */
+ struct map ambient_map;
+ struct map diffuse_map;
+ struct map specular_map;
+ struct map specular_exponent_map; /* Scalar texture */
+ struct map bump_map; /* Scalar texture with valid bump multiplier */
+};
+
static FINLINE void
-material_init(struct mem_allocator* allocator, struct aw_material* mtl)
+material_init(struct mem_allocator* allocator, struct material* mtl)
{
ASSERT(mtl);
- memset(mtl, 0, sizeof(struct aw_material));
+ memset(mtl, 0, sizeof(struct material));
+ str_init(allocator, &mtl->name);
map_init(allocator, &mtl->ambient_map);
map_init(allocator, &mtl->diffuse_map);
map_init(allocator, &mtl->specular_map);
@@ -109,11 +161,10 @@ material_init(struct mem_allocator* allocator, struct aw_material* mtl)
}
static FINLINE void
-material_release(struct aw_material* mtl)
+material_release(struct material* mtl)
{
ASSERT(mtl);
- if(mtl->name)
- mem_free(mtl->name);
+ str_release(&mtl->name);
map_release(&mtl->ambient_map);
map_release(&mtl->diffuse_map);
map_release(&mtl->specular_map);
@@ -122,7 +173,7 @@ material_release(struct aw_material* mtl)
}
static FINLINE void
-material_copy_pod__(struct aw_material* dst, const struct aw_material* src)
+material_copy_pod__(struct material* dst, const struct material* src)
{
ASSERT(dst && src);
dst->ambient = src->ambient;
@@ -135,33 +186,37 @@ material_copy_pod__(struct aw_material* dst, const struct aw_material* src)
}
static FINLINE res_T
-material_copy(struct aw_material* dst, const struct aw_material* src)
+material_copy(struct material* dst, const struct material* src)
{
res_T res;
-
- if(dst == src)
- return R_OK;
- if(src->name) {
- if(dst->name)
- mem_free(dst->name);
- dst->name = mem_alloc(strlen(src->name) + 1);
- if(!dst->name)
- return R_MEM_ERR;
- strcpy(dst->name, src->name);
- }
-
+ if(dst == src) return R_OK;
material_copy_pod__(dst, src);
- res = map_copy(&dst->ambient_map, &src->ambient_map);
- if(res != R_OK) return res;
- res = map_copy(&dst->diffuse_map, &src->diffuse_map);
- if(res != R_OK) return res;
- res = map_copy(&dst->specular_map, &src->specular_map);
- if(res != R_OK) return res;
- res = map_copy(&dst->specular_exponent_map, &src->specular_exponent_map);
- if(res != R_OK) return res;
- res = map_copy(&dst->bump_map, &src->bump_map);
- if(res != R_OK) return res;
+ #define CALL(Func) if(R_OK != (res = Func)) return res
+ CALL(str_copy(&dst->name, &src->name));
+ CALL(map_copy(&dst->ambient_map, &src->ambient_map));
+ CALL(map_copy(&dst->diffuse_map, &src->diffuse_map));
+ CALL(map_copy(&dst->specular_map, &src->specular_map));
+ CALL(map_copy(&dst->specular_exponent_map, &src->specular_exponent_map));
+ CALL(map_copy(&dst->bump_map, &src->bump_map));
+ #undef CALL
+ return R_OK;
+}
+static FINLINE res_T
+material_copy_and_release(struct material* dst, struct material* src)
+{
+ res_T res;
+ if(dst == src) return R_OK;
+ material_copy_pod__(dst, src);
+ #define CALL(Func) if(R_OK != (res = Func)) return res
+ CALL(str_copy_and_release(&dst->name, &src->name));
+ CALL(map_copy_and_release(&dst->ambient_map, &src->ambient_map));
+ CALL(map_copy_and_release(&dst->diffuse_map, &src->diffuse_map));
+ CALL(map_copy_and_release(&dst->specular_map, &src->specular_map));
+ CALL(map_copy_and_release
+ (&dst->specular_exponent_map, &src->specular_exponent_map));
+ CALL(map_copy_and_release(&dst->bump_map, &src->bump_map));
+ #undef CALL
return R_OK;
}
@@ -170,7 +225,7 @@ material_copy(struct aw_material* dst, const struct aw_material* src)
******************************************************************************/
/* Generate the darray_mtl data structure */
#define DARRAY_NAME material
-#define DARRAY_DATA struct aw_material
+#define DARRAY_DATA struct material
#define DARRAY_FUNCTOR_INIT material_init
#define DARRAY_FUNCTOR_RELEASE material_release
#define DARRAY_FUNCTOR_COPY material_copy
@@ -178,7 +233,7 @@ material_copy(struct aw_material* dst, const struct aw_material* src)
struct aw_mtl {
struct darray_material materials;
- struct aw_material* newmtl; /* Pointer toward the current material */
+ struct material* newmtl; /* Pointer toward the current material */
const char* filename; /* Currently parsed file. Use in error messages */
size_t iline; /* Currently parsed line index. Use in error messages */
@@ -191,12 +246,9 @@ struct aw_mtl {
/*******************************************************************************
* Helper functions
******************************************************************************/
-static res_T
-parse_name(char** name, char* tk, char** tk_ctxt)
+static INLINE res_T
+parse_name(struct str* name, char* tk, char** tk_ctxt)
{
- char* buf = NULL;
- size_t buf_len = 0;
- size_t buf_size_max = 0;
res_T res = R_OK;
ASSERT(tk_ctxt && name);
@@ -204,33 +256,16 @@ parse_name(char** name, char* tk, char** tk_ctxt)
res = R_BAD_ARG;
goto error;
}
-
+ str_clear(name);
do {
- const size_t capacity =
- buf_len + strlen(tk) + 1 /* null char */ + (buf_len > 0) /* space */;
-
- if( capacity >= buf_size_max) {
- buf_size_max = round_up_pow2(capacity);
- buf = mem_realloc(buf, buf_size_max);
- if(!buf) {
- res = R_MEM_ERR;
- goto error;
- }
- }
- if(buf_len > 0)
- buf[buf_len++] = ' ';
- buf[buf_len] = '\0';
- strcat(buf, tk);
- buf_len = capacity - 1 /* don't count null char */;
+ res = str_append(name, tk);
+ if(res != R_OK) goto error;
} while((tk = strtok_r(NULL, " \t", tk_ctxt)));
- *name = buf;
-
exit:
return res;
error:
- if(buf)
- mem_free(buf);
+ str_clear(name);
goto exit;
}
@@ -363,7 +398,7 @@ parse_imfchan_option(enum aw_map_channel* channel, char** tk_ctxt)
static res_T
parse_map
- (struct aw_map* map,
+ (struct map* map,
const enum map_type type,
char** tk_ctxt)
{
@@ -407,8 +442,6 @@ parse_map
res = parse_floatX
(&map->bump_multiplier, 1, 1, -FLT_MAX, FLT_MAX, 0.f, tk_ctxt);
} else {
- if(map->filename)
- mem_free(map->filename);
res = parse_name(&map->filename, word, tk_ctxt);
}
if(res != R_OK)
@@ -645,11 +678,65 @@ res_T
aw_mtl_material_get
(struct aw_mtl* mtl,
const size_t imaterial,
- const struct aw_material** material)
+ struct aw_material* material)
{
+ const struct material* mtr;
+ res_T res = R_OK;
+
if(!mtl || !material || imaterial>=darray_material_size_get(&mtl->materials))
return R_BAD_ARG;
- *material = darray_material_cdata_get(&mtl->materials) + imaterial;
+
+ mtr = darray_material_cdata_get(&mtl->materials) + imaterial;
+ material->name = mem_alloc(str_len(&mtr->name) + 1);
+ if(!material->name) {
+ res = R_MEM_ERR;
+ goto error;
+ }
+ strcpy(material->name, str_cget(&mtr->name));
+ material->ambient = mtr->ambient;
+ material->diffuse = mtr->diffuse;
+ material->specular = mtr->specular;
+ material->transmission = mtr->transmission;
+ material->specular_exponent = mtr->specular_exponent;
+ material->refraction_index = mtr->refraction_index;
+ material->illumination_model = mtr->illumination_model;
+
+ #define CALL(Func) if(R_OK != (res = Func)) goto error
+ CALL(map_to_aw_map(&material->ambient_map, &mtr->ambient_map));
+ CALL(map_to_aw_map(&material->diffuse_map, &mtr->diffuse_map));
+ CALL(map_to_aw_map(&material->specular_map, &mtr->specular_map));
+ CALL(map_to_aw_map
+ (&material->specular_exponent_map, &mtr->specular_exponent_map));
+ CALL(map_to_aw_map(&material->bump_map, &mtr->bump_map));
+ #undef CALL
+
+exit:
+ return res;
+error:
+ if(material)
+ AW(material_release(material));
+ goto exit;
+}
+
+res_T
+aw_material_init(struct aw_material* m)
+{
+ if(!m) return R_BAD_ARG;
+ memset(m, 0, sizeof(struct aw_material));
+ return R_OK;
+}
+
+res_T
+aw_material_release(struct aw_material* m)
+{
+ if(!m) return R_BAD_ARG;
+ if(m->name) mem_free(m->name);
+ if(m->ambient_map.filename) mem_free(m->ambient_map.filename);
+ if(m->diffuse_map.filename) mem_free(m->diffuse_map.filename);
+ if(m->specular_map.filename) mem_free(m->specular_map.filename);
+ if(m->specular_exponent_map.filename)
+ mem_free(m->specular_exponent_map.filename);
+ if(m->bump_map.filename) mem_free(m->bump_map.filename);
return R_OK;
}
diff --git a/src/test_aw_mtl.c b/src/test_aw_mtl.c
@@ -50,7 +50,7 @@ test_common(struct aw_mtl* mtl)
FILE* file;
size_t nmtls;
float tmp[3];
- const struct aw_material* mtr;
+ struct aw_material mtr;
NCHECK(mtl, NULL);
@@ -71,6 +71,8 @@ test_common(struct aw_mtl* mtl)
CHECK(nmtls, 1);
+ CHECK(aw_material_init(NULL), R_BAD_ARG);
+ CHECK(aw_material_init(&mtr), R_OK);
CHECK(aw_mtl_material_get(NULL, SIZE_MAX, NULL), R_BAD_ARG);
CHECK(aw_mtl_material_get(mtl, SIZE_MAX, NULL), R_BAD_ARG);
CHECK(aw_mtl_material_get(NULL, 0, NULL), R_BAD_ARG);
@@ -80,69 +82,72 @@ test_common(struct aw_mtl* mtl)
CHECK(aw_mtl_material_get(NULL, 0, &mtr), R_BAD_ARG);
CHECK(aw_mtl_material_get(mtl, 0, &mtr), R_OK);
- CHECK(strcmp(mtr->name, "my_mtl"), 0);
- CHECK(mtr->ambient.color_space, AW_COLOR_RGB);
+ CHECK(strcmp(mtr.name, "my_mtl"), 0);
+ CHECK(mtr.ambient.color_space, AW_COLOR_RGB);
f3(tmp, 0.0435f, 0.0436f, 0.0437f);
- CHECK(f3_eq(mtr->ambient.value, tmp), 1);
+ CHECK(f3_eq(mtr.ambient.value, tmp), 1);
- CHECK(mtr->diffuse.color_space, AW_COLOR_RGB);
+ CHECK(mtr.diffuse.color_space, AW_COLOR_RGB);
f3(tmp, 0.1086f, 0.1087f, 0.1088f);
- CHECK(f3_eq(mtr->diffuse.value, tmp), 1);
+ CHECK(f3_eq(mtr.diffuse.value, tmp), 1);
- CHECK(mtr->specular.color_space, AW_COLOR_RGB);
+ CHECK(mtr.specular.color_space, AW_COLOR_RGB);
f3_splat(tmp, 0.f);
- CHECK(f3_eq(mtr->specular.value, tmp), 1);
+ CHECK(f3_eq(mtr.specular.value, tmp), 1);
- CHECK(mtr->transmission.color_space, AW_COLOR_XYZ);
+ CHECK(mtr.transmission.color_space, AW_COLOR_XYZ);
f3(tmp, 0.987f, 0.988f, 0.989f);
- CHECK(f3_eq(mtr->transmission.value, tmp), 1);
-
- CHECK(mtr->specular_exponent, 10.f);
- CHECK(mtr->refraction_index, (float)1.19713f);
- CHECK(mtr->illumination_model, 6);
-
- CHECK(strcmp(mtr->ambient_map.filename, "chrome.mpc"), 0);
- CHECK(mtr->ambient_map.options_mask, 0);
- CHECK(mtr->ambient_map.image_bias, 0.f);
- CHECK(mtr->ambient_map.image_scale, 1.f);
- CHECK(f3_eq(mtr->ambient_map.texcoord_bias, f3_splat(tmp, 0.f)), 1);
- CHECK(f3_eq(mtr->ambient_map.texcoord_scale, f3_splat(tmp, 1.f)), 1);
- CHECK(f3_eq(mtr->ambient_map.texcoord_turbulence, f3_splat(tmp, 0.f)), 1);
-
- CHECK(strcmp(mtr->diffuse_map.filename, "chrome.mpc"), 0);
- CHECK(mtr->diffuse_map.options_mask, 0);
- CHECK(mtr->diffuse_map.image_bias, 0.f);
- CHECK(mtr->diffuse_map.image_scale, 1.f);
- CHECK(f3_eq(mtr->diffuse_map.texcoord_bias, f3_splat(tmp, 0.f)), 1);
- CHECK(f3_eq(mtr->diffuse_map.texcoord_scale, f3_splat(tmp, 1.f)), 1);
- CHECK(f3_eq(mtr->diffuse_map.texcoord_turbulence, f3_splat(tmp, 0.f)), 1);
-
- CHECK(strcmp(mtr->specular_map.filename, "chrome.mpc"), 0);
- CHECK(mtr->specular_map.options_mask, 0);
- CHECK(mtr->specular_map.image_bias, 0.f);
- CHECK(mtr->specular_map.image_scale, 1.f);
- CHECK(f3_eq(mtr->specular_map.texcoord_bias, f3_splat(tmp, 0.f)), 1);
- CHECK(f3_eq(mtr->specular_map.texcoord_scale, f3_splat(tmp, 1.f)), 1);
- CHECK(f3_eq(mtr->specular_map.texcoord_turbulence, f3_splat(tmp, 0.f)), 1);
-
- CHECK(strcmp(mtr->specular_exponent_map.filename, "wisp.mps"), 0);
- CHECK(mtr->specular_exponent_map.options_mask, 0);
- CHECK(mtr->specular_exponent_map.image_bias, 0.f);
- CHECK(mtr->specular_exponent_map.image_scale, 1.f);
- CHECK(f3_eq(mtr->specular_exponent_map.texcoord_scale, f3_splat(tmp, 1.f)), 1);
- CHECK(f3_eq(mtr->specular_exponent_map.texcoord_bias, f3_splat(tmp, 0.f)), 1);
- CHECK(f3_eq(mtr->specular_exponent_map.texcoord_turbulence, tmp), 1);
- CHECK(mtr->specular_exponent_map.scalar, AW_MAP_CHANNEL_LUMINANCE);
-
- CHECK(strcmp(mtr->bump_map.filename, "sand.mpb"), 0);
- CHECK(mtr->bump_map.options_mask, 0);
- CHECK(mtr->bump_map.image_bias, 0.f);
- CHECK(mtr->bump_map.image_scale, 1.f);
- CHECK(f3_eq(mtr->bump_map.texcoord_scale, f3_splat(tmp, 1.f)), 1);
- CHECK(f3_eq(mtr->bump_map.texcoord_bias, f3_splat(tmp, 0.f)), 1);
- CHECK(f3_eq(mtr->bump_map.texcoord_turbulence, tmp), 1);
- CHECK(mtr->bump_map.scalar, AW_MAP_CHANNEL_LUMINANCE);
- CHECK(mtr->bump_map.bump_multiplier, 1.f);
+ CHECK(f3_eq(mtr.transmission.value, tmp), 1);
+
+ CHECK(mtr.specular_exponent, 10.f);
+ CHECK(mtr.refraction_index, (float)1.19713f);
+ CHECK(mtr.illumination_model, 6);
+
+ CHECK(strcmp(mtr.ambient_map.filename, "chrome.mpc"), 0);
+ CHECK(mtr.ambient_map.options_mask, 0);
+ CHECK(mtr.ambient_map.image_bias, 0.f);
+ CHECK(mtr.ambient_map.image_scale, 1.f);
+ CHECK(f3_eq(mtr.ambient_map.texcoord_bias, f3_splat(tmp, 0.f)), 1);
+ CHECK(f3_eq(mtr.ambient_map.texcoord_scale, f3_splat(tmp, 1.f)), 1);
+ CHECK(f3_eq(mtr.ambient_map.texcoord_turbulence, f3_splat(tmp, 0.f)), 1);
+
+ CHECK(strcmp(mtr.diffuse_map.filename, "chrome.mpc"), 0);
+ CHECK(mtr.diffuse_map.options_mask, 0);
+ CHECK(mtr.diffuse_map.image_bias, 0.f);
+ CHECK(mtr.diffuse_map.image_scale, 1.f);
+ CHECK(f3_eq(mtr.diffuse_map.texcoord_bias, f3_splat(tmp, 0.f)), 1);
+ CHECK(f3_eq(mtr.diffuse_map.texcoord_scale, f3_splat(tmp, 1.f)), 1);
+ CHECK(f3_eq(mtr.diffuse_map.texcoord_turbulence, f3_splat(tmp, 0.f)), 1);
+
+ CHECK(strcmp(mtr.specular_map.filename, "chrome.mpc"), 0);
+ CHECK(mtr.specular_map.options_mask, 0);
+ CHECK(mtr.specular_map.image_bias, 0.f);
+ CHECK(mtr.specular_map.image_scale, 1.f);
+ CHECK(f3_eq(mtr.specular_map.texcoord_bias, f3_splat(tmp, 0.f)), 1);
+ CHECK(f3_eq(mtr.specular_map.texcoord_scale, f3_splat(tmp, 1.f)), 1);
+ CHECK(f3_eq(mtr.specular_map.texcoord_turbulence, f3_splat(tmp, 0.f)), 1);
+
+ CHECK(strcmp(mtr.specular_exponent_map.filename, "wisp.mps"), 0);
+ CHECK(mtr.specular_exponent_map.options_mask, 0);
+ CHECK(mtr.specular_exponent_map.image_bias, 0.f);
+ CHECK(mtr.specular_exponent_map.image_scale, 1.f);
+ CHECK(f3_eq(mtr.specular_exponent_map.texcoord_scale, f3_splat(tmp, 1.f)), 1);
+ CHECK(f3_eq(mtr.specular_exponent_map.texcoord_bias, f3_splat(tmp, 0.f)), 1);
+ CHECK(f3_eq(mtr.specular_exponent_map.texcoord_turbulence, tmp), 1);
+ CHECK(mtr.specular_exponent_map.scalar, AW_MAP_CHANNEL_LUMINANCE);
+
+ CHECK(strcmp(mtr.bump_map.filename, "sand.mpb"), 0);
+ CHECK(mtr.bump_map.options_mask, 0);
+ CHECK(mtr.bump_map.image_bias, 0.f);
+ CHECK(mtr.bump_map.image_scale, 1.f);
+ CHECK(f3_eq(mtr.bump_map.texcoord_scale, f3_splat(tmp, 1.f)), 1);
+ CHECK(f3_eq(mtr.bump_map.texcoord_bias, f3_splat(tmp, 0.f)), 1);
+ CHECK(f3_eq(mtr.bump_map.texcoord_turbulence, tmp), 1);
+ CHECK(mtr.bump_map.scalar, AW_MAP_CHANNEL_LUMINANCE);
+ CHECK(mtr.bump_map.bump_multiplier, 1.f);
+
+ CHECK(aw_material_release(NULL), R_BAD_ARG);
+ CHECK(aw_material_release(&mtr), R_OK);
}
static void
@@ -187,7 +192,7 @@ test_multiple_materials(struct aw_mtl* mtl)
FILE* file;
size_t nmtls;
float tmp[3];
- const struct aw_material* mtr;
+ struct aw_material mtr;
NCHECK(mtl, NULL);
@@ -203,71 +208,75 @@ test_multiple_materials(struct aw_mtl* mtl)
CHECK(aw_mtl_materials_count_get(mtl, &nmtls), R_OK);
CHECK(nmtls, 3);
+ CHECK(aw_material_init(&mtr), R_OK);
CHECK(aw_mtl_material_get(mtl, 0, &mtr), R_OK);
- CHECK(strcmp(mtr->name, "material_0"), 0);
- CHECK(mtr->specular_exponent, 8.f);
- CHECK(mtr->refraction_index, 1.5f);
- CHECK(mtr->transmission.color_space, AW_COLOR_RGB);
- CHECK(f3_eq(mtr->transmission.value, f3_splat(tmp, 1.f)), 1);
- CHECK(mtr->illumination_model, 2);
- CHECK(mtr->ambient.color_space, AW_COLOR_RGB);
- CHECK(f3_eq(mtr->ambient.value, f3_splat(tmp, 0.f)), 1);
- CHECK(mtr->diffuse.color_space, AW_COLOR_RGB);
- CHECK(f3_eq(mtr->diffuse.value, f3(tmp, 0.734118f, 0.730588f, 0.674118f)), 1);
- CHECK(mtr->specular.color_space, AW_COLOR_RGB);
- CHECK(f3_eq(mtr->specular.value, f3_splat(tmp, 0.f)), 1);
+ CHECK(strcmp(mtr.name, "material_0"), 0);
+ CHECK(mtr.specular_exponent, 8.f);
+ CHECK(mtr.refraction_index, 1.5f);
+ CHECK(mtr.transmission.color_space, AW_COLOR_RGB);
+ CHECK(f3_eq(mtr.transmission.value, f3_splat(tmp, 1.f)), 1);
+ CHECK(mtr.illumination_model, 2);
+ CHECK(mtr.ambient.color_space, AW_COLOR_RGB);
+ CHECK(f3_eq(mtr.ambient.value, f3_splat(tmp, 0.f)), 1);
+ CHECK(mtr.diffuse.color_space, AW_COLOR_RGB);
+ CHECK(f3_eq(mtr.diffuse.value, f3(tmp, 0.734118f, 0.730588f, 0.674118f)), 1);
+ CHECK(mtr.specular.color_space, AW_COLOR_RGB);
+ CHECK(f3_eq(mtr.specular.value, f3_splat(tmp, 0.f)), 1);
CHECK(strcmp
- (mtr->ambient_map.filename,
+ (mtr.ambient_map.filename,
"my_long_and_verbose_filename_of_a_RED_GREEN_BLUE_1024x64_image.png"), 0);
- CHECK(mtr->ambient_map.options_mask, 0);
- CHECK(mtr->ambient_map.image_bias, 0.f);
- CHECK(mtr->ambient_map.image_scale, 1.f);
- CHECK(f3_eq(mtr->ambient_map.texcoord_bias, f3_splat(tmp, 0.f)), 1);
- CHECK(f3_eq(mtr->ambient_map.texcoord_scale, f3_splat(tmp, 1.f)), 1);
- CHECK(f3_eq(mtr->ambient_map.texcoord_turbulence, f3_splat(tmp, 0.f)), 1);
- CHECK(mtr->ambient_map.resolution, 0);
- CHECK(strcmp(mtr->diffuse_map.filename, "tp.png"), 0);
- CHECK(mtr->specular_map.filename, NULL);
- CHECK(mtr->specular_exponent_map.filename, NULL);
- CHECK(mtr->bump_map.filename, NULL);
+ CHECK(mtr.ambient_map.options_mask, 0);
+ CHECK(mtr.ambient_map.image_bias, 0.f);
+ CHECK(mtr.ambient_map.image_scale, 1.f);
+ CHECK(f3_eq(mtr.ambient_map.texcoord_bias, f3_splat(tmp, 0.f)), 1);
+ CHECK(f3_eq(mtr.ambient_map.texcoord_scale, f3_splat(tmp, 1.f)), 1);
+ CHECK(f3_eq(mtr.ambient_map.texcoord_turbulence, f3_splat(tmp, 0.f)), 1);
+ CHECK(mtr.ambient_map.resolution, 0);
+ CHECK(strcmp(mtr.diffuse_map.filename, "tp.png"), 0);
+ CHECK(mtr.specular_map.filename, NULL);
+ CHECK(mtr.specular_exponent_map.filename, NULL);
+ CHECK(mtr.bump_map.filename, NULL);
+ CHECK(aw_material_release(&mtr), R_OK);
CHECK(aw_mtl_material_get(mtl, 1, &mtr), R_OK);
- CHECK(strcmp(mtr->name, "textured_material"), 0);
- CHECK(mtr->specular_exponent, 6.f);
- CHECK(mtr->refraction_index, (float)1.7f);
- CHECK(mtr->transmission.color_space, AW_COLOR_RGB);
- CHECK(f3_eq(mtr->transmission.value, f3(tmp, 1.f, 1.2f, 1.3f)), 1);
- CHECK(mtr->illumination_model, 0);
- CHECK(mtr->ambient.color_space, AW_COLOR_RGB);
- CHECK(f3_eq(mtr->ambient.value, f3_splat(tmp, 0.f)), 1);
- CHECK(mtr->diffuse.color_space, AW_COLOR_RGB);
- CHECK(f3_eq(mtr->diffuse.value, f3(tmp, 0.734118f, 0.709412f, 0.674118f)), 1);
- CHECK(mtr->specular.color_space, AW_COLOR_RGB);
- CHECK(f3_eq(mtr->specular.value, f3_splat(tmp, 0.f)), 1);
- CHECK(strcmp(mtr->ambient_map.filename, "tex6x6.png"), 0);
- CHECK(strcmp(mtr->diffuse_map.filename, "tex6x6.png"), 0);
- CHECK(strcmp(mtr->bump_map.filename, "tex6x6-bump.png"), 0);
- CHECK(mtr->bump_map.scalar, AW_MAP_CHANNEL_RED);
- CHECK(mtr->bump_map.bump_multiplier, (float)0.2f);
- CHECK(mtr->specular_exponent_map.filename, NULL);
+ CHECK(strcmp(mtr.name, "textured_material"), 0);
+ CHECK(mtr.specular_exponent, 6.f);
+ CHECK(mtr.refraction_index, (float)1.7f);
+ CHECK(mtr.transmission.color_space, AW_COLOR_RGB);
+ CHECK(f3_eq(mtr.transmission.value, f3(tmp, 1.f, 1.2f, 1.3f)), 1);
+ CHECK(mtr.illumination_model, 0);
+ CHECK(mtr.ambient.color_space, AW_COLOR_RGB);
+ CHECK(f3_eq(mtr.ambient.value, f3_splat(tmp, 0.f)), 1);
+ CHECK(mtr.diffuse.color_space, AW_COLOR_RGB);
+ CHECK(f3_eq(mtr.diffuse.value, f3(tmp, 0.734118f, 0.709412f, 0.674118f)), 1);
+ CHECK(mtr.specular.color_space, AW_COLOR_RGB);
+ CHECK(f3_eq(mtr.specular.value, f3_splat(tmp, 0.f)), 1);
+ CHECK(strcmp(mtr.ambient_map.filename, "tex6x6.png"), 0);
+ CHECK(strcmp(mtr.diffuse_map.filename, "tex6x6.png"), 0);
+ CHECK(strcmp(mtr.bump_map.filename, "tex6x6-bump.png"), 0);
+ CHECK(mtr.bump_map.scalar, AW_MAP_CHANNEL_RED);
+ CHECK(mtr.bump_map.bump_multiplier, (float)0.2f);
+ CHECK(mtr.specular_exponent_map.filename, NULL);
+ CHECK(aw_material_release(&mtr), R_OK);
CHECK(aw_mtl_material_get(mtl, 2, &mtr), R_OK);
- CHECK(strcmp(mtr->name, "hello_world"), 0);
- CHECK(mtr->specular_exponent, 8.f);
- CHECK(mtr->refraction_index, 1.5f);
- CHECK(mtr->transmission.color_space, AW_COLOR_RGB);
- CHECK(f3_eq(mtr->transmission.value, f3_splat(tmp, 1.f)), 1);
- CHECK(mtr->illumination_model, 2);
- CHECK(mtr->ambient.color_space, AW_COLOR_RGB);
- CHECK(f3_eq(mtr->ambient.value, f3_splat(tmp, 0.f)), 1);
- CHECK(mtr->diffuse.color_space, AW_COLOR_RGB);
- CHECK(f3_eq(mtr->diffuse.value, f3(tmp, 0.546274f, 0.219608f, 0.183922f)), 1);
- CHECK(mtr->specular.color_space, AW_COLOR_RGB);
- CHECK(f3_eq(mtr->specular.value, f3_splat(tmp, 2.f)), 1);
- CHECK(mtr->ambient_map.filename, NULL);
- CHECK(mtr->diffuse_map.filename, NULL);
- CHECK(mtr->bump_map.filename, NULL);
- CHECK(mtr->specular_exponent_map.filename, NULL);
+ CHECK(strcmp(mtr.name, "hello_world"), 0);
+ CHECK(mtr.specular_exponent, 8.f);
+ CHECK(mtr.refraction_index, 1.5f);
+ CHECK(mtr.transmission.color_space, AW_COLOR_RGB);
+ CHECK(f3_eq(mtr.transmission.value, f3_splat(tmp, 1.f)), 1);
+ CHECK(mtr.illumination_model, 2);
+ CHECK(mtr.ambient.color_space, AW_COLOR_RGB);
+ CHECK(f3_eq(mtr.ambient.value, f3_splat(tmp, 0.f)), 1);
+ CHECK(mtr.diffuse.color_space, AW_COLOR_RGB);
+ CHECK(f3_eq(mtr.diffuse.value, f3(tmp, 0.546274f, 0.219608f, 0.183922f)), 1);
+ CHECK(mtr.specular.color_space, AW_COLOR_RGB);
+ CHECK(f3_eq(mtr.specular.value, f3_splat(tmp, 2.f)), 1);
+ CHECK(mtr.ambient_map.filename, NULL);
+ CHECK(mtr.diffuse_map.filename, NULL);
+ CHECK(mtr.bump_map.filename, NULL);
+ CHECK(mtr.specular_exponent_map.filename, NULL);
+ CHECK(aw_material_release(&mtr), R_OK);
fclose(file);
}