-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVertexAttrib.h
153 lines (132 loc) · 3.83 KB
/
VertexAttrib.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#ifndef _VERTEXATTRIB_H_
#define _VERTEXATTRIB_H_
#include <glm/glm.hpp>
#include "IVertexData.h"
#include <sstream>
/*
* This class represents the attributes of a single vertex. It is useful in building
* PolygonMesh objects for many examples.
*
* It implements the IVertexData interface so that it can be converted into an array
* of floats, to work with OpenGL buffers
*/
class VertexAttrib:public util::IVertexData
{
public:
VertexAttrib()
{
position = glm::vec4(0,0,0,1);
normal = glm::vec4(0,0,0,0);
texcoord = glm::vec4(0,0,0,1);
}
~VertexAttrib(){}
bool hasData(string attribName)
{
if ((attribName == "position")
|| (attribName == "normal")
|| (attribName == "texcoord"))
{
return true;
}
else
{
return false;
}
}
vector<float> getData(string attribName)
{
vector<float> result;
stringstream message;
if (attribName == "position")
{
result.push_back(position.x);
result.push_back(position.y);
result.push_back(position.z);
result.push_back(position.w);
}
else if (attribName == "normal")
{
result.push_back(normal.x);
result.push_back(normal.y);
result.push_back(normal.z);
result.push_back(normal.w);
}
else if (attribName == "texcoord")
{
result.push_back(texcoord.x);
result.push_back(texcoord.y);
result.push_back(texcoord.z);
result.push_back(texcoord.w);
}
else
{
message << "No attribute: " << attribName << " found!";
throw runtime_error(message.str());
}
return result;
}
void setData(string attribName, const vector<float>& data)
{
stringstream message;
if (attribName == "position")
{
position = glm::vec4(0,0,0,1);
switch (data.size()) {
case 4: position.w = data[3];
case 3: position.z = data[2];
case 2: position.y = data[1];
case 1: position.x = data[0];
break;
default:
message << "Too much data for attribute: " << attribName;
throw runtime_error(message.str());
}
}
else if (attribName == "normal")
{
normal = glm::vec4(0,0,0,0);
switch (data.size()) {
case 4: normal.w = data[3];
case 3: normal.z = data[2];
case 2: normal.y = data[1];
case 1: normal.x = data[0];
break;
default:
message << "Too much data for attribute: " << attribName;
throw runtime_error(message.str());
}
}
else if (attribName == "texcoord")
{
texcoord = glm::vec4(0,0,0,1);
switch (data.size()) {
case 4: texcoord.w = data[3];
case 3: texcoord.z = data[2];
case 2: texcoord.y = data[1];
case 1: texcoord.x = data[0];
break;
default:
message << "Too much data for attribute: " << attribName;
throw runtime_error(message.str());
}
}
else
{
message << "Attribute: " << attribName << " unsupported!";
throw runtime_error(message.str());
}
}
vector<string> getAllAttributes()
{
vector<string> attributes;
attributes.push_back("position");
attributes.push_back("normal");
attributes.push_back("texcoord");
return attributes;
}
private:
glm::vec4 position;
glm::vec4 normal;
glm::vec4 texcoord;
};
#endif