そのため、私は iOS 開発で OpenGL を初めて使用し、現在、三角形 (非常に基本的なもの) にテクスチャを生成しようとしています。以下の私のコードが、テクスチャではなく、私が持っているテクスチャ付きの画像から1つの色しか生成しない理由を誰かが説明できますか? テクスチャ用に別のバッファを生成する必要がありますか? どんなアドバイスも素晴らしいでしょう!
@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);
}