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

@@ -86,25 +86,52 @@ void GLMesh::setPosBuffer(enzo::geo::Geometry& geometry)
std::vector<int> vertexCounts = vertexCountHandle.getAllValues();
unsigned int vertexCount = 0;
unsigned int primStartVert = 0;
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];
enzo::bt::Vector3& p = pointPositions[pointIndex];
IC(vertexCount, pointIndex);
IC(p.x(), p.y(), p.z());
vertices.push_back({
{ p.x(),
p.y(),
p.z()},
{ p.x(),
p.y(),
p.z()}
{ n.x(),
n.y(),
n.z()}
});
++vertexCount;
}
primStartVert+=faceVertCnt;
}
@@ -126,7 +153,7 @@ void GLMesh::setIndexBuffer(std::vector<int> pointIndices, std::vector<int> prim
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+i);

View File

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