00001
00002 GLfloat cluModelWidth(CLmodel* model)
00003 {
00004 GLuint i;
00005 GLuint j;
00006 CLmesh* mesh;
00007 GLfloat min_x;
00008 GLfloat max_x;
00009
00010 min_x = MAXFLOAT;
00011 max_x = -MAXFLOAT;
00012
00013 for (i = 0 ; i < model->num_meshes; i++)
00014 {
00015 mesh = model->context->meshes[model->mesh_indices[i]];
00016
00017 for (j = 0 ; j < mesh->num_vertices ; j++)
00018 {
00019 max_x = CL_MAX(max_x, mesh->vertices[j].x);
00020 min_x = CL_MIN(min_x, mesh->vertices[j].x);
00021 }
00022 }
00023
00024 return max_x - min_x;
00025 }
00026
00027 GLfloat cluModelHeight(CLmodel* model)
00028 {
00029 GLuint i;
00030 GLuint j;
00031 CLmesh* mesh;
00032 GLfloat min_y;
00033 GLfloat max_y;
00034
00035 min_y = MAXFLOAT;
00036 max_y = -MAXFLOAT;
00037
00038 for (i = 0 ; i < model->num_meshes; i++)
00039 {
00040 mesh = model->context->meshes[model->mesh_indices[i]];
00041
00042 for (j = 0 ; j < mesh->num_vertices ; j++)
00043 {
00044 max_y = CL_MAX(max_y, mesh->vertices[j].y);
00045 min_y = CL_MIN(min_y, mesh->vertices[j].y);
00046 }
00047 }
00048
00049 return max_y - min_y;
00050 }
00051
00052 GLfloat cluModelDepth(CLmodel* model)
00053 {
00054 GLuint i;
00055 GLuint j;
00056 CLmesh* mesh;
00057 GLfloat min_z;
00058 GLfloat max_z;
00059
00060 min_z = MAXFLOAT;
00061 max_z = -MAXFLOAT;
00062
00063 for (i = 0 ; i < model->num_meshes; i++)
00064 {
00065 mesh = model->context->meshes[model->mesh_indices[i]];
00066
00067 for (j = 0 ; j < mesh->num_vertices ; j++)
00068 {
00069 max_z = CL_MAX(max_z, mesh->vertices[j].z);
00070 min_z = CL_MIN(min_z, mesh->vertices[j].z);
00071 }
00072 }
00073
00074 return max_z - min_z;
00075 }
00076
00077 CLvertex* cluModelLocalOrigin(CLvertex* v, CLmodel* model)
00078 {
00079 GLuint i;
00080 GLuint j;
00081 CLmesh* mesh;
00082 GLfloat min_x;
00083 GLfloat max_x;
00084 GLfloat min_y;
00085 GLfloat max_y;
00086 GLfloat min_z;
00087 GLfloat max_z;
00088
00089 min_x = MAXFLOAT;
00090 max_x = -MAXFLOAT;
00091 min_y = MAXFLOAT;
00092 max_y = -MAXFLOAT;
00093 min_z = MAXFLOAT;
00094 max_z = -MAXFLOAT;
00095
00096 for (i = 0 ; i < model->num_meshes; i++)
00097 {
00098 mesh = model->context->meshes[model->mesh_indices[i]];
00099
00100 for (j = 0; j < mesh->num_vertices; j++)
00101 {
00102 min_x = CL_MIN(min_x, mesh->vertices[j].x);
00103 max_x = CL_MAX(max_x, mesh->vertices[j].x);
00104 min_y = CL_MIN(min_y, mesh->vertices[j].y);
00105 max_y = CL_MAX(max_y, mesh->vertices[j].y);
00106 min_z = CL_MIN(min_z, mesh->vertices[j].z);
00107 max_z = CL_MAX(max_z, mesh->vertices[j].z);
00108 }
00109 }
00110
00111 v->x = min_x + (max_x - min_x) / 2.0f;
00112 v->y = min_y + (max_y - min_y) / 2.0f;
00113 v->z = min_z + (max_z - min_z) / 2.0f;
00114
00115 return v;
00116 }
00117
00118 CLmodel* cluModelCentre(CLmodel* model)
00119 {
00120 GLuint i;
00121 GLuint j;
00122 CLmesh* mesh;
00123 CLnormal n;
00124
00125 cluModelLocalOrigin((CLvertex*)&n, model);
00126
00127 for (i = 0 ; i < model->num_meshes; i++)
00128 {
00129 mesh = model->context->meshes[model->mesh_indices[i]];
00130
00131 for (j = 0; j < mesh->num_vertices; j++)
00132 cluVertexSubtract(mesh->vertices + j, &n);
00133 }
00134
00135 return model;
00136 }
00137
00138 CLmodel* cluModelScale(CLmodel* model, GLfloat scale)
00139 {
00140 return cluModelResize(model, scale, scale, scale);
00141 }
00142
00143 CLmodel* cluModelScaleUnitCube(CLmodel* model)
00144 {
00145 return cluModelScale(model,
00146 1.0f / (CL_MAX(cluModelWidth(model),
00147 CL_MAX(cluModelHeight(model),
00148 cluModelDepth(model)))));
00149 }
00150
00151 CLmodel* cluModelResize(CLmodel* model, GLfloat sx, GLfloat sy, GLfloat sz)
00152 {
00153 GLuint i;
00154 GLuint j;
00155
00156 for (i = 0 ; i < model->num_meshes; i++)
00157 cluMeshResize(model->context->meshes[model->mesh_indices[i]], sx, sy, sz);
00158
00159 return model;
00160 }