2018-06-16 21:31:29 +00:00
2016-03-21 04:10:14 +00:00
/**
* * Supermodel
* * A Sega Model 3 Arcade Emulator .
* * Copyright 2011 Bart Trzynadlowski , Nik Henson
* *
* * This file is part of Supermodel .
* *
* * Supermodel is free software : you can redistribute it and / or modify it under
* * the terms of the GNU General Public License as published by the Free
* * Software Foundation , either version 3 of the License , or ( at your option )
* * any later version .
* *
* * Supermodel is distributed in the hope that it will be useful , but WITHOUT
* * ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY or
* * FITNESS FOR A PARTICULAR PURPOSE . See the GNU General Public License for
* * more details .
* *
* * You should have received a copy of the GNU General Public License along
* * with Supermodel . If not , see < http : //www.gnu.org/licenses/>.
* */
/*
* New3D . h
*
* Header file defining the CNew3D class : OpenGL Real3D graphics engine .
*/
# ifndef INCLUDED_NEW3D_H
# define INCLUDED_NEW3D_H
2020-07-31 19:18:51 +00:00
# include <GL/glew.h>
2016-03-21 04:10:14 +00:00
# include "Types.h"
# include "Graphics/IRender3D.h"
# include "Model.h"
# include "Mat4.h"
# include "R3DShader.h"
# include "VBO.h"
2016-03-28 20:11:46 +00:00
# include "R3DData.h"
2016-06-16 20:05:29 +00:00
# include "Plane.h"
# include "Vec.h"
2016-10-09 16:34:12 +00:00
# include "R3DScrollFog.h"
2016-12-06 14:39:46 +00:00
# include "PolyHeader.h"
2018-06-16 21:31:29 +00:00
# include "R3DFrameBuffers.h"
2022-01-02 12:48:09 +00:00
# include <mutex>
2024-06-13 12:36:30 +00:00
# include "TextureBank.h"
2016-03-21 04:10:14 +00:00
namespace New3D {
class CNew3D : public IRender3D
{
public :
/*
* RenderFrame ( void ) :
*
* Renders the complete scene database . Must be called between BeginFrame ( ) and
* EndFrame ( ) . This function traverses the scene database and builds up display
* lists .
*/
void RenderFrame ( void ) ;
/*
* BeginFrame ( void ) :
*
* Prepare to render a new frame . Must be called once per frame prior to
* drawing anything .
*/
void BeginFrame ( void ) ;
/*
* EndFrame ( void ) :
*
* Signals the end of rendering for this frame . Must be called last during
* the frame .
*/
void EndFrame ( void ) ;
/*
* UploadTextures ( x , y , width , height ) :
*
* Signals that a portion of texture RAM has been updated .
*
* Parameters :
* x X position within texture RAM .
* y Y position within texture RAM .
* width Width of texture data in texels .
* height Height .
*/
2017-03-25 00:06:24 +00:00
void UploadTextures ( unsigned level , unsigned x , unsigned y , unsigned width , unsigned height ) ;
2016-03-21 04:10:14 +00:00
/*
* AttachMemory ( cullingRAMLoPtr , cullingRAMHiPtr , polyRAMPtr , vromPtr ,
* textureRAMPtr ) :
*
* Attaches RAM and ROM areas . This must be done prior to any rendering
* otherwise the program may crash with an access violation .
*
* Parameters :
* cullingRAMLoPtr Pointer to low culling RAM ( 4 MB ) .
* cullingRAMHiPtr Pointer to high culling RAM ( 1 MB ) .
* polyRAMPtr Pointer to polygon RAM ( 4 MB ) .
* vromPtr Pointer to video ROM ( 64 MB ) .
* textureRAMPtr Pointer to texture RAM ( 8 MB ) .
*/
void AttachMemory ( const UINT32 * cullingRAMLoPtr ,
const UINT32 * cullingRAMHiPtr , const UINT32 * polyRAMPtr ,
const UINT32 * vromPtr , const UINT16 * textureRAMPtr ) ;
/*
2017-03-27 03:19:15 +00:00
* SetStepping ( stepping ) :
2016-03-21 04:10:14 +00:00
*
* Sets the Model 3 hardware stepping , which also determines the Real3D
* functionality . The default is Step 1.0 . This should be called prior to
* any other emulation functions and after Init ( ) .
*
* Parameters :
2017-03-27 03:19:15 +00:00
* stepping 0x10 for Step 1.0 , 0x15 for Step 1.5 , 0x20 for Step 2.0 , or
* 0x21 for Step 2.1 . Anything else defaults to 1.0 .
2016-03-21 04:10:14 +00:00
*/
2017-03-27 03:19:15 +00:00
void SetStepping ( int stepping ) ;
2016-03-21 04:10:14 +00:00
/*
* Init ( xOffset , yOffset , xRes , yRes , totalXRes , totalYRes ) :
*
* One - time initialization of the context . Must be called before any other
* members ( meaning it should be called even before being attached to any
* other objects that want to use it ) .
*
* External shader files are loaded according to configuration settings .
*
* Parameters :
* xOffset X offset of the viewable area within OpenGL display
* surface , in pixels .
* yOffset Y offset .
* xRes Horizontal resolution of the viewable area .
* yRes Vertical resolution .
* totalXRes Horizontal resolution of the complete display area .
* totalYRes Vertical resolution .
*
* Returns :
* OKAY is successful , otherwise FAILED if a non - recoverable error
* occurred . Any allocated memory will not be freed until the
* destructor is called . Prints own error messages .
*/
2023-12-26 18:25:03 +00:00
bool Init ( unsigned xOffset , unsigned yOffset , unsigned xRes , unsigned yRes , unsigned totalXRes , unsigned totalYRes , unsigned aaTarget ) ;
2016-03-21 04:10:14 +00:00
2017-09-02 14:54:16 +00:00
/*
* SetSunClamp ( bool enable ) ;
*
* Sets or unsets the clamped light model
*
* Parameters :
* enable Set clamp mode
*/
2018-01-15 21:27:21 +00:00
void SetSunClamp ( bool enable ) ;
/*
* SetSignedShade ( bool enable ) ;
*
* Sets the sign - ness of fixed shading value
*
* Parameters :
* enable Fixed shading is expressed as signed value
*/
void SetSignedShade ( bool enable ) ;
2018-10-19 20:59:46 +00:00
/*
* GetLosValue ( int layer ) ;
*
* Gets the line of sight value for the priority layer
*
* Parameters :
* layer Priority layer to read from
*/
float GetLosValue ( int layer ) ;
2018-01-15 21:27:21 +00:00
/*
* CRender3D ( config ) :
* ~ CRender3D ( void ) :
*
2016-03-21 04:10:14 +00:00
* Constructor and destructor .
2017-08-11 00:41:10 +00:00
*
* Parameters :
* config Run - time configuration .
2016-03-21 04:10:14 +00:00
*/
2022-08-19 19:34:22 +00:00
CNew3D ( const Util : : Config : : Node & config , const std : : string & gameName ) ;
2016-03-21 04:10:14 +00:00
~ CNew3D ( void ) ;
private :
/*
* Private Members
*/
// Real3D address translation
const UINT32 * TranslateCullingAddress ( UINT32 addr ) ;
const UINT32 * TranslateModelAddress ( UINT32 addr ) ;
// Matrix stack
void MultMatrix ( UINT32 matrixOffset , Mat4 & mat ) ;
void InitMatrixStack ( UINT32 matrixBaseAddr , Mat4 & mat ) ;
2023-09-27 15:33:53 +00:00
void ResetMatrix ( Mat4 & mat ) ;
2016-03-21 04:10:14 +00:00
// Scene database traversal
bool DrawModel ( UINT32 modelAddr ) ;
void DescendCullingNode ( UINT32 addr ) ;
void DescendPointerList ( UINT32 addr ) ;
void DescendNodePtr ( UINT32 nodeAddr ) ;
2016-04-19 22:05:12 +00:00
void RenderViewport ( UINT32 addr ) ;
2016-03-21 04:10:14 +00:00
// building the scene
2022-11-07 21:33:01 +00:00
int GetTexFormat ( int originalFormat , bool contour ) ;
2016-12-06 12:25:34 +00:00
void SetMeshValues ( SortingMesh * currentMesh , PolyHeader & ph ) ;
2016-03-21 04:10:14 +00:00
void CacheModel ( Model * m , const UINT32 * data ) ;
2018-09-13 12:50:34 +00:00
void CopyVertexData ( const R3DPoly & r3dPoly , std : : vector < FVertex > & vertexArray ) ;
2022-11-07 21:33:01 +00:00
void GetCoordinates ( int width , int height , UINT16 uIn , UINT16 vIn , float uvScale , float & uOut , float & vOut ) ;
2016-03-21 04:10:14 +00:00
2018-06-16 21:31:29 +00:00
bool RenderScene ( int priority , bool renderOverlay , Layer layer ) ; // returns if has overlay plane
2017-02-20 17:22:32 +00:00
bool IsDynamicModel ( UINT32 * data ) ; // check if the model has a colour palette
2016-03-26 22:44:26 +00:00
bool IsVROMModel ( UINT32 modelAddr ) ;
2016-10-09 16:34:12 +00:00
void DrawScrollFog ( ) ;
2023-10-22 20:07:35 +00:00
void DrawAmbientFog ( ) ;
2018-06-16 21:31:29 +00:00
bool SkipLayer ( int layer ) ;
void SetRenderStates ( ) ;
void DisableRenderStates ( ) ;
2018-10-19 20:59:46 +00:00
void TranslateLosPosition ( int inX , int inY , int & outX , int & outY ) ;
bool ProcessLos ( int priority ) ;
2023-12-26 18:25:03 +00:00
void CalcViewport ( Viewport * vp ) ;
2024-06-13 12:36:30 +00:00
void TranslateTexture ( unsigned & x , unsigned & y , int width , int height , int & page ) ;
2016-03-26 22:44:26 +00:00
2016-03-21 04:10:14 +00:00
/*
* Data
*/
2018-01-15 21:27:21 +00:00
// Misc
std : : string m_gameName ;
2018-09-13 12:50:34 +00:00
int m_numPolyVerts ;
GLenum m_primType ;
2018-01-15 21:27:21 +00:00
// GPU configuration
bool m_sunClamp ;
bool m_shadeIsSigned ;
// Stepping
int m_step ;
2016-03-21 04:10:14 +00:00
int m_offset ; // offset to subtract for words 3 and higher of culling nodes
float m_vertexFactor ; // fixed-point conversion factor for vertices
2024-07-05 14:57:25 +00:00
float m_textureNPFactor ; // fixed-point conversion factor for texture NP values
2016-03-21 04:10:14 +00:00
// Memory (passed from outside)
const UINT32 * m_cullingRAMLo ; // 4 MB
const UINT32 * m_cullingRAMHi ; // 1 MB
const UINT32 * m_polyRAM ; // 4 MB
const UINT32 * m_vrom ; // 64 MB
const UINT16 * m_textureRAM ; // 8 MB
// Resolution and scaling factors (to support resolutions higher than 496x384) and offsets
float m_xRatio , m_yRatio ;
unsigned m_xOffs , m_yOffs ;
2018-05-03 03:46:44 +00:00
unsigned m_xRes , m_yRes ; // resolution of Model 3's 496x384 display area within the window
unsigned m_totalXRes , m_totalYRes ; // total OpenGL window resolution
2024-07-19 13:19:09 +00:00
bool m_wideScreen ;
2016-03-21 04:10:14 +00:00
// Real3D Base Matrix Pointer
const float * m_matrixBasePtr ;
2016-05-18 23:06:41 +00:00
UINT32 m_colorTableAddr = 0x400 ; // address of color table in polygon RAM
2017-04-17 10:40:07 +00:00
LODBlendTable * m_LODBlendTable ;
2016-03-21 04:10:14 +00:00
NodeAttributes m_nodeAttribs ;
2016-03-26 22:44:26 +00:00
Mat4 m_modelMat ; // current modelview matrix
2016-03-21 04:10:14 +00:00
2022-01-02 12:48:09 +00:00
struct LOS
{
float value [ 4 ] = { 0 , 0 , 0 , 0 } ; // line of sight value for each priority layer
} m_los [ 2 ] ;
LOS * m_losFront = & m_los [ 0 ] ; // we need to double buffer this because 3d works in separate thread
LOS * m_losBack = & m_los [ 1 ] ;
std : : mutex m_losMutex ;
2018-10-19 20:59:46 +00:00
2018-09-02 21:36:24 +00:00
Vertex m_prev [ 4 ] ; // these are class variables because sega bass fishing starts meshes with shared vertices from the previous one
UINT16 m_prevTexCoords [ 4 ] [ 2 ] ; // basically relying on undefined behavour
2018-09-13 12:50:34 +00:00
std : : vector < Node > m_nodes ; // this represents the entire render frame
std : : vector < FVertex > m_polyBufferRam ; // dynamic polys
std : : vector < FVertex > m_polyBufferRom ; // rom polys
2016-03-26 22:44:26 +00:00
std : : unordered_map < UINT32 , std : : shared_ptr < std : : vector < Mesh > > > m_romMap ; // a hash table for all the ROM models. The meshes don't have model matrices or tex offsets yet
2024-06-13 12:36:30 +00:00
TextureBank m_textureBank [ 2 ] ;
2016-03-21 04:10:14 +00:00
2022-11-07 21:33:01 +00:00
GLuint m_vao ;
2016-03-26 22:44:26 +00:00
VBO m_vbo ; // large VBO to hold our poly data, start of VBO is ROM data, ram polys follow
2016-03-21 04:10:14 +00:00
R3DShader m_r3dShader ;
2016-10-09 16:34:12 +00:00
R3DScrollFog m_r3dScrollFog ;
2018-06-16 21:31:29 +00:00
R3DFrameBuffers m_r3dFrameBuffers ;
2023-12-26 18:25:03 +00:00
GLuint m_aaTarget ; // optional, maybe zero
2016-06-16 20:05:29 +00:00
2017-02-07 14:05:03 +00:00
int m_currentPriority ;
2023-12-22 02:00:47 +00:00
struct
{
float bnlu ;
float bnlv ;
float bntu ;
float bntw ;
float bnru ;
float bnrv ;
float bnbu ;
float bnbw ;
float correction ;
2023-12-26 18:25:03 +00:00
} m_planes ;
2016-03-21 04:10:14 +00:00
} ;
} // New3D
# endif // INCLUDED_NEW3D_H