Jump to content
  • Advertisement
Sign in to follow this  
Oammar

OpenGL Coordinate System problem w/ Ortho & View Matrices and GL.Viewport

This topic is 500 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Edited by m11hut

Share this post


Link to post
Share on other sites
Advertisement

When I change my render code to be just:

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.CreateOrthographicOffCenter(-ratio, ratio, -1f, 1f, -1f, 1f);
m_viewMatrix = Matrix4.LookAt(Vector3.UnitZ, Vector3.Zero, Vector3.UnitY);
m_mvpMatrix  = m_projMatrix * m_viewMatrix;

I do get lines to draw, but the coordinate system makes absolutely no sense. (0,0) appears to be the middle center of the device. I also changed my verts[] to be: (0, 0, 0) and (1,1, 0). Where the (1, 1) seems to go off somewhere to the top right. I don't have any idea why this is not working as expected, and it doesn't make any sense.

The only thing I can think of is the Xamarin.Forms.OpenGLView is doing something behind the scenes.

Share this post


Link to post
Share on other sites

I'm a little confused about the confusion.  So you are saying that when you send down a vertex at ( 0, 0 ) it is showing up in the center of the device and you want it to show up in the lower left of the device ?

Share this post


Link to post
Share on other sites
m_projMatrix = Matrix4.CreateOrthographicOffCenter(-ratio, ratio, -1f, 1f, -1f, 1f);

What this is doing is that it's scaling and moving [left, right] range so that it fits the left to right of you device meaning in the NDC you will have that range normalized to [-1, 1], and it's also moving and scaling [bottom, top] range to fit the bottom and top of the device, normalizing it to [-1, 1] range in NDC which in your case it already is.

As the both ranges are centered at 0, just like the NDC ranges, there is no need for moving, so it's just scaling the ranges to fid the NDC, that's why (0, 0, 0) is at the center of the screen, as it should be, and (1, 1, 0) when normalized to NDC is (1/ratio, 1, 0) which is the reason why it's at the top right.

So there is nothing going wrong, every thing is just working right.

You might want to read further on translations before doing any OGL, this link might be helpful:
http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/

Edited by IYP

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!