【问题标题】:Why isn't this texture repeating in iOS OpenGL ES 2.0?为什么这个纹理在 iOS OpenGL ES 2.0 中没有重复?
【发布时间】:2013-03-06 04:05:27
【问题描述】:

我有一个类可以在 iOS 中将带纹理的 2D 形状渲染到屏幕上。如果我使用小于 1.0 的纹理坐标,则纹理会适当缩放以匹配坐标。但是,如果我使用大于 1.0 的纹理坐标,纹理会正确缩放,但不会像我预期的那样重复。相反,它只绘制一次纹理并在形状的其余部分重复边缘像素(我希望这是有道理的:/)

有没有办法使用 GLKit 在 OpenGL ES 2.0 中打开重复纹理?

渲染函数为:

-(void)render
{
    // If we have a texture set, setup the Texture Environment Mode
    if (texture != nil) {
        self.effect.texture2d0.envMode = GLKTextureEnvModeReplace;
        self.effect.texture2d0.target = GLKTextureTarget2D;
        self.effect.texture2d0.name = texture.name;
    }

    // Perform any Transformations on the Sprite
    GLKMatrix4 modelViewMatrix = GLKMatrix4Multiply(
        GLKMatrix4MakeTranslation(self.position.x, self.position.y, 0.0f),
        GLKMatrix4MakeRotation(GLKMathDegreesToRadians(self.rotation), 0, 0, 1)
    );

    self.effect.transform.modelviewMatrix = modelViewMatrix;

    // Prepare to Draw
    [self.effect prepareToDraw];

    // Setup the Vertices
    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, self.vertices);

    // Setup the Texture Coordinates
    if (texture != nil) {
        glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
        glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, self.texCoords);
    }

    // Enable Alpha Blending for Transparent PNGs
    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

    // Draw the Triangle Strip from the Vertex Array
    glDrawArrays(GL_TRIANGLE_STRIP, 0, self.numVertices);

    // Disable Alpha Blending
    glDisable(GL_BLEND);

    // Disable Texture Environment
    if (texture != nil) {
        glDisableVertexAttribArray(GLKVertexAttribTexCoord0);
    }

    glDisableVertexAttribArray(GLKVertexAttribPosition);
}

【问题讨论】:

    标签: ios objective-c xcode opengl-es-2.0 glkit


    【解决方案1】:

    在创建纹理时,您可以设置用于处理边缘的模式。将模式设置为 REPEAT 而不是 CLAMP_TO_EDGE(这显然是默认值),如下所示:

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    

    【讨论】:

      【解决方案2】:

      您需要使用glTexParameter 将 GL_TEXTURE_WRAP_S 和 GL_TEXTURE_WRAP_T 设置为 GL_REPEAT。

      希望对您有所帮助。

      【讨论】:

      • 很遗憾,我无法将你们都标记为正确(因为您提出了同样的建议),但谢谢!
      • 不重要——很高兴我们回答了您的问题。感谢您的支持:)
      猜你喜欢
      • 1970-01-01
      • 2011-05-19
      • 1970-01-01
      • 1970-01-01
      • 2012-04-08
      • 1970-01-01
      • 2013-07-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多