zturtleman / mm3d

Maverick Model 3D is a 3D model editor and animator for games.
https://clover.moe/mm3d
GNU General Public License v2.0
114 stars 22 forks source link

glOrtho depth-buffer is broken #97

Open m-7761 opened 4 years ago

m-7761 commented 4 years ago

Here is the relevant code from modelviewport.cc. Notice 500000 is hardcoded.

-10000,10000 are better values for a depth-buffer, but ideally they should enclose the model.

Disclaimer: I'm not 100% positive these transformations are correct. They appear to work. If the depth-buffer is too compressed vertices are drawn on the wrong side of the model, and lines poke though, which is particularly noticeable in the modes where the wireframe is drawn over triangles. But it's noticeable too even in wireframe mode with the changes of #96 to eliminate back-facing vertices.

EDITED: I revised them to use averages (which amount to 0 in this case.) I'm more confident about correctness now. I meant to use the midpoint before. I was calculating it wrong.

    //m_farOrtho = 1000000; //???
    //m_nearOrtho = 0.001; //???
    m_farOrtho = 10000; //???
    m_nearOrtho = -10000; //???
            //glTranslatef(0,0,-m_farOrtho/2);
            glTranslatef(0,0,-(m_farOrtho+m_nearOrtho)/2);
        viewPoint[0] = 0;
        viewPoint[1] = 0;
        viewPoint[2] = (m_farOrtho+m_nearOrtho)/2; //500000;
m-7761 commented 4 years ago

EDITED: I revised them to use averages (which amount to 0 in this case.) I'm more confident about correctness now. I meant to use the midpoint before. I was calculating it wrong.

m-7761 commented 4 years ago

NOTE: The backgrounds also use m_farOrtho. I forgot to work on that.

I'm not sure what to do with them... they seem to want the background to drawn in front of 0,0 instead of at the back. So I removed the m_farOrtho/2 term. But this assumes the near/far planes have 0,0 in their center (i.e. they aren't offset to make better use of the depth-buffer precision.)

m-7761 commented 4 years ago

P.S. Sorry about all the posts lately. I know they must spam you (Zack) but it helps me to have a URL I can drop into my source code, so I don't have to make an elaborate C++ comment in the middle of the code I'm working on.