clmesh.c

00001 #include <cl.h>
00002 
00003 /* returns -1 on fail, input cannot be NULL
00004 GLuint clMeshPrimitiveSetIndex(const CLmesh* mesh, 
00005                                const CLprimitiveset* primitiveset)
00006 {
00007   GLuint i;
00008   
00009   for (i = 0; i < mesh->num_primitivesets; i++)
00010     if (mesh->primitivesets[i] == primitiveset)
00011       return i;
00012   
00013   return -1;
00014 }
00015 */
00016 
00017 /* this is a VERY conservative estimate, should fix to be less conservative */
00018 GLboolean clMeshOpaque(const CLmesh* mesh)
00019 {
00020   GLboolean temp;
00021   
00022   temp = GL_TRUE;
00023   
00024   if (clIsEnabled(CL_TEXTURE) && (mesh->texture_index != -1))
00025     {
00026       temp = GL_TRUE;
00027     }
00028   else if (clIsEnabled(CL_MATERIAL) && (mesh->material_index != -1))
00029     {
00030       if (mesh->context->materials[mesh->material_index]->diffuse.a != 1.0f)
00031         temp = GL_FALSE;
00032     }
00033   else if (clIsEnabled(CL_COLOUR) && (mesh->colour))
00034     {
00035       if (mesh->colour->a != 1.0f)
00036         temp = GL_FALSE;
00037     }
00038 
00039   return temp;
00040 }
00041 
00042 /* this is a VERY conservative estimate, should fix to be less conservative */
00043 GLboolean clMeshTranslucent(const CLmesh* mesh)
00044 {
00045   GLboolean temp;
00046 
00047   temp = GL_FALSE;
00048   
00049   if (clIsEnabled(CL_TEXTURE) && (mesh->texture_index != -1))
00050     {
00051       temp = GL_TRUE;
00052     }
00053   else if (clIsEnabled(CL_MATERIAL) && (mesh->material_index != -1))
00054     {
00055       if (mesh->context->materials[mesh->material_index]->diffuse.a != 1.0f)
00056         temp = GL_TRUE;
00057     }
00058   else if (clIsEnabled(CL_COLOUR) && (mesh->colour))
00059     {
00060       if (mesh->colour->a != 1.0f)
00061         temp = GL_TRUE;
00062     }
00063 
00064   return temp;
00065 }
00066 
00067 /* 
00068    add: returns new index or -1 on fail        
00069 */
00070 
00071 /* add a vertex to a mesh vertex cannot be null, other properties can
00072    be undefined if mesh or vertex is null */
00073 GLuint clMeshAddVertex(CLmesh* mesh, 
00074                        const CLvertex* vertex, 
00075                        const CLcolour* colour, 
00076                        const CLnormal* normal, 
00077                        const CLtexcoord* texcoord, 
00078                        const CLedgeflag* edgeflag)
00079 {
00080   GLuint ni; /* new index */
00081   
00082   ni = mesh->num_vertices;
00083 
00084   mesh->num_vertices++;
00085   
00086   mesh->vertices = (CLvertex*)realloc(mesh->vertices,
00087                                       mesh->num_vertices * sizeof(CLvertex));
00088   clCopyVertex(&mesh->vertices[ni], vertex);
00089   
00090   if (mesh->num_vertices == 1)
00091     {
00092       if (colour)
00093         {
00094           mesh->colours = (CLcolour*)realloc(mesh->colours, mesh->num_vertices 
00095                                              * sizeof(CLcolour));
00096           clCopyColour(&mesh->colours[0], colour);
00097         }
00098 
00099       if (normal)
00100         {
00101           mesh->normals = (CLnormal*)realloc(mesh->normals, mesh->num_vertices 
00102                                              * sizeof(CLnormal));
00103           clCopyNormal(&mesh->normals[0], normal);
00104         }
00105 
00106       if (texcoord)
00107         {
00108           mesh->texcoords = (CLtexcoord*)realloc(mesh->texcoords, 
00109                                                  mesh->num_vertices 
00110                                                  * sizeof(CLtexcoord));
00111           clCopyTexCoord(&mesh->texcoords[0], texcoord);
00112         }
00113 
00114       if (edgeflag)
00115         {
00116           mesh->edgeflags = (CLedgeflag*)realloc(mesh->edgeflags, 
00117                                                  mesh->num_vertices 
00118                                                  * sizeof(CLedgeflag));
00119           clCopyEdgeFlag(&mesh->edgeflags[0], edgeflag);
00120         }
00121     }
00122   else
00123     {
00124       if (mesh->colours)
00125         {
00126           mesh->colours = (CLcolour*)realloc(mesh->colours, mesh->num_vertices 
00127                                              * sizeof(CLcolour));
00128           if (colour)
00129             clCopyColour(&mesh->colours[ni], colour);
00130           else
00131             clDefaultColour(&mesh->colours[ni]);
00132         }
00133 
00134       if (mesh->normals)
00135         {
00136           mesh->normals = (CLnormal*)realloc(mesh->normals, mesh->num_vertices 
00137                                              * sizeof(CLnormal));
00138           if (normal)
00139             clCopyNormal(&mesh->normals[ni], normal);
00140           else
00141             clDefaultNormal(&mesh->normals[ni]);
00142         }
00143 
00144       if (mesh->texcoords)
00145         {
00146           mesh->texcoords = (CLtexcoord*)realloc(mesh->texcoords,
00147                                                  mesh->num_vertices
00148                                                  * sizeof(CLtexcoord));
00149           if (texcoord)
00150             clCopyTexCoord(&mesh->texcoords[ni], texcoord);
00151           else
00152             clDefaultTexCoord(&mesh->texcoords[ni]);
00153         }
00154 
00155       if (mesh->edgeflags)
00156         {
00157           mesh->edgeflags = (CLedgeflag*)realloc(mesh->edgeflags,
00158                                                  mesh->num_vertices
00159                                                  * sizeof(CLedgeflag));
00160           if (edgeflag)
00161             clCopyEdgeFlag(&mesh->edgeflags[ni], edgeflag);
00162           else
00163             clDefaultEdgeFlag(&mesh->edgeflags[ni]);
00164         }
00165     }
00166 
00167   /* FIX THIS!!! what's wrong? anyone?*/
00168   return ni;
00169 }
00170 
00171 unsigned int clMeshAddPrimitiveSet(CLmesh* mesh, CLprimitiveset* primitiveset)
00172 {
00173   GLuint i;
00174   
00175   if (!(mesh && primitiveset))
00176     return -1;
00177   
00178   for (i = 0; i < mesh->num_primitivesets; i++)
00179     if (mesh->primitivesets[i] == primitiveset)
00180       return i;
00181   
00182   mesh->num_primitivesets++;
00183   mesh->primitivesets = (CLprimitiveset**)realloc(mesh->primitivesets, 
00184                                                   mesh->num_primitivesets 
00185                                                   * sizeof(CLprimitiveset*));
00186   mesh->primitivesets[mesh->num_primitivesets - 1] = primitiveset;
00187 
00188   return mesh->num_primitivesets - 1;
00189 }
00190 
00191 /* LOOK AT ME!!!!! (remove)
00192 
00193  - make sure to update things that may reference by index
00194  . eg. remove vertex 10, must update primitive indices if > 10
00195 
00196    NO!!! that is up to the coder
00197 */
00198 
00199 GLboolean clMeshRemoveVertex(CLmesh* mesh, GLuint index)
00200 {
00201   GLuint i;
00202   
00203   for (i = index; i < mesh->num_vertices; i++)
00204     clCopyVertex(&mesh->vertices[i], &mesh->vertices[i+1]);
00205   
00206   if (mesh->colours)
00207     for (i = index; i < mesh->num_vertices; i++)
00208       clCopyColour(&mesh->colours[i], &mesh->colours[i+1]);
00209   
00210   if (mesh->normals)
00211     for (i = index; i < mesh->num_vertices; i++)
00212       clCopyNormal(&mesh->normals[i], &mesh->normals[i+1]);
00213   
00214   if (mesh->texcoords)
00215     for (i = index; i < mesh->num_vertices; i++)
00216       clCopyTexCoord(&mesh->texcoords[i], &mesh->texcoords[i+1]);
00217 
00218   if (mesh->edgeflags)
00219     for (i = index; i < mesh->num_vertices; i++)
00220       mesh->edgeflags[i] = mesh->edgeflags[i+1];
00221   
00222   mesh->num_vertices--;
00223   
00224   mesh->vertices = (CLvertex*)realloc(mesh->vertices, mesh->num_vertices
00225                                       * sizeof(CLvertex));
00226   
00227   if (mesh->colours)
00228     mesh->colours = (CLcolour*)realloc(mesh->colours, mesh->num_vertices
00229                                        * sizeof(CLcolour));
00230   
00231   if (mesh->normals)
00232     mesh->normals = (CLnormal*)realloc(mesh->normals, mesh->num_vertices 
00233                                        * sizeof(CLnormal));
00234   
00235   if (mesh->texcoords)
00236     mesh->texcoords = (CLtexcoord*)realloc(mesh->texcoords, mesh->num_vertices 
00237                                            * sizeof(CLtexcoord));
00238   
00239   if (mesh->edgeflags)
00240     mesh->edgeflags = (CLedgeflag*)realloc(mesh->edgeflags, mesh->num_vertices 
00241                                            * sizeof(CLedgeflag));
00242   
00243   return GL_TRUE;
00244 }
00245 
00246 GLboolean clMeshRemovePrimitiveSet(CLmesh* mesh, GLuint index)
00247 {
00248   GLuint i;
00249   
00250   for (i = index; i < mesh->num_primitivesets; i++)
00251     mesh->primitivesets[i] = mesh->primitivesets[i+1];
00252   
00253   mesh->num_primitivesets--;
00254   
00255   mesh->primitivesets = (CLprimitiveset**)realloc(mesh->primitivesets,
00256                                                   mesh->num_primitivesets 
00257                                                   * sizeof(CLprimitiveset*));
00258   
00259   return GL_TRUE;
00260 }

Generated on Thu Dec 27 13:53:41 2007 for CL by  doxygen 1.4.6