[go: up one dir, main page]

Menu

Example for polygons with color?

Feedback
2010-09-19
2013-04-23
  • Dov Grobgeld

    Dov Grobgeld - 2010-09-19

    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!

     
  • Marcus Geelnard

    Marcus Geelnard - 2010-09-20

    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).

     
  • Dov Grobgeld

    Dov Grobgeld - 2010-09-20

    Ok. No problem with duplicate vertices. But what is the command to attach an RGB color to a vertex?

     
  • Marcus Geelnard

    Marcus Geelnard - 2010-09-21

    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.

     
  • Dov Grobgeld

    Dov Grobgeld - 2010-10-12

    Here is my resulting cube program in Python. Feel free to add it as an example to the library:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    #!/usr/bin/python
    """
    An example of how to construct a colored ctm file.
    Dov Grobgeld <dov.grobgeld@gmail.com>
    Tuesday 2010-10-12 09:13
    This example is in the Public Domain.
    """
    from ctypes import *
    from openctm import *
    def make_blob(verts, T):
        """Convert a list of tuples of numbers into a ctypes pointer-to-array"""
        size = len(verts) * len(verts[0])
        Blob = T * size
        floats = [c for v in verts for c in v]
        blob = Blob(*floats)
        return cast(blob, POINTER(T))
    def cube():
        """Construct a cube with repeatitive coordinates for
        each face to support different colors."""
        f = -0.5
        verts = [ \
            (-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),
            ]
        return verts, faces
    verts, 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 vertex
    vertcolors = []
    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)]   # blue
    vertcolors = [c*4 for c in colors]
    pColors =make_blob(vertcolors, c_float)
    ctmAddAttribMap(ctm, pColors, "Color")
    ctmSave(ctm, "cube.ctm")
    # View the result
    import os
    os.system("ctmviewer cube.ctm")
    
     
  • Dov Grobgeld

    Dov Grobgeld - 2010-10-12

    I should have noted that I in my example is indepted to Philip Rideout at: http://prideout.net/blog/?p=44 !

     
  • Nobody/Anonymous

    a, 

     
  • Nobody/Anonymous

    d, 

     
  • Nobody/Anonymous

    e, 

     
  • Nobody/Anonymous

    d, 

     
  • Nobody/Anonymous

    b, 

     
  • Nobody/Anonymous

    c, 

     

Log in to post a comment.