feat: hard face normals per vertex

This commit is contained in:
parker
2025-07-31 21:33:16 +01:00
parent 8a77048ea0
commit 8b7793cc73
5 changed files with 95 additions and 15 deletions

View File

@@ -5,6 +5,8 @@
#include <memory> #include <memory>
#include <oneapi/tbb/task_group.h> #include <oneapi/tbb/task_group.h>
#include <stdexcept> #include <stdexcept>
#include <string>
#include <CGAL/Polygon_mesh_processing/orientation.h>
#include "icecream.hpp" #include "icecream.hpp"
using namespace enzo; using namespace enzo;
@@ -43,6 +45,8 @@ enzo::geo::HeMesh geo::Geometry::computeHalfEdgeMesh()
createdPoints.push_back(point); createdPoints.push_back(point);
} }
CGAL::Polygon_mesh_processing::orient(heMesh);
// iterate through each prim // iterate through each prim
for(int primIndx=0; primIndx<vertexCounts.size(); ++primIndx) for(int primIndx=0; primIndx<vertexCounts.size(); ++primIndx)
{ {
@@ -59,7 +63,29 @@ enzo::geo::HeMesh geo::Geometry::computeHalfEdgeMesh()
++vertexIndex; ++vertexIndex;
} }
heMesh.add_face(facePoints); // debug
std::cout << "Primitive " << primIndx << " has " << vertexCount << " vertices: ";
for (auto& v : facePoints)
{
auto pt = heMesh.point(v);
std::cout << "(" << pt.x() << ", " << pt.y() << ", " << pt.z() << ") ";
}
std::cout << std::endl;
std::cout << "Point indices: ";
for (int i = 0; i < vertexCount; ++i) {
int pointIndex = vertexPointIndices.at(vertexIndex - vertexCount + i);
std::cout << pointIndex << " ";
}
std::cout << std::endl;
// debug
auto face = heMesh.add_face(facePoints);
if (face != HeMesh::null_face()) {
// validFaceIndices.push_back(enzo::geo::F_index(primIndx));
} else {
// throw std::runtime_error("Warning: Face creation failed at primitive " + std::to_string(primIndx));
}
} }

View File

@@ -86,25 +86,52 @@ void GLMesh::setPosBuffer(enzo::geo::Geometry& geometry)
std::vector<int> vertexCounts = vertexCountHandle.getAllValues(); std::vector<int> vertexCounts = vertexCountHandle.getAllValues();
unsigned int vertexCount = 0; unsigned int vertexCount = 0;
unsigned int primStartVert = 0;
for (int primIndx=0; primIndx<vertexCounts.size(); ++primIndx) for (int primIndx=0; primIndx<vertexCounts.size(); ++primIndx)
{ {
for(int i=0; i< vertexCounts[primIndx]; ++i) auto faceIndex = primIndx;
int faceVertCnt = vertexCounts[primIndx];
enzo::bt::Vector3 n;
// compute normal
if(faceVertCnt>=3)
{
const unsigned v0 = primStartVert;
const unsigned v1 = primStartVert + 1;
const unsigned v2 = primStartVert + 2;
const enzo::bt::Vector3 pos1 = pointPositions[vertexPointIndices[v0]];
const enzo::bt::Vector3 pos2 = pointPositions[vertexPointIndices[v1]];
const enzo::bt::Vector3 pos3 = pointPositions[vertexPointIndices[v2]];
const enzo::bt::Vector3 tang1 = (pos2-pos1).normalized();
const enzo::bt::Vector3 tang2 = (pos3-pos1).normalized();
n = tang1.cross(tang2);
}
for(int i=0; i< faceVertCnt; ++i)
{ {
unsigned int pointIndex = vertexPointIndices[vertexCount]; unsigned int pointIndex = vertexPointIndices[vertexCount];
enzo::bt::Vector3& p = pointPositions[pointIndex]; enzo::bt::Vector3& p = pointPositions[pointIndex];
IC(vertexCount, pointIndex); IC(vertexCount, pointIndex);
IC(p.x(), p.y(), p.z()); IC(p.x(), p.y(), p.z());
vertices.push_back({ vertices.push_back({
{ p.x(), { p.x(),
p.y(), p.y(),
p.z()}, p.z()},
{ p.x(), { n.x(),
p.y(), n.y(),
p.z()} n.z()}
}); });
++vertexCount; ++vertexCount;
} }
primStartVert+=faceVertCnt;
} }
@@ -126,7 +153,7 @@ void GLMesh::setIndexBuffer(std::vector<int> pointIndices, std::vector<int> prim
int primVertexCount = primVertexCounts[primNum]; int primVertexCount = primVertexCounts[primNum];
for(size_t i=0; i<primVertexCount-1; ++i) for(size_t i=1; i<primVertexCount-1; ++i)
{ {
indexData.push_back(startVert); indexData.push_back(startVert);
indexData.push_back(startVert+i); indexData.push_back(startVert+i);

View File

@@ -85,22 +85,26 @@ void ViewportGLWidget::initializeGL()
#version 330 core #version 330 core
in vec3 Normal; in vec3 Normal;
out vec4 color; out vec4 FragColor;
float remap(float value, float inMin, float inMax, float outMin, float outMax) float remap(float value, float inMin, float inMax, float outMin, float outMax)
{ {
return outMin + (outMax - outMin) * (value - inMin) / (inMax - inMin); return outMin + (outMax - outMin) * (value - inMin) / (inMax - inMin);
} }
vec3 remap(vec3 value, vec3 inMin, vec3 inMax, vec3 outMin, vec3 outMax)
{
return outMin + (outMax - outMin) * (value - inMin) / (inMax - inMin);
}
void main() void main()
{ {
// FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f); // FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
vec3 lightDir = normalize(vec3(1.0,1.0,1.0)); vec3 lightDir = normalize(vec3(1.0,1.0,1.0));
float brightness = remap(dot(Normal, lightDir), -1, 1, 0.5, 1); float brightness = remap(dot(Normal, lightDir), -1, 1, 0.5, 1);
color = vec4(vec3(brightness), 1.0f); vec4 Color = vec4(remap(Normal, vec3(-1), vec3(1), vec3(0), vec3(1)), 1.0f);
color = vec4(Normal, 1.0f);
FragColor = Color;
} }
)"; )";
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

View File

@@ -25,17 +25,31 @@ void GOP_house::cookOp(enzo::op::Context context)
ga::AttributeHandleVector3 PAttrHandle(PAttr); ga::AttributeHandleVector3 PAttrHandle(PAttr);
int startPt = PAttrHandle.getSize(); int startPt = PAttrHandle.getSize();
std::vector<bt::Vector3> pts = { std::vector<bt::Vector3> pts = {
{-1,-1,-1},{1,-1,-1},{1,-1,1},{-1,-1,1}, {-1,-1,-1},
{-1,1,-1},{1,1,-1},{1,1,1},{-1,1,1}, {1,-1,-1},
{0,2,-1},{0,2,1} {1,-1,1},
{-1,-1,1},
{-1,1,-1},
{1,1,-1},
{1,1,1},
{-1,1,1},
{0,2,-1},
{0,2,1}
}; };
for (auto& p : pts) PAttrHandle.addValue(p); for (auto& p : pts) PAttrHandle.addValue(p);
auto pointAttr = geo.getAttribByName(ga::AttrOwner::VERTEX, "point"); auto pointAttr = geo.getAttribByName(ga::AttrOwner::VERTEX, "point");
ga::AttributeHandleInt pointAttrHandle(pointAttr); ga::AttributeHandleInt pointAttrHandle(pointAttr);
std::vector<std::vector<int>> faces = { std::vector<std::vector<int>> faces = {
{3,2,6,9,7},{0,1,5,8,4},{0,3,7,4},{1,2,6,5}, {7,9,6,2,3},
{0,1,2,3},{4,7,9},{4,9,8},{5,6,9},{5,9,8} {4,8,5,1,0},
{4,7,3,0},
{5,6,2,1},
{0,1,2,3},
{9,7,4},
{8,9,4},
{9,6,5},
{8,5,9}
}; };
for (auto& f : faces) for (int i : f) pointAttrHandle.addValue(startPt + i); for (auto& f : faces) for (int i : f) pointAttrHandle.addValue(startPt + i);

View File

@@ -1,6 +1,8 @@
#include "OpDefs/GopTransform.hpp" #include "OpDefs/GopTransform.hpp"
#include "Engine/Operator/AttributeHandle.h" #include "Engine/Operator/AttributeHandle.h"
#include "Engine/Parameter/Template.h" #include "Engine/Parameter/Template.h"
#include <Eigen/src/Core/Matrix.h>
#include <Eigen/src/Geometry/AngleAxis.h>
GopTransform::GopTransform(enzo::nt::NetworkManager* network, enzo::op::OpInfo opInfo) GopTransform::GopTransform(enzo::nt::NetworkManager* network, enzo::op::OpInfo opInfo)
: GeometryOpDef(network, opInfo) : GeometryOpDef(network, opInfo)
@@ -26,6 +28,10 @@ void GopTransform::cookOp(enzo::op::Context context)
for(int i=0; i<PAttrHandle.getAllValues().size(); ++i) for(int i=0; i<PAttrHandle.getAllValues().size(); ++i)
{ {
enzo::bt::Vector3 vector = PAttrHandle.getValue(i); enzo::bt::Vector3 vector = PAttrHandle.getValue(i);
Eigen::AngleAxisd rotationX(context.evalFloatParm("rotateX"), Eigen::Vector3d(1,0,0));
Eigen::AngleAxisd rotationY(context.evalFloatParm("rotateY"), Eigen::Vector3d(0,1,0));
Eigen::AngleAxisd rotationZ(context.evalFloatParm("rotateZ"), Eigen::Vector3d(0,0,1));
vector = (rotationX*rotationY*rotationZ)*vector;
vector.x()+=context.evalFloatParm("translateX"); vector.x()+=context.evalFloatParm("translateX");
vector.y()+=context.evalFloatParm("translateY"); vector.y()+=context.evalFloatParm("translateY");
vector.z()+=context.evalFloatParm("translateZ"); vector.z()+=context.evalFloatParm("translateZ");
@@ -46,6 +52,9 @@ enzo::prm::Template GopTransform::parameterList[] =
enzo::prm::Template(enzo::prm::Type::FLOAT, "translateX", 0), enzo::prm::Template(enzo::prm::Type::FLOAT, "translateX", 0),
enzo::prm::Template(enzo::prm::Type::FLOAT, "translateY", 0), enzo::prm::Template(enzo::prm::Type::FLOAT, "translateY", 0),
enzo::prm::Template(enzo::prm::Type::FLOAT, "translateZ", 0), enzo::prm::Template(enzo::prm::Type::FLOAT, "translateZ", 0),
enzo::prm::Template(enzo::prm::Type::FLOAT, "rotateX", 0),
enzo::prm::Template(enzo::prm::Type::FLOAT, "rotateY", 0),
enzo::prm::Template(enzo::prm::Type::FLOAT, "rotateZ", 0),
enzo::prm::Terminator enzo::prm::Terminator
}; };