首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >一次呈现多个GLKViewControllers

一次呈现多个GLKViewControllers
EN

Stack Overflow用户
提问于 2014-11-07 18:35:24
回答 1查看 876关注 0票数 1

我有一个简单的GLKViewcontroller子类,它在被告知时呈现纹理。这本身就很好,这正是我需要它做的。但是,由于某种原因,当我试图在一个场景中使用它时,我一次拥有6个相同视图控制器的副本(设置为指向同一个视图的容器视图),它不起作用。我有一个加载和显示纹理方法,以选择哪个纹理要从加载纹理的地图。似乎只有最后一个视图,我把纹理正确地画出来,剩下的作为黑色方块出现。

下面是我的故事板的截图:

以下是我的加载和绘制代码:

代码语言:javascript
复制
-(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];
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-10 11:44:51

这个问题最有可能发生在openGL上下文中。问题是,每个控制器将创建每个自己的上下文,每个必须在使用之前设置在任何,绘图,呈现,纹理加载。

我从不用GLKViewGLKViewController这样的胡言乱语来容忍我。要么是纹理只在一个上下文中加载,要么您需要为每个上下文(视图控制器)加载它们,要么找到一种方法从一个控制器中使用共享组创建上下文:当您创建一个上下文时,可以从它获得一个共享组,然后使用这个共享组(一个上下文属性)初始化新的上下文。结果是,新的上下文可以使用元素,比如在主上下文上创建的纹理,反之亦然。更大的问题可能是,这些控制器正在对openGL做一些额外的工作,并且在这样做之前不会重置上下文。

无论如何,使用openGL创建多个视图应该很好,所以我希望您能找到一个很好的解决方案。也许您所需要做的就是在loadTextures方法的顶部设置当前上下文。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26807835

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档