PS2GL
OpenGL*-like API for the PS2
Loading...
Searching...
No Matches
material.cpp
1/* Copyright (C) 2000,2001,2002 Sony Computer Entertainment America
2
3 This file is subject to the terms and conditions of the GNU Lesser
4 General Public License Version 2.1. See the file "COPYING" in the
5 main directory of this archive for more details. */
6
7#include "ps2gl/material.h"
8#include "ps2gl/dlist.h"
9#include "ps2gl/glcontext.h"
10#include "ps2gl/lighting.h"
11
12/********************************************
13 * CImmMaterial
14 */
15
16CImmMaterial::CImmMaterial(CGLContext& context)
17 : CMaterial(context)
18 , Ambient(0.2f, 0.2f, 0.2f, 1.0f)
19 , Diffuse(0.8f, 0.8f, 0.8f, 1.0f)
20 , Specular(0.0f, 0.0f, 0.0f, 1.0f)
21 , Emission(0.0f, 0.0f, 0.0f, 1.0f)
22 , Shininess(0.0f)
23{
24}
25
26void CImmMaterial::SetSpecular(cpu_vec_xyzw specular)
27{
28 TellRendererMaterialChanged();
29 Specular = specular;
30
31 if (Specular != cpu_vec_4(0, 0, 0, 0))
32 GLContext.GetImmLighting().MaterialHasSpecular();
33 else {
34 GLContext.SpecularEnabledChanged();
35 GLContext.GetImmGeomManager().GetRendererManager().SpecularEnabledChanged(false);
36 }
37}
38
39void CImmMaterial::LightsHaveSpecular()
40{
41 if (Specular != cpu_vec_4(0, 0, 0, 0)) {
42 GLContext.SpecularEnabledChanged();
43 GLContext.GetImmGeomManager().GetRendererManager().SpecularEnabledChanged(true);
44 }
45}
46
47/********************************************
48 * CDListMaterial
49 */
50
52 cpu_vec_xyzw Value;
53 GLenum Property;
54
55public:
56 CSetMaterialPropCmd(GLenum prop, cpu_vec_xyzw value)
57 : Value(value)
58 , Property(prop)
59 {
60 }
61 CDListCmd* Play()
62 {
63 glMaterialfv(GL_FRONT, Property, reinterpret_cast<float*>(&Value));
64 return CDListCmd::GetNextCmd(this);
65 }
66};
67
68void CDListMaterial::SetAmbient(cpu_vec_xyzw ambient)
69{
70 CDList& dlist = pGLContext->GetDListManager().GetOpenDList();
71 dlist += CSetMaterialPropCmd(GL_AMBIENT, ambient);
72 TellRendererMaterialChanged();
73}
74
75void CDListMaterial::SetDiffuse(cpu_vec_xyzw diffuse)
76{
77 CDList& dlist = pGLContext->GetDListManager().GetOpenDList();
78 dlist += CSetMaterialPropCmd(GL_DIFFUSE, diffuse);
79 TellRendererMaterialChanged();
80}
81
82void CDListMaterial::SetSpecular(cpu_vec_xyzw specular)
83{
84 CDList& dlist = pGLContext->GetDListManager().GetOpenDList();
85 dlist += CSetMaterialPropCmd(GL_SPECULAR, specular);
86 TellRendererMaterialChanged();
87 GLContext.SpecularEnabledChanged(); // maybe
88}
89
90void CDListMaterial::SetEmission(cpu_vec_xyzw emission)
91{
92 CDList& dlist = pGLContext->GetDListManager().GetOpenDList();
93 dlist += CSetMaterialPropCmd(GL_EMISSION, emission);
94 TellRendererMaterialChanged();
95}
96
97void CDListMaterial::SetShininess(float shine)
98{
99 CDList& dlist = pGLContext->GetDListManager().GetOpenDList();
100 dlist += CSetMaterialPropCmd(GL_SHININESS, cpu_vec_xyzw(shine, 0, 0, 0));
101 TellRendererMaterialChanged();
102}
103
104/********************************************
105 * CMaterialManager
106 */
107
108void CMaterialManager::Color(cpu_vec_xyzw color)
109{
110 CurColor = color;
111
112 if (UseColorMaterial) {
113 switch (ColorMaterialMode) {
114 case GL_EMISSION:
115 CurMaterial->SetEmission(color);
116 break;
117 case GL_AMBIENT:
118 CurMaterial->SetAmbient(color);
119 break;
120 case GL_DIFFUSE:
121 CurMaterial->SetDiffuse(color);
122 break;
123 case GL_SPECULAR:
124 CurMaterial->SetSpecular(color);
125 break;
126 case GL_AMBIENT_AND_DIFFUSE:
127 CurMaterial->SetAmbient(color);
128 CurMaterial->SetDiffuse(color);
129 break;
130 }
131 }
132 GLContext.CurMaterialChanged();
133}
134
136 bool UseCM;
137
138public:
140 : UseCM(use)
141 {
142 }
143 CDListCmd* Play()
144 {
145 pGLContext->GetMaterialManager().SetUseColorMaterial(UseCM);
146 return CDListCmd::GetNextCmd(this);
147 }
148};
149
150void CMaterialManager::SetUseColorMaterial(bool yesNo)
151{
152 if (!InDListDef) {
153 UseColorMaterial = yesNo;
154 if (yesNo)
155 Color(CurColor);
156 GLContext.CurMaterialChanged();
157 } else {
158 CDList& dlist = GLContext.GetDListManager().GetOpenDList();
159 dlist += CSetUseColorMaterialCmd(yesNo);
160 GLContext.CurMaterialChanged();
161 }
162}
163
165 GLenum Mode;
166
167public:
168 CSetColorMatModeCmd(GLenum mode)
169 : Mode(mode)
170 {
171 }
172 CDListCmd* Play()
173 {
174 pGLContext->GetMaterialManager().SetColorMaterialMode(Mode);
175 return CDListCmd::GetNextCmd(this);
176 }
177};
178
179void CMaterialManager::SetColorMaterialMode(GLenum mode)
180{
181 if (!InDListDef) {
182 ColorMaterialMode = mode;
183 Color(CurColor);
184 GLContext.CurMaterialChanged();
185 } else {
186 CDList& dlist = GLContext.GetDListManager().GetOpenDList();
187 dlist += CSetColorMatModeCmd(mode);
188 GLContext.CurMaterialChanged();
189 }
190}
191
192/********************************************
193 * gl interface
194 */
195
196void glMaterialfv(GLenum face, GLenum pname, const GLfloat* params)
197{
198 GL_FUNC_DEBUG("%s\n", __FUNCTION__);
199
200 CMaterial& material = pGLContext->GetMaterialManager().GetCurMaterial();
201
202 switch (face) {
203 case GL_BACK:
204 mNotImplemented("GL_BACK - only front material is available");
205 break;
206 case GL_FRONT_AND_BACK:
207 mNotImplemented("GL_FRONT_AND_BACK - only front material is available");
208 case GL_FRONT:
209 switch (pname) {
210 case GL_AMBIENT:
211 material.SetAmbient(cpu_vec_xyzw(params[0], params[1], params[2], params[3]));
212 break;
213 case GL_DIFFUSE:
214 material.SetDiffuse(cpu_vec_xyzw(params[0], params[1], params[2], params[3]));
215 break;
216 case GL_SPECULAR:
217 material.SetSpecular(cpu_vec_xyzw(params[0], params[1], params[2], params[3]));
218 break;
219 case GL_EMISSION:
220 material.SetEmission(cpu_vec_xyzw(params[0], params[1], params[2], params[3]));
221 break;
222 case GL_SHININESS:
223 material.SetShininess(*params);
224 break;
225 case GL_AMBIENT_AND_DIFFUSE:
226 material.SetAmbient(cpu_vec_xyzw(params[0], params[1], params[2], params[3]));
227 material.SetDiffuse(cpu_vec_xyzw(params[0], params[1], params[2], params[3]));
228 break;
229 default:
230 mError("shouldn't get here");
231 }
232 break;
233 default:
234 mError("shouldn't get here");
235 }
236}
237
238void glMaterialf(GLenum face, GLenum pname, GLfloat param)
239{
240 GL_FUNC_DEBUG("%s\n", __FUNCTION__);
241
242 CMaterial& material = pGLContext->GetMaterialManager().GetCurMaterial();
243
244 switch (face) {
245 case GL_BACK:
246 mNotImplemented("GL_BACK - only front material is available");
247 break;
248 case GL_FRONT_AND_BACK:
249 mNotImplemented("GL_FRONT_AND_BACK - only front material is available");
250 case GL_FRONT:
251 if (pname == GL_SHININESS)
252 material.SetShininess(param);
253 else {
254 mError("shoudn't get here");
255 }
256 break;
257 default:
258 mError("shouldn't get here");
259 }
260}
261
262void glColorMaterial(GLenum face, GLenum mode)
263{
264 GL_FUNC_DEBUG("%s\n", __FUNCTION__);
265
266 pGLContext->GetMaterialManager().SetColorMaterialMode(mode);
267}