]> git.cworth.org Git - vogl/blob - src/voglcommon/vogl_current_vertex_attrib_state.cpp
Initial vogl checkin
[vogl] / src / voglcommon / vogl_current_vertex_attrib_state.cpp
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * All Rights Reserved.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26 // File: vogl_current_vertex_attrib_state.cpp
27 #include "vogl_current_vertex_attrib_state.h"
28 #include "vogl_context_info.h"
29
30 vogl_current_vertex_attrib_state::vogl_current_vertex_attrib_state()
31     : m_valid(false)
32 {
33 }
34
35 vogl_current_vertex_attrib_state::~vogl_current_vertex_attrib_state()
36 {
37 }
38
39 bool vogl_current_vertex_attrib_state::snapshot(const vogl_context_info &context_info)
40 {
41     clear();
42
43     VOGL_CHECK_GL_ERROR;
44
45     m_current_vertex_attribs.resize(context_info.get_max_vertex_attribs());
46
47     if (context_info.get_max_vertex_attribs())
48     {
49         m_current_vertex_attribs[0].set(0, 0, 0, 1);
50
51         for (uint i = 1; i < context_info.get_max_vertex_attribs(); i++)
52         {
53             GL_ENTRYPOINT(glGetVertexAttribdv)(i, GL_CURRENT_VERTEX_ATTRIB, m_current_vertex_attribs[i].get_ptr());
54
55             VOGL_CHECK_GL_ERROR;
56         }
57     }
58
59     m_valid = true;
60
61     return true;
62 }
63
64 bool vogl_current_vertex_attrib_state::restore(const vogl_context_info &context_info) const
65 {
66     if (!m_valid)
67         return false;
68
69     VOGL_CHECK_GL_ERROR;
70
71     if (m_current_vertex_attribs.size() > context_info.get_max_vertex_attribs())
72     {
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());
74     }
75
76     uint max_vertex_attribs_to_restore = math::minimum<uint>(m_current_vertex_attribs.size(), context_info.get_max_vertex_attribs());
77
78     for (uint i = 1; i < max_vertex_attribs_to_restore; i++)
79     {
80         GL_ENTRYPOINT(glVertexAttrib4dv)(i, m_current_vertex_attribs[i].get_ptr());
81
82         VOGL_CHECK_GL_ERROR;
83     }
84
85     return true;
86 }
87
88 void vogl_current_vertex_attrib_state::clear()
89 {
90     m_current_vertex_attribs.clear();
91     m_valid = false;
92 }
93
94 bool vogl_current_vertex_attrib_state::serialize(json_node &node, vogl_blob_manager &blob_manager) const
95 {
96     VOGL_NOTE_UNUSED(blob_manager);
97
98     if (!m_valid)
99         return false;
100
101     node.init_array();
102     for (uint i = 0; i < m_current_vertex_attribs.size(); i++)
103     {
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]);
107     }
108
109     return true;
110 }
111
112 bool vogl_current_vertex_attrib_state::deserialize(const json_node &node, const vogl_blob_manager &blob_manager)
113 {
114     VOGL_NOTE_UNUSED(blob_manager);
115
116     clear();
117
118     if (!node.are_all_children_arrays())
119         return false;
120
121     m_current_vertex_attribs.resize(node.size());
122     for (uint i = 0; i < node.size(); i++)
123     {
124         const json_node &vec_node = *node.get_child(i);
125         if (vec_node.size() != 4)
126         {
127             clear();
128             return false;
129         }
130
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());
133     }
134
135     m_valid = true;
136
137     return true;
138 }