1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
26 // File: vogl_material_state.cpp
27 #include "vogl_material_state.h"
29 vogl_material_state::vogl_material_state()
35 vogl_material_state::~vogl_material_state()
40 bool vogl_material_state::snapshot(const vogl_context_info &context_info)
44 VOGL_NOTE_UNUSED(context_info);
50 bool any_gl_errors = false;
52 #define GET_FLOAT(side, pname) \
55 float values[4] = { 0, 0, 0, 0 }; \
56 GL_ENTRYPOINT(glGetMaterialfv)(get_face(side), pname, values); \
57 if (vogl_check_gl_error()) \
58 any_gl_errors = true; \
59 m_params[side].insert(pname, 0, values, sizeof(values[0])); \
61 for (uint s = 0; s < cTotalSides; s++)
63 GET_FLOAT(s, GL_AMBIENT);
64 GET_FLOAT(s, GL_DIFFUSE);
65 GET_FLOAT(s, GL_SPECULAR);
66 GET_FLOAT(s, GL_EMISSION);
67 GET_FLOAT(s, GL_SHININESS);
68 GET_FLOAT(s, GL_COLOR_INDEXES);
76 vogl_error_printf("%s: GL error while enumerating material params\n", VOGL_METHOD_NAME);
85 bool vogl_material_state::set_material_parameter(uint side, GLenum pname) const
89 const GLenum face = get_face(side);
91 const vogl_state_data *pData = m_params[side].find(pname);
102 if (pData->get_num_elements() > cMaxElements)
108 if ((pData->get_data_type() == cSTFloat) || (pData->get_data_type() == cSTDouble))
110 float fvals[cMaxElements];
111 pData->get_float(fvals);
112 if (pData->get_num_elements() == 1)
113 GL_ENTRYPOINT(glMaterialf)(face, pname, fvals[0]);
115 GL_ENTRYPOINT(glMaterialfv)(face, pname, fvals);
119 int ivals[cMaxElements];
120 pData->get_int(ivals);
121 if (pData->get_num_elements() == 1)
122 GL_ENTRYPOINT(glMateriali)(face, pname, ivals[0]);
124 GL_ENTRYPOINT(glMaterialiv)(face, pname, ivals);
127 return !vogl_check_gl_error();
130 bool vogl_material_state::restore(const vogl_context_info &context_info) const
134 VOGL_NOTE_UNUSED(context_info);
144 #define SET_FLOAT(side, pname) set_material_parameter(side, pname)
145 for (uint s = 0; s < cTotalSides; s++)
147 SET_FLOAT(s, GL_AMBIENT);
148 SET_FLOAT(s, GL_DIFFUSE);
149 SET_FLOAT(s, GL_SPECULAR);
150 SET_FLOAT(s, GL_EMISSION);
151 SET_FLOAT(s, GL_SHININESS);
152 SET_FLOAT(s, GL_COLOR_INDEXES);
156 return !vogl_check_gl_error();
159 void vogl_material_state::clear()
163 for (uint i = 0; i < cTotalSides; i++)
169 bool vogl_material_state::serialize(json_node &node, vogl_blob_manager &blob_manager) const
173 if (!m_params[cFront].serialize(node.add_object("front"), blob_manager) || !m_params[cBack].serialize(node.add_object("back"), blob_manager))
179 bool vogl_material_state::deserialize(const json_node &node, const vogl_blob_manager &blob_manager)
185 if (!m_params[cFront].deserialize("front", node, blob_manager) || !m_params[cBack].deserialize("back", node, blob_manager))
196 vogl_state_vector *vogl_material_state::get_state_vector(int side)
200 if (side < 0 || side >= cTotalSides)
205 return &(m_params[side]);
208 const vogl_state_vector *vogl_material_state::get_state_vector(int side) const
212 if (side < 0 || side >= cTotalSides)
217 return &(m_params[side]);