2017-04-02 103 views
0

我試圖在使用OpenGLES的iOS中顯示香蕉。我很確定我已經正確設置了一切,但是當應用程序啓動時,香蕉並未正確繪製。它顯示如下:OpenGLES 3 iOS對象顯示爲三角形而未填充

我試圖展示的香蕉對象來自obj2opengl提供的示例banana.h。

App running on iPhone 5 simulator

這裏是設置:

- (void)setupGL { 
    [EAGLContext setCurrentContext:self.context]; 

    [self loadShaders]; 

    glEnable(GL_BLEND); 

    self.effect = [[GLKBaseEffect alloc] init]; 
    self.effect.light0.enabled = GL_TRUE; 
    self.effect.light0.diffuseColor = GLKVector4Make(1.0f, 0.4f, 0.4f, 1.0f); 

    glGenVertexArrays(1, &_vertexArray); 
    glBindVertexArray(_vertexArray); 

    glGenBuffers(1, &_vertexBuffer); 
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(bananaVerts), bananaVerts, GL_STATIC_DRAW); 

    glGenBuffers(1, &_textureCoords); 
    glBindBuffer(GL_ARRAY_BUFFER, _textureCoords); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(bananaTexCoords), bananaTexCoords, GL_STATIC_DRAW); 

    //Vertex attribute 
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 0,0); 
    glEnableVertexAttribArray(GLKVertexAttribPosition); 
    // Normal attribute 
    /*glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 0,0); 
    glEnableVertexAttribArray(GLKVertexAttribNormal);*/ 
    // TexCoord attribute 
    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, 0); 
    glEnableVertexAttribArray(GLKVertexAttribTexCoord0); 

    [self loadTexture]; 

    glBindVertexArray(0); 
} 

這是繪圖方法:

- (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); 

    [self calculateMatrices]; 

    glUseProgram(_program); 
    glActiveTexture(GL_TEXTURE0); 
    glBindTexture(GL_TEXTURE_2D, _texture.name); 
    glUniform1i(glGetUniformLocation(_program, "texture1"), 0); 

    glUniformMatrix4fv(glGetUniformLocation(_program, "viewMatrix"), 1, GL_FALSE, _modelViewMatrix.m); 
    glUniformMatrix4fv(glGetUniformLocation(_program, "projectionMatrix"), 1, GL_FALSE, _projectionMatrix.m); 

    glBindVertexArray(_vertexArray); 
    glDrawArrays(GL_TRIANGLE_STRIP, 0, bananaNumVerts); 
} 

頂點着色器:

attribute vec3 position; 
attribute vec2 texCoord; 

uniform mat4 viewMatrix; 
uniform mat4 projectionMatrix; 

varying vec2 TexCoord; 
varying vec3 vNormal; 

void main() 
{ 
    gl_Position = viewMatrix * projectionMatrix * vec4(position, 1.0); 
    TexCoord = vec2(texCoord.x, 1.0 - texCoord.y); 
} 

片斷S hader:

precision mediump float; 

varying vec2 TexCoord; 

uniform sampler2D texture1; 

void main() 
{ 
    gl_FragColor = texture2D(texture1, TexCoord); 
} 

編輯:

我通過改變繪圖命令修正了問題

GL_LINES 

代替

GL_TRIANGLES. 

回答

0

您使用的glVertexAttribPointer看起來是錯誤的。

在調用glVertexAttribPointer來引用它之前,您需要調用相關緩衝區的glBindBuffer(GL_ARRAY_BUFFER, ...)。目前,您正在參考位置和紋理座標的_textureCoords緩衝區。

+0

感謝您指出。我修好了,但問題仍然存在。 – ModGod