iOS / GL bug,问题生成索引数组

时间:2011-03-17 06:54:24

标签: ios opengl-es indexing buffer

这让我很烦恼。

我重构了一些丑陋的代码,生成了几个索引数组。据我所知,一切都应该是一样的。我在两种情况下都记录了数组的元素;生成相同大小的数组,并使用相同的数据填充。正在创建相同的缓冲区ID ......但旧代码有效,新代码失败。

请有人帮忙吗?

- (void) setupIndexArray_
{
    NSLog(@"OLD (works)");

    // shape with centerpoint (4 triangles)
    #define INDICES_FOR_SHAPE 10
    // fill out indices for the first button
    static GLushort shapeIndices[INDICES_FOR_SHAPE * NUM_QUADS] = {2, 2, 0, 1, 4,   4, 0, 3, 2, 2};   
    indexCount_background = INDICES_FOR_SHAPE * NUM_QUADS;

    // make the remaining 11 buttons the same pattern
    for (int j=INDICES_FOR_SHAPE; j < indexCount_background; j++)
        shapeIndices[j] = shapeIndices[j-INDICES_FOR_SHAPE] + VERTICES_FOR_BUTTON;

    glGenBuffers( 1, & indexBufID_btnBg );
    glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, indexBufID_btnBg ); 
    glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof( shapeIndices ), shapeIndices, GL_STATIC_DRAW );  

    NSLog(@"BufID: %d", (int) indexBufID_btnBg);
    NSLog(@"writing %d bytes... ", (int) sizeof( shapeIndices ));
    for (int j=0; j < sizeof( shapeIndices ) / sizeof( GLushort ); j++)
        printf( "%d,", shapeIndices[j] );

    printf("\n");

    //for (int j=0; j < indexCount_background; j++)
    //    printf("%d,",shapeIndices[j]);

    // - - - 

    // text rect (2 triangles)
    #define INDICES_FOR_TEXTRECT  6
    GLushort textIndices[INDICES_FOR_TEXTRECT * NUM_QUADS] = {5, 5, 8,   6, 7, 7};  
    indexCount_text = INDICES_FOR_TEXTRECT * NUM_QUADS;
    // glGenBuffers( 1, & indexBufID_btnText );

    // make the remaining 11 buttons the same pattern
    for (int j=INDICES_FOR_TEXTRECT; j < indexCount_text; j++)
        textIndices[j] = textIndices[j-INDICES_FOR_TEXTRECT] + VERTICES_FOR_BUTTON;

    glGenBuffers( 1, & indexBufID_btnText );
    glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, indexBufID_btnText ); 
    glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof( textIndices ), textIndices, GL_STATIC_DRAW );  

    NSLog(@"BufID: %d", (int) indexBufID_btnText);
    NSLog(@"writing %d bytes... ", (int) sizeof( textIndices ));
    for (int j=0; j < sizeof( textIndices ) / sizeof( GLushort ); j++)
        printf( "%d,", textIndices[j] );

}


// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

// make triangle-shapes for each button-background-shape 
// and text-quad from vertex-array
- (void) setupIndexArray
{
    NSLog(@"NEW (fails)");

    // SHAPE
    static GLushort shapeIndexPattern [] = {2, 2, 0, 1, 4,   4, 0, 3, 2, 2};   

    [self createIABufForPattern: shapeIndexPattern
                          bytes: sizeof(shapeIndexPattern)
                vertexArrayStep: VERTICES_FOR_BUTTON
                   makingClones: 12
                 returningBufID: & indexBufID_btnBg ];


    // TEXT
    static GLushort textIndexPattern [] = {5, 5, 8,   6, 7, 7};  

    [self createIABufForPattern: textIndexPattern
                          bytes: sizeof(textIndexPattern)
                vertexArrayStep: VERTICES_FOR_BUTTON
                   makingClones: 12
                 returningBufID: & indexBufID_btnText ];
}

- (void) createIABufForPattern: (GLushort *) pattern
    bytes: (size_t) bytesPattern
    vertexArrayStep: (int) STEP
    makingClones: (int) copies
    returningBufID: (GLuint *) pBufID
{

    size_t eltsPattern = bytesPattern / sizeof( GLushort );

    size_t eltsTotal = eltsPattern * copies;
    size_t bytesTotal = bytesPattern * copies;

    GLushort * array = (GLushort *) malloc( bytesTotal );

    // copy pattern into big array
    memcpy( (void *) array, 
           ( const void *) pattern, 
           bytesPattern );

    // make the remaining items follow the same pattern
    for (int j = eltsPattern; j < eltsTotal; j++)
        array[j] = array[ j - eltsPattern ] + STEP;


    GLuint bufID;
    glGenBuffers( 1, & bufID );
    glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, bufID ); 
    glBufferData( GL_ELEMENT_ARRAY_BUFFER, bytesTotal, array, GL_STATIC_DRAW ); 

    NSLog(@"BufID: %d", (int) bufID);
    NSLog(@"writing %d bytes... ", (int) bytesTotal);
    for (int j=0; j < eltsTotal; j++)
        printf( "%d,", array[j] );
    printf("\n");

    free (array);

    * pBufID = bufID;
}

这是输出(相同):

2011-03-17 14:53:10.010 glWheel1[321:207] OLD (works)
2011-03-17 14:53:10.016 glWheel1[321:207] BufID: 2
2011-03-17 14:53:10.018 glWheel1[321:207] writing 240 bytes... 
2,2,0,1,4,4,0,3,2,2,11,11,9,10,13,13,9,12,11,11,20,20,18,19,22,22,18,21,20,20,29,29,27,28,31,31,27,30,29,29,38,38,36,37,40,40,36,39,38,38,47,47,45,46,49,49,45,48,47,47,56,56,54,55,58,58,54,57,56,56,65,65,63,64,67,67,63,66,65,65,74,74,72,73,76,76,72,75,74,74,83,83,81,82,85,85,81,84,83,83,92,92,90,91,94,94,90,93,92,92,101,101,99,100,103,103,99,102,101,101,
2011-03-17 14:53:10.022 glWheel1[321:207] BufID: 3
2011-03-17 14:53:10.023 glWheel1[321:207] writing 144 bytes... 
5,5,8,6,7,7,14,14,17,15,16,16,23,23,26,24,25,25,32,32,35,33,34,34,41,41,44,42,43,43,50,50,53,51,52,52,59,59,62,60,61,61,68,68,71,69,70,70,77,77,80,78,79,79,86,86,89,87,88,88,95,95,98,96,97,97,104,104,107,105,106,106,



2011-03-17 14:54:11.015 glWheel1[365:207] NEW (fails)
2011-03-17 14:54:11.018 glWheel1[365:207] BufID: 2
2011-03-17 14:54:11.020 glWheel1[365:207] writing 240 bytes... 
2,2,0,1,4,4,0,3,2,2,11,11,9,10,13,13,9,12,11,11,20,20,18,19,22,22,18,21,20,20,29,29,27,28,31,31,27,30,29,29,38,38,36,37,40,40,36,39,38,38,47,47,45,46,49,49,45,48,47,47,56,56,54,55,58,58,54,57,56,56,65,65,63,64,67,67,63,66,65,65,74,74,72,73,76,76,72,75,74,74,83,83,81,82,85,85,81,84,83,83,92,92,90,91,94,94,90,93,92,92,101,101,99,100,103,103,99,102,101,101,
2011-03-17 14:54:11.024 glWheel1[365:207] BufID: 3
2011-03-17 14:54:11.025 glWheel1[365:207] writing 144 bytes... 
5,5,8,6,7,7,14,14,17,15,16,16,23,23,26,24,25,25,32,32,35,33,34,34,41,41,44,42,43,43,50,50,53,51,52,52,59,59,62,60,61,61,68,68,71,69,70,70,77,77,80,78,79,79,86,86,89,87,88,88,95,95,98,96,97,97,104,104,107,105,106,106,

1 个答案:

答案 0 :(得分:0)

indexCount_background = INDICES_FOR_SHAPE * NUM_QUADS;

我的修改后的代码中缺少这个...所以当实际绘图时,GL认为它必须呈现零点

我投票支持关闭,因为我怀疑这些代码对任何人都有用,并且互联网上有足够的混乱。

抱歉浪费时间&amp;焦点:|