0

所以,我对使用 iOS 开发的 OpenGL 非常陌生,我目前正在尝试在三角形上生成纹理(非常基本的东西)。谁能向我解释为什么我下面的代码只从我拥有的纹理图片而不是纹理中生成一种颜色?我必须为纹理生成另一个缓冲区吗?任何建议都会很棒!

@synthesize baseEffect;

typedef struct {
    GLKVector3 positionCoords;
    GLKVector2 textureCoords;
}
SceneVertex;

static const SceneVertex vertices[]={
    {{-0.5f, -0.5f, 0.0f}, {0.0f, 0.0f}}, // lower left corner
    {{ 0.5f, -0.5f, 0.0f}, {1.0f, 0.0f}}, // lower right corner
    {{-0.5f,  0.5f, 0.0f}, {0.0f, 1.0f}}  // upper left corner
};



- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    GLKView *view = (GLKView *)self.view;

    NSAssert([view isKindOfClass:[GLKView class]], @"View controller's view is not a GLKView");

    [view setContext:[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]];
    [EAGLContext setCurrentContext:[view context]];

    [self setBaseEffect:[[GLKBaseEffect alloc] init]];
    [[self baseEffect] setUseConstantColor:GL_TRUE];
    [[self baseEffect] setConstantColor:GLKVector4Make(1.0f, 1.0f, 1.0f, 1.0f)];
    glClearColor(0.0f, 0.0f, 0.0, 1.0f);

    glGenBuffers(1, &vertexBufferID); //pass vertex address to generate a buffer ID to it
    glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    //glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), offsetof(SceneVertex, positionCoords), GL_STATIC_DRAW);
    //NSLog(@"%lul, %p",sizeof(vertices), vertices );

#pragma mark - Implementing Generating Texture Code Image here -


    //CGImageRef imageRef = [[UIImage imageNamed:@"leaves.gif"] CGImage];

    NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES],GLKTextureLoaderOriginBottomLeft, nil];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"leaves" ofType:@"gif"];

    //GLKTextureInfo *textureInfo = [GLKTextureLoader textureWithCGImage:imageRef options:nil error:NULL];
    NSError * error;
    GLKTextureInfo * textureInfo = [GLKTextureLoader textureWithContentsOfFile:path options:options error:&error];
    if (textureInfo == nil) {
        NSLog(@"Error loading file: %@", [error localizedDescription]);
    }
    self.baseEffect.texture2d0.name = textureInfo.name;
    self.baseEffect.texture2d0.target = textureInfo.target;



}


-(void)glkView:(GLKView *)view drawInRect:(CGRect)rect{


    [[self baseEffect] prepareToDraw];
    glClear(GL_COLOR_BUFFER_BIT);


    glEnableVertexAttribArray(GLKVertexAttribPosition);
    //glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(SceneVertex), NULL);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(SceneVertex), (const GLvoid *) offsetof(SceneVertex, positionCoords));
    #pragma mark - Implementing Drawing Texture Code Image here -
    glEnable(GLKVertexAttribTexCoord0);
    //glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, offsetof(SceneVertex, textureCoords), NULL);

    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(SceneVertex), (const GLvoid *) offsetof(SceneVertex, textureCoords));

    NSLog(@"%lu, %lu, %lu",offsetof(SceneVertex, positionCoords), offsetof(SceneVertex, textureCoords), sizeof(SceneVertex));

    glDrawArrays(GL_TRIANGLES, 0, 3);

    NSLog(@"%lul", sizeof(vertices)/sizeof(SceneVertex));
    //glDrawElements(GL_TRIANGLES, sizeof(vertices)/sizeof(SceneVertex), GL_UNSIGNED_BYTE, 0);


}
4

1 回答 1

0

哇,我觉得很傻。我不小心输入了错误的方法来启用纹理。我应该放:glEnableVertexAttribArray(GLKVertexAttribTexCoord0);而不是glEnable(GLKVertexAttribTexCoord0);. 自动完成总是让我哈哈大笑。

于 2012-12-06T17:10:58.007 回答