为什么我的OpenGL ES纹理无法正确渲染? [英] Why will my OpenGL ES textures not render properly?

查看:97
本文介绍了为什么我的OpenGL ES纹理无法正确渲染?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为尝试制作的应用程序使用OpenGL.我一直在从畅销书《开始iPhone游戏开发》中学习.

Im using OpenGL for an app that im trying to make. I've been learning from the apress book Beggining iPhone Games Development.

我的问题是作为我的网格类的子类的TexturedQuad似乎无法正确渲染. (尽管它可以在模拟器中使用.)四边形只能作为我在TexturedQuad类中设置的纯色进行渲染,并且似乎没有在渲染纹理.

The problem I have is that a texturedQuad which is a subclass of my mesh class doesn't seem to be rendering properly. (Although it works in the simulator.) The quad renders as just a plain colors that I have set in the texturedQuad class and doesn't seem to be rendering the texture.

这在一个名为MenuOptionObject.m的类中,该类是我尝试呈现的实际对象,与示例中使用的spaceShip.m文件类似.该文件确实起作用了,所以我不明白为什么我在使用几乎相同的引擎"时却无法调用它.

This is in a class that is called MenuOptionObject.m which is the actual object that I'm trying to render which I feel is similar to the spaceShip.m file used in the examples. That file did work so I dont get why this wont when I'm using virtually the same 'engine' if you could call it that.

我感觉自己缺少一些隐秘的东西.我工作的例子奏效了.我也有一个名为TexturedButton的类,它既可以工作,也可以使用TexturedQuad.

I have a feeling I'm missing something blatent. The examples I worked on worked. I also have a class called texturedButton that does work and also uses the texturedQuad.

这里有mesh.m代码:

Heres the mesh.m code:

 #import "Mesh.h"
#import "MaterialController.h"
#import "TexturedQuad.h"


@implementation Mesh

@synthesize vertexCount,vertexSize,colorSize,renderStyle,vertexes,colors;

- (id)initWithVertexes:(CGFloat*)verts 
           vertexCount:(NSInteger)vertCount 
            vertexSize:(NSInteger)vertSize
           renderStyle:(GLenum)style;
{
    self = [super init];
    if (self != nil) 
    {
        self.vertexes = verts;
        self.vertexCount = vertCount;
        self.vertexSize = vertSize;
        self.renderStyle = style;
    }
    return self;
}

// called once every frame
-(void)render
{
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisable(GL_TEXTURE_2D);
    // load arrays into the engine
    glVertexPointer(vertexSize, GL_FLOAT, 0, vertexes);
    glEnableClientState(GL_VERTEX_ARRAY);
    glColorPointer(colorSize, GL_FLOAT, 0, colors); 
    glEnableClientState(GL_COLOR_ARRAY);

    //render
    glDrawArrays(renderStyle, 0, vertexCount);  
}


+(CGRect)meshBounds:(Mesh*)mesh scale:(BBPoint)scale
{
    if (mesh == nil) return CGRectZero;
    // need to run through my vertexes and find my extremes
    if (mesh.vertexCount < 2) return CGRectZero;
    CGFloat xMin,yMin,xMax,yMax;
    xMin = xMax = mesh.vertexes[0];
    yMin = yMax = mesh.vertexes[1];
    NSInteger index;
    for (index = 0; index < mesh.vertexCount; index++) {
        NSInteger position = index * mesh.vertexSize;
        if (xMin > mesh.vertexes[position] * scale.x) xMin = mesh.vertexes[position] * scale.x;
        if (xMax < mesh.vertexes[position] * scale.x) xMax = mesh.vertexes[position] * scale.x;
        if (yMin > mesh.vertexes[position + 1] * scale.y) yMin = mesh.vertexes[position + 1] * scale.y;
        if (yMax < mesh.vertexes[position + 1] * scale.y) yMax = mesh.vertexes[position + 1] * scale.y;
    }
    CGRect meshBounds = CGRectMake(xMin, yMin, xMax - xMin, yMax - yMin);
    if (CGRectGetWidth(meshBounds) < 1.0) meshBounds.size.width = 1.0;
    if (CGRectGetHeight(meshBounds) < 1.0) meshBounds.size.height = 1.0;
    return meshBounds;
}

- (void) dealloc
{
    [super dealloc];
}



@end

这里有TexturedQuad.m代码:

Heres the texturedQuad.m code:

    #import "TexturedQuad.h"

static CGFloat TexturedQuadVertexes[8] = 
{
    -0.5,-0.5, 0.5,-0.5, 
    -0.5,0.5, 0.5,0.5
};
static CGFloat TexturedQuadColorValues[16] = 
{
    1.0,1.0,1.0,1.0, 
    1.0,1.0,1.0,1.0, 
    1.0,1.0,1.0,1.0, 
    1.0,1.0,1.0,1.0
};

@implementation TexturedQuad

@synthesize uvCoordinates,materialKey;

- (id) init
{
    self = [super initWithVertexes:TexturedQuadVertexes vertexCount:4 vertexSize:2 renderStyle:GL_TRIANGLE_STRIP];
    if (self != nil) {
        // 4 vertexes
        uvCoordinates = (CGFloat *) malloc(8 * sizeof(CGFloat));
        colors = TexturedQuadColorValues;
        colorSize = 4;
    }
    return self;
}

// called once every frame
-(void)render
{
    glVertexPointer(vertexSize, GL_FLOAT, 0, vertexes);
    glEnableClientState(GL_VERTEX_ARRAY);
    glColorPointer(colorSize, GL_FLOAT, 0, colors); 
    glEnableClientState(GL_COLOR_ARRAY);

    if (materialKey != nil) {
        [[MaterialController sharedController] bindMaterial:materialKey];

        glEnableClientState(GL_TEXTURE_COORD_ARRAY); 
        glTexCoordPointer(2, GL_FLOAT, 0, uvCoordinates);
    } 
    //render
    glDrawArrays(renderStyle, 0, vertexCount);  
}


- (void) dealloc
{
    free(uvCoordinates);
    [super dealloc];
}

@end

最后是MenuOptionObject.m:

And lastly for the MenuOptionObject.m:

#import "MenuOptionObject.h"
#import "MaterialController.h"
#import "MenuSceneController.h"

@implementation MenuOptionObject
@synthesize bounds, circleBouncePoint;

-(id)initWithQuad:(NSString *)quad
{
    self = [super init];
    if (self != nil)
    {
        self.mesh = [[MaterialController sharedController] quadFromAtlasKey:quad];
    }
    return self;
}

// called once when the object is first created.
-(void)awake
{   
    self.scale = BBPointMake(28.0, 28.0, 1.0);
    self.sceneController = (SceneController *)[MenuSceneController sharedController];
}

-(void)update
{
    [super update];
    //[self sideCollisionUpdates];
    //[self circleCollisionUpdates];
}


-(void)dealloc
{
    [sceneController release];
    [super dealloc];
}

@end

推荐答案

即使我的纹理在侧面的文件管理器中以及在代码中都被称为menuAtlas,但答案仍然是正确的,因此无法同时进行更改到mA工作了.我不明白为什么,但是我怀疑它与缓存有关.

Answer turned out to be the even though my texture was called menuAtlas both in the file manager at the side and in the code it wouldn't work so changing both to mA worked. I don't understand why but I have a suspicion that it involves something to do with caching.

这篇关于为什么我的OpenGL ES纹理无法正确渲染?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆