From 64fefe05551483af85a97a723b49f9b122c839bc Mon Sep 17 00:00:00 2001 From: Allanis Date: Fri, 17 Nov 2017 19:37:02 +0000 Subject: [PATCH] [Add] Depth sorting? Two odd things happened here. Magically lasers started working! YEYY! But also Earth disapeared. Nayy! --- src/sbre/brender.cpp | 5 +++++ src/sbre/collfunc.cpp | 2 +- src/sbre/models.cpp | 34 +++++++++++++++++++--------------- src/sbre/primfunc.cpp | 13 +++++++++---- src/sbre/sbre.h | 9 +++++++++ src/sbre/sbre_int.h | 2 ++ src/space.cpp | 5 +++++ src/world_view.cpp | 7 +++---- 8 files changed, 53 insertions(+), 24 deletions(-) diff --git a/src/sbre/brender.cpp b/src/sbre/brender.cpp index 8dfaae7..d275572 100644 --- a/src/sbre/brender.cpp +++ b/src/sbre/brender.cpp @@ -89,6 +89,11 @@ static void ResolveVertices(Model* pMod, Vector* pRes, ObjParams* pObjParam) { static float g_dn, g_df, g_sd; static int g_wireframe = 0; +void sbreSetDepthRange(float sd, float dn, float df) { + glDepthRange(dn+SBRE_ZBIAS, df); + g_dn = dn; g_df = df; g_sd = sd; +} + void sbreSetViewport(int w, int h, float d, float zn, float zf, float dn, float df) { glViewport(0, 0, w, h); diff --git a/src/sbre/collfunc.cpp b/src/sbre/collfunc.cpp index 9c01e2e..77231e6 100644 --- a/src/sbre/collfunc.cpp +++ b/src/sbre/collfunc.cpp @@ -28,7 +28,7 @@ static int CollFuncMatVar(uint16* pData, Model* pMod, RState* pState) { } static int CollFuncZBias(uint16* pData, Model* pMod, RState* pState) { - return 3; + return 4; } static int CollFuncTriFlat(uint16* pData, Model* pMod, RState* pState) { diff --git a/src/sbre/models.cpp b/src/sbre/models.cpp index ad63fc8..1de6e8b 100644 --- a/src/sbre/models.cpp +++ b/src/sbre/models.cpp @@ -323,17 +323,17 @@ static uint16 ship1data[] = { PTYPE_SUBOBJECT, 0x8000, SUB_WING1, 26, 52, 2, 100, PTYPE_MATFIXED, 20, 20, 20, 0, 0, 0, 100, 0, 0, 0, - PTYPE_ZBIAS, 54, 5, + PTYPE_ZBIAS, 14, 54, 5, PTYPE_TEXT, 0, 0x8000, 14, 54, 5, 500, 200, 1000, - PTYPE_ZBIAS, 55, 5, + PTYPE_ZBIAS, 12, 55, 5, PTYPE_TEXT, 0, 0x8000, 12, 55, 2, 1100, 200, 1000, - PTYPE_ZBIAS, 4, 5, + PTYPE_ZBIAS, 40, 4, 5, PTYPE_SUBOBJECT, 0, SUB_NWUNIT, 40, 4, 5, 200, PTYPE_SUBOBJECT, 0, SUB_MWUNIT, 41, 4, 5, 200, // PTYPE_TEXT, -1, -1, 12, 0, 1, 5000, - PTYPE_ZBIAS, 0x8000, 5, + PTYPE_ZBIAS, 0x8000, 0, 0, PTYPE_END, }; @@ -587,7 +587,7 @@ static uint16 ship2data[] = { COMP_HERMITE, 16, 0, 47, 50, COMP_END, - PTYPE_ZBIAS, 5, 5, + PTYPE_ZBIAS, 72, 5, 5, PTYPE_MATFIXED, 30, 30, 30, 30, 30, 30, 200, 0, 0, 0, PTYPE_TUBE | RFLAG_XREF, 8, 12, 72, 76, 1, 250, 200, PTYPE_MATANIM, AFUNC_THRUSTPULSE, @@ -595,21 +595,23 @@ static uint16 ship2data[] = { 0, 0, 0, 0, 0, 0, 100, 0, 0, 50, PTYPE_CIRCLE | RFLAG_XREF, 9, 12, 72, 5, 1, 200, - PTYPE_ZBIAS, 120, 5, + PTYPE_ZBIAS, 77, 120, 5, // PTYPE_MATFIXED, 30, 30, 30, 0, 0, 0, 100, 0, 0, 0, PTYPE_QUADFLAT | RFLAG_XREF, 77, 78, 80, 79, PTYPE_MATFIXED, 20, 20, 20, 0, 0, 0, 100, 0, 0, 0, - PTYPE_ZBIAS, 95, 5, + PTYPE_ZBIAS, 68, 95, 5, PTYPE_TEXT, 0, 0x8000, 68, 95, 2, 1900, 50, 250, - PTYPE_ZBIAS, 96, 5, + PTYPE_ZBIAS, 97, 96, 5, PTYPE_TEXT, 0, 0x8000, 97, 96, 5, 400, 50, 250, - PTYPE_ZBIAS, 4, 5, + PTYPE_ZBIAS, 73, 4, 5, PTYPE_SUBOBJECT, 0, SUB_NWUNIT, 73, 4, 5, 100, + PTYPE_ZBIAS, 74, 4, 5, PTYPE_SUBOBJECT, 0, SUB_NWUNIT, 74, 4, 5, 64, PTYPE_SUBOBJECT, 0, SUB_NWUNIT, 75, 4, 5, 64, + PTYPE_ZBIAS, 0x8000, 0, 0, PTYPE_END, }; @@ -873,7 +875,7 @@ static uint16 ship3data[] = { COMP_LINE, 12, 4, COMP_END, - PTYPE_ZBIAS, 5, 5, + PTYPE_ZBIAS, 33, 5, 5, PTYPE_MATFIXED, 30, 30, 30, 30, 30, 30, 200, 0, 0, 0, PTYPE_TUBE | RFLAG_XREF, 4, 12, 33, 34, 1, 300, 250, PTYPE_MATANIM, AFUNC_THRUSTPULSE, @@ -891,11 +893,12 @@ static uint16 ship3data[] = { PTYPE_ZBIAS, 96, 5, PTYPE_TEXT, 0, 0x8000, 97, 96, 5, 400, 50, 250, */ - PTYPE_ZBIAS, 4, 5, + PTYPE_ZBIAS, 35, 4, 5, PTYPE_SUBOBJECT, 0, SUB_NWUNIT, 35, 4, 5, 100, PTYPE_SUBOBJECT, 0, SUB_MWUNIT, 36, 4, 5, 100, PTYPE_SUBOBJECT, 0, SUB_MWUNIT, 37, 4, 5, 100, + PTYPE_ZBIAS, 0x8000, 0, 0, PTYPE_END, }; @@ -995,12 +998,12 @@ static uint16 ship4data[] = { PTYPE_EXTRUSION, 9, 8, 20, 21, 1, 85, 10, PTYPE_MATFIXED, 20, 20, 20, 0, 0, 0, 100, 0, 0, 0, - PTYPE_ZBIAS, 0, 5, + PTYPE_ZBIAS, 32, 0, 5, PTYPE_TEXT, 0, 0x8000, 32, 0, 2, 300, 250, 400, - PTYPE_ZBIAS, 3, 5, + PTYPE_ZBIAS, 33, 3, 5, PTYPE_TEXT, 0, 0x8000, 33, 3, 5, 300, 250, 400, - PTYPE_ZBIAS, 4, 5, + PTYPE_ZBIAS, 34, 4, 5, PTYPE_SUBOBJECT, 0, SUB_MWUNIT, 34, 4, 5, 60, PTYPE_SUBOBJECT, 0, SUB_MWUNIT, 35, 4, 5, 60, PTYPE_SUBOBJECT, 0, SUB_MWUNIT, 36, 4, 5, 50, @@ -1008,9 +1011,10 @@ static uint16 ship4data[] = { PTYPE_SUBOBJECT, 0, SUB_MWUNIT, 38, 4, 5, 50, PTYPE_SUBOBJECT, 0, SUB_MWUNIT, 39, 4, 5, 50, - PTYPE_ZBIAS, 1, 5, + PTYPE_ZBIAS, 40, 1, 5, PTYPE_SUBOBJECT, 0x8000, SUB_DISH, 40, 1, 100, 200, + PTYPE_ZBIAS, 0x8000, 0, 0, PTYPE_END, }; diff --git a/src/sbre/primfunc.cpp b/src/sbre/primfunc.cpp index b5a029d..37570cf 100644 --- a/src/sbre/primfunc.cpp +++ b/src/sbre/primfunc.cpp @@ -85,15 +85,20 @@ static int PrimFuncMatVar(uint16* pData, Model* pMod, RState* pState) { #if 0 uint16 PFUNC_ZBIAS - uint16 offset /* To test if nearer - 0x8000 = reset. */ + uint16 pos /* To test if nearer - 0x8000 = reset. + uint16 norm uint16 units /* Integer units. not used. */ #endif static int PrimFuncZBias(uint16* pData, Model* pMod, RState* pState) { if(pData[1] & 0x8000) glDepthRange (pState->dn+SBRE_ZBIAS, pState->df); - else if(VecDot(pState->pVtx+pData[1], &pState->campos) > 0.0f) - glDepthRange(pState->dn, pState->df-SBRE_ZBIAS); - return 3; + else { + Vector tv; + VecSub(&pState->campos, pState->pVtx+pData[1], &tv); + if(VecDot(&tv, pState->pVtx+pData[2]) > 0.0f) + glDepthRange(pState->dn, pState->df-SBRE_ZBIAS); + } + return 4; } static int PrimFuncTriFlat(uint16* pData, Model* pMod, RState* pState) { diff --git a/src/sbre/sbre.h b/src/sbre/sbre.h index 3819ad7..22a45f6 100644 --- a/src/sbre/sbre.h +++ b/src/sbre/sbre.h @@ -41,10 +41,19 @@ struct CollMesh { int cflag; }; +/* + * If you don't call SetDepthRange, z bias and LOD will fail. + * sd is screen depth in pixels, dn and df are like glDepthRange params. + */ +void sbreSetDepthRange(float sd, float dn, float df); void sbreSetViewport(int w, int h, float d, float zn, float zf, float dn, float df); void sbreSetDirLight(float* pColor, float* pDir); void sbreSetWireframe(int val); void sbreRenderModel(Vector* pPos, Matrix* pOrient, int model, ObjParams* pParam, float s=1.0f, Vector* pCompos=0); + +/* + * Will preserve and realloc pointers in pCMesh. + */ void sbreGenCollMesh(CollMesh* pCMesh, int model, ObjParams* pParam, float s = 1.0f); diff --git a/src/sbre/sbre_int.h b/src/sbre/sbre_int.h index 2541c5c..efccf09 100644 --- a/src/sbre/sbre_int.h +++ b/src/sbre/sbre_int.h @@ -155,6 +155,8 @@ static const int SUBOBJ_THRUST = 0x4000; static const int THRUST_XREF = 0x8000; static const int THRUST_NOANG = 0x4000; +/* Proper value for 16-bit depth buffer. */ +//static const float SBRE_ZBIAS = 0.00002f; static const float SBRE_ZBIAS = 0.00002f; static const float SBRE_AMB = 0.3f; diff --git a/src/space.cpp b/src/space.cpp index c651058..7ca99e5 100644 --- a/src/space.cpp +++ b/src/space.cpp @@ -9,6 +9,7 @@ #include "l3d.h" #include "player.h" #include "star_system.h" +#include "sbre/sbre.h" dWorldID Space::world; std::list Space::bodies; @@ -220,6 +221,10 @@ void Space::Render(const Frame* cam_frame) { idx++; } sort(bz, bz+bodies.size(), body_zsort_compare()); + + /* Probably the right place for this when partitioning is done. */ + sbreSetDepthRange(L3D::GetScrWidth()*0.5f, 0.0f, 1.0f); + for(unsigned int i = 0; i < bodies.size(); i++) { bz[i].b->Render(cam_frame); } diff --git a/src/world_view.cpp b/src/world_view.cpp index ed176cc..3ccdeca 100644 --- a/src/world_view.cpp +++ b/src/world_view.cpp @@ -60,10 +60,9 @@ void WorldView::Draw3D(void) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); /* Wth did I give these functions large names for.. :/ */ - glFrustum(-L3D::GetScrWidth()*.5, L3D::GetScrWidth()*.5, - -L3D::GetScrHeight()*.5, L3D::GetScrHeight()*.5, - L3D::GetScrWidth()*.5, 100000); - glDepthRange(-10, -100000); + float fracH = L3D::GetScrHeight() * 0.5f / L3D::GetScrWidth(); + glFrustum(-0.5, 0.5, -fracH, fracH, 1.0f, 1000.0f); + //glDepthRange(-10, -100000); /* Uh, What?! */ glMatrixMode(GL_MODELVIEW); glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);