gpt4 book ai didi

渲染多个对象时 iOS OpenGLES 奇怪的行为

转载 作者:行者123 更新时间:2023-11-29 12:57:20 25 4
gpt4 key购买 nike

我正在尝试渲染两个带有纹理的立方体(“ body ”和“头部”)。有一个包含所有立方体面的纹理图集。为此,我创建了一个类来封装绘制立方体的所有代码:MSTexturedCube

// 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];
}

在我的 GLKViewController 子类中,它实现了 GLKViewDelegateGLKViewControllerDelegate,我创建了两个 MSTexturedCube 对象我使用必要的顶点数据和纹理顶点数据进行初始化。在 GLKViewDelegate 中我写了这个函数:

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
// Set background color
glClearColor(0.5, 0.5, 0.5, 1.0);
glClear(GL_COLOR_BUFFER_BIT);

// Set properties
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);

[self.cubes makeObjectsPerformSelector:@selector(draw)];
}

它工作得很好,但有一个问题:当我旋转 View 时,立方体数组中的第二个对象渲染没有任何问题,但是第一个对象,即使它离相机更近,如果有另一个对象,也会显得绝对透明下面的对象。如果它只有下面的背景,它也会呈现不透明。 ¿如何解决这个问题,并使对象遵守 z 顺序?

最佳答案

我解决了我的问题。

我忘记通过将 GLView 的 drawableDepthFormat 属性设置为 GLKViewDrawableDepthFormat16 来向我的 EAGLContext 添加深度缓冲区。

虽然没有出现在问题中,但我曾尝试在GLView的委托(delegate)方法中添加以下两行,但他们什么也没做:

glDepthFunc(GL_LEQUAL);
glClear(GL_DEPTH_BUFFER_BIT);

很明显,因为没有设置缓冲区。当我设置 GLKViewDrawableDepthFormat16 并添加这两行时,它完美地工作。

关于渲染多个对象时 iOS OpenGLES 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20722159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com