Skip to content

Commit 25e27b9

Browse files
authored
Add missing setAttribute(), use them almost everywhere (#377)
* add missing setAttribute() variants * set attribute directly in camera view (fixes multiple-set bug) * use direct set attribute almost everywhere
1 parent a534b55 commit 25e27b9

File tree

8 files changed

+111
-31
lines changed

8 files changed

+111
-31
lines changed

include/polyscope/render/engine.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,10 @@ class ShaderProgram {
381381
virtual void setAttribute(std::string name, const std::vector<double>& data) = 0;
382382
virtual void setAttribute(std::string name, const std::vector<int32_t>& data) = 0;
383383
virtual void setAttribute(std::string name, const std::vector<uint32_t>& data) = 0;
384+
385+
virtual void setAttribute(std::string name, const std::vector<std::array<glm::vec3, 2>>& data) = 0;
386+
virtual void setAttribute(std::string name, const std::vector<std::array<glm::vec3, 3>>& data) = 0;
387+
virtual void setAttribute(std::string name, const std::vector<std::array<glm::vec3, 4>>& data) = 0;
384388
// clang-format on
385389

386390

include/polyscope/render/mock_opengl/mock_gl_engine.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ class GLShaderProgram : public ShaderProgram {
274274
void setAttribute(std::string name, const std::vector<double>& data) override;
275275
void setAttribute(std::string name, const std::vector<int32_t>& data) override;
276276
void setAttribute(std::string name, const std::vector<uint32_t>& data) override;
277+
278+
void setAttribute(std::string name, const std::vector<std::array<glm::vec3, 2>>& data) override;
279+
void setAttribute(std::string name, const std::vector<std::array<glm::vec3, 3>>& data) override;
280+
void setAttribute(std::string name, const std::vector<std::array<glm::vec3, 4>>& data) override;
277281
// clang-format on
278282

279283
// Indices

include/polyscope/render/opengl/gl_engine.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,10 @@ class GLShaderProgram : public ShaderProgram {
317317
void setAttribute(std::string name, const std::vector<double>& data) override;
318318
void setAttribute(std::string name, const std::vector<int32_t>& data) override;
319319
void setAttribute(std::string name, const std::vector<uint32_t>& data) override;
320+
321+
void setAttribute(std::string name, const std::vector<std::array<glm::vec3, 2>>& data) override;
322+
void setAttribute(std::string name, const std::vector<std::array<glm::vec3, 3>>& data) override;
323+
void setAttribute(std::string name, const std::vector<std::array<glm::vec3, 4>>& data) override;
320324
// clang-format on
321325

322326

src/camera_view.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -317,11 +317,7 @@ void CameraView::fillCameraWidgetGeometry(render::ShaderProgram* nodeProgram, re
317317
std::array<glm::vec3, 3>{pickColor, pickColor, pickColor});
318318

319319

320-
std::shared_ptr<render::AttributeBuffer> tripleColorsBuff =
321-
render::engine->generateAttributeBuffer(RenderDataType::Vector3Float, 3);
322-
tripleColorsBuff->setData(tripleColors);
323-
324-
pickFrameProgram->setAttribute("a_vertexColors", tripleColorsBuff);
320+
pickFrameProgram->setAttribute("a_vertexColors", tripleColors);
325321
pickFrameProgram->setAttribute("a_faceColor", faceColor);
326322
if (wantsCullPosition()) {
327323
pickFrameProgram->setAttribute("a_cullPos", cullPos);

src/render/mock_opengl/mock_gl_engine.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,51 @@ void GLShaderProgram::setAttribute(std::string name, const std::vector<uint32_t>
12281228
throw std::invalid_argument("Tried to set nonexistent attribute with name " + name);
12291229
}
12301230

1231+
void GLShaderProgram::setAttribute(std::string name, const std::vector<std::array<glm::vec3, 2>>& data) {
1232+
1233+
// pass-through to the buffer
1234+
for (GLShaderAttribute& a : attributes) {
1235+
if (a.name == name) {
1236+
if (a.arrayCount != 2) throw std::invalid_argument("Tried to set attribute " + name + " with wrong array count");
1237+
ensureBufferExists(a);
1238+
a.buff->setData(data);
1239+
return;
1240+
}
1241+
}
1242+
1243+
throw std::invalid_argument("Tried to set nonexistent attribute with name " + name);
1244+
}
1245+
1246+
void GLShaderProgram::setAttribute(std::string name, const std::vector<std::array<glm::vec3, 3>>& data) {
1247+
1248+
// pass-through to the buffer
1249+
for (GLShaderAttribute& a : attributes) {
1250+
if (a.name == name) {
1251+
if (a.arrayCount != 3) throw std::invalid_argument("Tried to set attribute " + name + " with wrong array count");
1252+
ensureBufferExists(a);
1253+
a.buff->setData(data);
1254+
return;
1255+
}
1256+
}
1257+
1258+
throw std::invalid_argument("Tried to set nonexistent attribute with name " + name);
1259+
}
1260+
1261+
void GLShaderProgram::setAttribute(std::string name, const std::vector<std::array<glm::vec3, 4>>& data) {
1262+
1263+
// pass-through to the buffer
1264+
for (GLShaderAttribute& a : attributes) {
1265+
if (a.name == name) {
1266+
if (a.arrayCount != 4) throw std::invalid_argument("Tried to set attribute " + name + " with wrong array count");
1267+
ensureBufferExists(a);
1268+
a.buff->setData(data);
1269+
return;
1270+
}
1271+
}
1272+
1273+
throw std::invalid_argument("Tried to set nonexistent attribute with name " + name);
1274+
}
1275+
12311276
bool GLShaderProgram::hasTexture(std::string name) {
12321277
for (GLShaderTexture& t : textures) {
12331278
if (t.name == name) {

src/render/opengl/gl_engine.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,6 +1751,54 @@ void GLShaderProgram::setAttribute(std::string name, const std::vector<uint32_t>
17511751
throw std::invalid_argument("Tried to set nonexistent attribute with name " + name);
17521752
}
17531753

1754+
void GLShaderProgram::setAttribute(std::string name, const std::vector<std::array<glm::vec3, 2>>& data) {
1755+
glBindVertexArray(vaoHandle);
1756+
1757+
// pass-through to the buffer
1758+
for (GLShaderAttribute& a : attributes) {
1759+
if (a.name == name && a.location != -1) {
1760+
if (a.arrayCount != 2) throw std::invalid_argument("Tried to set attribute " + name + " with wrong array count");
1761+
ensureBufferExists(a);
1762+
a.buff->setData(data);
1763+
return;
1764+
}
1765+
}
1766+
1767+
throw std::invalid_argument("Tried to set nonexistent attribute with name " + name);
1768+
}
1769+
1770+
void GLShaderProgram::setAttribute(std::string name, const std::vector<std::array<glm::vec3, 3>>& data) {
1771+
glBindVertexArray(vaoHandle);
1772+
1773+
// pass-through to the buffer
1774+
for (GLShaderAttribute& a : attributes) {
1775+
if (a.name == name && a.location != -1) {
1776+
if (a.arrayCount != 3) throw std::invalid_argument("Tried to set attribute " + name + " with wrong array count");
1777+
ensureBufferExists(a);
1778+
a.buff->setData(data);
1779+
return;
1780+
}
1781+
}
1782+
1783+
throw std::invalid_argument("Tried to set nonexistent attribute with name " + name);
1784+
}
1785+
1786+
void GLShaderProgram::setAttribute(std::string name, const std::vector<std::array<glm::vec3, 4>>& data) {
1787+
glBindVertexArray(vaoHandle);
1788+
1789+
// pass-through to the buffer
1790+
for (GLShaderAttribute& a : attributes) {
1791+
if (a.name == name && a.location != -1) {
1792+
if (a.arrayCount != 4) throw std::invalid_argument("Tried to set attribute " + name + " with wrong array count");
1793+
ensureBufferExists(a);
1794+
a.buff->setData(data);
1795+
return;
1796+
}
1797+
}
1798+
1799+
throw std::invalid_argument("Tried to set nonexistent attribute with name " + name);
1800+
}
1801+
17541802
bool GLShaderProgram::hasTexture(std::string name) {
17551803
for (GLShaderTexture& t : textures) {
17561804
if (t.name == name && t.location != -1) {

src/surface_mesh.cpp

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,27 +1040,11 @@ void SurfaceMesh::setMeshPickAttributes(render::ShaderProgram& p) {
10401040

10411041
// == Store data in buffers
10421042

1043-
std::shared_ptr<render::AttributeBuffer> vertexColorsBuff =
1044-
render::engine->generateAttributeBuffer(RenderDataType::Vector3Float, 3);
1045-
vertexColorsBuff->setData(vertexColors);
1046-
pickProgram->setAttribute("a_vertexColors", vertexColorsBuff);
1047-
1048-
std::shared_ptr<render::AttributeBuffer> faceColorsBuff =
1049-
render::engine->generateAttributeBuffer(RenderDataType::Vector3Float);
1050-
faceColorsBuff->setData(faceColor);
1051-
pickProgram->setAttribute("a_faceColor", faceColorsBuff);
1052-
1043+
pickProgram->setAttribute("a_vertexColors", vertexColors);
1044+
pickProgram->setAttribute("a_faceColor", faceColor);
10531045
if (!usingSimplePick) {
1054-
1055-
std::shared_ptr<render::AttributeBuffer> halfedgeColorsBuff =
1056-
render::engine->generateAttributeBuffer(RenderDataType::Vector3Float, 3);
1057-
halfedgeColorsBuff->setData(halfedgeColors);
1058-
pickProgram->setAttribute("a_halfedgeColors", halfedgeColorsBuff);
1059-
1060-
std::shared_ptr<render::AttributeBuffer> cornerColorsBuff =
1061-
render::engine->generateAttributeBuffer(RenderDataType::Vector3Float, 3);
1062-
cornerColorsBuff->setData(cornerColors);
1063-
pickProgram->setAttribute("a_cornerColors", cornerColorsBuff);
1046+
pickProgram->setAttribute("a_halfedgeColors", halfedgeColors);
1047+
pickProgram->setAttribute("a_cornerColors", cornerColors);
10641048
}
10651049
}
10661050

src/volume_mesh.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -599,12 +599,7 @@ void VolumeMesh::preparePick() {
599599
}
600600

601601
// === Store data in buffers
602-
603-
std::shared_ptr<render::AttributeBuffer> vertexColorsBuff =
604-
render::engine->generateAttributeBuffer(RenderDataType::Vector3Float, 3);
605-
vertexColorsBuff->setData(vertexColors);
606-
607-
pickProgram->setAttribute("a_vertexColors", vertexColorsBuff);
602+
pickProgram->setAttribute("a_vertexColors", vertexColors);
608603
pickProgram->setAttribute("a_faceColor", faceColor);
609604
}
610605

0 commit comments

Comments
 (0)