Skip to content

Commit

Permalink
helper to get material effect from bind-material for given mesh primi…
Browse files Browse the repository at this point in the history
…tive
  • Loading branch information
recp committed Jan 27, 2024
1 parent 01b5b59 commit fb80131
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
20 changes: 20 additions & 0 deletions include/ak/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ extern "C" {
#include "common.h"
#include "texture.h"

struct AkBindMaterial;
struct AkMeshPrimitive;
struct AkEffect;
struct AkInstanceMaterial;

typedef enum AkGlMaterialType {
AK_GL_MATERIAL_TYPE_EMISSION = 1,
AK_GL_MATERIAL_TYPE_AMBIENT = 2,
Expand Down Expand Up @@ -122,6 +127,21 @@ typedef struct AkSpecularGlossiness {
float glossiness;
} AkSpecularGlossiness;

/*!
* @brief a helper that returns effect for given mesh prim for a bindMaterial
*
* @param bindMat bind material object in AkNode
* @param meshPrim mesh primitive
* @param foundInstMat instance material
*
* @return effect that points by a AkMaterial
*/
AK_EXPORT
struct AkEffect*
ak_effectForBindMaterial(struct AkBindMaterial * __restrict bindMat,
struct AkMeshPrimitive * __restrict meshPrim,
struct AkInstanceMaterial ** __restrict foundInstMat);

#ifdef __cplusplus
}
#endif
Expand Down
7 changes: 6 additions & 1 deletion include/ak/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ typedef struct AkTexture {

typedef struct AkTextureRef {
struct AkTexture *texture;
const char *texcoord; /* to bind texture to input coord dynamically */

/* to bind texture to input coord dynamically, e.g. used by bindMaterial in
node like COLLADA */
const char *texcoord;

/* glTF like texture bind */
const char *coordInputName;
int slot;
} AkTextureRef;
Expand Down
71 changes: 71 additions & 0 deletions src/mat/mat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (C) 2020 Recep Aslantas
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "../common.h"
#include "../../include/ak/material.h"

AK_EXPORT
AkEffect*
ak_effectForBindMaterial(AkBindMaterial * __restrict bindMat,
AkMeshPrimitive * __restrict meshPrim,
AkInstanceMaterial ** __restrict foundInstMat) {
AkMaterial *material;
AkInstanceMaterial *materialInst;
AkGeometry *geom;
AkMap *materialMap;
AkMapItem *mi;

if (!meshPrim || !meshPrim->mesh || !meshPrim->mesh->geom)
return NULL;

geom = meshPrim->mesh->geom;
materialInst = bindMat->tcommon;
materialMap = geom->materialMap;

while (materialInst) {
/* there is symbol, bind only to specified primitive */
if (materialInst->symbol) {
mi = ak_map_find(materialMap, (void *)materialInst->symbol);
while (mi) {
if ((AkMeshPrimitive *)mi->data == meshPrim) {
material = ak_instanceObject(&materialInst->base);
if (material && material->effect) {
*foundInstMat = materialInst;
return ak_instanceObject(&material->effect->base);
} else {
return NULL;
}
}
mi = mi->next;
}
}

/* bind to whole geometry, TODO: is this OK ? */
else {
material = ak_instanceObject(&materialInst->base);
if (material && material->effect) {
*foundInstMat = materialInst;
return ak_instanceObject(&material->effect->base);
} else {
return NULL;
}
}

materialInst = (AkInstanceMaterial *)materialInst->base.next;
}

return NULL;
}

0 comments on commit fb80131

Please sign in to comment.