頂点の位置、色、インデックスデータを分離するために3つのバッファを作成しました。
頂点は正しく正方形としてレンダリングされますが、配列で定義された色ではなく白になりdynamicVertexData
ます。
私はOpenGLES2.0を使用していますが、一般的なOpenGLの間違いを犯していると思います。
誰かがそれを見つけることができますか?
typedef struct _vertexStatic
{
GLfloat position[2];
} vertexStatic;
typedef struct _vertexDynamic
{
GLubyte color[4];
} vertexDynamic;
enum {
ATTRIB_POSITION,
ATTRIB_COLOR,
NUM_ATTRIBUTES
};
// Separate buffers for static and dynamic data.
GLuint staticBuffer;
GLuint dynamicBuffer;
GLuint indexBuffer;
const vertexStatic staticVertexData[] = {
{0, 0},
{50, 0},
{50, 50},
{0, 50},
};
vertexDynamic dynamicVertexData[] = {
{0, 0, 255, 255},
{0, 0, 255, 255},
{0, 0, 255, 255},
{0, 0, 255, 255},
};
const GLubyte indices[] = {
0, 1, 2,
2, 3, 0,
};
- (void)setupGL {
CGSize screenSize = [UIApplication currentSize];
CGSize screenSizeHalved = CGSizeMake(screenSize.width/2, screenSize.height/2);
numIndices = sizeof(indices)/sizeof(indices[0]);
[EAGLContext setCurrentContext:self.context];
glEnable(GL_CULL_FACE); // Improves perfromance
self.effect = [[GLKBaseEffect alloc] init];
// The near and far plane are measured in units from the eye
self.effect.transform.projectionMatrix = GLKMatrix4MakeOrtho(-screenSizeHalved.width,
screenSizeHalved.width,
-screenSizeHalved.height,
screenSizeHalved.height,
0.0f, 1.0f);
self.preferredFramesPerSecond = 30;
CreateBuffers();
}
void CreateBuffers()
{
// Static position data
glGenBuffers(1, &staticBuffer);
glBindBuffer(GL_ARRAY_BUFFER, staticBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(staticVertexData), staticVertexData, GL_STATIC_DRAW);
// Dynamic color data
// While not shown here, the expectation is that the data in this buffer changes between frames.
glGenBuffers(1, &dynamicBuffer);
glBindBuffer(GL_ARRAY_BUFFER, dynamicBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(dynamicVertexData), dynamicVertexData, GL_DYNAMIC_DRAW);
// Static index data
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
}
void DrawModelUsingMultipleVertexBuffers()
{
glBindBuffer(GL_ARRAY_BUFFER, staticBuffer);
glVertexAttribPointer(ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(vertexStatic), 0);
glEnableVertexAttribArray(ATTRIB_POSITION);
glBindBuffer(GL_ARRAY_BUFFER, dynamicBuffer);
glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(vertexDynamic), 0);
glEnableVertexAttribArray(ATTRIB_COLOR);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glDrawElements(GL_TRIANGLES, sizeof(indices)/sizeof(GLubyte), GL_UNSIGNED_BYTE, (void*)0);
}
- (void)tearDownGL {
[EAGLContext setCurrentContext:self.context];
glDeleteBuffers(1, &_vertexBuffer);
glDeleteBuffers(1, &_indexBuffer);
//glDeleteVertexArraysOES(1, &_vertexArray);
self.effect = nil;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
if (!self.context) {
NSLog(@"Failed to create ES context");
}
GLKView *view = (GLKView *)self.view;
view.context = self.context;
// view.drawableMultisample = GLKViewDrawableMultisample4X; // Smoothes jagged lines. More processing/memory
view.drawableColorFormat = GLKViewDrawableColorFormatRGB565; // Lower colour range. Less processing/memory
[self setupGL];
}
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
[self.effect prepareToDraw];
DrawModelUsingMultipleVertexBuffers();
}
@end