Hello, I've been in the process of getting my OpenGL ES 2.0 application setup to render correctly. I'm currently just trying to draw simple lines to verify that my coordinate system is setup correctly. I'm also using Xamarin w/ their provided OpenGLView that provides a cross platform opengl context with OpenTK.
However, once I introduced GL.Viewport, a orthographic projection matrix and a view matrix I am unable to get any simple lines to display. Before I introduced these pieces I was able to get lines to display, however they weren't at the desired coordinates, which is what led me to using GL.Viewport and the matrices. I've tried setting my view matrix to look down the positive or negative z-axis, nixed the view matrix all together, different multiplication ordering for the mvpMatrix, as well as removing the use of GL.Viewport. Also, different z-values for the verts of the lines. I am doing something wrong, and I am not sure what.
This is my OpenGL initialization code, w/ my ShaderProgram setup for my verts buffer object and model view projection matrix:
Matrix4 m_projMatrix, m_viewMatrix, m_mvpMatrix;
int m_vpWidth, m_vpHeight;
// Vertex Buffer Data
float[] verts = { 0, 0, 1,
100, 100, 1};
private void InitOpengl(int width, int height)
{
GL.Enable(EnableCap.DepthTest);
m_vpWidth = width;
m_vpHeight = height;
float ratio = (float)width / height;
m_projMatrix = Matrix4.CreateOrthographic(m_vpWidth, m_vpHeight, 1f, 100f);
m_viewMatrix = Matrix4.LookAt(Vector3.UnitZ, Vector3.Zero, Vector3.UnitY);
m_mvpMatrix = m_projMatrix * m_viewMatrix;// Matrix4.Mult(m_viewMatrix, m_projMatrix);
//m_mvpMatrix = Matrix4.Mult(m_mvpMatrix, Matrix4.CreateRotationY(0));
m_shaderPrograms = new ArrayList();
string vertexLineShaderSrc =
@"attribute vec3 vertex_position;
uniform mat4 mvp;
void main()
{
gl_Position = mvp * vec4(vertex_position, 1.0);
}";
string fragLineShaderSrc =
@"void main()
{
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
}";
ShaderProgram lineShader = ShaderProgram.CreateShaderProgram(vertexLineShaderSrc, fragLineShaderSrc, () => { GL.DrawArrays(BeginMode.Lines, 0, verts.Length); });
if (lineShader != null)
{
lineShader.AddAttributeLocationData(new AttributeLocationData(lineShader.ShaderProgramId,
"vertex_position",
() =>
{
int vertexPosAttrib = GL.GetAttribLocation(lineShader.ShaderProgramId, "vertex_position");
GL.EnableVertexAttribArray(vertexPosAttrib);
GL.VertexAttribPointer(vertexPosAttrib, 3, VertexAttribPointerType.Float, false, 0, verts);
}));
lineShader.AddUniformLocationData(new UniformLocationData(lineShader.ShaderProgramId,
"mvp",
() =>
{
int mvpUniform = GL.GetUniformLocation(lineShader.ShaderProgramId, "mvp");
GL.UniformMatrix4(mvpUniform, false, ref m_mvpMatrix);
}));
m_shaderPrograms.Add(lineShader);
}
}
This is my OpenGLView initialization and render code:
m_ogl = new OpenGLView();
m_ogl.HeightRequest = Forms.Context.Resources.DisplayMetrics.HeightPixels;
m_ogl.WidthRequest = Forms.Context.Resources.DisplayMetrics.WidthPixels;
m_ogl.HasRenderLoop = true;
m_ogl.OnDisplay = (r) =>
{
if (!m_init)
{
InitOpengl((int)m_ogl.HeightRequest, (int)m_ogl.WidthRequest);
m_init = true;
}
GL.ClearColor(System.Drawing.Color.CornflowerBlue);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.Viewport(0, 0, m_vpWidth, m_vpHeight);
float ratio = (float)m_vpWidth / m_vpHeight;
m_projMatrix = Matrix4.CreateOrthographic(m_vpWidth, m_vpHeight, 0, 100);
m_viewMatrix = Matrix4.LookAt(Vector3.UnitZ, Vector3.Zero, Vector3.UnitY);
m_mvpMatrix = m_projMatrix * m_viewMatrix;
//Vector4 value = Vector4.Transform(new Vector4(0, 0, 0, 1), m_mvpMatrix);
foreach (ShaderProgram shader in m_shaderPrograms)
{
GL.UseProgram(shader.ShaderProgramId);
shader.CallAttributeLocationDataFuncs();
shader.CallUniformLocationDataFuncs();
shader.ShaderUseFunc();
GL.Finish();
}
};
All I want to do is draw a line from the bottom left of my screen to somewhere in the top right, which is correctly projected into NDC [-1,-1] -> [1,1]
Thank you very much for any insight.