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_current_vertex_attrib_state.cpp
27 #include "vogl_current_vertex_attrib_state.h"
28 #include "vogl_context_info.h"
30 vogl_current_vertex_attrib_state::vogl_current_vertex_attrib_state()
35 vogl_current_vertex_attrib_state::~vogl_current_vertex_attrib_state()
39 bool vogl_current_vertex_attrib_state::snapshot(const vogl_context_info &context_info)
45 m_current_vertex_attribs.resize(context_info.get_max_vertex_attribs());
47 if (context_info.get_max_vertex_attribs())
49 m_current_vertex_attribs[0].set(0, 0, 0, 1);
51 for (uint i = 1; i < context_info.get_max_vertex_attribs(); i++)
53 GL_ENTRYPOINT(glGetVertexAttribdv)(i, GL_CURRENT_VERTEX_ATTRIB, m_current_vertex_attribs[i].get_ptr());
64 bool vogl_current_vertex_attrib_state::restore(const vogl_context_info &context_info) const
71 if (m_current_vertex_attribs.size() > context_info.get_max_vertex_attribs())
73 vogl_error_printf("%s: Unable to restore all current vertex attribs, object has %u attribs, context only supports %u attribs\n", VOGL_METHOD_NAME, m_current_vertex_attribs.size(), context_info.get_max_vertex_attribs());
76 uint max_vertex_attribs_to_restore = math::minimum<uint>(m_current_vertex_attribs.size(), context_info.get_max_vertex_attribs());
78 for (uint i = 1; i < max_vertex_attribs_to_restore; i++)
80 GL_ENTRYPOINT(glVertexAttrib4dv)(i, m_current_vertex_attribs[i].get_ptr());
88 void vogl_current_vertex_attrib_state::clear()
90 m_current_vertex_attribs.clear();
94 bool vogl_current_vertex_attrib_state::serialize(json_node &node, vogl_blob_manager &blob_manager) const
96 VOGL_NOTE_UNUSED(blob_manager);
102 for (uint i = 0; i < m_current_vertex_attribs.size(); i++)
104 json_node &vec_node = node.add_array();
105 for (uint j = 0; j < 4; j++)
106 vec_node.add_value(m_current_vertex_attribs[i][j]);
112 bool vogl_current_vertex_attrib_state::deserialize(const json_node &node, const vogl_blob_manager &blob_manager)
114 VOGL_NOTE_UNUSED(blob_manager);
118 if (!node.are_all_children_arrays())
121 m_current_vertex_attribs.resize(node.size());
122 for (uint i = 0; i < node.size(); i++)
124 const json_node &vec_node = *node.get_child(i);
125 if (vec_node.size() != 4)
131 // TODO: We could have roundtrip errors here.
132 m_current_vertex_attribs[i].set(vec_node[0U].as_double(), vec_node[1U].as_double(), vec_node[2U].as_double(), vec_node[3U].as_double());