| 1 |
/* |
| 2 |
* Copyright (C) 2019 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#ifndef GLTFIO_GLTFENUMS_H |
| 18 |
#define GLTFIO_GLTFENUMS_H |
| 19 |
|
| 20 |
#include <filament/IndexBuffer.h> |
| 21 |
#include <filament/RenderableManager.h> |
| 22 |
#include <filament/TextureSampler.h> |
| 23 |
#include <filament/VertexBuffer.h> |
| 24 |
|
| 25 |
#include <cgltf.h> |
| 26 |
|
| 27 |
#define GL_NEAREST 0x2600 |
| 28 |
#define GL_LINEAR 0x2601 |
| 29 |
#define GL_NEAREST_MIPMAP_NEAREST 0x2700 |
| 30 |
#define GL_LINEAR_MIPMAP_NEAREST 0x2701 |
| 31 |
#define GL_NEAREST_MIPMAP_LINEAR 0x2702 |
| 32 |
#define GL_LINEAR_MIPMAP_LINEAR 0x2703 |
| 33 |
#define GL_REPEAT 0x2901 |
| 34 |
#define GL_MIRRORED_REPEAT 0x8370 |
| 35 |
#define GL_CLAMP_TO_EDGE 0x812F |
| 36 |
|
| 37 |
inline filament::TextureSampler::WrapMode getWrapMode(cgltf_int wrap) { |
| 38 |
switch (wrap) { |
| 39 |
case GL_REPEAT: |
| 40 |
return filament::TextureSampler::WrapMode::REPEAT; |
| 41 |
case GL_MIRRORED_REPEAT: |
| 42 |
return filament::TextureSampler::WrapMode::MIRRORED_REPEAT; |
| 43 |
case GL_CLAMP_TO_EDGE: |
| 44 |
return filament::TextureSampler::WrapMode::CLAMP_TO_EDGE; |
| 45 |
} |
| 46 |
return filament::TextureSampler::WrapMode::REPEAT; |
| 47 |
} |
| 48 |
|
| 49 |
inline filament::TextureSampler::MinFilter getMinFilter(cgltf_int minFilter) { |
| 50 |
switch (minFilter) { |
| 51 |
case GL_NEAREST: |
| 52 |
return filament::TextureSampler::MinFilter::NEAREST; |
| 53 |
case GL_LINEAR: |
| 54 |
return filament::TextureSampler::MinFilter::LINEAR; |
| 55 |
case GL_NEAREST_MIPMAP_NEAREST: |
| 56 |
return filament::TextureSampler::MinFilter::NEAREST_MIPMAP_NEAREST; |
| 57 |
case GL_LINEAR_MIPMAP_NEAREST: |
| 58 |
return filament::TextureSampler::MinFilter::LINEAR_MIPMAP_NEAREST; |
| 59 |
case GL_NEAREST_MIPMAP_LINEAR: |
| 60 |
return filament::TextureSampler::MinFilter::NEAREST_MIPMAP_LINEAR; |
| 61 |
case GL_LINEAR_MIPMAP_LINEAR: |
| 62 |
return filament::TextureSampler::MinFilter::LINEAR_MIPMAP_LINEAR; |
| 63 |
} |
| 64 |
return filament::TextureSampler::MinFilter::LINEAR_MIPMAP_LINEAR; |
| 65 |
} |
| 66 |
|
| 67 |
inline filament::TextureSampler::MagFilter getMagFilter(cgltf_int magFilter) { |
| 68 |
switch (magFilter) { |
| 69 |
case GL_NEAREST: |
| 70 |
return filament::TextureSampler::MagFilter::NEAREST; |
| 71 |
case GL_LINEAR: |
| 72 |
return filament::TextureSampler::MagFilter::LINEAR; |
| 73 |
} |
| 74 |
return filament::TextureSampler::MagFilter::LINEAR; |
| 75 |
} |
| 76 |
|
| 77 |
inline bool getVertexAttrType(cgltf_attribute_type atype, filament::VertexAttribute* attrType) { |
| 78 |
switch (atype) { |
| 79 |
case cgltf_attribute_type_position: |
| 80 |
*attrType = filament::VertexAttribute::POSITION; |
| 81 |
return true; |
| 82 |
case cgltf_attribute_type_texcoord: |
| 83 |
*attrType = filament::VertexAttribute::UV0; |
| 84 |
return true; |
| 85 |
case cgltf_attribute_type_color: |
| 86 |
*attrType = filament::VertexAttribute::COLOR; |
| 87 |
return true; |
| 88 |
case cgltf_attribute_type_joints: |
| 89 |
*attrType = filament::VertexAttribute::BONE_INDICES; |
| 90 |
return true; |
| 91 |
case cgltf_attribute_type_weights: |
| 92 |
*attrType = filament::VertexAttribute::BONE_WEIGHTS; |
| 93 |
return true; |
| 94 |
case cgltf_attribute_type_normal: |
| 95 |
case cgltf_attribute_type_tangent: |
| 96 |
default: |
| 97 |
return false; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
inline bool getIndexType(cgltf_component_type ctype, filament::IndexBuffer::IndexType* itype) { |
| 102 |
switch (ctype) { |
| 103 |
case cgltf_component_type_r_8u: |
| 104 |
case cgltf_component_type_r_16u: |
| 105 |
*itype = filament::IndexBuffer::IndexType::USHORT; |
| 106 |
return true; |
| 107 |
case cgltf_component_type_r_32u: |
| 108 |
*itype = filament::IndexBuffer::IndexType::UINT; |
| 109 |
return true; |
| 110 |
default: |
| 111 |
break; |
| 112 |
} |
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
inline bool getPrimitiveType(cgltf_primitive_type in, |
| 117 |
filament::RenderableManager::PrimitiveType* out) { |
| 118 |
switch (in) { |
| 119 |
case cgltf_primitive_type_points: |
| 120 |
*out = filament::RenderableManager::PrimitiveType::POINTS; |
| 121 |
return true; |
| 122 |
case cgltf_primitive_type_lines: |
| 123 |
*out = filament::RenderableManager::PrimitiveType::LINES; |
| 124 |
return true; |
| 125 |
case cgltf_primitive_type_triangles: |
| 126 |
*out = filament::RenderableManager::PrimitiveType::TRIANGLES; |
| 127 |
return true; |
| 128 |
case cgltf_primitive_type_line_loop: |
| 129 |
case cgltf_primitive_type_line_strip: |
| 130 |
case cgltf_primitive_type_triangle_strip: |
| 131 |
case cgltf_primitive_type_triangle_fan: |
| 132 |
return false; |
| 133 |
} |
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
// This converts a cgltf component type into a Filament Attribute type. |
| 138 |
// |
| 139 |
// This function has two out parameters. One result is a safe "permitted type" which we know is |
| 140 |
// universally accepted across GPU's and backends, but may require conversion (see Transcoder). The |
| 141 |
// other result is the "actual type" which requires no conversion. |
| 142 |
// |
| 143 |
// Returns false if the given component type is invalid. |
| 144 |
inline bool getElementType(cgltf_type type, cgltf_component_type ctype, |
| 145 |
filament::VertexBuffer::AttributeType* permitType, |
| 146 |
filament::VertexBuffer::AttributeType* actualType) { |
| 147 |
switch (type) { |
| 148 |
case cgltf_type_scalar: |
| 149 |
switch (ctype) { |
| 150 |
case cgltf_component_type_r_8: |
| 151 |
*permitType = filament::VertexBuffer::AttributeType::BYTE; |
| 152 |
*actualType = filament::VertexBuffer::AttributeType::BYTE; |
| 153 |
return true; |
| 154 |
case cgltf_component_type_r_8u: |
| 155 |
*permitType = filament::VertexBuffer::AttributeType::UBYTE; |
| 156 |
*actualType = filament::VertexBuffer::AttributeType::UBYTE; |
| 157 |
return true; |
| 158 |
case cgltf_component_type_r_16: |
| 159 |
*permitType = filament::VertexBuffer::AttributeType::SHORT; |
| 160 |
*actualType = filament::VertexBuffer::AttributeType::SHORT; |
| 161 |
return true; |
| 162 |
case cgltf_component_type_r_16u: |
| 163 |
*permitType = filament::VertexBuffer::AttributeType::USHORT; |
| 164 |
*actualType = filament::VertexBuffer::AttributeType::USHORT; |
| 165 |
return true; |
| 166 |
case cgltf_component_type_r_32u: |
| 167 |
*permitType = filament::VertexBuffer::AttributeType::UINT; |
| 168 |
*actualType = filament::VertexBuffer::AttributeType::UINT; |
| 169 |
return true; |
| 170 |
case cgltf_component_type_r_32f: |
| 171 |
*permitType = filament::VertexBuffer::AttributeType::FLOAT; |
| 172 |
*actualType = filament::VertexBuffer::AttributeType::FLOAT; |
| 173 |
return true; |
| 174 |
default: |
| 175 |
return false; |
| 176 |
} |
| 177 |
break; |
| 178 |
case cgltf_type_vec2: |
| 179 |
switch (ctype) { |
| 180 |
case cgltf_component_type_r_8: |
| 181 |
*permitType = filament::VertexBuffer::AttributeType::BYTE2; |
| 182 |
*actualType = filament::VertexBuffer::AttributeType::BYTE2; |
| 183 |
return true; |
| 184 |
case cgltf_component_type_r_8u: |
| 185 |
*permitType = filament::VertexBuffer::AttributeType::UBYTE2; |
| 186 |
*actualType = filament::VertexBuffer::AttributeType::UBYTE2; |
| 187 |
return true; |
| 188 |
case cgltf_component_type_r_16: |
| 189 |
*permitType = filament::VertexBuffer::AttributeType::SHORT2; |
| 190 |
*actualType = filament::VertexBuffer::AttributeType::SHORT2; |
| 191 |
return true; |
| 192 |
case cgltf_component_type_r_16u: |
| 193 |
*permitType = filament::VertexBuffer::AttributeType::USHORT2; |
| 194 |
*actualType = filament::VertexBuffer::AttributeType::USHORT2; |
| 195 |
return true; |
| 196 |
case cgltf_component_type_r_32f: |
| 197 |
*permitType = filament::VertexBuffer::AttributeType::FLOAT2; |
| 198 |
*actualType = filament::VertexBuffer::AttributeType::FLOAT2; |
| 199 |
return true; |
| 200 |
default: |
| 201 |
return false; |
| 202 |
} |
| 203 |
break; |
| 204 |
case cgltf_type_vec3: |
| 205 |
switch (ctype) { |
| 206 |
case cgltf_component_type_r_8: |
| 207 |
*permitType = filament::VertexBuffer::AttributeType::FLOAT3; |
| 208 |
*actualType = filament::VertexBuffer::AttributeType::BYTE3; |
| 209 |
return true; |
| 210 |
case cgltf_component_type_r_8u: |
| 211 |
*permitType = filament::VertexBuffer::AttributeType::FLOAT3; |
| 212 |
*actualType = filament::VertexBuffer::AttributeType::UBYTE3; |
| 213 |
return true; |
| 214 |
case cgltf_component_type_r_16: |
| 215 |
*permitType = filament::VertexBuffer::AttributeType::FLOAT3; |
| 216 |
*actualType = filament::VertexBuffer::AttributeType::SHORT3; |
| 217 |
return true; |
| 218 |
case cgltf_component_type_r_16u: |
| 219 |
*permitType = filament::VertexBuffer::AttributeType::FLOAT3; |
| 220 |
*actualType = filament::VertexBuffer::AttributeType::USHORT3; |
| 221 |
return true; |
| 222 |
case cgltf_component_type_r_32f: |
| 223 |
*permitType = filament::VertexBuffer::AttributeType::FLOAT3; |
| 224 |
*actualType = filament::VertexBuffer::AttributeType::FLOAT3; |
| 225 |
return true; |
| 226 |
default: |
| 227 |
return false; |
| 228 |
} |
| 229 |
break; |
| 230 |
case cgltf_type_vec4: |
| 231 |
switch (ctype) { |
| 232 |
case cgltf_component_type_r_8: |
| 233 |
*permitType = filament::VertexBuffer::AttributeType::BYTE4; |
| 234 |
*actualType = filament::VertexBuffer::AttributeType::BYTE4; |
| 235 |
return true; |
| 236 |
case cgltf_component_type_r_8u: |
| 237 |
*permitType = filament::VertexBuffer::AttributeType::UBYTE4; |
| 238 |
*actualType = filament::VertexBuffer::AttributeType::UBYTE4; |
| 239 |
return true; |
| 240 |
case cgltf_component_type_r_16: |
| 241 |
*permitType = filament::VertexBuffer::AttributeType::SHORT4; |
| 242 |
*actualType = filament::VertexBuffer::AttributeType::SHORT4; |
| 243 |
return true; |
| 244 |
case cgltf_component_type_r_16u: |
| 245 |
*permitType = filament::VertexBuffer::AttributeType::USHORT4; |
| 246 |
*actualType = filament::VertexBuffer::AttributeType::USHORT4; |
| 247 |
return true; |
| 248 |
case cgltf_component_type_r_32f: |
| 249 |
*permitType = filament::VertexBuffer::AttributeType::FLOAT4; |
| 250 |
*actualType = filament::VertexBuffer::AttributeType::FLOAT4; |
| 251 |
return true; |
| 252 |
default: |
| 253 |
return false; |
| 254 |
} |
| 255 |
break; |
| 256 |
default: |
| 257 |
return false; |
| 258 |
} |
| 259 |
return false; |
| 260 |
} |
| 261 |
|
| 262 |
inline bool requiresConversion(cgltf_type type, cgltf_component_type ctype) { |
| 263 |
filament::VertexBuffer::AttributeType permitted; |
| 264 |
filament::VertexBuffer::AttributeType actual; |
| 265 |
bool supported = getElementType(type, ctype, &permitted, &actual); |
| 266 |
return supported && permitted != actual; |
| 267 |
} |
| 268 |
|
| 269 |
#endif // GLTFIO_GLTFENUMS_H |