我正在使用OpenGL作为我想要制作的应用程序。我一直在学习本书“Beggining iPhone Games Development”。
我遇到的问题是,如果我的网格类的子类的texturesQuad似乎没有正确呈现。 (虽然它在模拟器中有效。)四边形渲染为我在texturesQuad类中设置的纯色,并且似乎不渲染纹理。
这是一个名为MenuOptionObject.m的类,它是我尝试渲染的实际对象,我觉得它与示例中使用的spaceShip.m文件类似。该文件确实有效,所以当我使用几乎相同的'引擎'时,我不明白为什么这样做。
我有一种感觉,我错过了一些blatent。我工作的例子奏效了。我还有一个名为texturedButton的类,它可以工作并且还使用了texturesQuad。
继承mesh.m代码:
#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
下面是texturesQuad.m代码:
#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:
#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
答案 0 :(得分:3)
你还记得在渲染纹理几何体之前调用glEnable(GL_TEXTURE_2D)
吗?
答案 1 :(得分:0)
答案结果证明是即使我的纹理在侧面的文件管理器中都被称为menuAtlas,而在代码中它也不会工作,因此将两者都改为mA工作。我不明白为什么,但我怀疑它涉及与缓存有关。