【问题标题】:OpenGLES rendering both triangle sides, alpha don't workingOpenGLES渲染两个三角形边,alpha不起作用
【发布时间】:2013-12-24 14:41:30
【问题描述】:

我正在尝试在 OpenGLES 2.0 iOS 上渲染一个侧面带有纹理的立方体。纹理的一部分具有 alpha 透明度,我想看到它后面的脸的背面。为了渲染立方体的两边,我使用了

glDisable(GL_CULL_FACE);

并启用透明度,

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

在绘制之前。我的立方体被渲染创建以下类的实例:

// In MSTexturedCube.h
#import <Foundation/Foundation.h>
#import <GLKit/GLKit.h>

@interface MSTexturedCube : NSObject {
    GLKVector3 _vertices[8];
    GLKVector3 _triangleVertices[36];
    GLKVector2 _textureVertices[36];
}

@property (nonatomic, assign) float xRotationAngle;
@property (nonatomic, assign) float yRotationAngle;
@property (nonatomic, assign) GLKVector3 scale;             // x, y, z scale
@property (nonatomic, assign) GLKVector3 translation;   // x, y, z translation

- (id)initWithTexture:(GLKTextureInfo *)textureInfo vertexData:(GLKVector2[36])textureVertexData;
- (id)initWithTexture:(GLKTextureInfo *)textureInfo frontFaceCoords:(GLKVector2[4])front right:(GLKVector2[4])right back:(GLKVector2[4])back left:(GLKVector2[4])left top:(GLKVector2[4])top bottom:(GLKVector2[4])bottom;

- (void)draw;

- (void)setVertices:(GLKVector3[8])verticesArray;

@end



// In MSTexturedCube.m
#import "MSTexturedCube.h"

static int vertexIndices[36] = {
    // Front
    0, 1, 2,
    0, 2, 3,
    // Right
    1, 5, 6,
    1, 6, 2,
    // Back
    5, 4, 7,
    5, 7, 6,
    // Left
    4, 0, 3,
    4, 3, 7,
    // Top
    3, 2, 6,
    3, 6, 7,
    // Bottom
    4, 5, 1,
    4, 1, 0,
};

@interface MSTexturedCube ()

@property (nonatomic, strong) GLKBaseEffect *effect;
@property (nonatomic, strong) GLKTextureInfo *textureInfo;

- (void)setupOpenGL;

@end

@implementation MSTexturedCube

@synthesize effect = _effect;
@synthesize textureInfo = _textureInfo;

@synthesize xRotationAngle = _xRotationAngle;
@synthesize yRotationAngle = _yRotationAngle;
@synthesize scale = _scale;
@synthesize translation = _translation;

// Init methods
...

- (void)draw {
    // Create matrices
    GLKMatrix4 yRotation = GLKMatrix4MakeYRotation(self.yRotationAngle);
    GLKMatrix4 xRotation = GLKMatrix4MakeXRotation(self.xRotationAngle);
    GLKMatrix4 scale = GLKMatrix4MakeScale(self.scale.x, self.scale.y, self.scale.z);
    GLKMatrix4 translation = GLKMatrix4MakeTranslation(self.translation.x, self.translation.y, self.translation.z);

    GLKMatrix4 modelMatrix = GLKMatrix4Multiply(translation, GLKMatrix4Multiply(xRotation, GLKMatrix4Multiply(yRotation, scale)));
    GLKMatrix4 viewMatrix = GLKMatrix4MakeLookAt(0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    self.effect.transform.modelviewMatrix = GLKMatrix4Multiply(viewMatrix, modelMatrix);
    self.effect.transform.projectionMatrix = GLKMatrix4MakePerspective(0.25 * M_PI, 1.0, 2.0, 500.0);

    // Set texture properties if a texture is set
    if (self.textureInfo) {
        self.effect.texture2d0.envMode = GLKTextureEnvModeReplace;
        self.effect.texture2d0.target = GLKTextureTarget2D;
        self.effect.texture2d0.name = self.textureInfo.name;
    }

    // Prepare the effect to draw after creating matrices
    [self.effect prepareToDraw];

    // Set texture
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(self.textureInfo.target, self.textureInfo.name);
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );

    // Set vertices
    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, _triangleVertices);

    // Set texture (if set)
    if (self.textureInfo) {
        glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
        glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, _textureVertices);
    }

    // Draw
    glDrawArrays(GL_TRIANGLES, 0, 36);

    // Disable arrays
    glDisableVertexAttribArray(GLKVertexAttribPosition);
    glDisableVertexAttribArray(GLKVertexAttribTexCoord0);
}

// Getters & setters
...

#pragma mark - Private methods

- (void)setupOpenGL {
    // Set vertices array
    _vertices[0] = GLKVector3Make(-0.5, -0.5,  0.5); // Left  bottom front
    _vertices[1] = GLKVector3Make( 0.5, -0.5,  0.5); // Right bottom front
    _vertices[2] = GLKVector3Make( 0.5,  0.5,  0.5); // Right top    front
    _vertices[3] = GLKVector3Make(-0.5,  0.5,  0.5); // Left  top    front
    _vertices[4] = GLKVector3Make(-0.5, -0.5, -0.5); // Left  bottom back
    _vertices[5] = GLKVector3Make( 0.5, -0.5, -0.5); // Right bottom back
    _vertices[6] = GLKVector3Make( 0.5,  0.5, -0.5); // Right top    back
    _vertices[7] = GLKVector3Make(-0.5,  0.5, -0.5); // Left  top    back

    // Set the triangle vertices
    for (int i = 0; i < 36; i++) {
        _triangleVertices[i] = _vertices[vertexIndices[i]];
    }

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

现在的问题是,当我创建一个正面透明而其他所有面不透明的立方体时,这个面就像后面没有任何东西一样,显示背景。但是,如果我在里面放一个较小的不透明立方体,它会完美渲染,我可以通过最大立方体的透明面看到它。有谁知道为什么会发生这种情况,我该如何解决?

提前致谢!如果您需要有关我的代码的更多信息,请询问我。

【问题讨论】:

  • 你能在填充_textureVertices的地方添加代码吗?
  • 它们是硬编码的......我现在不在我的电脑上,但我会尽快发布它们。
  • 听起来你也应该禁用写入深度缓冲区。
  • 但是如果我这样做,不透明的立方体就不会像现在这样显示了,对吗?
  • 我已经实施了一种解决方法,我将把它作为答案发布并接受。但是,如果有人提供“更干净”的答案,我会接受它而不是我的。

标签: ios iphone opengl-es opengl-es-2.0


【解决方案1】:

最后我实施了一个解决方法。它包括创建两个 MSTexturedCube 对象,具有完全相同的顶点和纹理顶点,并渲染一个启用了剔除面和glCullFace(GL_BACK),另一个使用glCullFace(GL_FRONT)。它完全按照预期工作。

【讨论】:

    【解决方案2】:

    原因是在您的情况下首先渲染透明面并填充深度缓冲区,背面没有通过深度测试并且没有被绘制。要解决这个问题,您可以分别绘制不透明和透明对象,首先是不透明对象,然后是透明对象,您还必须从后到前绘制透明对象。另一种选择是对完全透明的像素使用丢弃,但这仅适用于完全透明的情况,而且通常很慢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-08
      • 1970-01-01
      • 1970-01-01
      • 2019-05-30
      • 2020-04-05
      相关资源
      最近更新 更多