geminiCoder
geminiCoder

Reputation: 2906

How to colour individual 3D cubes

Im new to Open Gl and I need to create an app that consists of shapes on the screen. These shapes are to consist of identical cubes. I don't know how I can colour each cube separately in the shader as the coordinates are just passed in as a single attribute via the buffer?

glGenVertexArraysOES(1, &_vertexArray);
glBindVertexArrayOES(_vertexArray);

glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, 1527 * sizeof(GLfloat), arrayOfVerticies, GL_STATIC_DRAW);

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

glBindVertexArrayOES(0);

Shader:

attribute vec4 position;
attribute vec3 normal;

varying lowp vec4 colorVarying;

uniform mat4 modelViewProjectionMatrix;
uniform mat3 normalMatrix;

void main()
{
vec3 eyeNormal = normalize(normalMatrix * normal);
vec3 lightPosition = vec3(0.0, 0.0, 1.0);
vec4 diffuseColor = vec4(0.4, 0.4, 1.0, 1.0);

float nDotVP = max(0.0, dot(eyeNormal, normalize(lightPosition)));

colorVarying = diffuseColor * nDotVP;

gl_Position = modelViewProjectionMatrix * position;
}

Upvotes: 1

Views: 85

Answers (1)

Fabien R
Fabien R

Reputation: 685

You may transmit the colors as an attribute.

More details can be found here.

Upvotes: 1

Related Questions