From 3743b9d032f867ff08bafb920b3a0426a0375cec Mon Sep 17 00:00:00 2001 From: bjorn Date: Sat, 21 Oct 2017 14:00:33 -0700 Subject: [PATCH] Add Material; --- src/graphics/material.c | 44 +++++++++++++++++++++++++++++++++++++++++ src/graphics/material.h | 19 ++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/graphics/material.c create mode 100644 src/graphics/material.h diff --git a/src/graphics/material.c b/src/graphics/material.c new file mode 100644 index 00000000..a1dc6539 --- /dev/null +++ b/src/graphics/material.c @@ -0,0 +1,44 @@ +#include "graphics/graphics.h" +#include "graphics/material.h" + +Material* lovrMaterialCreate(MaterialData* materialData, int isDefault) { + Material* material = lovrAlloc(sizeof(Material), lovrMaterialDestroy); + if (!material) return NULL; + + material->materialData = materialData; + material->isDefault = isDefault; + + for (int i = 0; i < MAX_MATERIAL_TEXTURES; i++) { + if (materialData->textures[i]) { + material->textures[i] = lovrTextureCreate(TEXTURE_2D, &materialData->textures[i], 1); + } + } + + return material; +} + +void lovrMaterialDestroy(const Ref* ref) { + Material* material = containerof(ref, Material); + for (int i = 0; i < MAX_MATERIAL_TEXTURES; i++) { + if (material->textures[i]) { + lovrRelease(&material->textures[i]->ref); + } + } + free(material); +} + +Color lovrMaterialGetColor(Material* material, MaterialColor colorType) { + return material->materialData->colors[colorType]; +} + +void lovrMaterialSetColor(Material* material, MaterialColor colorType, Color color) { + material->materialData->colors[colorType] = color; +} + +Texture* lovrMaterialGetTexture(Material* material, MaterialTexture textureType) { + return material->textures[textureType]; +} + +void lovrMaterialSetTexture(Material* material, MaterialTexture textureType, Texture* texture) { + material->textures[textureType] = texture; +} diff --git a/src/graphics/material.h b/src/graphics/material.h new file mode 100644 index 00000000..ea3197be --- /dev/null +++ b/src/graphics/material.h @@ -0,0 +1,19 @@ +#include "util.h" +#include "graphics/texture.h" +#include "loaders/material.h" + +#pragma once + +typedef struct { + Ref ref; + MaterialData* materialData; + Texture* textures[MAX_MATERIAL_TEXTURES]; + int isDefault; +} Material; + +Material* lovrMaterialCreate(MaterialData* materialData, int isDefault); +void lovrMaterialDestroy(const Ref* ref); +Color lovrMaterialGetColor(Material* material, MaterialColor colorType); +void lovrMaterialSetColor(Material* material, MaterialColor colorType, Color color); +Texture* lovrMaterialGetTexture(Material* material, MaterialTexture textureType); +void lovrMaterialSetTexture(Material* material, MaterialTexture textureType, Texture* texture);