aarelovich
aarelovich

Reputation: 5566

GL Triangle in Unity displayed incorrectly in VR Headset

I'm trying to test out how it would be to render an assortment of 2D Shapes in in the screen on an existing unity VR Application.

I started by adding this code onPostRender:

  GL.PushMatrix();
  GL.LoadOrtho();
  GL.Begin(GL.TRIANGLES);
  GL.Color(Color.red); // This doesn't work. 
  GL.Vertex3(0.25f, 0.25f, -50);
  GL.Vertex3(0.5f, 0.75f, -50);
  GL.Vertex3(0.75f, 0.25f, -50);
  GL.End();
  GL.PopMatrix();

By following some examples and documentation. This does draw a triangle in white (the last thing drawn is a white cube that is moving from side to side on the screen) and more or less where I wanted however when I look into the headset I see the triangle as double.

I supposed that this is because some transformation needs to be applied for the right enad left eye rendering, but I have not idea how to do this. Any help would be appreciated.

Upvotes: 0

Views: 71

Answers (1)

aarelovich
aarelovich

Reputation: 5566

I managed to do it. IN case this helps anyone. Here is the working code:

void OnRenderObject(){


    float Z = 1;

    lineMaterial.SetPass(0);
    GL.PushMatrix();
    
    var proj = Matrix4x4.Ortho(0, 1, 0, 1, -1, 100);

    GL.LoadIdentity();      
    GL.MultMatrix(transform.localToWorldMatrix);      
    GL.MultMatrix(Camera.main.projectionMatrix);
    GL.MultMatrix(proj);      

    GL.Begin(GL.TRIANGLES);
    GL.Vertex3(0.25f, 0.25f, Z);
    GL.Vertex3(0.5f, 0.75f, Z);
    GL.Vertex3(0.75f, 0.25f, Z);
    GL.End();
    GL.PopMatrix();                 

}

So the key was to break up the Ortho Loading into two parts which I was able to do thanks to the documentation. I loaded the identity matrix (to start from scratch so to speak) and applied both the local to world transformation matrix and the main.projectionMatrix. lineMaterial was simply created to provide a solid color for the triangle.

And was created just like it is with the same name and everything in the doc here: https://docs.unity3d.com/ScriptReference/GL.html

Upvotes: 1

Related Questions