2012-12-01 185 views
0

首先對不起我的英語, 我的問題一個三角形是:繪製與OpenGL ES的

我下面的教程來學習OpenGL ES的,我必須在屏幕的中間畫一個三角形。在他們顯示,三角形沒有觸摸屏的邊緣的圖像(我認爲這是因爲頂點的z座標)

他們告訴我們做的頂點是那些座標:

// 
// EAGLView.m 
// OpenGlintro 
// 
// Created by Arnaud Miguet on 01/12/12. 
// Copyright (c) 2012 Tap‘n'Develop. All rights reserved. 
// 

#import "EAGLView.h" 

@implementation EAGLView 

+ (Class) layerClass { 
    return [CAEAGLLayer class]; 
} 

- (void)setupVBOs { 

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

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

} 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
     CAEAGLLayer *EAGLLayer = (CAEAGLLayer *) super.layer; 
     EAGLLayer.opaque = YES; 

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

     if (!context || ![EAGLContext setCurrentContext:context]) { 
      [self release]; 
      return nil; 
     } 

     glEnable (GL_DEPTH_TEST); 

     GLuint framebuffer , renderbuffer; 

     glGenBuffers(1, &framebuffer); 
     glGenBuffers(1, &renderbuffer); 

     glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); 
     glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer); 

     [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:EAGLLayer]; 
     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffer); 
     glViewport(10, 0, CGRectGetWidth(frame), CGRectGetHeight(frame)); 
     [self compileShaders]; 
     [self setupVBOs]; 
     [self render]; 
    } 
    return self; 
} 


- (void)render { 
    glClearColor(0.7, 0.7, 0.7, 1.0); 


    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    // 1 
    glViewport(0, 0, self.frame.size.width, self.frame.size.height); 

    // 2 
    glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, 
          sizeof(Vertex), 0); 
    glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, 
          sizeof(Vertex), (GLvoid*) (sizeof(float) * 3)); 

    // 3 
    glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]),GL_UNSIGNED_BYTE, 0); 

    [context presentRenderbuffer:GL_RENDERBUFFER]; 
} 

- (GLuint)compileShader:(NSString*)shaderName withType:(GLenum)shaderType { 

    // 1 
    NSString* shaderPath = [[NSBundle mainBundle] pathForResource:shaderName 
                  ofType:@"glsl"]; 
    NSError* error; 
    NSString* shaderString = [NSString stringWithContentsOfFile:shaderPath 
                 encoding:NSUTF8StringEncoding error:&error]; 
    if (!shaderString) { 
     NSLog(@"Error loading shader: %@", error.localizedDescription); 
     exit(1); 
    } 

    // 2 
    GLuint shaderHandle = glCreateShader(shaderType); 

    // 3 
    const char * shaderStringUTF8 = [shaderString UTF8String]; 
    int shaderStringLength = [shaderString length]; 
    glShaderSource(shaderHandle, 1, &shaderStringUTF8, &shaderStringLength); 

    // 4 
    glCompileShader(shaderHandle); 

    // 5 
    GLint compileSuccess; 
    glGetShaderiv(shaderHandle, GL_COMPILE_STATUS, &compileSuccess); 
    if (compileSuccess == GL_FALSE) { 
     GLchar messages[256]; 
     glGetShaderInfoLog(shaderHandle, sizeof(messages), 0, &messages[0]); 
     NSString *messageString = [NSString stringWithUTF8String:messages]; 
     NSLog(@"%@", messageString); 
     exit(1); 
    } 

    return shaderHandle; 

} 

- (void)compileShaders { 

    // 1 
    GLuint vertexShader = [self compileShader:@"SimpleVertex" 
            withType:GL_VERTEX_SHADER]; 
    GLuint fragmentShader = [self compileShader:@"SimpleFragment" 
             withType:GL_FRAGMENT_SHADER]; 

    // 2 
    GLuint programHandle = glCreateProgram(); 
    glAttachShader(programHandle, vertexShader); 
    glAttachShader(programHandle, fragmentShader); 
    glLinkProgram(programHandle); 

    // 3 
    GLint linkSuccess; 
    glGetProgramiv(programHandle, GL_LINK_STATUS, &linkSuccess); 
    if (linkSuccess == GL_FALSE) { 
     GLchar messages[256]; 
     glGetProgramInfoLog(programHandle, sizeof(messages), 0, &messages[0]); 
     NSString *messageString = [NSString stringWithUTF8String:messages]; 
     NSLog(@"%@", messageString); 
     exit(1); 
    } 

    // 4 
    glUseProgram(programHandle); 

    // 5 
    _positionSlot = glGetAttribLocation(programHandle, "Position"); 
    _colorSlot = glGetAttribLocation(programHandle, "SourceColor"); 
    glEnableVertexAttribArray(_positionSlot); 
    glEnableVertexAttribArray(_colorSlot); 
} 

typedef struct { 
    float Position[3]; 
    float Color[4]; 
} Vertex; 

const Vertex Vertices[] = { 
    {{0.0, 1.0, -2}, {1, 0, 0, 1}}, 
    {{1.0, 0.0, -2}, {1, 0, 0, 1}}, 
    {{-1.0, 0.0, 0}, {1, 0, 0, 1}} 
}; 

const GLubyte Indices[] = { 
    0,1,2 
}; 
/* 
// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 
// Drawing code 
} 
*/ 

@end 
:(0.0,1.0,-3.0) (1.0,0.0,-3.0) (-1.0,0.0,-3.0)

的教程不是UPTODATE所以與個人代碼做到了

但頂點不會出現因爲e在z = -1處繪製的背景座標...有人可以幫助我嗎?我找不到任何解決方案...

回答

0

乍一看,你的渲染函數似乎只被調用一次,在viewDidLoad。這應該在具有持續更新的方法中實現,例如drawRect。

OpenGL ES的學習曲線非常陡峭,在iOS中可以通過實現GLKit框架來幫助自己。它可以將所有這些調用保存到不同類型的緩衝區中,併爲您處理大部分的樣板代碼以供繪製/更新功能。

本教程是一個很好的開始的地方: http://www.raywenderlich.com/5223/beginning-opengl-es-2-0-with-glkit-part-1

+0

THX,我會看到 –