2012-03-16 24 views
2

最後に、私はiPhoneの単純なゲームapp.IのためにXCode 4.2のOpenGL ESフレームワークを使って線を描こうとしました。私は、GLKViewとGLKViewControllerについてiPhoneで線を描画する方法について学びました。私は画面に登場するだけでグレー色は、ラインが表示されていないプロジェクトを実行すると、ここに私は私のプロジェクトで試みた私のサンプルコード、iPhoneでOpenGL ESを使用して直線を描画しますか?

@synthesize context = _context; 
@synthesize effect = _effect; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; 

    if (!self.context) { 
     NSLog(@"Failed to create ES context"); 
    } 


    GLKView *view = (GLKView *)self.view; 
    view.context = self.context; 
    view.drawableDepthFormat = GLKViewDrawableDepthFormat24; 
    [EAGLContext setCurrentContext:self.context]; 

    //[self setupGL]; 
} 

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect 
{ 
    NSLog(@"DrawInRect Method"); 

    [EAGLContext setCurrentContext:self.context]; 

    // This method is calling multiple times.... 

    glClearColor(0.65f, 0.65f, 0.65f, 1.0f); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 


    const GLfloat line[] = 
    { 
     -0.5f, -0.5f, //point A 
     0.5f, -0.5f, //point B 
    }; 
    glVertexPointer(2, GL_FLOAT, 0, line); 
    glEnableClientState(GL_VERTEX_ARRAY); 
    glDrawArrays(GL_LINES, 0, 2); 
} 

です。また、- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect代理人は無限の時間を呼び出しています。私が間違っているところで私を案内してください。なぜ線が現れていないか描いていないのですか?あなたは助けてもらえますか?これを2日間試しています。前もって感謝します。

答えて

5

私はむしろ自分で今はOpenGL ES 2.0の学生です。 Appleが提供する "OpenGL Game"テンプレートを使用して、Xcodeで新しいプロジェクトを最初に開始することをお勧めします。

とりわけ、Appleのテンプレートコードには、OpenGL ES 2.0で描画するために必要と思われるShaderの機能を提供するGLKBaseEffectの作成が含まれます。

このテンプレートは "setupGL"関数を作成します。これを次のように変更しました:

このテンプレートは、次のように変更しました。
- (void)setupGL 
{ 
    [EAGLContext setCurrentContext:self.context]; 

    self.effect = [[[GLKBaseEffect alloc] init] autorelease]; 

    // Let's color the line 
    self.effect.useConstantColor = GL_TRUE; 

    // Make the line a cyan color 
    self.effect.constantColor = GLKVector4Make(
     0.0f, // Red 
     1.0f, // Green 
     1.0f, // Blue 
     1.0f);// Alpha 
} 

私はいくつかのステップを追加することで描画することができました。それはすべて、処理されるGPUにデータを送信することを伴う。ここに私のglkViewです:drawInRect機能:

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect 
{ 
    glClearColor(0.65f, 0.65f, 0.65f, 1.0f); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    // Prepare the effect for rendering 
    [self.effect prepareToDraw]; 

    const GLfloat line[] = 
    { 
     -1.0f, -1.5f, //point A 
     1.5f, -1.0f, //point B 
    }; 

    // Create an handle for a buffer object array 
    GLuint bufferObjectNameArray; 

    // Have OpenGL generate a buffer name and store it in the buffer object array 
    glGenBuffers(1, &bufferObjectNameArray); 

    // Bind the buffer object array to the GL_ARRAY_BUFFER target buffer 
    glBindBuffer(GL_ARRAY_BUFFER, bufferObjectNameArray); 

    // Send the line data over to the target buffer in GPU RAM 
    glBufferData(
     GL_ARRAY_BUFFER, // the target buffer 
     sizeof(line),  // the number of bytes to put into the buffer 
     line,    // a pointer to the data being copied 
     GL_STATIC_DRAW); // the usage pattern of the data 

    // Enable vertex data to be fed down the graphics pipeline to be drawn 
    glEnableVertexAttribArray(GLKVertexAttribPosition); 

    // Specify how the GPU looks up the data 
    glVertexAttribPointer(
     GLKVertexAttribPosition, // the currently bound buffer holds the data 
     2,      // number of coordinates per vertex 
     GL_FLOAT,    // the data type of each component 
     GL_FALSE,    // can the data be scaled 
     2*4,      // how many bytes per vertex (2 floats per vertex) 
     NULL);     // offset to the first coordinate, in this case 0 

    glDrawArrays(GL_LINES, 0, 2); // render 

} 

ところで、それは起こっていないので、私はあなたがオライリー(書籍の早期フォームを通じて「ラフカット」の形で購入することができますLearning OpenGL ES for iOS by Erik Buck、を経由してきました年末までに完全に公開される)。この本には、この段階でかなりの数のタイプミスがあり、写真はありませんが、私はまだそれが非常に有用であると感じています。この本のソースコードは非常に遠いので、his blogにつかむことができます。著者は優れた本「Cocoa Design Patterns」も書いた。

+0

Mr.Chrisはあなたの大きな助けに感謝します。私はこの答えのために7日間待った。今私はあなたの助けを借りて線を引く。私はあなたの助けを将来したい。再度、感謝します。 – Gopinath

+0

こんにちはMr.Chirs私はあなたの助けが必要です。ポイントAとポイントBの値をどのように割り当てるのか教えてください。前もって感謝します。 – Gopinath

+0

あなたの質問が分かりません。あなたはポイントAとポイントBの値を設定する方法を尋ねていますか? –

関連する問題