OpenGL es2.0纹理对象的多个实例

时间:2022-09-10 22:54:02

I am using Blender to create a 3D cow and obtain the correct vertices (optimized afterwards to remove duplicates). I have created a Cow Class to be able to draw multiple instances of this cow on screen.

我使用Blender创建了一个3D牛,并获得了正确的顶点(经过优化后删除了副本)。我创建了一个Cow类,以便能够在屏幕上绘制这个Cow的多个实例。

Cow 1 is drawn at (-2,0,-10) and Cow 2 is drawn at (2,0,-10). When I render cow 1 only I see Cow 1 at (-2,0,-10). When I render either both cows or only cow 2 (comment render cow 1 out), I only get cow 2 (do not see cow 1) at (2,0,-10). I am making a game where I will have many enemies running around and I need to be able to draw multiple instances of these objects and render them independently at different locations. Can anyone tell me what I am doing wrong? Thanks

牛1被画在(-2,0,-10)和牛2被画在(2,0,-10)。当我渲染奶牛1时,我只看到奶牛1在(-2,0,-10)处。当我同时渲染奶牛或只渲染奶牛2(注释渲染奶牛1)时,我只得到奶牛2(不看到奶牛1)在(2,0,-10)处。我正在制作一个游戏,在这个游戏中,我将会有很多敌人在我周围奔跑,我需要能够绘制这些对象的多个实例,并在不同的位置独立地呈现它们。有人能告诉我我做错了什么吗?谢谢

//
//  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

1 个解决方案

#1


3  

From the code you gave, you are calling a glClear() every time before you draw the cow. With all those clear calls, only the last cow you draw will be visible.

根据您给出的代码,每次绘制奶牛之前,您都要调用glClear()。所有这些清晰的呼唤,只有你画的最后一头奶牛是可见的。

Move this call into the scene's render function so you only clear the scene once at the start of each frame.

将此调用移动到场景的渲染函数中,因此您只能在每一帧的开始清除一次场景。

#1


3  

From the code you gave, you are calling a glClear() every time before you draw the cow. With all those clear calls, only the last cow you draw will be visible.

根据您给出的代码,每次绘制奶牛之前,您都要调用glClear()。所有这些清晰的呼唤,只有你画的最后一头奶牛是可见的。

Move this call into the scene's render function so you only clear the scene once at the start of each frame.

将此调用移动到场景的渲染函数中,因此您只能在每一帧的开始清除一次场景。