Objective-C iOS-使用GLKit将图像渲染到屏幕上
就我个人而言,我无法在iPhone模拟器屏幕上渲染图像。我已经尽可能地简化了我的代码 以下代码位于ViewController.m中,该类扩展了GLKViewController,也是GLKViewDelegateObjective-C iOS-使用GLKit将图像渲染到屏幕上,ios,objective-c,iphone,glkit,Ios,Objective C,Iphone,Glkit,就我个人而言,我无法在iPhone模拟器屏幕上渲染图像。我已经尽可能地简化了我的代码 以下代码位于ViewController.m中,该类扩展了GLKViewController,也是GLKViewDelegate - (void)viewDidLoad { [super viewDidLoad]; /*Setup EAGLContext*/ self.context = [self createBestEAGLContext]; [EAGLContext se
- (void)viewDidLoad {
[super viewDidLoad];
/*Setup EAGLContext*/
self.context = [self createBestEAGLContext];
[EAGLContext setCurrentContext:self.context];
/*Setup View*/
GLKView *view = [[GLKView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
view.context = self.context;
view.delegate = self;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
self.view = view;
}
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
/*Setup GLK effect*/
self.effect = [[GLKBaseEffect alloc] init];
self.effect.transform.projectionMatrix = GLKMatrix4MakeOrtho(0, 320, 480, 0, -1, 1);
glClearColor(0.5, 1, 1, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES],
GLKTextureLoaderOriginBottomLeft,
nil];
NSError * error;
NSString *path = [[NSBundle mainBundle] pathForResource:@"soccerball" ofType:@"jpg"];
GLKTextureInfo * textureInfo = [GLKTextureLoader textureWithContentsOfFile:path options:options error:&error];
if (textureInfo == nil) {
NSLog(@"Error loading file: %@", [error localizedDescription]);
}
TexturedQuad newQuad;
newQuad.bl.geometryVertex = CGPointMake(0, 0);
newQuad.br.geometryVertex = CGPointMake(textureInfo.width, 0);
newQuad.tl.geometryVertex = CGPointMake(0, textureInfo.height);
newQuad.tr.geometryVertex = CGPointMake(textureInfo.width, textureInfo.height);
newQuad.bl.textureVertex = CGPointMake(0, 0);
newQuad.br.textureVertex = CGPointMake(1, 0);
newQuad.tl.textureVertex = CGPointMake(0, 1);
newQuad.tr.textureVertex = CGPointMake(1, 1);
self.effect.texture2d0.name = textureInfo.name;
self.effect.texture2d0.enabled = YES;
GLKMatrix4 modelMatrix = GLKMatrix4Identity;
modelMatrix = GLKMatrix4Translate(modelMatrix, 100, 200, 0);
self.effect.transform.modelviewMatrix = modelMatrix;
[self.effect prepareToDraw];
long offset = (long)&(newQuad);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset + offsetof(TexturedVertex, geometryVertex)));
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset + offsetof(TexturedVertex, textureVertex)));
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
还有一些使用的结构
typedef struct {
CGPoint geometryVertex;
CGPoint textureVertex;
} TexturedVertex;
typedef struct {
TexturedVertex bl;
TexturedVertex br;
TexturedVertex tl;
TexturedVertex tr;
} TexturedQuad;
现在唯一有效的是
glClearColor(0.5, 1, 1, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
它可以调整背景颜色。没有“足球”的形象
非常感谢您的帮助
编辑-TextureEvertex CG点不正确,因此我修复了它们。问题仍然存在。解决方案:
TexturedVertex结构不能使用CGPoint,而是使用GLKVector 2
这是因为这些点中存储的浮点值存在转换问题。GLKit期望浮点值具有单点精度,但CGPoint浮点值具有双点精度,所以事情变得很奇怪。此外,此问题仅在iOS 7.0之后出现
有关此问题的更多详细信息,请参阅此处。
如果有帮助的话,这段代码应该不会太远,因为我大约一年前就有了这段代码。我不知道从那以后它发生了什么。