Iphone OpenGL ES 2.0纹理对象的多个实例

Iphone OpenGL ES 2.0纹理对象的多个实例,iphone,opengl-es,opengl-es-2.0,xcode4.6,Iphone,Opengl Es,Opengl Es 2.0,Xcode4.6,我正在使用Blender创建一个3D cow并获得正确的顶点(随后进行优化以删除重复项)。我创建了一个Cow类,可以在屏幕上绘制此Cow的多个实例 奶牛1在(-2,0,-10)处绘制,奶牛2在(2,0,-10)处绘制。当我仅渲染cow 1时,我看到cow 1位于(-2,0,-10)。当我渲染两个cow或仅渲染cow 2(注释render cow 1 out)时,我只在(2,0,-10)处得到cow 2(看不到cow 1)。我正在做一个游戏,我将有许多敌人在我周围运行,我需要能够绘制这些对象的多个

我正在使用Blender创建一个3D cow并获得正确的顶点(随后进行优化以删除重复项)。我创建了一个Cow类,可以在屏幕上绘制此Cow的多个实例

奶牛1在(-2,0,-10)处绘制,奶牛2在(2,0,-10)处绘制。当我仅渲染cow 1时,我看到cow 1位于(-2,0,-10)。当我渲染两个cow或仅渲染cow 2(注释render cow 1 out)时,我只在(2,0,-10)处得到cow 2(看不到cow 1)。我正在做一个游戏,我将有许多敌人在我周围运行,我需要能够绘制这些对象的多个实例,并在不同的位置独立渲染它们。谁能告诉我我做错了什么?谢谢

//
//  Cow.m
//

#import "Cow.h"
#import "cow_verts.h"

@implementation Cow

- (id)initWithContext:(EAGLContext *)aContext {

    context = aContext;

    effect = [[GLKBaseEffect alloc] init];

    [EAGLContext setCurrentContext:context];

    sharedResourceManager = [ResourceManager sharedResourceManager];

    UIImage *textureImage = [UIImage imageNamed:@"cowUV.png"];
    texture = [sharedResourceManager addTexture:textureImage];

    position = GLKVector3Make(0,0,0);
    rotation = GLKVector3Make(0,0,0);
    scale    = GLKVector3Make(1,1,1);

    [self setupGL];

    return self;
}

- (void)setupGL {

    glGenVertexArraysOES(1, &_vao);
    glBindVertexArrayOES(_vao);

    glGenBuffers(1, &_dynamicVBO);
    glBindBuffer(GL_ARRAY_BUFFER, _dynamicVBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(CowDynamicVertexData), CowDynamicVertexData, GL_DYNAMIC_DRAW);

    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(dynamicVertexData), (void *)offsetof(dynamicVertexData, position));
    glEnableVertexAttribArray(GLKVertexAttribPosition);

    glGenBuffers(1, &_staticVBO);
    glBindBuffer(GL_ARRAY_BUFFER, _staticVBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(CowStaticVertexData), CowStaticVertexData, GL_STATIC_DRAW);

    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(staticVertexData), (void *)offsetof(staticVertexData, texCoord));
    glEnableVertexAttribArray(GLKVertexAttribTexCoord0);

    glGenBuffers(1, &_indexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(CowIndices), CowIndices, GL_STATIC_DRAW);

    glBindVertexArrayOES(0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

}

- (void)updateWithDelta:(float)aDelta {

    float aspect = fabsf([[UIScreen mainScreen] bounds].size.width/ [[UIScreen mainScreen] bounds].size.height);
    GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 4.0f, 20.0f);
    effect.transform.projectionMatrix = projectionMatrix;

    rotation.y += 90 * aDelta;

}

- (void)render {

    [EAGLContext setCurrentContext:context];

    glClearColor(0, 1, 1, 1);
    glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

    GLKMatrix4 xRotationMatrix = GLKMatrix4MakeXRotation(GLKMathDegreesToRadians(rotation.x));
    GLKMatrix4 yRotationMatrix = GLKMatrix4MakeYRotation(GLKMathDegreesToRadians(rotation.y));
    GLKMatrix4 zRotationMatrix = GLKMatrix4MakeZRotation(GLKMathDegreesToRadians(rotation.z));
    GLKMatrix4 scaleMatrix     = GLKMatrix4MakeScale(scale.x, scale.y, scale.z);
    GLKMatrix4 translateMatrix = GLKMatrix4MakeTranslation(position.x, position.y, position.z);

    GLKMatrix4 modelMatrix =    GLKMatrix4Multiply(translateMatrix,
                            GLKMatrix4Multiply(scaleMatrix,
                            GLKMatrix4Multiply(zRotationMatrix,
                            GLKMatrix4Multiply(yRotationMatrix,
                            xRotationMatrix))));

    effect.transform.modelviewMatrix = modelMatrix;

    //GLKMatrix4 viewMatrix = GLKMatrix4MakeLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
    //effect.transform.modelviewMatrix = GLKMatrix4Multiply(viewMatrix, modelMatrix);

    effect.texture2d0.name = texture.name;
    effect.texture2d0.target = texture.target;
    effect.texture2d0.envMode = GLKTextureEnvModeReplace;

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_FRONT);

    glBindVertexArrayOES(_vao);

    [effect prepareToDraw];

    glDrawElements(GL_TRIANGLES, sizeof(CowIndices)/sizeof(CowIndices[0]), GL_UNSIGNED_INT, (void *)0);

    glBindVertexArrayOES(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);


    glDisable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);

}

@end


//
// Cow.h
//

@interface Cow : NSObject {

    EAGLContext *context;

    GLuint _vao;
    GLuint _staticVBO;
    GLuint _dynamicVBO;
    GLuint _indexBuffer;
    GLKTextureInfo *texture;
    GLKBaseEffect *effect;

    GLKVector3 position;
    GLKVector3 rotation;
    GLKVector3 scale;

    ResourceManager *sharedResourceManager;

}

- (id)initWithContext:(EAGLContext *)aContext;
- (void)updateWithDelta:(float)aDelta;
- (void)render;


@end


// 
// Scene which creates these objects and calls the functions
//

#import "Cow.h"

@implementation GameScene {

    NSMutableArray *cows;

}

- (id)init {

    if(self = [super init]) {

        sharedDirector = [Director sharedDirector];

        [EAGLContext setCurrentContext:[sharedDirector context]];

        cows = [[NSMutableArray alloc] init];

        AbstractEnemy *cow1 = [[Cow alloc] initWithContext:[sharedDirector context]];
        cow1.position = GLKVector3Make(-2, 0, -10);
        [cows addObject:cow1];

        AbstractEnemy *cow2 = [[Cow alloc] initWithContext:[sharedDirector context]];
        cow2.position = GLKVector3Make(2, 0, -10);
        [cows addObject:cow2];

        AbstractEnemy *cow3 = [[Cow alloc] initWithContext:[sharedDirector context]];
        cow3.position = GLKVector3Make(0, 0, -15);
        [cows addObject:cow3];
    }
    return self;
} 

- (void)updateWithDelta:(GLfloat)aDelta {

    for (int i = 0; i < cows.count; i++)
    {
        [cows[i] updateWithDelta:aDelta];
    }

}

- (void)render {
    for (int i = 0; i < cows.count; i++)
    {
        [cows[i] render];
    }
}

@end
//
//奶牛
//
#进口“Cow.h”
#导入“cow_verts.h”
@实施奶牛
-(id)initWithContext:(EAGLContext*)aContext{
上下文=文本;
效应=[[glkbaseffect alloc]init];
[EagleContext setCurrentContext:context];
sharedResourceManager=[ResourceManager sharedResourceManager];
UIImage*textureImage=[UIImage ImageName:@“cowUV.png”];
纹理=[sharedResourceManager addTexture:textureImage];
位置=GLKVector3Make(0,0,0);
旋转=GLKVector3Make(0,0,0);
比例=GLKVector3Make(1,1,1);
[自设置GL];
回归自我;
}
-(无效)设置GL{
glGenVertexArraysOES(1和U vao);
glbindvertexarayes(_vao);
glGenBuffers(1,&u dynamicVBO);
glBindBuffer(GL_数组_BUFFER,_dynamicVBO);
glBufferData(GL_数组_缓冲区、sizeof(CowDynamicVertexData)、CowDynamicVertexData、GL_动态_绘图);
glvertexattributepointer(glkVertexAttributePosition,3,GL_FLOAT,GL_FALSE,sizeof(dynamicVertexData),(void*)offset of(dynamicVertexData,position));
GlenableVertexAttributeArray(GLkVertexAttributePosition);
glGenBuffers(1,&u staticVBO);
glBindBuffer(GL_数组_BUFFER,_staticVBO);
glBufferData(GL_数组_缓冲区、sizeof(CowStaticVertexData)、CowStaticVertexData、GL_静态_绘图);
glVertexAttributePointer(glkVertexAttributeCoord0,2,GL_FLOAT,GL_FALSE,sizeof(staticVertexData),(void*)offsetof(staticVertexData,texCoord));
GlenableVertexAttributeArray(GlkVertexAttributeCoord0);
glGenBuffers(1,&u indexBuffer);
glBindBuffer(GL_元素_数组_BUFFER,_indexBuffer);
glBufferData(GL元素\数组\缓冲区、sizeof(CowIndex)、CowIndex、GL静态\绘图);
glBindVertexArrayOES(0);
glBindBuffer(GL_元素_数组_缓冲区,0);
glBindBuffer(GL_数组_BUFFER,0);
}
-(void)updateWithDelta:(float)aDelta{
浮动纵横比=fabsf([[UIScreen mainScreen]界限].size.width/[UIScreen mainScreen]界限].size.height);
GLKMatrix4 projectionMatrix=GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f),纵横比,4.0f,20.0f);
effect.transform.projectionMatrix=projectionMatrix;
旋转y+=90*aDelta;
}
-(无效)渲染{
[EagleContext setCurrentContext:context];
glClearColor(0,1,1,1);
glClear(GL_深度_缓冲_位| GL_颜色_缓冲_位);
GLKMatrix4 xRotationMatrix=GLKMatrix4MakeXRotation(GLKMathDegreesToRadians(rotation.x));
GLKMatrix4旋转矩阵=GLKmatrix4MakeRotation(GLKMathDegreesToRadians(rotation.y));
GLKMatrix4 zRotationMatrix=GLKMatrix4MakeZRotation(GLKMathDegreesToRadians(rotation.z));
GLKMatrix4 scaleMatrix=GLKMatrix4MakeScale(scale.x,scale.y,scale.z);
GLKMatrix4 translateMatrix=GLKmatrix4MakeTransation(位置.x,位置.y,位置.z);
GLKMatrix4 modelMatrix=GLKMatrix4Multiply(translateMatrix,
GLKMATRIX4乘法(比例矩阵,
GLKmatrix4乘法(zRotationMatrix,
GLKMATRIX4乘法(旋转矩阵,
x旋转矩阵);
effect.transform.modelviewMatrix=modelMatrix;
//GLKMatrix4 viewMatrix=GLKMatrix4MakeLookAt(0,0,3,0,0,0,1,0);
//effect.transform.modelviewMatrix=GLKMatrix4Multiply(viewMatrix,modelMatrix);
effect.texture2d0.name=texture.name;
effect.texture2d0.target=texture.target;
effect.texture2d0.envMode=GLKTextureEnvModeReplace;
glEnable(GLU深度试验);
glEnable(GL_CULL_面);
GLU正面(GL_正面);
glbindvertexarayes(_vao);
[效果图];
glpaurements(GL_三角形,sizeof(cowIndexes)/sizeof(cowIndexes[0]),GL_UNSIGNED_INT,(void*)0;
glBindVertexArrayOES(0);
glBindBuffer(GL_数组_BUFFER,0);
glBindBuffer(GL_元素_数组_缓冲区,0);
glDisable(GLU深度测试);
glDisable(GLU消隐面);
}
@结束
//
//母牛
//
@接口:NSObject{
EAGLContext*上下文;
GLuint_vao;
GLuint VBO;
GLuint _dynamicVBO;
胶合指数缓冲;
GLKTextureInfo*纹理;
GLKBASE效应*效应;
GLKVector3位置;
GLKVector3旋转;
GLKVector3量表;
ResourceManager*sharedResourceManager;
}
-(id)initWithContext:(EAGLContext*)一个文本;
-(void)updateWithDelta:(float)aDelta;
-(无效)作出;
@结束
// 
//创建这些对象并调用函数的场景
//
#进口“Cow.h”
@实现游戏场景{
NSMutableArray*奶牛;
}
-(id)init{
if(self=[super init]){
sharedDirector=[董事sharedDirector];
[EagleContext setCurrentContext:[sharedDirector context]];
cows=[[NSMutableArray alloc]init];
AbstractForeign*cow1=[[Cow alloc]initWithContext:[sharedDirector context]];
cow1.position=GLKVector3Make(-2,0,-10);
[对象:cow1];
AbstractForeign*cow2=[[Cow alloc]initWithContext:[sharedDirector context]];
cow2.position=GLKVector3Make(2,0,-10);
[对象:cow2];
AbstractForeign*cow3=[[Cow alloc]initWithContext:[sharedDirector context]];
cow3.position=GLKVector3Make(0,0,-15);
[对象:cow3];
}
回归自我;
} 
-(void)updateWithDelta:(GLfloat)aDelta{
for(int)