-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglmesh.cpp
99 lines (79 loc) · 2.52 KB
/
glmesh.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
#include "glmesh.h"
GLMesh::GLMesh()
{
}
GLMesh::GLMesh(Mesh &mesh)
{
_vertices=mesh.vertices();
_indices=mesh.indices();
setupMesh();
}
GLMesh::GLMesh(const GLMesh& mesh)
{
_vertices=mesh._vertices;
_indices=mesh._indices;
texture=mesh.texture;
modelMatrix=mesh.modelMatrix;
setupMesh();
}
GLMesh &GLMesh::operator=(const GLMesh &rhs)
{
_vertices=rhs._vertices;
_indices=rhs._indices;
texture=rhs.texture;
modelMatrix=rhs.modelMatrix;
return *this;
}
void GLMesh::draw(Shader &shader)
{
shader.program.bind();
VAO.bind();
glBindTexture(GL_TEXTURE_2D, texture.id);
glDrawElements(GL_TRIANGLES, _indices.size(), GL_UNSIGNED_INT, 0);
VAO.release();
}
void GLMesh::createTexture(cv::Mat &img)
{
glDeleteTextures(1,&texture.id);
glEnable(GL_TEXTURE_2D);
cv::flip(img, img, 0);
glGenTextures(1, &texture.id);
glBindTexture(GL_TEXTURE_2D, texture.id);
// set min-mag filter
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Set texture clamping method
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
// fill texture with webcam frame
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.cols, img.rows, 0, GL_BGR, GL_UNSIGNED_BYTE, img.ptr());
glBindTexture(GL_TEXTURE_2D, 0);
}
void GLMesh::setupMesh()
{
initializeOpenGLFunctions();
EBO=QOpenGLBuffer(QOpenGLBuffer::IndexBuffer);
VAO.create();
VAO.bind();
VBO.create();
VBO.setUsagePattern( QOpenGLBuffer::StaticDraw );
VBO.bind();
VBO.allocate( &_vertices[0], _vertices.size()*sizeof(Vertex) );
EBO.create();
EBO.setUsagePattern( QOpenGLBuffer::StaticDraw );
EBO.bind();
EBO.allocate( &_indices[0], _indices.size()*sizeof(unsigned int) );
// Vertex Positions
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),(GLvoid*)0);
// Vertex Normals
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, normal));
// Vertex Colors
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, color));
// Vertex Texture Coords
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, texCoords));
VAO.release();
}