AJ Bertenshaw
AJ Bertenshaw

Reputation: 291

How do I enable colored vertices in OpenGL ES (using GLKit)?

I started learning OpenGL this weekend, and discovered quite a learning curve. Most things I've managed to grapple through, but now I'm stuck...

I have created an array of vertices. Each vertex (vertexT) consists of 3 vectors (position, normal and colour). Each vector (GLKVector3) is a triple of floats (i.e., x,y,z or r,g,b). Since GLKVector3 is defined to be applicable to colors, I am assuming that opengl is happy to work with color values that do not specify a third float (ie alpha)

My function to setup my gl objects looks like this: glBindVertexArrayOES(_vertexArrayObject);

glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexT) * _vertexCount, [_vertexData mutableBytes], GL_STATIC_DRAW);

glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(vertexT), BUFFER_OFFSET(0));

glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(vertexT), BUFFER_OFFSET(sizeof(GLKVector3)));

So far so good. I'm not using the color part of the interleaved array, and the whole object renders as white, using the following calls in my draw function:

glBindVertexArrayOES(_vertexArrayObject);
glDrawElements(GL_TRIANGLES, _triangleCount * 3, GL_UNSIGNED_SHORT, [_triangleData mutableBytes]);

So now I want to set up a per-vertex color for my model, so I added the following:

glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 3, GL_FLOAT, GL_FALSE, sizeof(vertexT), BUFFER_OFFSET(sizeof(GLKVector3)*2));

But it is still white. I managed to find a question on SO that sounded like my problem, but the offered solution was to call glEnable with GL_COLOR_MATERIAL and as far as I can tell, this constant is not valid in OpenGL ES (according to the sdk page at Khronos).

I'm sure it is something simple. But I'm not seeing it. A little help?

Upvotes: 1

Views: 1089

Answers (1)

AJ Bertenshaw
AJ Bertenshaw

Reputation: 291

Eventually found a way to enable color materials in GLKit.

This line does the trick:

self.effect.colorMaterialEnabled = GL_TRUE;

Upvotes: 2

Related Questions