feat: use attribute for glmesh indices

This commit is contained in:
parker
2025-07-03 00:27:48 +01:00
parent 1c708c62dc
commit 503ca43713
4 changed files with 31 additions and 19 deletions

View File

@@ -9,6 +9,7 @@ using namespace enzo;
geo::Geometry::Geometry() geo::Geometry::Geometry()
{ {
addVector3Attribute(ga::AttrOwner::POINT, "P"); addVector3Attribute(ga::AttrOwner::POINT, "P");
addIntAttribute(ga::AttrOwner::VERTEX, "point");
} }
ga::AttributeHandle<int> geo::Geometry::addIntAttribute(ga::AttributeOwner owner, std::string name) ga::AttributeHandle<int> geo::Geometry::addIntAttribute(ga::AttributeOwner owner, std::string name)

View File

@@ -15,21 +15,10 @@ void GLMesh::init()
initBuffers(); initBuffers();
std::vector<int> foo = {0,1,2,3,4};
for(int i=1;i+1<foo.size();++i)
{
indexData.push_back(foo.at(0));
indexData.push_back(foo.at(i));
indexData.push_back(foo.at(i+1));
}
// indexData =
// {
// 0, 1, 2, 1, 2, 3
// };
// store data in the buffer // store data in the buffer
glBufferData(GL_ARRAY_BUFFER, vertexPosData.size()*sizeof(GLfloat), vertexPosData.data(), GL_STATIC_DRAW); // glBufferData(GL_ARRAY_BUFFER, vertexPosData.size()*sizeof(GLfloat), vertexPosData.data(), GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexData.size()*sizeof(GLint), indexData.data(), GL_STATIC_DRAW); // glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexData.size()*sizeof(GLint), indexData.data(), GL_STATIC_DRAW);
// unbind vertex array // unbind vertex array
@@ -56,6 +45,7 @@ void GLMesh::initBuffers()
void GLMesh::setPosBuffer(std::vector<enzo::bt::Vector3> data) void GLMesh::setPosBuffer(std::vector<enzo::bt::Vector3> data)
{ {
bind();
vertexPosData.clear(); vertexPosData.clear();
for(auto vector : data) for(auto vector : data)
{ {
@@ -65,6 +55,22 @@ void GLMesh::setPosBuffer(std::vector<enzo::bt::Vector3> data)
} }
glBufferData(GL_ARRAY_BUFFER, vertexPosData.size()*sizeof(GLfloat), vertexPosData.data(), GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, vertexPosData.size()*sizeof(GLfloat), vertexPosData.data(), GL_STATIC_DRAW);
unbind();
}
void GLMesh::setIndexBuffer(std::vector<int> data)
{
bind();
indexData.clear();
for(int i=1;i+1<data.size();++i)
{
indexData.push_back(data.at(0));
indexData.push_back(data.at(i));
indexData.push_back(data.at(i+1));
}
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexData.size()*sizeof(GLint), indexData.data(), GL_STATIC_DRAW);
unbind();
} }

View File

@@ -18,6 +18,7 @@ public:
void init(); void init();
void initBuffers(); void initBuffers();
void setPosBuffer(std::vector<enzo::bt::Vector3> data); void setPosBuffer(std::vector<enzo::bt::Vector3> data);
void setIndexBuffer(std::vector<int> data);
void bind(); void bind();
void unbind(); void unbind();
void draw(); void draw();

View File

@@ -156,12 +156,16 @@ std::unique_ptr<GLMesh> ViewportGLWidget::meshFromGeo(std::unique_ptr<enzo::geo:
PAttrHandle.addValue(bt::Vector3(1.0f, 1.0f, 0.0f)); PAttrHandle.addValue(bt::Vector3(1.0f, 1.0f, 0.0f));
mesh->setPosBuffer(PAttrHandle.getData()); mesh->setPosBuffer(PAttrHandle.getData());
// mesh->setPosBuffer(std::vector<bt::Vector3>{
// bt::Vector3(-0.5f, -0.5f, 0.0f), std::shared_ptr<ga::Attribute> pointAttr = geometry->getAttribByName(ga::AttrOwner::VERTEX, "point");
// bt::Vector3(0.5f, -0.5f, 0.0f), ga::AttributeHandleInt pointAttrHandle = ga::AttributeHandleInt(pointAttr);
// bt::Vector3(0.0f, 0.5f, 0.0f), pointAttrHandle.addValue(0);
// bt::Vector3(0.5f, 0.5f, 0.0f), pointAttrHandle.addValue(1);
// }); pointAttrHandle.addValue(2);
pointAttrHandle.addValue(3);
pointAttrHandle.addValue(4);
mesh->setIndexBuffer(pointAttrHandle.getData());
return mesh; return mesh;
} }