forked from LibreCAD/LibreCAD_3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shape_entity.cpp
executable file
·209 lines (163 loc) · 4.58 KB
/
shape_entity.cpp
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "shape_entity.h"
using namespace lc::viewer::opengl;
Shape_Entity :: Shape_Entity()
{
_fill_mode=GL_LINE;
_render_mode=GL_LINE_STRIP_ADJACENCY;
_model=glm::mat4(1.0f);
_linewidth=1.0f;
_dashes_sum=0.0f;
_dashes_size=0;
_type=Entity_Type::BASIC;
_shader=NULL;
}
Shape_Entity::~Shape_Entity()
{
}
void Shape_Entity::loadVertexData(float* vertices,int size,std::vector<int> &jumps)
{
//--------VAO-----------
_vao.gen();
//--------VBO ------
_vbo.gen(vertices , size);
_jumps=jumps;
//--------layout--------
VertexBufferLayout layout;
layout.push<float>(3); // (x,y,z)
layout.push<float>(1); // (d)
//--------attaching VB and (its)layout to VA---
_vao.addBuffer(_vbo,layout);
}
void Shape_Entity::bind()
{
//---------bind--------------
_vbo.bind();
_vao.bind();
}
void Shape_Entity::unbind()
{
//---------unbind--------------
_vbo.unbind();
_vao.unbind();
}
void Shape_Entity::setType(Shaders_book& shaders)
{
if( _type == Entity_Type::BASIC ) // By default
{
_shader=shaders.basic_shader;
_fill_mode=GL_LINE;
_render_mode=GL_LINE_STRIP_ADJACENCY;
}
else if( _type == Entity_Type::FILL ) // Filled shape
{
_shader=shaders.basic_shader;
_fill_mode=GL_FILL;
_render_mode=GL_TRIANGLE_FAN;
}
else if( _type == Entity_Type::THICK ) // Thickline
{
_shader=shaders.thickline_shader;
_fill_mode=GL_FILL;
_render_mode=GL_LINE_STRIP_ADJACENCY;
}
else if( _type == Entity_Type::PATTERN ) // Pattern
{
_shader=shaders.linepattern_shader;
_fill_mode=GL_FILL;
_render_mode=GL_LINE_STRIP_ADJACENCY;
}
}
void Shape_Entity::setModelMatrix(glm::mat4 model)
{
_model=model;
}
void Shape_Entity::setFillMode(bool fill)
{
if(fill==true)
_type = Entity_Type::FILL; // Will set the type to FILL
}
void Shape_Entity::setLineWidth(float width)
{
_linewidth=width;
if(width>1.0f)
{
_type = Entity_Type::THICK; // if width>1 change type to THICK
}
}
void Shape_Entity::setDashes(std::vector<float> &dashes, int num_dashes,float sum_dashes)
{
if(num_dashes>0)
{
_dashes_size=num_dashes;
_dashes_sum=sum_dashes;
_dashes=dashes;
_type = Entity_Type::PATTERN;
}
}
void Shape_Entity::setColor(float R,float G,float B,float A)
{
// NO Need
}
void Shape_Entity::addLinearGradient(float x0,float y0,float x1,float y1)
{
// NO Need (Used by Gradient_Entity)
}
void Shape_Entity::addGradientColorPoint(float R,float G,float B,float A)
{
// NO Need (Used by Gradient_Entity)
}
void Shape_Entity::applyGradient(float* vertices,int size)
{
// NO Need (Used by Gradient_Entity)
}
void Shape_Entity::setFont(Font_Book& fonts,const std::string& style)
{
// NO Need (Used by Tex_Entity)
}
void Shape_Entity::addTextData(glm::vec4 pos, std::string text_val , float font_size, bool retain)
{
// NO Need (Used by Text_Entity)
}
void Shape_Entity::freeGPU()
{
_vbo.freeGPU();
_vao.freeGPU();
}
void Shape_Entity::draw(glm::mat4 _proj,glm::mat4 projB,glm::mat4 _view)
{
//Set the Fill Mode
glPolygonMode(GL_FRONT_AND_BACK, _fill_mode);
_shader->bind();
//compute the MVP matrix ( received -V ,-P .. already have -M)
glm::mat4 mvp=_proj * _view * _model;
_shader->setUniformMat4f("u_MVP",mvp); // Set MVP
//----------------------------------------------------------
if( _type == Entity_Type::THICK ) //Set the Width (if it is THICK)
{
_shader->setUniform1f("u_W",_linewidth);
// _shader->setUniform1f("u_W",std::max(_linewidth,_linewidth*_view[2][2] ) ); // If width is magnified..
}
if(_type == Entity_Type::PATTERN ) // Set Width,projB,Dashes (If it is PATTERN)
{
_shader->setUniform1f("u_W",_linewidth);
//_shader->setUniform1f("u_W",std::max(_linewidth,_linewidth*_view[2][2] ) ); // If width is magnified..
_view[3][0]=0;
_view[3][1]=0; //neglect translations
glm::mat4 scale=glm::scale(glm::mat4(1.0f),glm::vec3(_view[2][2],_view[2][2],_view[2][2]));
_shader->setUniformMat4f("u_X", (projB*scale) ); // Set u_X
// SET the dashes
_shader->setUniform1fv("dashes",_dashes_size,&_dashes[0]);
_shader->setUniform1i("dashes_size",_dashes_size);
_shader->setUniform1f("dashes_sum",_dashes_sum);
}
// Bind this Entity
this->bind();
//finally draw
std::vector<int> :: iterator it;
int l=0;
for(it=_jumps.begin();it!=_jumps.end();it++)
{
glDrawArrays(_render_mode,l,*(it));
l+=*(it);
}
}