Re: [AD] Vertex Attribute Types

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


> I have no idea what this is about... if you want input from
> me you need to 
> describe in way more detail, best with including code
> examples of the different 
> alternatives.
> 
> (Just mentioning so there won't be complaints like with
> the fshooks addon.)
> 

Okay, so a vertex can have various attributes. For example, a vertex may keep track of the its position in 3D space, its colour, its position in texture space. Those attributes can be stored as different types (floats, ints etc) and in different numbers (texture coordinates can have 2 components, or up to 4). The question is whether/how to implement that.

I thought up of at 2 ways of doing it so far. Here they are with some code snippets:

1. Let users explicitly define vertex structs:

    typedef struct
    {
        float x, y, z;
        int u, v;
        ALLEGRO_PRIM_COLOR color;
    } MyVertex;

    ...

    /* Explain to the addon what the vertex contains
       and how it's laid out */
    ALLEGRO_VERTEX_DESCRIPTION vtx_type;
    /* zero memory somehow*/
    vtx_type.position_type = ALLEGRO_PRIM_3F;
    vtx_type.position_offset = offsetof(MyVertex, x);
    vtx_type.texture1_type = ALLEGRO_PRIM_2I;
    vtx_type.texture1_offset = offsetof(MyVertex, u);
    vtx_type.color1_type = ALLEGRO_PRIM_COLOR_T:
    vtx_type.color1_offset = offsetof(MyVertex, color);
    vtx_type.total_size = sizeof(MyVertex);

    /* Create a vertex buffer of 25 of the above vertices*/
    ALLEGRO_VBUFF* vbuff = al_create_vbuff(25, vtx_type);
    /* Fiddle with the vertices */
    MyVertex vtx;
    al_get_vertex(vbuff, 12, &vtx);
    vtx.x += 5;
    vtx.u += 2;
    al_set_vertex(vbuff, 12, &vtx);

Pros:
-Easy to use custom vertices, perhaps with lots of extraneous interleaved data.
-Easy for the user to alter various fields (just cast to your own type)
-Allows for interfacing with other storage schemes (e.g. store vertices in an std:vector)

Cons:
-Need to make sure you use ALLEGRO_PRIM_COLOR for both DirectX and OpenGL to work
-No consistent way to set vertex attributes (can be remedied in C++ via templates though, by the user)
-Does require the user to keep track of his own vertex types, and mess with the offsets


2. Don't let user define their own types:
    /* Explain to the addon what the vertex contains
       and how it's laid out */
    ALLEGRO_VERTEX_DESCRIPTION vtx_type;
    /* zero memory somehow*/
    vtx_type.position_type = ALLEGRO_PRIM_3F;
    vtx_type.texture1_type = ALLEGRO_PRIM_2I;
    vtx_type.color1_type = ALLEGRO_PRIM_COLOR_T:

    /* Create a vertex buffer of 25 of the above vertices*/
    ALLEGRO_VBUFF* vbuff = al_create_vbuff(25, vtx_type);
    /* Fiddle with the vertices */
    /* Takes in 4 doubles (or floats), converts <= 4 of them
       to the type used in the vertex */
    al_set_vbuff_pos(vbuff, 12, 3, 4, 5, 0);
    /* Maybe do this: al_set_vbuff_pos_{d, f, i, b}
       to deliniate by type */

Pros:
-No need to bother with creating own vertex structure
-Common attribute setting functions for any vertex type
-No need to know about the implementation of color storage

Cons:
-Can't be used with user made buffers of vertices
-Need a gajillion(about 15 + 15 * number of supported types) functions to get/set the attributes

The second method is the one used by the addon right now. If we keep the second method, then it'd be far simpler not to allow the user to use anything but 'float' for the type of the attribute. But, as StApostol said, there are valid uses for non-float types, so perhaps this is a bad idea. Perhaps switching to the first method is better as well.

Hopefully that is more clear.

-SiegeLord


> -- 
> Elias Pschernig <elias@xxxxxxxxxx>


      




Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/