简体   繁体   English

GLKit:处理大型3D对象时的透明纹理

[英]GLKit: Transparent texture when dealing with big 3D object

I'm writing a 3D Viewer that displays .obj file and offers some basic gestures (pinning, rotating, zooming) by following 2 Raywenderlich tutorials: http://www.raywenderlich.com/48293/how-to-export-blender-models-to-opengl-es-part-1 我正在编写3D查看器,它通过以下2个Raywenderlich教程来显示.obj文件并提供一些基本手势(固定,旋转,缩放): http ://www.raywenderlich.com/48293/how-to-export-blender- 模型对OpenGL的ES-部分1

http://www.raywenderlich.com/50398/opengl-es-transformations-gestures http://www.raywenderlich.com/50398/opengl-es-transformations-gestures

I'm able to load and display correctly small .obj files but for big .obj files, the textures become suddenly transparent ! 我能够正确加载和显示小的.obj文件,但是对于大的.obj文件,纹理突然变得透明 I have tested with the same texture files so I think the image size is not the cause . 我已经对相同的纹理文件进行了测试,所以我认为不是图像大小的原因 Please take a look at the below screenshot to have an idea: 请查看以下屏幕截图以了解一个想法:

Big obj: about 20000 vertices 大obj:约20000个顶点 大obj:约20000个顶点

Small obj: about 5000 vertices: 小obj:约5000个顶点: 小obj:约5000个顶点

Quite big obj: about 67000 vertices: 相当大的obj:约67000个顶点: 很大的obj:大约有67000个顶点

A texture bitmap with only 2 colors (blue and yellow) which are used in big and quite big objs 仅2种颜色(蓝色和黄色)的纹理位图,用于大型和大型objs 大对象中仅使用2种颜色(蓝色和黄色)的纹理位图

The capsule texture (I don't use the 2 colors texture so that we can see clearly there is no transparency in the small obj) 胶囊纹理(我不使用2种颜色的纹理,这样我们就可以清楚地看到小obj中没有透明度) 胶囊质地

I have tried different textures on different obj and it's always the same problem: the texture starts to be transparent when the obj file is big. 我在不同的obj上尝试了不同的纹理,这始终是相同的问题:当obj文件很大时,纹理开始透明。 I have also tested on different physical Iphone so it's not specific to the simulator. 我还在不同的物理Iphone上进行了测试,因此它并非特定于模拟器。 Enable/disable gl_blend doesn't solve the problem neither. 启用/禁用gl_blend也不能解决问题。

You can find the full code at http://pastecode.org/index.php/view/32247978 Here is the code I use to create the GLKBaseEffect and load texture image: 您可以在http://pastecode.org/index.php/view/32247978中找到完整的代码,这是我用来创建GLKBaseEffect并加载纹理图像的代码:

// Initialize
self.effect = [[GLKBaseEffect alloc] init];

// Texture
NSDictionary* options = @{ GLKTextureLoaderOriginBottomLeft: @YES };
NSError* error;
NSString* path = [[NSBundle mainBundle] pathForResource:@"capsule0.jpg" ofType:nil];

GLKTextureInfo* texture = [GLKTextureLoader textureWithContentsOfFile:path
                                                              options:options
                                                                error:&error];

if(texture == nil)
    NSLog(@"Error loading file: %@", [error localizedDescription]);

self.effect.texture2d0.name = texture.name;
self.effect.texture2d0.enabled = true;
self.effect.texture2d0.envMode = GLKTextureEnvModeReplace;


// Light
self.effect.light0.enabled = GL_TRUE;
self.effect.light0.position = GLKVector4Make(1.0f, 1.0f, 1.0f, 1.0f);
self.effect.lightingType = GLKLightingTypePerVertex;

And this part is for setting up OpenGL in viewDidLoad() after creating the effect: 这部分是在创建效果后在viewDidLoad()中设置OpenGL的:

// OpenGL ES Settings
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);

I have finally found the solution: 我终于找到了解决方案:

First, you need to set the depth format to 24 or 16 in the storyboard for the GLKView: 首先,您需要在GLKView的情节提要中将深度格式设置为24或16: 在此处输入图片说明

Then enable the depth in viewDidLoad() by 然后通过在viewDidLoad()中启用深度

    glEnable(GL_DEPTH_TEST);

And finally for each draw call, you need to clear the depth buffer: 最后,对于每个绘制调用,您需要清除深度缓冲区:

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM