我正在使用opengl调用在cocos2d中绘制图像,但图像以相反的顺序显示并按比例缩小,图像的原始尺寸为320 * 480。我以前遇到过同样的问题,我曾经在 cocos2d api v1 的 ccConfig 文件中将 CC_TEXTURE_NPOT_SUPPORT 的值从 0 更改为 1,然后它正确显示图像现在我在这里创建新的 cocos2d 项目我面临同样的问题,我遵循了我使用的程序以前但静止图像显示相反的顺序只有我不知道出了什么问题......
输出截图
这是我的代码......
init 方法中的代码
if( (self=[super init])) {
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
if (originalImage!=nil) {
originalImage=nil;
}
self.originalImage=[Utilities scaleAndRotateImage:[UIImage imageNamed:@"2.jpg"]];
texture2D = [[CCTexture2D alloc] initWithImage:originalImage];
NSLog(@"Width %f Height %f",texture2D.contentSize.width,texture2D.contentSize.height);
[self body_init];
self.isTouchEnabled = YES;
/* 绘制下一帧动画。*/
- (void)body_redraw {
int k;
int i, j;
if(mass == NULL) {
NSLog(@"mass is null");
return;
}
glBindTexture(GL_TEXTURE_2D, [texture2D name]);
k = 0;
for (i = 0; i < GRID_SIZE_X - 1; i++)
{
for (j = 0; j < GRID_SIZE_Y - 1; j++)
{
GLfloat vertices[]= {
mass[k].x[0],mass[k].x[1],mass[k].x[2],
mass[k + 1].x[0],mass[k + 1].x[1],mass[k + 1].x[2],
mass[k + GRID_SIZE_Y + 1].x[0],mass[k + GRID_SIZE_Y + 1].x[1],
mass[k + GRID_SIZE_Y + 1].x[2],
mass[k + GRID_SIZE_Y].x[0],mass[k + GRID_SIZE_Y].x[1],
mass[k + GRID_SIZE_Y].x[2]
};
GLfloat tex[]={
mass[k].t[0], mass[k].t[1],
mass[k + 1].t[0], mass[k + 1].t[1],
mass[k + GRID_SIZE_Y + 1].t[0], mass[k + GRID_SIZE_Y + 1].t[1],
mass[k + GRID_SIZE_Y].t[0], mass[k + GRID_SIZE_Y].t[1]
};
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, tex);
glDrawArrays(GL_TRIANGLE_FAN, 0,4);
k++;
}
k++;
}
}
- (void)body_init {
GLint width = texture2D.contentSizeInPixels.width;
GLint height = texture2D.contentSizeInPixels.height;
int i, j;
int k;
if (mass == NULL)
{
mass = (MASS *) malloc(sizeof(MASS)*GRID_SIZE_X*GRID_SIZE_Y);
if (mass == NULL)
{
fprintf(stderr, "body: Can't allocate memory.\n");
exit(-1);
}
}
k = 0;
for (i = 0; i < GRID_SIZE_X; i++)
for (j = 0; j < GRID_SIZE_Y; j++)
{
//this code implements grid on texture2D, gets vertex & side vertex in array
mass[k].nail = (i == 0 || j == 0 || i == GRID_SIZE_X - 1
|| j == GRID_SIZE_Y - 1);//value is 0/1
mass[k].x[0] = i/(GRID_SIZE_X - 1.0)*width;
// NSLog(@"mass[%d].x[0]:: %f",k,mass[k].x[0]);
mass[k].x[1] = j/(GRID_SIZE_Y - 1.0)*height;
// NSLog(@"mass[%d].x[1]:: %f",k,mass[k].x[1]);
mass[k].x[2] = -(CLIP_FAR - CLIP_NEAR)/4.0;
// NSLog(@"mass[%d].x[2]:: %f",k,mass[k].x[2]);
mass[k].v[0] = 0.0;
mass[k].v[1] = 0.0;
mass[k].v[2] = 0.0;
mass[k].t[0] = i/(GRID_SIZE_X - 1.0);
mass[k].t[1] = j/(GRID_SIZE_Y - 1.0);
k++;
}
}
- (void)draw {
glDisableClientState(GL_COLOR_ARRAY);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4ub(224,224,244,200);
[self body_redraw];
glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
glEnableClientState(GL_COLOR_ARRAY);
}