OpenGL 2.0 Добавяне на текстури

И така, аз съм много нов в OpenGL с разработката на iOS и в момента се опитвам да генерирам текстура върху триъгълник (нещо много основно). Някой ще може ли да ми обясни защо моят код по-долу генерира само един цвят от текстурираната картина, която имам, а не текстурата? Трябва ли да генерирам друг буфер за текстурата? Всеки съвет би бил чудесен!

@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);


}

person TheCodingArt    schedule 06.12.2012    source източник


Отговори (1)


Леле, чувствам се тъпо. Случайно въведох грешен метод за активиране на текстури. Трябваше да сложа: glEnableVertexAttribArray(GLKVertexAttribTexCoord0); вместо glEnable(GLKVertexAttribTexCoord0);. Автоматичното завършване винаги ме хваща хаха.

person TheCodingArt    schedule 06.12.2012
comment
Добра идея е винаги да проверявате glGetError, за да улавяте неща като това (предаване на невалидни/неправилни изброявания към gl функции). Можете да дефинирате макрос, който твърди, че резултатът е GL_NO_ERROR в компилации за отстраняване на грешки и да го разпръснете навсякъде във вашия код. - person eodabash; 06.12.2012