I am having a hard time trying to draw simple lines through an OpenGL ES 2.0 shader.
I am trying to use glDrawArrays with GL_LINES, but so far i’ve been unsuccessful.
My vertex shader is the most basic you can get:
attribute vec4 Position;
attribute vec4 SourceColor;
uniform mat4 Projection;
uniform mat4 ModelView;
varying vec4 DestinationColor;
void main(void) {
DestinationColor = SourceColor;
gl_Position = Projection * ModelView * Position;
}
And the fragment shader:
varying lowp vec4 DestinationColor;
void main(void) {
gl_FragColor = DestinationColor;
}
I have some doubts:
- How do you setup the Origin and Destination points coordinates?
- How to setup the Projection and ModelView matrices in this case? With a square, the ModelView matrix will hold the its Center coordinate, but what happens in the case of a Line?
Does anyone have an example?
EDIT:
Ok, i have the following code:
glLineVertices[0].Position[0] = origin.x;
glLineVertices[0].Position[1] = origin.y;
glLineVertices[0].Position[2] = 0;
glLineVertices[0].Color[0] = 1.0f;
glLineVertices[0].Color[1] = 0.0f;
glLineVertices[0].Color[2] = 0.0f;
glLineVertices[0].Color[3] = 0.0f;
glLineVertices[0].Normal[0] = 0.0f;
glLineVertices[0].Normal[1] = 0.0f;
glLineVertices[0].Normal[2] = 1.0f;
glLineVertices[1].Position[0] = end.x;
glLineVertices[1].Position[1] = end.y;
glLineVertices[1].Position[2] = 0;
glLineVertices[1].Color[0] = 1.0f;
glLineVertices[1].Color[1] = 0.0f;
glLineVertices[1].Color[2] = 0.0f;
glLineVertices[1].Color[3] = 0.0f;
glLineVertices[1].Normal[0] = 0.0f;
glLineVertices[1].Normal[1] = 0.0f;
glLineVertices[1].Normal[2] = 1.0f;
glGenVertexArraysOES(1, &vertexArray);
glBindVertexArrayOES(vertexArray);
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, vertexStructureSize, glLineVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(shaderManager.currentAttributeVertexPosition);
glVertexAttribPointer(shaderManager.currentAttributeVertexPosition, 3, GL_FLOAT, GL_FALSE, sizeof(glLineVertex), 0);
glEnableVertexAttribArray(shaderManager.currentAttributeSourceColor);
glVertexAttribPointer(shaderManager.currentAttributeSourceColor, 4, GL_FLOAT, GL_FALSE, sizeof(glLineVertex), (char *)12);
glEnableVertexAttribArray(shaderManager.currentAttributeVertexNormal);
glVertexAttribPointer(shaderManager.currentAttributeVertexNormal, 3, GL_FLOAT, GL_FALSE, sizeof(glLineVertex), (char *)28);
With a render method:
- (void)render
{
//It might as well be the Identity Matrix, nothing happens either way.
//modelViewMatrix = GLKMatrix4Identity;
modelViewMatrix = GLKMatrix4MakeTranslation(0, 0, 0.0f);
glUniformMatrix4fv(shaderManager.currentUniformModelViewMatrix, 1, 0, modelViewMatrix.m);
glBindVertexArrayOES(vertexArray);
glDrawArrays(GL_LINES, 0, size);
}
And with this projection matrix:
_projectionMatrix = GLKMatrix4MakeOrtho(0, self.view.bounds.size.width, 0, self.view.bounds.size.height, -1.0f, 1.0f);
[shaderManager useShaderProgram:@"LineShader"];
glUniformMatrix4fv([shaderManager getUniform:@"Projection" ofShader:@"LineShader"], 1, 0, _projectionMatrix.m);
As soon as i use:
[lineDraw render];
Nothing happens.
Anyone knows why?
I have finally figured it out.
These simple lines:
were causing the issue, because i temporarily disabled the use of normals in the shader, commenting out the correspondent attribute. Didn’t know that just by doing that, no draw would occur, and neither getError() would throw any error out.
I will consider the above answer as correct, though, because it helped me clarify the coordinates and correspondent matrices calculations. In this example, plain 2D lines, one doesn’t need any special modelViewMatrix, it may stay as “GLKMatrix4Identity”.
Thanks again 🙂