1 /*
2 Copyright (C) 2003 Rice1964
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 
18 */
19 
20 #ifndef _RICE_RENDER_BASE_H
21 #define _RICE_RENDER_BASE_H
22 
23 #include "osal_preproc.h"
24 #include "Debugger.h"
25 #include "RSP_Parser.h"
26 #include "Video.h"
27 
28 #include "../../Graphics/RSP/gSP_state.h"
29 
30 /*
31  *  Global variables defined in this file were moved out of Render class
32  *  to make them be accessed faster
33  */
34 
35 #define RICE_MATRIX_STACK   60
36 #define MAX_TEXTURES         8
37 
38 enum FillMode
39 {
40     RICE_FILLMODE_WINFRAME,
41     RICE_FILLMODE_SOLID,
42 };
43 
44 enum { MAX_VERTS = 80 };        // F3DLP.Rej supports up to 80 verts!
45 
46 void myVec3Transform(float *vecout, float *vecin, float* m);
47 
48 // All these arrays are moved out of the class CRender
49 // to be accessed in faster speed
50 extern ALIGN(16, XVECTOR4 g_vtxTransformed[MAX_VERTS]);
51 extern ALIGN(16, XVECTOR4 g_vecProjected[MAX_VERTS]);
52 extern float        g_vtxProjected5[1000][5];
53 extern float        g_vtxProjected5Clipped[2000][5];
54 extern VECTOR2      g_fVtxTxtCoords[MAX_VERTS];
55 extern uint32_t       g_dwVtxDifColor[MAX_VERTS];
56 //extern uint32_t     g_dwVtxFlags[MAX_VERTS];            // Z_POS Z_NEG etc
57 
58 extern RenderTexture g_textures[MAX_TEXTURES];
59 
60 extern TLITVERTEX       g_vtxBuffer[1000];
61 extern unsigned short   g_vtxIndex[1000];
62 
63 extern TLITVERTEX       g_clippedVtxBuffer[2000];
64 extern int              g_clippedVtxCount;
65 
66 extern uint8_t            g_oglVtxColors[1000][4];
67 extern uint32_t           g_clipFlag[MAX_VERTS];
68 extern uint32_t           g_clipFlag2[MAX_VERTS];
69 extern float            g_fFogCoord[MAX_VERTS];
70 
71 extern TLITVERTEX       g_texRectTVtx[4];
72 
73 extern EXTERNAL_VERTEX  g_vtxForExternal[MAX_VERTS];
74 
75 
76 //#define INIT_VERTEX_METHOD_2
77 
78 /*
79  *  Global variables
80  */
81 
82 /************************************************************************/
83 /*      Don't move                                                      */
84 /************************************************************************/
85 
86 extern Light              gRSPlights[16];
87 extern ALIGN(16, Matrix   gRSPworldProjectTransported);
88 extern ALIGN(16, Matrix   gRSPworldProject);
89 extern N64Light           gRSPn64lights[16];
90 extern ALIGN(16, Matrix   gRSPmodelViewTop);
91 extern ALIGN(16, Matrix   gRSPmodelViewTopTranspose);
92 extern float              gRSPfFogMin;
93 extern float              gRSPfFogMax;
94 extern float              gRSPfFogDivider;
95 
96 /************************************************************************/
97 /*      Don't move                                                      */
98 /************************************************************************/
99 typedef struct
100 {
101     /************************************************************************/
102     /*      Don't move                                                      */
103     /************************************************************************/
104     union {
105         struct {
106             float   fAmbientLightR;
107             float   fAmbientLightG;
108             float   fAmbientLightB;
109             float   fAmbientLightA;
110         };
111         float fAmbientColors[4];
112     };
113     /************************************************************************/
114     /*      Don't move above                                                */
115     /************************************************************************/
116     bool    bTextureEnabled;
117     uint32_t  curTile;
118     float   fTexScaleX;
119     float   fTexScaleY;
120 
121     RenderShadeMode shadeMode;
122     bool    bCullFront;
123     bool    bCullBack;
124     bool    bLightingEnable;
125     bool    bTextureGen;
126     bool    bFogEnabled;
127     bool    bZBufferEnabled;
128 
129     uint32_t  ambientLightColor;
130     uint32_t  ambientLightIndex;
131 
132     uint32_t  projectionMtxTop;
133     uint32_t  modelViewMtxTop;
134 
135     uint32_t  numVertices;
136     uint32_t  maxVertexID;
137 
138     int     nVPLeftN, nVPTopN, nVPRightN, nVPBottomN, nVPWidthN, nVPHeightN, maxZ;
139     int     clip_ratio_negx,    clip_ratio_negy,    clip_ratio_posx,    clip_ratio_posy;
140     int     clip_ratio_left,    clip_ratio_top, clip_ratio_right,   clip_ratio_bottom;
141     int     real_clip_scissor_left, real_clip_scissor_top,  real_clip_scissor_right,    real_clip_scissor_bottom;
142     float   real_clip_ratio_negx,   real_clip_ratio_negy,   real_clip_ratio_posx,   real_clip_ratio_posy;
143 
144     Matrix  projectionMtxs[RICE_MATRIX_STACK];
145     Matrix  modelviewMtxs[RICE_MATRIX_STACK];
146 
147     bool    bWorldMatrixIsUpdated;
148     bool    bMatrixIsUpdated;
149     bool    bCombinedMatrixIsUpdated;
150     bool    bLightIsUpdated;
151 
152     int     DKRCMatrixIndex;
153     int     DKRVtxCount;
154     bool    DKRBillBoard;
155     uint32_t  dwDKRVtxAddr;
156     uint32_t  dwDKRMatrixAddr;
157     Matrix  DKRMatrixes[4];
158     XVECTOR4  DKRBaseVec;
159 
160     int     ucode;
161     int     vertexMult;
162     bool    bNearClip;
163     bool    bRejectVtx;
164 
165     bool    bProcessDiffuseColor;
166     bool    bProcessSpecularColor;
167 
168     float   vtxXMul;
169     float   vtxXAdd;
170     float   vtxYMul;
171     float   vtxYAdd;
172 
173     // Texture coordinates computation constants
174     float   tex0scaleX;
175     float   tex0scaleY;
176     float   tex1scaleX;
177     float   tex1scaleY;
178     float   tex0OffsetX;
179     float   tex0OffsetY;
180     float   tex1OffsetX;
181     float   tex1OffsetY;
182     float   texGenYRatio;
183     float   texGenXRatio;
184 
185 } RSP_Options;
186 
187 extern ALIGN(16, RSP_Options gRSP);
188 
189 typedef struct
190 {
191     uint32_t  keyR;
192     uint32_t  keyG;
193     uint32_t  keyB;
194     uint32_t  keyA;
195     uint32_t  keyRGB;
196     uint32_t  keyRGBA;
197     float   fKeyA;
198 
199     bool    bFogEnableInBlender;
200 
201     uint32_t  fogColor;
202     uint32_t  primitiveColor;
203     uint32_t  envColor;
204     uint32_t  primitiveDepth;
205     uint32_t  primLODMin;
206     uint32_t  primLODFrac;
207     uint32_t  LODFrac;
208 
209     float   fPrimitiveDepth;
210     float   fvFogColor[4];
211     float   fvPrimitiveColor[4];
212     float   fvEnvColor[4];
213 
214     uint32_t  fillColor;
215     uint32_t  originalFillColor;
216 
217     RDP_OtherMode otherMode;
218 
219     TileAdditionalInfo    tilesinfo[8];
220     ScissorType scissor;
221 
222     bool    textureIsChanged;
223     bool    texturesAreReloaded;
224     bool    colorsAreReloaded;
225 } RDP_Options;
226 
227 extern ALIGN(16, RDP_Options gRDP);
228 
229 /*
230 *   Global functions
231 */
232 void InitRenderBase();
233 void SetFogMinMax(float fMin, float fMax, float fMul, float fOffset);
234 void InitVertex(uint32_t dwV, uint32_t vtxIndex, bool bTexture);
235 void InitVertexTextureConstants();
236 bool PrepareTriangle(uint32_t dwV0, uint32_t dwV1, uint32_t dwV2);
237 bool IsTriangleVisible(uint32_t dwV0, uint32_t dwV1, uint32_t dwV2);
238 extern void (*ProcessVertexData)(uint32_t dwAddr, uint32_t dwV0, uint32_t dwNum);
239 void ProcessVertexDataNoSSE(uint32_t dwAddr, uint32_t dwV0, uint32_t dwNum);
240 void ProcessVertexDataNEON(uint32_t dwAddr, uint32_t dwV0, uint32_t dwNum);
241 void ProcessVertexDataExternal(uint32_t dwAddr, uint32_t dwV0, uint32_t dwNum);
242 void SetPrimitiveColor(uint32_t dwCol, uint32_t LODMin, uint32_t LODFrac);
243 void SetPrimitiveDepth(uint32_t z, uint32_t dwDZ);
244 void SetVertexXYZ(uint32_t vertex, float x, float y, float z);
245 void ricegSPModifyVertex(uint32_t vtx, uint32_t where,  uint32_t val);
246 void ProcessVertexDataDKR(uint32_t dwAddr, uint32_t dwV0, uint32_t dwNum);
247 void ricegSPLightColor(uint32_t dwLight, uint32_t dwCol);
248 void SetLightDirection(uint32_t dwLight, float x, float y, float z, float range);
249 void ForceMainTextureIndex(int dwTile);
250 void UpdateCombinedMatrix();
251 
252 void ClipVertexes();
253 void ClipVertexesOpenGL();
254 void ClipVertexesForRect();
255 
256 void LogTextureCoords(float fTex0S, float fTex0T, float fTex1S, float fTex1T);
257 bool CheckTextureCoords(int tex);
258 void ResetTextureCoordsLog(float maxs0, float maxt0, float maxs1, float maxt1);
259 
ViewPortTranslatef_x(float x)260 inline float ViewPortTranslatef_x(float x) { return ( (x+1) * windowSetting.vpWidthW/2) + windowSetting.vpLeftW; }
ViewPortTranslatef_y(float y)261 inline float ViewPortTranslatef_y(float y) { return ( (1-y) * windowSetting.vpHeightW/2) + windowSetting.vpTopW; }
ViewPortTranslatei_x(int x)262 inline float ViewPortTranslatei_x(int x) { return x*windowSetting.fMultX; }
ViewPortTranslatei_y(int y)263 inline float ViewPortTranslatei_y(int y) { return y*windowSetting.fMultY; }
ViewPortTranslatei_x(float x)264 inline float ViewPortTranslatei_x(float x) { return x*windowSetting.fMultX; }
ViewPortTranslatei_y(float y)265 inline float ViewPortTranslatei_y(float y) { return y*windowSetting.fMultY; }
266 
GetPrimitiveDepth()267 inline float GetPrimitiveDepth() { return gRDP.fPrimitiveDepth; }
GetPrimitiveColor()268 inline uint32_t GetPrimitiveColor() { return gRDP.primitiveColor; }
GetPrimitiveColorfv()269 inline float* GetPrimitiveColorfv() { return gRDP.fvPrimitiveColor; }
GetLODFrac()270 inline uint32_t GetLODFrac() { return gRDP.LODFrac; }
GetEnvColor()271 inline uint32_t GetEnvColor() { return gRDP.envColor; }
GetEnvColorfv()272 inline float* GetEnvColorfv() { return gRDP.fvEnvColor; }
273 
SetAmbientLight(uint32_t color)274 inline void SetAmbientLight(uint32_t color)
275 {
276     gRSP.ambientLightColor = color;
277     gRSP.fAmbientLightR = (float)RGBA_GETRED(gRSP.ambientLightColor);
278     gRSP.fAmbientLightG = (float)RGBA_GETGREEN(gRSP.ambientLightColor);
279     gRSP.fAmbientLightB = (float)RGBA_GETBLUE(gRSP.ambientLightColor);
280     LIGHT_DUMP(TRACE1("Set Ambient Light: %08X", color));
281 }
282 
SetLighting(bool bLighting)283 inline void SetLighting(bool bLighting) { gRSP.bLightingEnable = bLighting; }
284 
285 // Generate texture coords?
SetTextureGen(bool bTextureGen)286 inline void SetTextureGen(bool bTextureGen) { gRSP.bTextureGen = bTextureGen; }
GetVertexDiffuseColor(uint32_t ver)287 inline COLOR GetVertexDiffuseColor(uint32_t ver) { return g_dwVtxDifColor[ver]; }
SetScreenMult(float fMultX,float fMultY)288 inline void SetScreenMult(float fMultX, float fMultY) { windowSetting.fMultX = fMultX; windowSetting.fMultY = fMultY; }
GetLightCol(uint32_t dwLight)289 inline COLOR GetLightCol(uint32_t dwLight) { return gRSPlights[dwLight].col; }
290 
291 void ricegSPNumLights(int32_t n);
292 
293 #endif
294 
295