00001 00007 #include "Object.h" 00008 00009 float getValue(const char *src, const unsigned int index, const AttributeFormat attFormat){ 00010 switch (attFormat){ 00011 case ATT_FLOAT: return *(((float *) src) + index); 00012 case ATT_UNSIGNED_BYTE: return *(((unsigned char *) src) + index) * (1.0f / 255.0f); 00013 default: 00014 return 0; 00015 } 00016 } 00017 00018 void setValue(const char *dest, const unsigned int index, const AttributeFormat attFormat, float value){ 00019 switch (attFormat){ 00020 case ATT_FLOAT: 00021 *(((float *) dest) + index) = value; 00022 break; 00023 case ATT_UNSIGNED_BYTE: 00024 *(((unsigned char *) dest) + index) = (unsigned char) (value * 255.0f); 00025 break; 00026 } 00027 } 00028 00029 Object::Object() { 00030 vertices = NULL; 00031 indices = NULL; 00032 clear(); 00033 } 00034 00035 Object::~Object() { 00036 clear(); 00037 LOG_SUCCESS(("Object unloaded")); 00038 } 00039 00040 void Object::clear() { 00041 if (vertices != NULL) delete vertices; 00042 if (indices != NULL) delete indices; 00043 vertices = NULL; 00044 indices = NULL; 00045 nVertices = 0; 00046 nIndices = 0; 00047 vertexSize = 0; 00048 indiceSize = 0; 00049 } 00050 00051 void Object::setPrimitive(const Primitive prim) { 00052 primitive = prim; 00053 } 00054 00055 void Object::setVertices(void *vertexArray, const unsigned int numVertices, const unsigned int size) { 00056 vertices = (char *)vertexArray; 00057 nVertices = numVertices; 00058 vertexSize = size; 00059 } 00060 00061 void Object::setIndices(void *indiceArray, const unsigned int numIndices, const unsigned int size) { 00062 indices = (char *)indiceArray; 00063 nIndices = numIndices; 00064 indiceSize = size; 00065 } 00066 00067 void Object::addFormat(const AttributeType attType, const AttributeFormat attFormat, const unsigned int size, const unsigned int offset) { 00068 Format format; 00069 format.attType = attType; 00070 format.attFormat = attFormat; 00071 format.size = size; 00072 format.offset = offset; 00073 00074 formats.add(format); 00075 } 00076 00077 bool Object::findAttribute(const AttributeType attType, unsigned int *where) const { 00078 for (unsigned int i=0; i < formats.getCount(); i++) { 00079 if (formats[i].attType == attType) { 00080 if (where != NULL) *where = i; 00081 return true; 00082 } 00083 } 00084 return false; 00085 } 00086 00087 bool Object::flipNormals() { 00088 unsigned int normalOffset; 00089 00090 if (!findAttribute(ATT_NORMAL, &normalOffset)) return false; 00091 00092 AttributeFormat normalFormat = formats[normalOffset].attFormat; 00093 normalOffset = formats[normalOffset].offset; 00094 00095 for (unsigned int i=0; i<nVertices; i++) { 00096 char *src = vertices + i*vertexSize + normalOffset; 00097 setValue(src, 0, normalFormat, -getValue(src, 0, normalFormat)); 00098 setValue(src, 1, normalFormat, -getValue(src, 1, normalFormat)); 00099 setValue(src, 2, normalFormat, -getValue(src, 2, normalFormat)); 00100 } 00101 00102 return true; 00103 }