-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObject2D.cpp
More file actions
106 lines (83 loc) · 2.57 KB
/
Copy pathObject2D.cpp
File metadata and controls
106 lines (83 loc) · 2.57 KB
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
#include "Object2D.h"
#include <Core/Engine.h>
#include <math.h>
Mesh* Object2D::CreateSquare(std::string name, glm::vec3 leftBottomCorner, float length, glm::vec3 color, bool fill)
{
glm::vec3 corner = leftBottomCorner;
std::vector<VertexFormat> vertices =
{
VertexFormat(corner, color),
VertexFormat(corner + glm::vec3(length, 0, 0), color),
VertexFormat(corner + glm::vec3(length, length, 0), color),
VertexFormat(corner + glm::vec3(0, length, 0), color)
};
Mesh* square = new Mesh(name);
std::vector<unsigned short> indices = { 0, 1, 2, 3 };
if (!fill) {
square->SetDrawMode(GL_LINE_LOOP);
}
else {
// draw 2 triangles. Add the remaining 2 indices
indices.push_back(0);
indices.push_back(2);
}
square->InitFromData(vertices, indices);
return square;
}
Mesh* Object2D::CreateCircle(std::string name, glm::vec3 leftBottomCorner, float radius, glm::vec3 color)
{
float PI = (float)M_PI;
glm::vec3 corner = leftBottomCorner;
std::vector<VertexFormat> vertices =
{
VertexFormat(corner, color),
};
std::vector<unsigned short> indices =
{
};
int triangleAmount = 100;
float t, cost, sint;
for (int i = 0; i < triangleAmount; i++) {
t = i * 2 * PI / triangleAmount;
cost = radius * cos(t);
sint = radius * sin(t);
vertices.push_back(VertexFormat(glm::vec3(cost, sint, 0), color));
}
for (int i = 0; i < triangleAmount; i++) {
indices.push_back(i);
}
indices.push_back(1);
Mesh* circle = new Mesh(name);
circle->InitFromData(vertices, indices);
circle->SetDrawMode(GL_TRIANGLE_FAN);
return circle;
}
Mesh* Object2D::CreateTriangle(std::string name, glm::vec3 leftBottomCorner, float length, glm::vec3 color)
{
glm::vec3 corner = leftBottomCorner;
std::vector<VertexFormat> vertices =
{
VertexFormat(corner, color),
VertexFormat(corner + glm::vec3(length - length / 4, length / 6, 0), color),
VertexFormat(corner + glm::vec3(0, length, 0), color)
};
Mesh* triangle = new Mesh(name);
std::vector<unsigned short> indices = { 0, 1, 2 };
triangle->InitFromData(vertices, indices);
return triangle;
}
Mesh* Object2D::CreateRectangle(std::string name, float width, float height, glm::vec3 color)
{
glm::vec3 corner = glm::vec3(0, 0, 0);
std::vector<VertexFormat> vertices =
{
VertexFormat(corner, color),
VertexFormat(corner + glm::vec3(width, 0, 0), color),
VertexFormat(corner + glm::vec3(width, height, 0), color),
VertexFormat(corner + glm::vec3(0, height, 0), color)
};
Mesh* rectangle = new Mesh(name);
std::vector<unsigned short> indices = { 0, 1, 2, 3, 0, 2 };
rectangle->InitFromData(vertices, indices);
return rectangle;
}