3Dオブジェクトでテクスチャを使用すると、Xcode4.5およびiOS6SDK以降で奇妙な動作が発生しました。この問題は、OS X10.8SDKに対してビルドするときに私のMacアプリケーションでも発生します。
iOSではOpenGLES2.0を使用し、OS X 10.8ではOpenGLレガシープロファイル(<3.0)を使用しています。
テクスチャが正しい座標に配置されなくなり、アーティファクトがたくさんあります。VAOは正しくアップロードされ、テクスチャリングなしで見栄えがします。XCode4.4.1およびiOS5.1SDKを使用する場合、すべてが正常です。
VAOはまったく同じであり(OpenGL ESフレームキャプチャでチェック)、テクスチャユニフォームも正しくバインドされています。
Xcode4.4VAOの概要
Xcode4.5VAOの概要
XCode 4.4.1(iOS 5.1 SDK)
XCode 4.5(iOS 6 SDK)
コード/シェーダースニペット
テクスチャのアップロードと処理に関連するパーツ。シェーダーをミニウムまで剥がさなければなりませんでした。
バーテックスシェーダー
precision highp float;
attribute vec2 a_vTextureCoordinate;
uniform mat4 u_mModelViewMatrix;
uniform mat4 u_mModelViewMatrixInverse;
uniform mat4 u_mProjectionMatrix;
uniform mat4 u_mNormalMatrix;
void main()
{
....
// Transform output position
gl_Position = u_mProjectionMatrix * u_mModelViewMatrix * a_vPosition;
// Pass through texture coordinate v_texcoord = a_vTextureCoordinate.xy;
v_vPosition = vec3(u_mModelViewMatrix * a_vPosition);
v_vTextureCoordinate = a_vTextureCoordinate.xy;
....
}
フラグメントシェーダー
precision highp float;
// location 1
uniform sampler2D u_diffuseTexture;
varying vec2 v_vTextureCoordinate;
varying vec3 v_vPosition;
....
void main() {
....
vec4 base = texture2D(u_diffuseTexture, v_vTextureCoordinate);
gl_FragColor = base;
....
}
テクスチャの読み込み
NSDictionary *options = @{GLKTextureLoaderOriginBottomLeft: @(YES), [NSNumber numberWithBool:YES] : GLKTextureLoaderGenerateMipmaps};
NSError *error;
path = [path stringByReplacingOccurrencesOfString:@"/" withString:@""];
path = [[NSBundle mainBundle] pathForResource:[path stringByDeletingPathExtension] ofType:[path pathExtension]];
GLKTextureInfo *texture = [GLKTextureLoader textureWithContentsOfFile:path options:options error:&error];
レンダリングループ(アクティブテクスチャのユニフォームのみを送信)
....
[self setShaderTexture:[[materials objectForKey:@"diffuse"] objectForKey:@"glktexture"]
forKey:@"u_diffuseTexture"
withUniform1i:0
andTextureUnit:GL_TEXTURE0+0];
....
#pragma mark - Texture communication
-(void)setShaderTexture:(GLKTextureInfo*)texture forKey:(NSString*)key withUniform1i:(int32_t)uniform andTextureUnit:(int32_t)unit {
glActiveTexture(unit);
glBindTexture(texture.target, texture.name);
[self.shaderManager sendUniform1Int:key parameter:uniform];
}
iOS 6以降、誰かが私の身近な問題を抱えていましたか?