OpenGL ES 2.0仅显示纹理的一个(左上角)像素

时间:2015-10-28 22:01:09

标签: ios swift opengl-es

我正在使用openGL ES 2.0和swift。我终于要显示纹理,但它只显示了一个像素! 任何人都可以帮忙吗?

我的简单顶点着色器

attribute vec2 TexCoordIn;
varying vec2 TexCoordOut;

void main(void) {
    gl_Position = Position;
    TexCoordOut = TexCoordIn;
}

片段着色器

varying lowp vec2 TexCoordOut; 
uniform sampler2D Texture; 

void main(void) {
    gl_FragColor = texture2D(Texture, TexCoordOut); 
}

我的视图控制器:

// My Vertex, Vertices and Indices
struct Vertex {
    var Position: (CFloat, CFloat, CFloat)
    var Color: (CFloat, CFloat, CFloat, CFloat)
    var TexCoord: (CFloat, CFloat)
}

var Vertices = [
    Vertex(Position: (1, -1, 0) , Color: (1, 0, 0, 1), TexCoord: (1, 0)), 
    Vertex(Position: (1, 1, 0)  , Color: (0, 1, 0, 1), TexCoord: (1, 1)), 
    Vertex(Position: (-1, 1, 0) , Color: (0, 0, 1, 1), TexCoord: (0, 1)), 
    Vertex(Position: (-1, -1, 0), Color: (0, 0, 0, 1), TexCoord: (0, 0))  
]

var Indices: [GLubyte] = [
    0, 1, 2,
    2, 3, 0
]

func compileShaders() {
    var vertexShader: GLuint = self.compileShader("Vertex", shaderType: GLenum(GL_VERTEX_SHADER))
    var fragmentShader: GLuint = self.compileShader("Fragment", shaderType: GLenum(GL_FRAGMENT_SHADER))
    var programHandle: GLuint = glCreateProgram()
    glAttachShader(programHandle, vertexShader)
    glAttachShader(programHandle, fragmentShader)
    glLinkProgram(programHandle)
    glUseProgram(programHandle)

    glEnableVertexAttribArray(self.positionSlot)
    glEnableVertexAttribArray(self.colorSlot)

    texCoordSlot = GLuint(glGetAttribLocation(programHandle, "TexCoordIn"))
    glEnableVertexAttribArray(texCoordSlot)
    let pointer = UnsafePointer<Int>(bitPattern: sizeof(Float) * 7)
    glVertexAttribPointer(texCoordSlot, 2, GLenum(GL_FLOAT), GLboolean(UInt8(GL_FALSE)), GLsizei(sizeof(Vertex)), pointer)

    textureUniform = GLuint(glGetUniformLocation(programHandle, "Texture"))
}

func setupVBOs() {
    glGenVertexArraysOES(1, &VAO);
    glBindVertexArrayOES(VAO);

    glGenBuffers(1, &vertexBuffer)
    glBindBuffer(GLenum(GL_ARRAY_BUFFER), vertexBuffer)
    glBufferData(GLenum(GL_ARRAY_BUFFER), Vertices.size(), Vertices, GLenum(GL_STATIC_DRAW))

    glEnableVertexAttribArray(positionSlot)
    glVertexAttribPointer(positionSlot, 3, GLenum(GL_FLOAT), GLboolean(UInt8(GL_FALSE)), GLsizei(sizeof(Vertex)), nil)

    glGenBuffers(1, &indexBuffer)
    glBindBuffer(GLenum(GL_ELEMENT_ARRAY_BUFFER), indexBuffer)
    glBufferData(GLenum(GL_ELEMENT_ARRAY_BUFFER), Indices.size(), Indices, GLenum(GL_STATIC_DRAW))

    glBindBuffer(GLenum(GL_ARRAY_BUFFER), 0)
    glBindVertexArrayOES(0)
}

func render() {
    glBindVertexArrayOES(VAO);

    glActiveTexture(GLenum(GL_TEXTURE0))
    glBindTexture(GLenum(GL_TEXTURE_2D), floorTexture)
    glUniform1i(GLint(textureUniform), GLint(0))

    glDrawElements(GLenum(GL_TRIANGLES), GLsizei(Indices.count), GLenum(GL_UNSIGNED_BYTE), nil)

    glContext!.presentRenderbuffer(Int(GL_RENDERBUFFER))
    glBindVertexArrayOES(0)
}

我在伟大的Ray Wenderlich Tutorial中学到的基础。但我无法让它迅速发挥作用。

0 个答案:

没有答案