Search Unity

How Can I Render Using Gl From Another Perspective? Help!

Discussion in 'General Graphics' started by FranFndz, Apr 10, 2019.

  1. FranFndz

    FranFndz

    Joined:
    Sep 20, 2018
    Posts:
    178
    Hi!

    Im trying for some days but in not able to do it.
    The idea is having a script on my Main Camera that Render using GL a mesh.
    But I don't want the mesh to be rendered using the main camera perspective. I want it to be renderer using another view. Also, I want to use only one camera.

    I have tried a lot of codes but the mesh is always using the main camera.

    The only code that is working is this one , but I consider a cheating code.

    ---- This code work because I'm doing Camera.SetupCurrent and passing another camera as my point of view, the camera im passing as property is not enable (is just copying the matrix I guess).

    Camera.SetupCurrent(_cam);
    GL.Clear(false, true, Color.black);
    mat.SetPass(0);
    GL.PushMatrix();
    Graphics.DrawMeshNow(mesh, obj.transform.localToWorldMatrix);
    GL.End();
    GL.PopMatrix();

    The upcode work as planned but I'm using a disable camera, my idea is to do the same but not passing a camera.

    I tried:
    GL.PushMatrix();
    // None of those code work, the DrawMesh always render the mesh using the main camera view
    GL.LoadProjectionMatrix(projMtrx);
    GL.modelview = projMtrx;
    GL.MultMatrix(projMtrx);
    proj = GL.GetGPUProjectionMatrix(projMtrx, false);
    //
    Graphics.DrawMeshNow(mesh, obj.transform.localToWorldMatrix);
    GL.End();
    GL.PopMatrix();


    What im doing wrong?
    How can I render the Mesh using other matrix or point of view?

    I know is a difficult question, I hope some one can answer me :D
     
    mimimiprod likes this.
  2. mimimiprod

    mimimiprod

    Joined:
    Oct 2, 2014
    Posts:
    17
    Hi, did you solve this problem by any chance? I'm kind of stuck at the same point
     
  3. FranFndz

    FranFndz

    Joined:
    Sep 20, 2018
    Posts:
    178
    actually I did, but as i needed to render skinned mesh render I stop using draw gl.
    I will check the code again.

    by the way this is the final image. (Soft Shadow for mobile, is really fast! CPU around 2 MS faster, GPU 0.4 MS faster, Memory only 64kb)


     
  4. mimimiprod

    mimimiprod

    Joined:
    Oct 2, 2014
    Posts:
    17
    That looks really cool :) What did you end up using? Commandbuffers? It would be really nice if you could look up how you got this working with GL back than.
    When I look into my FrameDebugger the unity_MatrixVP is correct but it is still rendering from my MainCamera View. Only the Projection Matrix seems to have an affect to GL (FOV and Clipping planes are working)
    This is my code:

    Code (CSharp):
    1. GL.PushMatrix();
    2. GL.LoadProjectionMatrix(GL.GetGPUProjectionMatrix(_cam.projectionMatrix,false));
    3. GL.LoadIdentity();
    4. GL.modelview = (_cam.worldToCameraMatrix);
    5.  
    6. Graphics.DrawMeshNow(mesh.m_mesh,  mesh.m_mat4x4);
    7.  
    8. GL.PopMatrix();
     
    Last edited: Jul 9, 2019
  5. FranFndz

    FranFndz

    Joined:
    Sep 20, 2018
    Posts:
    178
    I can't find that code but I remember having to reset the camera values.

    At the end I I'm just using a second camera rendering on a small texture as I couldn't have GL to draw skinned mesh animations.

    Not command buffer but, OnPreRender , OnPostRender.
    And I couldn't stop the;
    Shadows.CullDirectionalShadowCasters even if shadows are turned off.
     
  6. mimimiprod

    mimimiprod

    Joined:
    Oct 2, 2014
    Posts:
    17
    Thank you, i figured this out form me now. But it is still a hacky.
    Code (CSharp):
    1.   void renderGraphicsGL(Camera _camReference)
    2.     {
    3.         Camera _camCurrent = Camera.current;
    4.  
    5.         GL.PushMatrix();
    6.  
    7.         _camCurrent.worldToCameraMatrix = _camReference.worldToCameraMatrix;
    8.         GL.LoadProjectionMatrix(_camReference.projectionMatrix);
    9.  
    10.         Graphics.DrawMeshNow(m_transClipPlaneShMap.sharedMesh, m_transClipPlaneShMap.localToWorldMatrix);
    11.  
    12.         GL.PopMatrix();
    13.  
    14.         _camCurrent.ResetWorldToCameraMatrix();
    15.  
    16.     }