0

私は OpenGL ES プログラミングが初めてです。開始するのに適したチュートリアルを見つけたところ、プロジェクトに四角形が表示されました。次に、ほとんどの OpenGL コードを View Controller から、頂点や色などを格納する Model オブジェクトに移動しようとしました。次に、View Controller で次のように呼び出します。

[model update];
[model load];

このようにして、表示するモデルが複数ある場合は、より実践的になります。これを行ったので、立方体が画面に表示されなくなりました。このメソッドをコメントアウトすると、キューブは表示されますが、画面いっぱいになるだけなので、すべてのモデル ビュー マトリックス計算が行われる update メソッドにエラーを特定したと思います。

-(void)update: (int)width And:(int)height{
float aspect = (width/height);
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(
                                      GLKMathDegreesToRadians(65.0f), aspect,
                                      4.0f, 10.0f);
self.effect.transform.projectionMatrix = projectionMatrix;

GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, 6.0f);   
rotation += 90;
modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix,
                                       GLKMathDegreesToRadians(rotation), 0, 0, 1);
self.effect.transform.modelviewMatrix = modelViewMatrix;
}  

そして、ここにロード方法があります:

- (void)load{
self.effect = [[GLKBaseEffect alloc] init];

NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithBool:YES],
                          GLKTextureLoaderOriginBottomLeft, 
                          nil];

NSError * error;    
NSString *path = [[NSBundle mainBundle] pathForResource:@"tile_floor" ofType:@"png"];
GLKTextureInfo * info = [GLKTextureLoader textureWithContentsOfFile:path options:options error:&error];
if (info == nil) {
    NSLog(@"Error loading file: %@", [error localizedDescription]);
}
self.effect.texture2d0.name = info.name;
self.effect.texture2d0.enabled = true;

glGenVertexArraysOES(1, &vertexArray);
glBindVertexArrayOES(vertexArray);

glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);

glEnableVertexAttribArray(GLKVertexAttribPosition);        
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Position));
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Color));
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, TexCoord));

glBindVertexArrayOES(0);
}

そして、ここにレンダリング方法があります:

- (void)render{
[self.effect prepareToDraw];
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);

glBindVertexArrayOES(vertexArray);
glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
}

どんな助けでも大歓迎です。ありがとう。

4

1 に答える 1

1

ここには実際には「エラー」はありません。問題は、モデルを適切に構成するために、投影とモデルビューのマトリックスを実際に設定していないことです。魔法の解決策はありません。描画しているモデルの頂点を考慮し (最大 100 ですか? 1000 ですか? ちょうど 0.5 ですか?)、GLKMatrix4MakePerspective の適切な設定を選択して、それらを画面に正しく配置する必要があります。

ショートカットとして (平行移動と回転なしで画面全体を埋めると言ったので)、これらの変換をスケール コマンドに置き換えて、オブジェクトのサイズを縮小することができます。

何かのようなもの:

GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.1f, 0.1f, 0.1f);   

一般的なアドバイスとして、各コマンドの効果を個別に検討してみてください。投影/変換の概念の紹介を探している場合は、次のリンクを見つけました: http://duriansoftware.com/joe/An-intro-to-modern-OpenGL.-Chapter-3:-3D-transformation-and -projection.htmlプロジェクションとワールド スペースというセクションまでスキップします。

于 2012-01-23T04:02:51.667 に答える