2011-10-21 3 views
2

私は単純なレンダマネージャを実装しようとしています。別のクラスはクワッドを表す構造を提供し、これはレンダマネージャの頂点配列バッファにコピーされます。ポインタとOpenGLの描画に関する問題

しかし、コピーは動作していないようです。手動で構造体を作成して頂点配列にコピーすると、すべてがうまくレンダリングされます。しかし、コピーのどこかにすべてが間違っています。私はgdbを使ってこれを見ることができます(値は異なります)が、何が起こっているのか理解できません。

RenderManager方法

-(id)init 
{ 
    if([super init]){ 
     iva = calloc(MAX_QUADS, sizeof(quad)); 

     quad draw = { 
      0.0f,0.0f,0.0f,   0.0f,0.0f,1.0f, //Bottom left 
      1.0f,0.0f,0.0f,   0.0f,0.0f,1.0f, //Top Left 
      1.0f,1.0f,0.0f,   0.0f,0.0f,1.0f, //Top Right 
      0.0f,1.0f,0.0f,   0.0f,0.0f,1.0f, //Bottom Right 

      0.5f,0.5f,0.0f,   0.0f,0.0f,1.0f, //Center 
     }; 

私は何も描画しませんこれをコメントアウトした場合。それを使用し、私は質問に対して

 memcpy(iva, &draw, sizeof(draw)); 
     . . . OpenGl methods 

//Add the quad details to the render queue 
-(void)addDetailsToRenderQueue:(quad *)details 
{ 
    if(iva == NULL) 
     NSLog(@"Error claiming memory, something is very wrong"); 

    if(renderCount + 1 > MAX_QUADS){ 
     NSLog(@"Render buffer full, dumping..."); 
     [self render]; 
    } 

    memcpy((quad *)iva + renderCount++, &details, sizeof(quad)); 
} 

//Render the details in the render queue, for each quad we need to make two triangles. 
-(void)render 
{ 
    int counter = 0; 

    renderCount *= 5; 

    for(int i;i < renderCount;){ 
     ivaIndices[counter++] = i + 0;  // Bottom left 
     ivaIndices[counter++] = i + 1;  //Top Left 
     ivaIndices[counter++] = i + 4;  //Center 

     ivaIndices[counter++] = i + 1;  //Top Left 
     ivaIndices[counter++] = i + 2;  //Top right 
     ivaIndices[counter++] = i + 4;  //Center 

     ivaIndices[counter++] = i + 2;  //Top right 
     ivaIndices[counter++] = i + 3;  //Bottom right 
     ivaIndices[counter++] = i + 4;  //Center 

     ivaIndices[counter++] = i + 3;  //Bottom right 
     ivaIndices[counter++] = i + 0;  //Bottom Left 
     ivaIndices[counter++] = i + 4;  //Center 

     i += 5; 
    } 
    glBindVertexArrayOES(_vertexArray); 

    glDrawElements(GL_LINE_STRIP,12, GL_UNSIGNED_SHORT, ivaIndices); 

    // Reset the number of quads which need to be rendered 
    renderCount = 0; 
} 

タイルマネージャー法

@interface TileSet : NSObject 
{ 
    quad *tiles; 
    NSUInteger tileCount; 

    RenderManager *sharedRenderManager; 
} 

-(id)init 
{ 
    if([super init]){ 
     tiles = calloc(MAX_HEIGHT * MAX_WIDTH, sizeof(quad)); 
     tileCount = 0; 

     sharedRenderManager = [RenderManager sharedManager]; 
    } 
    [self fillArray]; 
    return self; 
} 

-(void)fillArray 
{ 
    int rows = 4; 
    int columns = 12; 

    for(int i = 0; i < rows;i += 2) { 
     for(int j = 0;j < columns;j += 2) { 
      quad tmp; 

      tmp.v1.x = i; 
      tmp.v1.y = j; 
      tmp.v1.z = 0.0f; 
      tmp.v1.nx = 0.0f; 
      tmp.v1.ny = 0.0f; 
      tmp.v1.nz = 1.0f; 

      tmp.v2.x = i + 1; 
      tmp.v2.y = j; 
      tmp.v2.z = 0.0f; 
      tmp.v2.nx = 0.0f; 
      tmp.v2.ny = 0.0f; 
      tmp.v2.nz = 1.0f; 

      tmp.v3.x = i; 
      tmp.v3.y = j+1; 
      tmp.v3.z = 0.0f; 
      tmp.v3.nx = 0.0f; 
      tmp.v3.ny = 0.0f; 
      tmp.v3.nz = 1.0f; 

      tmp.v4.x = i + 1; 
      tmp.v4.y = j+1; 
      tmp.v4.z = 0.0f; 
      tmp.v4.nx = 0.0f; 
      tmp.v4.ny = 0.0f; 
      tmp.v4.nz = 1.0f; 

      tmp.v5.x = i + 0.5; 
      tmp.v5.y = j + 0.5; 
      tmp.v5.z = 0.0f; 
      tmp.v5.nx = 0.0f; 
      tmp.v5.ny = 0.0f; 
      tmp.v5.nz = 1.0f; 

      memcpy((quad *)tiles + (i*j + j), &tmp, sizeof(quad)); 
      tileCount++; 
     } 
    } 
} 

//Draw the tiles in the tileset, loop through and add to renderManagers queue 
-(void)render 
{ 
for(int i = 0; i < tileCount;i++) 
    [sharedRenderManager addDetailsToRenderQueue:&tiles[0]]; 

} 

EDIT を期待してい素敵なクワッドを取得し、

NSLog(@"Tiles:%x\tDisgusting pointer math:%x",tiles,(quad *)tiles + (i*j + j)); 

を追加する与えます結果

2011-10-21 21:29:21.456 TerrainRendering[11293:fb03] Tiles:7316200 Disgusting pointer math:7316200 
2011-10-21 21:29:21.458 TerrainRendering[11293:fb03] Tiles:7316200 Disgusting pointer math:73162f0 
2011-10-21 21:29:21.463 TerrainRendering[11293:fb03] Tiles:7316200 Disgusting pointer math:73163e0 
2011-10-21 21:29:21.464 TerrainRendering[11293:fb03] Tiles:7316200 Disgusting pointer math:73164d0 
2011-10-21 21:29:21.465 TerrainRendering[11293:fb03] Tiles:7316200 Disgusting pointer math:73165c0 
2011-10-21 21:29:21.465 TerrainRendering[11293:fb03] Tiles:7316200 Disgusting pointer math:7316200 
2011-10-21 21:29:21.466 TerrainRendering[11293:fb03] Tiles:7316200 Disgusting pointer math:73164d0 
2011-10-21 21:29:21.467 TerrainRendering[11293:fb03] Tiles:7316200 Disgusting pointer math:73167a0 
2011-10-21 21:29:21.470 TerrainRendering[11293:fb03] Tiles:7316200 Disgusting pointer math:7316a70 
2011-10-21 21:29:21.470 TerrainRendering[11293:fb03] Tiles:7316200 Disgusting pointer math:7316d40 
+0

のNSLog( "%X%x" は@に、...) 'tiles'と'(クワッド*)タイル+(私はJ + jは*) 'のアドレスを助け、私は思います計算された住所はあなたが期待したものではありません。 –

+0

これで、期待したポインタ値が得られましたか?またはそれは何でしょうか? –

+0

正直、私は何を期待していたのか分かりません。しかし、それは正しいと思われる。 – botptr

答えて

0

memcpyのオフセットは(i*MAX_HEIGHT + j)である必要があります。あなたは "i"(外側のループ)で行をジャンプし、 "j"(内側のループ)でそれらを順番に調べたいと思っています。

希望これは

関連する問題