简体   繁体   中英

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

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 ! 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个顶点

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

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

A texture bitmap with only 2 colors (blue and yellow) which are used in big and quite big 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) 胶囊质地

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. I have also tested on different physical Iphone so it's not specific to the simulator. Enable/disable gl_blend doesn't solve the problem neither.

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:

// 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:

// 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: 在此处输入图片说明

Then enable the depth in viewDidLoad() by

    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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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