1 /*
2  *******************************************************************************************
3  * Dgame (a D game framework) - Copyright (c) Randy Schütt
4  * 
5  * This software is provided 'as-is', without any express or implied warranty.
6  * In no event will the authors be held liable for any damages arising from
7  * the use of this software.
8  * 
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  * 
13  * 1. The origin of this software must not be misrepresented; you must not claim
14  *    that you wrote the original software. If you use this software in a product,
15  *    an acknowledgment in the product documentation would be appreciated but is
16  *    not required.
17  * 
18  * 2. Altered source versions must be plainly marked as such, and must not be
19  *    misrepresented as being the original software.
20  * 
21  * 3. This notice may not be removed or altered from any source distribution.
22  *******************************************************************************************
23  */
24 module Dgame.Math.Vertex;
25 
26 public {
27 	debug import std.stdio : writeln;
28 	
29 	import Dgame.Math.Vector2;
30 	import Dgame.Math.Vector3;
31 	import Dgame.Graphics.Color;
32 }
33 
34 /**
35  * A Vertex has coordinates, a color and texture coordinates
36  *
37  * Author: rschuett
38  */
39 struct Vertex {
40 	/**
41 	 * The coordinates
42 	 */
43 	float x, y, z;
44 	/**
45 	 * The color components
46 	 */
47 	float r, g, b, a;
48 	/**
49 	 * The texcoords
50 	 */
51 	float tx, ty;
52 	
53 	/**
54 	 * Disable default CTor
55 	 */
56 	@disable
57 	this();
58 	
59 	/**
60 	 * CTor
61 	 */
62 	this(float x, float y, float z = 0f, float tx = 0f, float ty = 0f) pure nothrow {
63 		this.x = x;
64 		this.y = y;
65 		this.z = z;
66 		
67 		this.tx = tx;
68 		this.ty = ty;
69 		
70 		this.setColor(Color.Black);
71 	}
72 	
73 	/**
74 	 * CTor
75 	 */
76 	this(ref const Vector2f position) pure nothrow {
77 		this(position.x, position.y);
78 	}
79 	
80 	/**
81 	 * CTor
82 	 */
83 	this(ref const Vector2f position, ref const Vector2f texcoord,
84 	     ref const Color col) pure nothrow
85 	{
86 		this.setPosition(position);
87 		this.setTexCoord(texcoord);
88 		this.setColor(col);
89 	}
90 	
91 	/**
92 	 * CTor
93 	 */
94 	this(ref const Vector2f position, ref const Color col) pure nothrow {
95 		this.setPosition(position);
96 		this.setColor(col);
97 		
98 		this.tx = 0f;
99 		this.ty = 0f;
100 	}
101 	
102 	/**
103 	 * Set a (new) position
104 	 */
105 	void setPosition(ref const Vector2f position) pure nothrow {
106 		this.x = position.x;
107 		this.y = position.y;
108 		this.z = 0f;
109 	}
110 	
111 	/**
112 	 * Set a (new) color
113 	 */
114 	void setColor(ref const Color col) pure nothrow {
115 		const float[4] rgba = col.asGLColor();
116 		
117 		this.r = rgba[0];
118 		this.g = rgba[1];
119 		this.b = rgba[2];
120 		this.a = rgba[3];
121 	}
122 	
123 	/**
124 	 * Set (new) texcoords
125 	 */
126 	void setTexCoord(ref const Vector2f texcoord) pure nothrow {
127 		this.tx = texcoord.x;
128 		this.ty = texcoord.y;
129 	}
130 	
131 	/**
132 	 * Create a Color from the color data
133 	 */
134 	Color getAsColor() const {
135 		return Color(this.r, this.g, this.b, this.a);
136 	}
137 	
138 	/**
139 	 * Create a Vector3f from the position data.
140 	 */
141 	Vector3f getPositionAsVector() const pure nothrow {
142 		return Vector3f(this.x, this.y, this.z);
143 	}
144 	
145 	/**
146 	 * Create a Vector2f from the position data (the z coordinate is ignored).
147 	 */
148 	Vector2f getTexCoordAsVector() const pure nothrow {
149 		return Vector2f(this.tx, this.ty);
150 	}
151 }