-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLine.cpp
120 lines (95 loc) · 3.42 KB
/
Line.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
#include "Line.h"
Line::Line()
{
glm::vec3 defaultVec = glm::vec3(1, 1, 1);
this->createGLBuffer(defaultVec, defaultVec, defaultVec);
}
Line::Line(const glm::vec3& startPosition, const glm::vec3& endPosition, const glm::vec3& color)
{
this->createGLBuffer(startPosition, endPosition, color);
}
void Line::setStartPosition(glm::vec3 newPosition)
{
this->line_vertices[0].position = newPosition;
this->vbo->SetVertex(0, line_vertices[0]);
}
glm::vec3 Line::getStartPosition()
{
return this->line_vertices[0].position;
}
void Line::setEndPosition(glm::vec3 newPosition)
{
this->line_vertices[1].position = newPosition;
this->vbo->SetVertex(1, line_vertices[1]);
}
glm::vec3 Line::getEndPosition()
{
return this->line_vertices[1].position;
}
void Line::setPoints(glm::vec3 startPosition, glm::vec3 endPosition)
{
this->setStartPosition(startPosition);
this->setEndPosition(endPosition);
}
// Reference: https://stackoverflow.com/questions/14486291/how-to-draw-line-in-opengl
void Line::Draw
(
Shader& shader,
Camera& camera
)
{
shader.Activate();
this->vao.Bind();
glUniform3f(glGetUniformLocation(shader.ID, "camPos"), camera.Position.x, camera.Position.y, camera.Position.z);
camera.Matrix(shader, "camMatrix");
glm::mat4 trans = glm::mat4(1.0f);
glm::mat4 rot = glm::mat4(1.0f);
glm::mat4 sca = glm::mat4(1.0f);
glm::mat4 modelMatrix = glm::mat4(1.0f);
glUniformMatrix4fv(glGetUniformLocation(shader.ID, "translation"), 1, GL_FALSE, glm::value_ptr(trans));
glUniformMatrix4fv(glGetUniformLocation(shader.ID, "rotation"), 1, GL_FALSE, glm::value_ptr(rot));
glUniformMatrix4fv(glGetUniformLocation(shader.ID, "scale"), 1, GL_FALSE, glm::value_ptr(sca));
glUniformMatrix4fv(glGetUniformLocation(shader.ID, "model"), 1, GL_FALSE, glm::value_ptr(modelMatrix));
// Reference: https://registry.khronos.org/OpenGL-Refpages/gl4/html/glDrawArrays.xhtml
glDrawArrays(GL_LINE_STRIP, 0, 2);
}
void Line::createGLBuffer
(
const glm::vec3& startPosition,
const glm::vec3& endPosition,
const glm::vec3& color
)
{
this->vao.Bind();
//COORDINATES / NORMALS / COLORS / TEXTURE COORDINATES
this->line_vertices.push_back(Vertex{ startPosition, glm::vec3(0.0f, 1.0f, 0.0f), color, glm::vec2(0.0f, 0.0f) });
this->line_vertices.push_back(Vertex{ endPosition, glm::vec3(0.0f, 1.0f, 0.0f), color, glm::vec2(0.0f, 1.0f) });
this->vbo = new VBO(line_vertices, false);
this->vao.LinkAttribute(*vbo, 0, 3, GL_FLOAT, sizeof(Vertex), (void*)0);
this->vao.LinkAttribute(*vbo, 1, 3, GL_FLOAT, sizeof(Vertex), (void*)(3 * sizeof(float)));
this->vao.LinkAttribute(*vbo, 2, 3, GL_FLOAT, sizeof(Vertex), (void*)(6 * sizeof(float)));
//vao.LinkAttribute(vbo, 3, 2, GL_FLOAT, sizeof(Vertex), (void*)(9 * sizeof(float)));
this->vao.Unbind();
this->vbo->Unbind();
}
void Line::setColor(glm::vec3 newColor)
{
this->line_vertices[0].color = newColor;
this->vbo->SetVertex(0, line_vertices[0]);
this->line_vertices[1].color = newColor;
this->vbo->SetVertex(1, line_vertices[1]);
}
float Line::getLength()
{
glm::vec3 direction = this->line_vertices[1].position - this->line_vertices[0].position;
return glm::length(direction);
}
glm::vec3 Line::getMidPoint()
{
return glm::vec3
(
(this->line_vertices[0].position.x + this->line_vertices[1].position.x) / 2.0f,
(this->line_vertices[0].position.y + this->line_vertices[1].position.y) / 2.0f,
(this->line_vertices[0].position.z + this->line_vertices[1].position.z) / 2.0f
);
}