I need to create faces with different colors. Is there an example showing how to generate a ctm file that contains different colors. E.g. a cube with 6 different colors for its faces?
Thanks!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
There's no example readily available, but here's the trick:
Since all vertex attributes go with each vertex (you can not separate vertex colors from vertex coordinates), you need (in the case of a cube) to create three vertices for each corner of the cube (3 * 8 vertices in total). That way each corner can have three colors (one for each side that it's part of). That way, you can also set three different normals for the corner - if you need to use them.
The reason for this is that OpenCTM is designed to work well with e.g. OpenGL, in which a "vertex" is always a complete set of vertex attributes (including color and normal). Higher level 3D structures (e.g. the one found in Blender and other modeling tools) typically do not have a 1:1 mapping to the OpenCTM data strucutre, since it is common to separate vertex attributes from the points (vertex coordinates).
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Vertex colors are not natively supported. However, the current convention is to create a custom vertex attribute with the name "Color", ctmAddAttribMap(context, myColorArray, "Color"), and let the four attribute elements map as follows:
a = Red (0.0 - 1.0)
b = Green (0.0 - 1.0)
c = Blue (0.0 - 1.0)
d = Alpha (0.0 - 1.0)
ctmviewer and ctmconv will understand that and use it as vertex colors.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
#!/usr/bin/python"""An example of how to construct a colored ctm file.Dov Grobgeld <dov.grobgeld@gmail.com>Tuesday 2010-10-12 09:13This example is in the Public Domain."""fromctypesimport*fromopenctmimport*defmake_blob(verts,T):"""Convert a list of tuples of numbers into a ctypes pointer-to-array"""size=len(verts)*len(verts[0])Blob=T*sizefloats=[cforvinvertsforcinv]blob=Blob(*floats)returncast(blob,POINTER(T))defcube():"""Construct a cube with repeatitive coordinates for each face to support different colors."""f=-0.5verts=[ \
(-f,-f,-f),(-f,f,-f),(f,f,-f),(f,-f,-f),(-f,-f,f),(-f,f,f),(f,f,f),(f,-f,f),(f,-f,-f),(f,-f,f),(f,f,f),(f,f,-f),(-f,-f,-f),(-f,-f,f),(-f,f,f),(-f,f,-f),(-f,f,-f),(-f,f,f),(f,f,f),(f,f,-f),(-f,-f,-f),(-f,-f,f),(f,-f,f),(f,-f,-f),]faces=[ \
(0,1,2),(0,2,3),(4,5,6),(4,6,7),(8,9,10),(8,10,11),(12,13,14),(12,14,15),(16,17,18),(16,18,19),(20,21,22),(20,22,23),]returnverts,facesverts,faces=cube()pVerts=make_blob(verts,c_float)pFaces=make_blob(faces,c_uint)pNormals=POINTER(c_float)()ctm=ctmNewContext(CTM_EXPORT)ctmDefineMesh(ctm,pVerts,len(verts),pFaces,len(faces),pNormals)# This adds color to the cube# There should be one 4-float entry for each vertexvertcolors=[]colors=[(1,1,1,1),# white(1,1,0,1),# yellow(1,0.5,0,1),# orange(0,1,0,1),# red(1,0,0,1),# red(0,0,1,1)]# bluevertcolors=[c*4forcincolors]pColors=make_blob(vertcolors,c_float)ctmAddAttribMap(ctm,pColors,"Color")ctmSave(ctm,"cube.ctm")# View the resultimportosos.system("ctmviewer cube.ctm")
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I need to create faces with different colors. Is there an example showing how to generate a ctm file that contains different colors. E.g. a cube with 6 different colors for its faces?
Thanks!
There's no example readily available, but here's the trick:
Since all vertex attributes go with each vertex (you can not separate vertex colors from vertex coordinates), you need (in the case of a cube) to create three vertices for each corner of the cube (3 * 8 vertices in total). That way each corner can have three colors (one for each side that it's part of). That way, you can also set three different normals for the corner - if you need to use them.
The reason for this is that OpenCTM is designed to work well with e.g. OpenGL, in which a "vertex" is always a complete set of vertex attributes (including color and normal). Higher level 3D structures (e.g. the one found in Blender and other modeling tools) typically do not have a 1:1 mapping to the OpenCTM data strucutre, since it is common to separate vertex attributes from the points (vertex coordinates).
Ok. No problem with duplicate vertices. But what is the command to attach an RGB color to a vertex?
Ok…
Vertex colors are not natively supported. However, the current convention is to create a custom vertex attribute with the name "Color", ctmAddAttribMap(context, myColorArray, "Color"), and let the four attribute elements map as follows:
a = Red (0.0 - 1.0)
b = Green (0.0 - 1.0)
c = Blue (0.0 - 1.0)
d = Alpha (0.0 - 1.0)
ctmviewer and ctmconv will understand that and use it as vertex colors.
Here is my resulting cube program in Python. Feel free to add it as an example to the library:
I should have noted that I in my example is indepted to Philip Rideout at: http://prideout.net/blog/?p=44 !
a,
d,
e,
d,
b,
c,