1

現在、配列と変数(配列内の変数の量を指定)を介して3Dモデルをロードできるクラスを作成しました。コードは私には問題ないように見えますが、画面には何も描画されません。

これが頂点構造体です。

typedef struct
{
    float pos[3];
    float texCoord[2];
    float colour[4];
} Vertex;

これは、モデルが初期化される方法です。

- (id)VModelWithArray:(Vertex[])Vertices count:(unsigned short)count
{
    self.Vertices = Vertices;
    self.count = count;
    return self;
}

2つのクラス変数は、このようにヘッダーで宣言されます。

@property (nonatomic, assign) Vertex *Vertices;
@property (nonatomic, assign) unsigned short count;

それは私のビューコントローラでそのように呼び出されます。

Vertex array[] = {
        {{1, -1, 0}, {1, 0}, {1, 0, 0, 1}},
        {{1, 1, 0}, {1, 1}, {0, 1, 0, 1}},
        {{-1, 1, 0}, {0, 1}, {1, 1, 1, 1}}
    };

    unsigned short elements = sizeof(array)/sizeof(Vertex);
    self.model = [[VModel alloc] VModelWithArray:array count:elements];

Modelクラスは、そのようにヘッダーで宣言されます。

@property (strong, nonatomic) VModel *model;

VBOはこの方法で作成されます。

- (void)CreateVBO
{
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*self.count, self.Vertices, GL_STATIC_DRAW);
}

このようにViewControllerでモデルが初期化された後に呼び出されます。

[self.model CreateVBO];

モデルはこのメソッドでレンダリングされます。

- (void)Render
{
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, pos));
    glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, texCoord));
    glEnableVertexAttribArray(GLKVertexAttribColor);
    glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, colour));
    glDrawArrays(GL_TRIANGLES, 0, self.count);
    glDisableVertexAttribArray(GLKVertexAttribPosition);
    glDisableVertexAttribArray(GLKVertexAttribTexCoord0);
    glDisableVertexAttribArray(GLKVertexAttribColor);
}

このメソッドで呼び出されます。

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect 
{
    glClearColor(0.5, 0.5, 0.5, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    [self.effect prepareToDraw];
    [self.model Render];
}

どんな助けでも大歓迎です。

4

1 に答える 1

1

私はそれを理解しました、前のプロジェクトからのコードをマージするときに誤ってこれらの行を省略しました。

[EAGLContext setCurrentContext:self.context];
self.effect = [[GLKBaseEffect alloc] init];
于 2013-01-08T13:57:47.230 に答える