diff --git a/Src/Graphics/Legacy3D/Legacy3D.cpp b/Src/Graphics/Legacy3D/Legacy3D.cpp index 47a3282..1f99961 100644 --- a/Src/Graphics/Legacy3D/Legacy3D.cpp +++ b/Src/Graphics/Legacy3D/Legacy3D.cpp @@ -176,8 +176,8 @@ namespace Legacy3D { // Microsoft doesn't provide isnan() and isinf() #ifdef _MSC_VER #include - #define ISNAN(x) (_isnan(x)) - #define ISINF(x) (!_finite(x)) + #define ISNAN(x) (_isnanf(x)) + #define ISINF(x) (!_finitef(x)) #else #define ISNAN(x) (std::isnan(x)) #define ISINF(x) (std::isinf(x)) @@ -812,7 +812,7 @@ void CLegacy3D::DescendNodePtr(UINT32 nodeAddr) // Draws viewports of the given priority void CLegacy3D::RenderViewport(UINT32 addr, int pri, bool wideScreen) { - static const GLfloat color[8][3] = { + static constexpr GLfloat color[8][3] = { { 0.0, 0.0, 0.0 }, // off { 0.0, 0.0, 1.0 }, // blue { 0.0, 1.0, 0.0 }, // green @@ -825,7 +825,7 @@ void CLegacy3D::RenderViewport(UINT32 addr, int pri, bool wideScreen) // Translate address and obtain pointer const UINT32 *vpnode = TranslateCullingAddress(addr); - if (NULL == vpnode) + if (nullptr == vpnode) return; // Recursively process next viewport @@ -852,8 +852,8 @@ void CLegacy3D::RenderViewport(UINT32 addr, int pri, bool wideScreen) int vpHeight = (vpnode[0x14]>>18)&0x3FFF; // height (14.2) // Field of view and clipping - GLfloat vpTopAngle = asinf(Util::UintAsFloat(vpnode[0x0E])); // FOV Y upper half-angle (radians) - GLfloat vpBotAngle = asinf(Util::UintAsFloat(vpnode[0x12])); // FOV Y lower half-angle + GLfloat vpTopAngle = asinf(Util::Uint32AsFloat(vpnode[0x0E])); // FOV Y upper half-angle (radians) + GLfloat vpBotAngle = asinf(Util::Uint32AsFloat(vpnode[0x12])); // FOV Y lower half-angle GLfloat fovYDegrees = (vpTopAngle+vpBotAngle)*(float)(180.0/M_PI); // TO-DO: investigate clipping planes @@ -876,15 +876,15 @@ void CLegacy3D::RenderViewport(UINT32 addr, int pri, bool wideScreen) viewportY = yOffs + (GLint) ((float)(384-(vpY+vpHeight))*yRatio); viewportWidth = (GLint) ((float)vpWidth*xRatio); viewportHeight = (GLint) ((float)vpHeight*yRatio); - gluPerspective(fovYDegrees,(GLfloat)vpWidth/(GLfloat)vpHeight,0.1f,1e5); // use Model 3 viewport ratio + gluPerspective(fovYDegrees,(GLdouble)vpWidth/(GLdouble)vpHeight,0.1,1e5); // use Model 3 viewport ratio } // Lighting (note that sun vector points toward sun -- away from vertex) - lightingParams[0] = *(float *) &vpnode[0x05]; // sun X - lightingParams[1] = *(float *) &vpnode[0x06]; // sun Y - lightingParams[2] = *(float *) &vpnode[0x04]; // sun Z - lightingParams[3] = *(float *) &vpnode[0x07]; // sun intensity - lightingParams[4] = (float) ((vpnode[0x24]>>8)&0xFF) * (1.0f/255.0f); // ambient intensity + lightingParams[0] = Util::Uint32AsFloat(vpnode[0x05]); // sun X + lightingParams[1] = Util::Uint32AsFloat(vpnode[0x06]); // sun Y + lightingParams[2] = Util::Uint32AsFloat(vpnode[0x04]); // sun Z + lightingParams[3] = Util::Uint32AsFloat(vpnode[0x07]); // sun intensity + lightingParams[4] = (float) ((vpnode[0x24]>>8)&0xFF) * (float)(1.0/255.0); // ambient intensity lightingParams[5] = 0.0; // reserved // Spotlight @@ -893,8 +893,8 @@ void CLegacy3D::RenderViewport(UINT32 addr, int pri, bool wideScreen) spotEllipse[1] = (float) ((vpnode[0x1D]>>3)&0x1FFF); // spotlight Y spotEllipse[2] = (float) ((vpnode[0x1E]>>16)&0xFFFF); // spotlight X size (16-bit? May have fractional component below bit 16) spotEllipse[3] = (float) ((vpnode[0x1D]>>16)&0xFFFF); // spotlight Y size - spotRange[0] = 1.0f/(*(float *) &vpnode[0x21]); // spotlight start - spotRange[1] = *(float *) &vpnode[0x1F]; // spotlight extent + spotRange[0] = 1.0f/Util::Uint32AsFloat(vpnode[0x21]); // spotlight start + spotRange[1] = Util::Uint32AsFloat(vpnode[0x1F]); // spotlight extent spotColor[0] = color[spotColorIdx][0]; // spotlight color spotColor[1] = color[spotColorIdx][1]; spotColor[2] = color[spotColorIdx][2]; @@ -913,30 +913,30 @@ void CLegacy3D::RenderViewport(UINT32 addr, int pri, bool wideScreen) spotEllipse[3] *= yRatio; // Fog - fogParams[0] = (float) ((vpnode[0x22]>>16)&0xFF) * (1.0f/255.0f); // fog color R - fogParams[1] = (float) ((vpnode[0x22]>>8)&0xFF) * (1.0f/255.0f); // fog color G - fogParams[2] = (float) ((vpnode[0x22]>>0)&0xFF) * (1.0f/255.0f); // fog color B - fogParams[3] = *(float *) &vpnode[0x23]; // fog density - fogParams[4] = (float) (INT16) (vpnode[0x25]&0xFFFF)*(1.0f/255.0f); // fog start + fogParams[0] = (float) ((vpnode[0x22]>>16)&0xFF) * (float)(1.0/255.0); // fog color R + fogParams[1] = (float) ((vpnode[0x22]>>8)&0xFF) * (float)(1.0/255.0); // fog color G + fogParams[2] = (float) ((vpnode[0x22]>>0)&0xFF) * (float)(1.0/255.0); // fog color B + fogParams[3] = Util::Uint32AsFloat(vpnode[0x23]); // fog density + fogParams[4] = (float) (INT16) (vpnode[0x25]&0xFFFF) * (float)(1.0/255.0); // fog start if (ISINF(fogParams[3]) || ISNAN(fogParams[3]) || ISINF(fogParams[4]) || ISNAN(fogParams[4])) // Star Wars Trilogy fogParams[3] = fogParams[4] = 0.0f; - + // Unknown light/fog parameters - //GLfloat scrollFog = (float) (vpnode[0x20]&0xFF) * (1.0f/255.0f); // scroll fog - //GLfloat scrollAtt = (float) (vpnode[0x24]&0xFF) * (1.0f/255.0f); // scroll attenuation + //GLfloat scrollFog = (float) (vpnode[0x20]&0xFF) * (float)(1.0/255.0); // scroll fog + //GLfloat scrollAtt = (float) (vpnode[0x24]&0xFF) * (float)(1.0/255.0); // scroll attenuation //printf("scrollFog = %g, scrollAtt = %g\n", scrollFog, scrollAtt); //printf("Fog: R=%02X G=%02X B=%02X density=%g (%X) %d start=%g\n", ((vpnode[0x22]>>16)&0xFF), ((vpnode[0x22]>>8)&0xFF), ((vpnode[0x22]>>0)&0xFF), fogParams[3], vpnode[0x23], (fogParams[3]==fogParams[3]), fogParams[4]); - + // Clear texture offsets before proceeding m_textureOffset = TextureOffset(); - + // Set up coordinate system and base matrix UINT32 matrixBase = vpnode[0x16] & 0xFFFFFF; glMatrixMode(GL_MODELVIEW); InitMatrixStack(matrixBase); // Safeguard: weird coordinate system matrices usually indicate scenes that will choke the renderer - if (NULL != matrixBasePtr) + if (nullptr != matrixBasePtr) { float m21, m32, m13; diff --git a/Src/Graphics/New3D/New3D.cpp b/Src/Graphics/New3D/New3D.cpp index b625326..6e167dd 100644 --- a/Src/Graphics/New3D/New3D.cpp +++ b/Src/Graphics/New3D/New3D.cpp @@ -553,8 +553,8 @@ void CNew3D::DescendCullingNode(UINT32 addr) m_nodeAttribs.Push(); // save current attribs if (!m_offset) { // Step 1.5+ - - float modelScale = Util::UintAsFloat(node[1]); + + float modelScale = Util::Uint32AsFloat(node[1]); if (modelScale > std::numeric_limits::min()) { m_nodeAttribs.currentModelScale = modelScale; } @@ -574,9 +574,9 @@ void CNew3D::DescendCullingNode(UINT32 addr) // apply translation vector if (node[0x00] & 0x10) { - float x = Util::UintAsFloat(node[0x04 - m_offset]); - float y = Util::UintAsFloat(node[0x05 - m_offset]); - float z = Util::UintAsFloat(node[0x06 - m_offset]); + float x = Util::Uint32AsFloat(node[0x04 - m_offset]); + float y = Util::Uint32AsFloat(node[0x05 - m_offset]); + float z = Util::Uint32AsFloat(node[0x06 - m_offset]); m_modelMat.Translate(x, y, z); } // multiply matrix, if specified @@ -832,16 +832,16 @@ void CNew3D::RenderViewport(UINT32 addr) m_LODBlendTable = (LODBlendTable*)TranslateCullingAddress(vpnode[0x17] & 0xFFFFFF); /* - vp->angle_left = -atan2f(Util::UintAsFloat(vpnode[12]), Util::UintAsFloat(vpnode[13])); // These values work out as the normals for the clipping planes. - vp->angle_right = atan2f(Util::UintAsFloat(vpnode[16]), -Util::UintAsFloat(vpnode[17])); // Sometimes these values (dirt devils,lost world) are totally wrong - vp->angle_top = atan2f(Util::UintAsFloat(vpnode[14]), Util::UintAsFloat(vpnode[15])); // and don't work for the frustum values exactly. - vp->angle_bottom = -atan2f(Util::UintAsFloat(vpnode[18]), -Util::UintAsFloat(vpnode[19])); // Perhaps they are just used for culling and not rendering. + vp->angle_left = -atan2f(Util::Uint32AsFloat(vpnode[12]), Util::Uint32AsFloat(vpnode[13])); // These values work out as the normals for the clipping planes. + vp->angle_right = atan2f(Util::Uint32AsFloat(vpnode[16]), -Util::Uint32AsFloat(vpnode[17])); // Sometimes these values (dirt devils,lost world) are totally wrong + vp->angle_top = atan2f(Util::Uint32AsFloat(vpnode[14]), Util::Uint32AsFloat(vpnode[15])); // and don't work for the frustum values exactly. + vp->angle_bottom = -atan2f(Util::Uint32AsFloat(vpnode[18]), -Util::Uint32AsFloat(vpnode[19])); // Perhaps they are just used for culling and not rendering. */ - float cv = Util::UintAsFloat(vpnode[0x8]); // 1/(left-right) - float cw = Util::UintAsFloat(vpnode[0x9]); // 1/(top-bottom) - float io = Util::UintAsFloat(vpnode[0xa]); // top / bottom (ratio) - ish - float jo = Util::UintAsFloat(vpnode[0xb]); // left / right (ratio) + float cv = Util::Uint32AsFloat(vpnode[0x8]); // 1/(left-right) + float cw = Util::Uint32AsFloat(vpnode[0x9]); // 1/(top-bottom) + float io = Util::Uint32AsFloat(vpnode[0xa]); // top / bottom (ratio) - ish + float jo = Util::Uint32AsFloat(vpnode[0xb]); // left / right (ratio) vp->angle_left = (0.0f - jo) / cv; vp->angle_right = (1.0f - jo) / cv; @@ -855,10 +855,10 @@ void CNew3D::RenderViewport(UINT32 addr) CalcFrustumPlanes(m_planes, vp->projectionMatrix); // we need to calc a 'projection matrix' to get the correct frustum planes for clipping // Lighting (note that sun vector points toward sun -- away from vertex) - vp->lightingParams[0] = Util::UintAsFloat(vpnode[0x05]); // sun X - vp->lightingParams[1] = -Util::UintAsFloat(vpnode[0x06]); // sun Y (- to convert to ogl cordinate system) - vp->lightingParams[2] = -Util::UintAsFloat(vpnode[0x04]); // sun Z (- to convert to ogl cordinate system) - vp->lightingParams[3] = std::max(0.f, std::min(Util::UintAsFloat(vpnode[0x07]), 1.0f)); // sun intensity (clamp to 0-1) + vp->lightingParams[0] = Util::Uint32AsFloat(vpnode[0x05]); // sun X + vp->lightingParams[1] = -Util::Uint32AsFloat(vpnode[0x06]); // sun Y (- to convert to ogl cordinate system) + vp->lightingParams[2] = -Util::Uint32AsFloat(vpnode[0x04]); // sun Z (- to convert to ogl cordinate system) + vp->lightingParams[3] = std::max(0.f, std::min(Util::Uint32AsFloat(vpnode[0x07]), 1.0f)); // sun intensity (clamp to 0-1) vp->lightingParams[4] = (float)((vpnode[0x24] >> 8) & 0xFF) * (float)(1.0 / 255.0); // ambient intensity vp->lightingParams[5] = 0.0f; // reserved @@ -874,8 +874,8 @@ void CNew3D::RenderViewport(UINT32 addr) vp->spotEllipse[2] = (float)((vpnode[0x1E] >> 16) & 0xFFFF); // spotlight X size (16-bit) vp->spotEllipse[3] = (float)((vpnode[0x1D] >> 16) & 0xFFFF); // spotlight Y size - vp->spotRange[0] = 1.0f / Util::UintAsFloat(vpnode[0x21]); // spotlight start - vp->spotRange[1] = Util::UintAsFloat(vpnode[0x1F]); // spotlight extent + vp->spotRange[0] = 1.0f / Util::Uint32AsFloat(vpnode[0x21]); // spotlight start + vp->spotRange[1] = Util::Uint32AsFloat(vpnode[0x1F]); // spotlight extent vp->spotColor[0] = color[spotColorIdx][0]; // spotlight color vp->spotColor[1] = color[spotColorIdx][1]; @@ -909,7 +909,7 @@ void CNew3D::RenderViewport(UINT32 addr) vp->fogParams[0] = (float)((vpnode[0x22] >> 16) & 0xFF) * (float)(1.0 / 255.0); // fog color R vp->fogParams[1] = (float)((vpnode[0x22] >> 8) & 0xFF) * (float)(1.0 / 255.0); // fog color G vp->fogParams[2] = (float)((vpnode[0x22] >> 0) & 0xFF) * (float)(1.0 / 255.0); // fog color B - vp->fogParams[3] = std::abs(Util::UintAsFloat(vpnode[0x23])); // fog density - ocean hunter uses negative values, but looks the same + vp->fogParams[3] = std::abs(Util::Uint32AsFloat(vpnode[0x23])); // fog density - ocean hunter uses negative values, but looks the same vp->fogParams[4] = (float)(INT16)(vpnode[0x25] & 0xFFFF)* (float)(1.0 / 255.0); // fog start // Avoid Infinite and NaN values for Star Wars Trilogy diff --git a/Src/Graphics/New3D/R3DFloat.cpp b/Src/Graphics/New3D/R3DFloat.cpp index 57b95b3..4256e18 100644 --- a/Src/Graphics/New3D/R3DFloat.cpp +++ b/Src/Graphics/New3D/R3DFloat.cpp @@ -35,5 +35,5 @@ UINT32 R3DFloat::Convert16BitProFloat(UINT32 a1) float R3DFloat::ToFloat(UINT32 a1) { - return Util::UintAsFloat(a1); + return Util::Uint32AsFloat(a1); } diff --git a/Src/Util/BitCast.h b/Src/Util/BitCast.h index 20f158f..6cdf602 100644 --- a/Src/Util/BitCast.h +++ b/Src/Util/BitCast.h @@ -8,10 +8,10 @@ namespace Util { #if __cplusplus >= 202002L #include -#define FloatAsInt(x) std::bit_cast(x) -#define IntAsFloat(x) std::bit_cast(x) -#define UintAsFloat(x) std::bit_cast(x) -#elif 1 +#define FloatAsInt32(x) std::bit_cast(x) +#define Int32AsFloat(x) std::bit_cast(x) +#define Uint32AsFloat(x) std::bit_cast(x) +#else template inline Dest bit_cast(Source const& source) { static_assert(sizeof(Dest) == sizeof(Source), "size of destination and source objects must be equal"); @@ -22,39 +22,9 @@ inline Dest bit_cast(Source const& source) { std::memcpy(&dest, &source, sizeof(dest)); return dest; } -#define FloatAsInt(x) bit_cast(x) -#define IntAsFloat(x) bit_cast(x) -#define UintAsFloat(x) bit_cast(x) -#else -inline int FloatAsInt(const float x) -{ - union { - float f; - int i; - } uc; - uc.f = x; - return uc.i; -} - -inline float IntAsFloat(const int i) -{ - union { - int i; - float f; - } iaf; - iaf.i = i; - return iaf.f; -} - -inline float UintAsFloat(const unsigned int i) -{ - union { - unsigned int u; - float f; - } iaf; - iaf.u = i; - return iaf.f; -} +#define FloatAsInt32(x) bit_cast(x) +#define Int32AsFloat(x) bit_cast(x) +#define Uint32AsFloat(x) bit_cast(x) #endif } // Util