why this function does not give correct shaded wine glasses Surface Surface::makeSurfRev(const Curve& profile, unsigned steps) {
code
Surface surface;
if (!checkFlat(profile))
{
std::cerr << "surfRev profile curve must be flat on xy plane." << std::endl;
exit(0);
}
// 2.4.1 - Create the functionality to process surface revolutions
// Here you should build the surface. See surf.h for details.
//Get control points of the profile curve
const auto& profilePoints = profile.getCurvePoints();
const float full = 2.0f * PI; // full circle rad
const float angleStep = full / steps;
// along v (horizonal)
for (unsigned s = 0; s <= steps; s++) {
float theta = s * angleStep;
Matrix3f rotY = Matrix3f::rotateY(theta);
// along u (vertical)
for (const auto& profilePoint: profilePoints) {
Vector3f rotatedPoint = rotY * profilePoint.V;
//since rotation matrix is orthogonal,
//N^T=N^-1 =>(N^T)^-1=(N^T)^T=N
Vector3f rotatedNormal = rotY * profilePoint.N;
surface.VV.push_back(rotatedPoint);
rotatedNormal.normalize();
surface.VN.push_back(rotatedNormal);
}
}
// generate faces
unsigned n = profilePoints.size(); // number of control points
for (unsigned s = 0; s < steps; s++) {
unsigned offset1 = s * n;
unsigned offset2 = (s + 1) * n;
for (unsigned i = 0; i < n - 1; i++) {
//[i] * *, *
//[i] * , * *
surface.VF.push_back(Tup3u(offset1 + i, offset2 + i, offset1 + i + 1));
surface.VF.push_back(Tup3u(offset2 + i, offset2 + i + 1, offset1 + i + 1));
}
}
// std::cerr << "\t>>> makeSurfRev called (but not implemented).\n\t>>> Returning empty surface." << std::endl;
return surface;
Assistant
}
The issue with your makeSurfRev function likely stems from a mismatch between the expected orientation of the profile curve and the axis around which you're performing the rotation. Let's break down the potential problems and how to fix them:
1. Profile Orientation vs. Rotation Axis
Current Setup:
Rotation Axis: Y-axis (Matrix3f::rotateY(theta)).
Profile Orientation Check:checkFlat(profile) ensures the profile lies flat on the XY-plane.
Problem:
Rotating a profile that lies flat on the XY-plane around the Y-axis results in sweeping the profile horizontally. For generating a wine glass (or similar objects), you'd typically want the profile to sweep vertically around a central axis, which usually aligns with the Y-axis.
Expected Orientation for Y-axis Rotation: The profile should lie on the XZ-plane, not the XY-plane. This way, rotating around the Y-axis sweeps the profile vertically, creating the desired surface of revolution.
2. Solution: Align Profile with Rotation Axis
Option 1: Adjust the Profile Orientation
Ensure that the profile curve lies on the XZ-plane before calling makeSurfRev.
Modify the checkFlat function to verify that the profile is flat on the XZ-plane instead of the XY-plane.
Option 2: Change the Rotation Axis
If you prefer to keep the profile on the XY-plane, change the rotation to be around the Z-axis instead of the Y-axis.
3. Implementing the Fix
Assuming you want to rotate around the Y-axis and align the profile on the XZ-plane, here's how you can adjust your code:
Modify the checkFlat Function:
Ensure it checks that all profile points have y = 0 (flat on the XZ-plane).
Ensure Profile Alignment:
When creating your Curve for the profile, align it so that it lies on the XZ-plane.
Verify Rotation Matrix:
Your current rotation matrix around the Y-axis is correct for a profile on the XZ-plane.
4. Additional Considerations
Handle Duplicate Vertices:
Rotating from s = 0 to s = steps (inclusive) results in duplicate vertices at s = 0 and s = steps (both representing theta = 0 and theta = 2π). While this is common in surface of revolution implementations for texture mapping, ensure your rendering pipeline can handle these duplicates correctly.
Normals Calculation:
Rotating normals as you do (rotY * profilePoint.N) is appropriate, provided the original normals are correctly defined relative to the profile's orientation.
Rendering Pipeline:
Ensure that your rendering pipeline correctly interprets the vertex and normal data. Incorrect interpretation here can also lead to shading issues.
5. Revised Code Snippet
Here's a revised version of the critical sections of your function, assuming the profile is now correctly aligned on the XZ-plane:
cpp
Surface Surface::makeSurfRev(const Curve& profile, unsigned steps) {
Surface surface;
if (!checkFlat(profile)) {
std::cerr << "surfRev profile curve must be flat on XZ plane." << std::endl;
exit(0);
}
const auto& profilePoints = profile.getCurvePoints();
const float full = 2.0f * PI; // full circle rad
const float angleStep = full / steps;
// Generate vertices and normals
for (unsigned s = 0; s <= steps; s++) {
float theta = s * angleStep;
Matrix3f rotY = Matrix3f::rotateY(theta);
for (const auto& profilePoint : profilePoints) {
Vector3f rotatedPoint = rotY * profilePoint.V;
Vector3f rotatedNormal = (rotY * profilePoint.N).normalized();
surface.VV.push_back(rotatedPoint);
surface.VN.push_back(rotatedNormal);
}
}
// Generate faces
unsigned n = profilePoints.size(); // number of control points
for (unsigned s = 0; s < steps; s++) {
unsigned offset1 = s * n;
unsigned offset2 = (s + 1) * n;
for (unsigned i = 0; i < n - 1; i++) {
surface.VF.emplace_back(offset1 + i, offset2 + i, offset1 + i + 1);
surface.VF.emplace_back(offset2 + i, offset2 + i + 1, offset1 + i + 1);
}
}
return surface;
}
6. Conclusion
The primary issue is likely the mismatch between the profile's orientation and the rotation axis. By ensuring that the profile lies on the correct plane relative to the rotation axis, your surface of revolution should correctly represent a shaded wine glass or similar object.
If after making these adjustments you still encounter issues, consider debugging by visualizing the vertices and normals to ensure they're positioned and oriented as expected. Additionally, double-check your rendering pipeline to ensure it correctly interprets the generated surface data.