I’m trying to draw a cube. I have a data structure that keeps all of the relevant vertex data on the cube (position, texture mapping, normals, color) that looks like this:
typedef struct {
GLKVector3 positionCoordinates;
GLKVector2 textureCoordinates;
GLKVector3 normalCoordinates;
GLKVector4 colorCoordinates;
} VertexData;
I define a unit cube centered at the origin with this data:
VertexData modelViewVertices[] = {
//{ {position x, position y, position z}, {texture}, {normalX, normalY, normalZ}, { red, blue, green, alpha} }
{ { 0.5f, -0.5f, -0.5f}, {0.0f, 0.0f}, { 1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f}}, // rightward facing (+X)
{ { 0.5f, 0.5f, -0.5f}, {1.0f, 0.0f}, { 1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f} },
{ { 0.5f, -0.5f, 0.5f}, {0.0f, 1.0f}, { 1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f} },
{ { 0.5f, -0.5f, 0.5f}, {0.0f, 1.0f}, { 1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f} },
{ { 0.5f, 0.5f, -0.5f}, {1.0f, 0.0f}, { 1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f} },
{ { 0.5f, 0.5f, 0.5f}, {1.0f, 1.0f}, { 1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f} },
{ { 0.5f, 0.5f, -0.5f}, {0.0f, 0.0f}, { 0.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f} }, // upward facing (+Y)
{ {-0.5f, 0.5f, -0.5f}, {1.0f, 0.0f}, { 0.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f} },
{ { 0.5f, 0.5f, 0.5f}, {0.0f, 1.0f}, { 0.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f} },
{ { 0.5f, 0.5f, 0.5f}, {0.0f, 1.0f}, { 0.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f} },
{ {-0.5f, 0.5f, -0.5f}, {1.0f, 0.0f}, { 0.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f} },
{ {-0.5f, 0.5f, 0.5f}, {1.0f, 1.0f}, { 0.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f} },
{ {-0.5f, 0.5f, -0.5f}, {0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f} }, // leftward facing (-X)
{ {-0.5f, -0.5f, -0.5f}, {1.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f} },
{ {-0.5f, 0.5f, 0.5f}, {0.0f, 1.0f}, {-1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f} },
{ {-0.5f, 0.5f, 0.5f}, {0.0f, 1.0f}, {-1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f} },
{ {-0.5f, -0.5f, -0.5f}, {1.0f, 0.0f}, {-1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f} },
{ {-0.5f, -0.5f, 0.5f}, {1.0f, 1.0f}, {-1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f} },
{ {-0.5f, -0.5f, -0.5f}, {0.0f, 0.0f}, { 0.0f, -1.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f} }, // downward facing (-Y)
{ { 0.5f, -0.5f, -0.5f}, {1.0f, 0.0f}, { 0.0f, -1.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f} },
{ {-0.5f, -0.5f, 0.5f}, {0.0f, 1.0f}, { 0.0f, -1.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f} },
{ {-0.5f, -0.5f, 0.5f}, {0.0f, 1.0f}, { 0.0f, -1.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f} },
{ { 0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{ 0.0f, 0.0f, -1.0f}, {0.0f, 0.0f, 0.0f, 0.0f} },
{ {-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f}, { 0.0f, 0.0f, -1.0f}, {0.0f, 0.0f, 0.0f, 0.0f} },
};
Then, I go to draw the cube with this code:
- (void)drawCube
{
glGenVertexArraysOES(1, &_vertexArray);
glBindVertexArrayOES(_vertexArray);
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(modelViewVertices), modelViewVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (GLubyte *)0 + offsetof(VertexData, positionCoordinates));
glEnableVertexAttribArray(GLKVertexAttribNormal);
// glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (GLubyte *)0 + offsetof(VertexData, normalCoordinates));
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (const GLvoid*)(5*sizeof(GLfloat)));
glBindVertexArrayOES(0);
}
The cube draws, but the normals seem to not be working. My guess is that my glVertexAttribPointer command is wrong, but I don’t know how to fix it. Can anyone help me to get this to draw correctly. Thanks!
If it helps, here is the vertex shader code:
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;
}