View Full Version : Creating a rectangle with a specific color

11-02-2013, 06:51 PM
Very simple objective: create a rectangle with a single color (no textures). I'm using OpenGL ES 2.0 in iOS 6 (which I'm very new at). I can draw the rectangle easily enough, but the color isn't working. It's being interpolated from differently colored vertices, but my intention is that all of the vertices should have the same color. Can anyone help me get this rectangle to be a solid color (blue, in this case)? I'm guessing that my offsets are not defined correctly but I have absolutely no idea how to correct them. Thank you!

By the way, I'd like to post a screen shot of the rectangle, but I don't know how to. The Image icon wants a URL. If someone can explain this to me, I'm happy to post it.

Here is the relevant code:

typedef struct {
GLKVector3 positionCoordinates;
GLKVector2 textureCoordinates;
GLKVector3 normalCoordinates;
GLKVector4 colorCoordinates;
} VertexData;

VertexData unitSquare[] = {
{ {-0.5f, -0.5f, 0.0f}, {0.0f, 0.0f}, { 0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 1.0f, 1.0f} }, // 2D - forward facing only
{ { 0.5f, -0.5f, 0.0f}, {0.0f, 0.0f}, { 0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 1.0f, 1.0f} },
{ {-0.5f, 0.5f, 0.0f}, {0.0f, 0.0f}, { 0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 1.0f, 1.0f} },
{ {-0.5f, 0.5f, 0.0f}, {0.0f, 0.0f}, { 0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 1.0f, 1.0f} },
{ { 0.5f, -0.5f, 0.0f}, {0.0f, 0.0f}, { 0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 1.0f, 1.0f} },
{ { 0.5f, 0.5f, 0.0f}, {0.0f, 0.0f}, { 0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 1.0f, 1.0f} }

-(void) viewDidLoad {
// OES is for vertex array objects (not vertex buffer objects)
glGenVertexArraysOES(1, &_vertexArraySquare);

glGenBuffers(1, &_vertexBufferSquare);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferSquare);
glBufferData(GL_ARRAY_BUFFER, sizeof(unitSquare), unitSquare, GL_STATIC_DRAW);

glEnableVertexAttribArray(GLKVertexAttribPosition) ;
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (GLubyte *)0 + offsetof(VertexData, positionCoordinates));
glEnableVertexAttribArray(GLKVertexAttribPosition) ;
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (GLubyte *)0 + offsetof(VertexData, normalCoordinates));
glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(GLKVector4), (GLubyte *)0 + offsetof(VertexData, colorCoordinates));

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
glClearColor(0.65f, 0.65f, 0.65f, 1.0f);

aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height); //1024 x 768

projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians( 65.0f), aspect, 0.1f, 100.0f);
self.effect.transform.projectionMatrix = projectionMatrix;

// Draw the upper left background
self.effect.transform.modelviewMatrix = modelViewMatrix;
modelViewMatrix = GLKMatrix4MakeTranslation(-1.5f, 1.25f, -4.0f);
modelViewMatrix = GLKMatrix4Scale(modelViewMatrix, 2.75, 2, 0);

_normalMatrix = GLKMatrix3InvertAndTranspose(GLKMatrix4GetMatrix3( modelViewMatrix), NULL);
_modelViewProjectionMatrix = GLKMatrix4Multiply(projectionMatrix, modelViewMatrix);

glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], 1, 0, _modelViewProjectionMatrix.m);
glUniformMatrix3fv(uniforms[UNIFORM_NORMAL_MATRIX], 1, 0, _normalMatrix.m);



[self.effect prepareToDraw];
glDrawArrays(GL_TRIANGLES, 0, 6);

in my loadShaders function I have these lines:

glBindAttribLocation(_program, GLKVertexAttribPosition, "position");
glBindAttribLocation(_program, GLKVertexAttribNormal, "normal");
glBindAttribLocation(_program, GLKVertexAttribColor, "color");

Here is my vertex shader file:

attribute vec4 position;
attribute vec3 normal;
attribute vec4 color;

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(1.0, 0.0, 0.0, 1.0);

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

// colorVarying = diffuseColor * nDotVP;

colorVarying = color;

gl_Position = modelViewProjectionMatrix * position;

And finally the fragment shader file:

varying lowp vec4 colorVarying;

void main()
gl_FragColor = colorVarying;

11-02-2013, 06:54 PM
I'm a moron. The instant I hit submit, I noticed that my sizeof argument was wrong. Sorry to trouble anyone. It's fixed.