1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2020, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
20 /**
21  * \file graphics/core/vertex.h
22  * \brief Vertex structs
23  */
24 
25 #pragma once
26 
27 
28 #include "graphics/core/color.h"
29 #include "graphics/core/type.h"
30 
31 #include "math/point.h"
32 #include "math/vector.h"
33 
34 #include <sstream>
35 #include <cstdint>
36 
37 
38 // Graphics module namespace
39 namespace Gfx
40 {
41 
42 enum VertexType
43 {
44     VERTEX_TYPE_NORMAL,
45     VERTEX_TYPE_TEX2,
46     VERTEX_TYPE_COL,
47 };
48 
49 /**
50  * \struct Vertex
51  * \brief Vertex of a primitive
52  *
53  * This structure was created as analog to DirectX's D3DVERTEX.
54  *
55  * It contains:
56  *  - vertex coordinates (x,y,z) as Math::Vector,
57  *  - normal coordinates (nx,ny,nz) as Math::Vector
58  *  - texture coordinates (u,v) as Math::Point.
59  */
60 struct Vertex
61 {
62     static constexpr VertexType VERTEX_TYPE = VERTEX_TYPE_NORMAL;
63 
64     Math::Vector coord;
65     Math::Vector normal;
66     Math::Point texCoord;
67 
68     explicit Vertex(Math::Vector aCoord = Math::Vector(),
69                     Math::Vector aNormal = Math::Vector(),
70                     Math::Point aTexCoord = Math::Point())
coordVertex71         : coord(aCoord), normal(aNormal),
72           texCoord(aTexCoord) {}
73 
74 
75     //! Returns a string "(c: [...], n: [...], tc: [...])"
ToStringVertex76     inline std::string ToString() const
77     {
78         std::stringstream s;
79         s.precision(3);
80         s << "(c: " << coord.ToString() << ", n: " << normal.ToString()
81           << ", tc: " << texCoord.ToString() << ")";
82         return s.str();
83     }
84 };
85 
86 /**
87  * \struct VertexCol
88  * \brief Colored vertex
89  *
90  * It contains:
91  *  - vertex coordinates (x,y,z) as Math::Vector,
92  *  - RGBA color as Color
93  */
94 struct VertexCol
95 {
96     static constexpr VertexType VERTEX_TYPE = VERTEX_TYPE_COL;
97 
98     Math::Vector coord;
99     Color color;
100 
101     VertexCol() = default;
102 
103     explicit VertexCol(Math::Vector aCoord,
104                        Color aColor = Color())
coordVertexCol105         : coord(aCoord), color(aColor) {}
106 
107     //! Returns a string "(c: [...], col: [...])"
ToStringVertexCol108     inline std::string ToString() const
109     {
110         std::stringstream s;
111         s.precision(3);
112         s << "(c: " << coord.ToString() << ", col: " << color.ToString() << ")";
113         return s.str();
114     }
115 };
116 
117 
118 /**
119  * \struct VertexTex2
120  * \brief Vertex with secondary texture coordinates
121  *
122  * In addition to fields from Vector, it contains
123  * secondary texture coordinates (u2, v2) as Math::Point
124  */
125 struct VertexTex2
126 {
127     static constexpr VertexType VERTEX_TYPE = VERTEX_TYPE_TEX2;
128 
129     Math::Vector coord;
130     Math::Vector normal;
131     Math::Point texCoord;
132     Math::Point texCoord2;
133 
134     explicit VertexTex2(Math::Vector aCoord = Math::Vector(),
135                         Math::Vector aNormal = Math::Vector(),
136                         Math::Point aTexCoord = Math::Point(),
137                         Math::Point aTexCoord2 = Math::Point())
coordVertexTex2138         : coord(aCoord), normal(aNormal),
139           texCoord(aTexCoord), texCoord2(aTexCoord2) {}
140 
141     //! Sets the fields from Vertex with texCoord2 = (0,0)
FromVertexVertexTex2142     void FromVertex(const Vertex &v)
143     {
144         coord = v.coord;
145         normal = v.normal;
146         texCoord = v.texCoord;
147         texCoord2 = Math::Point();
148     }
149 
150     //! Returns a string "(c: [...], n: [...], tc: [...], tc2: [...])"
ToStringVertexTex2151     inline std::string ToString() const
152     {
153         std::stringstream s;
154         s.precision(3);
155         s << "(c: " << coord.ToString() << ", n: " << normal.ToString()
156           << ", tc: " << texCoord.ToString() << ", tc2: " << texCoord2.ToString() << ")";
157         return s.str();
158     }
159 };
160 
161 
162 } // namespace Gfx
163 
164