Arachnoid GLESv1.1 plugin. Compile and run (a bit glitchy and no frameskip) on the...
[mupen64plus-pandora.git] / source / mupen64plus-video-arachnoid / src / RSP / RSPLightManager.cpp
1 /******************************************************************************
2  * Arachnoid Graphics Plugin for Mupen64Plus
3  * http://bitbucket.org/wahrhaft/mupen64plus-video-arachnoid/
4  *
5  * Copyright (C) 2007 Kristofer Karlsson, Rickard Niklasson
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  *****************************************************************************/
21
22 #include "RSPLightManager.h"
23 #include "Memory.h"
24 #include "MathLib.h"
25 #include "GBI.h"
26 #include "Logger.h"
27
28 #define RGBA_GETALPHA(rgb)      ((rgb) >> 24)
29 #define RGBA_GETRED(rgb)        (((rgb) >> 16) & 0xff)
30 #define RGBA_GETGREEN(rgb)      (((rgb) >> 8) & 0xff)
31 #define RGBA_GETBLUE(rgb)       ((rgb) & 0xff)
32
33 //-----------------------------------------------------------------------------
34 //! Constructor
35 //-----------------------------------------------------------------------------
36 RSPLightManager::RSPLightManager() 
37 {
38 }
39
40 //-----------------------------------------------------------------------------
41 //! Destructor
42 //-----------------------------------------------------------------------------
43 RSPLightManager::~RSPLightManager()
44 {
45 }
46
47 bool RSPLightManager::initialize(Memory* memory)
48 {
49     m_memory = memory;
50     m_numLights = 0;
51     m_lightEnabled = false;
52
53     return true;
54 }
55
56 //-----------------------------------------------------------------------------
57 // Set Light
58 //-----------------------------------------------------------------------------
59 void RSPLightManager::setLight( unsigned int lightIndex, unsigned int rdramAddress  )
60 {
61     //Error control 
62     if ((rdramAddress + sizeof(RDRAMLight)) > m_memory->getRDRAMSize() ) {
63         return;
64     }
65
66     //Get Light from memory
67     RDRAMLight* light = (RDRAMLight*)m_memory->getRDRAM(rdramAddress);
68
69     if ( lightIndex < 8 ) //Only supports 8 lights
70     {
71         m_lights[lightIndex].r = light->r * 0.0039215689f;  //Convert from 0-255 to 0-1
72         m_lights[lightIndex].g = light->g * 0.0039215689f;
73         m_lights[lightIndex].b = light->b * 0.0039215689f;
74
75         m_lights[lightIndex].x = light->x;
76         m_lights[lightIndex].y = light->y;
77         m_lights[lightIndex].z = light->z;    
78
79         Vec3Normalize( &m_lights[lightIndex].x );
80     }
81 }
82
83 //-----------------------------------------------------------------------------
84 // Set Num Lights
85 //-----------------------------------------------------------------------------
86 void RSPLightManager::setNumLights(int numLights)
87 {
88     if (numLights <= 8) 
89     {
90         m_numLights = numLights;    
91     }
92 }
93
94 //-----------------------------------------------------------------------------
95 // Set Light Color
96 //-----------------------------------------------------------------------------
97 void RSPLightManager::setLightColor( unsigned int lightIndex, unsigned int packedColor )
98 {
99     if (lightIndex < 8)
100     {
101         m_lights[lightIndex].r = _SHIFTR( packedColor, 24, 8 ) * 0.0039215689f;
102         m_lights[lightIndex].g = _SHIFTR( packedColor, 16, 8 ) * 0.0039215689f;
103         m_lights[lightIndex].b = _SHIFTR( packedColor, 8, 8 ) * 0.0039215689f;
104     }
105 }