gpt4 book ai didi

ios - 一次渲染多个 GLKViewControllers

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

我有一个简单的 GLKViewcontroller 子类,它在被告知时呈现纹理。这本身就很好用,正是我需要它做的。但是,出于某种原因,当我试图在一个场景中使用它时,我一次有 6 个相同 View Controller 的副本(设置为指向相同 View 的容器 View )它不起作用。我有一个加载和显示纹理方法来选择从加载纹理的 map 中绘制哪个纹理。似乎只有我加载纹理的最后一个 View 才能正确绘制它们,其余部分显示为黑色方 block 。

这是我的 Storyboard的截图:

enter image description here

这是我的加载和绘制代码:

-(void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
[EAGLContext setCurrentContext:self.context];
self.view.backgroundColor = [UIColor clearColor];
self.view.opaque = NO;
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);

//sprite render:
if ( self.showTexture )
{
if ( firstDraw )
{
self.imageShowTime = [ATAppDelegate getCurrentTime];
firstDraw = NO;
}
self.effect.texture2d0.name = selectedTexture.name;
self.effect.texture2d0.enabled = YES;
self.effect.transform.modelviewMatrix = self.modelMatrix;

[self.effect prepareToDraw];

long offset = (long)&_quad;

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);
}
else
{
firstDraw = YES;
}
}

-(void)loadTextures:(NSArray *)textureNames
{
if ( textures == nil )
{
textures = [[NSMutableDictionary alloc] init];
}
[textures removeAllObjects];

NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES],
GLKTextureLoaderOriginBottomLeft,
nil];
NSError * error;

for ( NSString *texName in textureNames )
{

NSString *path = [[NSBundle mainBundle] pathForResource:texName ofType:nil];
GLKTextureInfo *newTex = [GLKTextureLoader textureWithContentsOfFile:path options:options error:&error];
if ( newTex == nil || error != nil )
{
NSLog(@"Error loading file: %@", [error localizedDescription]);
return ;
}

NSLog(@"Loaded texture: %@ for name: %@", newTex, texName);

CGSize contentSize = CGSizeMake(newTex.width, newTex.height);

NSDictionary *texInfo = @{@"texture" : newTex, @"contentSize" : [NSValue valueWithCGSize:contentSize]};

textures[texName] = texInfo;
}
}

-(void)showTexture:(NSString *)texture
{
NSDictionary *texInfo = textures[texture];

if ( texInfo == nil )
{
NSLog(@"ERROR: no texture info found for texture name :%@ in %@", texture, textures);
}
else
{
NSLog(@"Showing texture: %@", texInfo);
}

selectedTexture = texInfo[@"texture"];
curContentSize = [texInfo[@"contentSize"] CGSizeValue];
}

最佳答案

问题很可能出现在 openGL 上下文中。问题是每个 Controller 都会创建自己的上下文,并且每个 Controller 都必须在用于任何绘制、呈现、纹理加载之前进行设置。

我从不使用像 GLKViewGLKViewController 这样的废话,所以请多多包涵。问题是纹理仅在一个上下文中加载,您需要为每个上下文( View Controller )加载它们,或者找到一种方法从一个 Controller 创建具有共享组的上下文:您创建一个上下文,您可以从中获取共享组,然后使用该共享组(上下文属性)初始化新上下文。结果是新上下文可能会使用在主上下文中创建的元素,例如纹理,反之亦然。一个更大的问题可能是这些 Controller 正在使用 openGL 做一些额外的工作,并且在这样做之前没有重置上下文。

无论如何,使用 openGL 制作多个 View 应该可以正常工作,所以我希望您能为您的问题找到一个好的解决方案。也许您需要做的就是在 loadTextures 方法的顶部设置当前上下文。

关于ios - 一次渲染多个 GLKViewControllers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26807835/

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