Skip to content

Commit e9924e8

Browse files
committed
finishing touches
1 parent aff40ed commit e9924e8

File tree

5 files changed

+389
-4
lines changed

5 files changed

+389
-4
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ package_add_scene(BunnyDropExplicit)
4040
package_add_scene(StrandDropExplicit)
4141
package_add_scene(StrandBendExplicit)
4242
package_add_scene(StrandTwistExplicit)
43+
package_add_scene(FrameDebug)
4344

4445
add_executable(viewer main.cc)
4546
target_link_libraries(viewer PRIVATE ${OPENGL_LIBRARIES} GLUT::GLUT ${PROJECT_NAME})

Lib/Energy/Strand/DiscreteElasticRod.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <Energy/Spring/MassSpring.h>
55
#include <LibMath.h>
66

7-
constexpr Real gBendingModulus = 0.0001;
7+
constexpr Real gBendingModulus = 0.1;
88
constexpr Real gTwistingModulus = 1;
99

1010
class DiscreteElasticRod {

Scenes/FrameDebug.cc

+227
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
#include "Camera.h"
2+
#include "OpenGL.h"
3+
#include <LibGui.h>
4+
#include <Scenes/StrandDropScene.h>
5+
#include <igl/writeOBJ.h>
6+
#include <memory>
7+
8+
Eigen::Vector2i gMouseDiff;
9+
Eigen::Vector2i gMouseLast;
10+
Eigen::Vector2i gMouseCur;
11+
12+
Eigen::Vector2i gScreenSize(1000, 800);
13+
14+
bool gCameraRotating = false;
15+
bool gCameraZooming = false;
16+
bool gCameraPanning = false;
17+
bool gAnimating = false;
18+
bool gSingleStep = false;
19+
bool gShowGrid = false;
20+
bool gShowCenterAxes = false;
21+
bool gSaveFrame = false;
22+
23+
int gSteps = 0;
24+
25+
auto gCamera = std::make_shared<Camera<float>>();
26+
std::unique_ptr<StrandMesh> gMesh;
27+
28+
void GlutMotionFunc(int x, int y) {
29+
gMouseCur[0] = x;
30+
gMouseCur[1] = y;
31+
32+
Real xDiff = gMouseLast[0] - gMouseCur[0];
33+
Real yDiff = gMouseLast[1] - gMouseCur[1];
34+
35+
if (gCameraPanning) {
36+
gCamera->Pan(xDiff, yDiff);
37+
}
38+
if (gCameraRotating) {
39+
gCamera->Rotate(xDiff, yDiff);
40+
}
41+
if (gCameraZooming) {
42+
gCamera->Zoom(yDiff * 0.1);
43+
}
44+
45+
gMouseLast[0] = gMouseCur[0];
46+
gMouseLast[1] = gMouseCur[1];
47+
48+
glutPostRedisplay();
49+
}
50+
51+
void GlutMouseFunc(int button, int state, int x, int y) {
52+
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
53+
gCameraRotating = true;
54+
}
55+
if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
56+
gCameraRotating = false;
57+
}
58+
59+
if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
60+
gCameraZooming = true;
61+
}
62+
if (button == GLUT_RIGHT_BUTTON && state == GLUT_UP) {
63+
gCameraZooming = false;
64+
}
65+
66+
if (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) {
67+
gCameraPanning = true;
68+
}
69+
if (button == GLUT_MIDDLE_BUTTON && state == GLUT_UP) {
70+
gCameraPanning = false;
71+
}
72+
73+
gMouseCur[0] = x;
74+
gMouseCur[1] = y;
75+
gMouseLast[0] = gMouseCur[0];
76+
gMouseLast[1] = gMouseCur[1];
77+
78+
glutPostRedisplay();
79+
}
80+
81+
void GlutReshapeFunc(int width, int height) {
82+
glViewport(0, 0, width, height);
83+
glMatrixMode(GL_PROJECTION);
84+
glLoadIdentity();
85+
gCamera->Resize(width, height);
86+
glMultMatrixf(gCamera->GetProjectionMatrix().data());
87+
glMatrixMode(GL_MODELVIEW);
88+
glLoadIdentity();
89+
glutPostRedisplay();
90+
}
91+
92+
static void KeyboardControls() {
93+
std::cout << "Keyboard Controls:" << std::endl;
94+
std::cout << " a: Toggle animation" << std::endl;
95+
std::cout << " c: Print camera info" << std::endl;
96+
std::cout << " g: Toggle grid mesh" << std::endl;
97+
std::cout << " space: Single step of animation" << std::endl;
98+
}
99+
100+
void GlutKeyboardFunc(unsigned char key, int x, int y) {
101+
if (key == ' ') {
102+
// Pause the animation too
103+
if (gAnimating) {
104+
gAnimating = false;
105+
}
106+
gSingleStep = !gSingleStep;
107+
}
108+
109+
if (key == 'a') {
110+
gAnimating = !gAnimating;
111+
}
112+
113+
if (key == 'g') {
114+
gShowGrid = !gShowGrid;
115+
gShowCenterAxes = !gShowCenterAxes;
116+
}
117+
118+
if (key == 'c') {
119+
const auto &disp = gCamera->GetDisplacement().transpose();
120+
std::cout << "->SetRadius(" << gCamera->GetR() << ")" << std::endl;
121+
std::cout << "->SetTheta(" << gCamera->GetTheta() << ")" << std::endl;
122+
std::cout << "->SetPhi(" << gCamera->GetPhi() << ")" << std::endl;
123+
std::cout << "->SetDisplacement(Vec3<Real>(" << disp.x() << ", " << disp.y()
124+
<< ", " << disp.z() << "))" << std::endl;
125+
126+
const auto &eye = gCamera->GetEye();
127+
const auto &lookAt = gCamera->GetLookAt();
128+
const auto &up = gCamera->GetUp();
129+
130+
std::cout << "Eye " << eye.x() << ", " << eye.y() << ", " << eye.z()
131+
<< std::endl;
132+
std::cout << "Look At " << lookAt.x() << ", " << lookAt.y() << ", "
133+
<< lookAt.z() << std::endl;
134+
std::cout << "Up " << up.x() << ", " << up.y() << ", " << up.z()
135+
<< std::endl;
136+
std::cout << "FOV " << gCamera->GetFOV() << std::endl;
137+
}
138+
139+
glutPostRedisplay();
140+
}
141+
142+
void GlutSpecialInputFunc(int key, int x, int y) { glutPostRedisplay(); }
143+
144+
void Display() {
145+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
146+
147+
glPushMatrix();
148+
glMultMatrixf(gCamera->ToViewMatrix().data());
149+
150+
if (gShowCenterAxes) {
151+
DrawCenterAxes();
152+
}
153+
154+
// Show the frame number in the bottom right
155+
DrawText("Frame: " + std::to_string(gSteps), gScreenSize.x(),
156+
gScreenSize.y());
157+
158+
float density = 0.15;
159+
float fogColor[4] = {0.15, 0.15, 0.15, 1.0};
160+
float fogStart[1] = {0.0f};
161+
float fogEnd[1] = {800.0f};
162+
163+
glFogi(GL_FOG_MODE, GL_LINEAR);
164+
glFogfv(GL_FOG_START, fogStart);
165+
glFogfv(GL_FOG_END, fogEnd);
166+
glFogfv(GL_FOG_COLOR, fogColor);
167+
glFogf(GL_FOG_DENSITY, density);
168+
glHint(GL_FOG_HINT, GL_NICEST);
169+
170+
if (gShowGrid) {
171+
DrawGLGrid(100, 0.25);
172+
}
173+
174+
gMesh->Draw();
175+
176+
glPopMatrix();
177+
glFlush();
178+
}
179+
180+
static void GlutIdle() {}
181+
182+
auto main(int argc, char **argv) -> int {
183+
if (argc > 1) {
184+
gSaveFrame = strcmp(argv[1], "--save") == 0;
185+
} else {
186+
std::cout << "No Scene specified, defaulting to Scenes/CoarseBunnyExplicit"
187+
<< std::endl;
188+
}
189+
190+
KeyboardControls();
191+
192+
// Set initial camera zoom
193+
gCamera->Zoom(10);
194+
195+
// Set the mesh
196+
Mat<Real> v(5, 3);
197+
for (int ii = 0; ii < v.rows(); ++ii) {
198+
v.row(ii) = Vec3<Real>(ii, (ii * ii) * 0.25, 0);
199+
}
200+
201+
gMesh = std::make_unique<StrandMesh>(v);
202+
gMesh->drawMaterialFrame = true;
203+
204+
glutInit(&argc, argv);
205+
glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA);
206+
glutInitWindowSize(gScreenSize.x(), gScreenSize.y());
207+
glutCreateWindow("Simulator");
208+
glutDisplayFunc(Display);
209+
glutMouseFunc(GlutMouseFunc);
210+
glutMotionFunc(GlutMotionFunc);
211+
glutReshapeFunc(GlutReshapeFunc);
212+
glutKeyboardFunc(GlutKeyboardFunc);
213+
glutSpecialFunc(GlutSpecialInputFunc);
214+
glutIdleFunc(GlutIdle);
215+
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
216+
glPointSize(4.0f);
217+
glEnable(GL_DEPTH_TEST);
218+
219+
glClearColor(0.15, 0.15, 0.15, 1.0f);
220+
221+
GLfloat lightPosition[] = {0.0, 0.0, 0.0, 1.0};
222+
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
223+
224+
glEnable(GL_FOG);
225+
226+
glutMainLoop();
227+
}

Scenes/StrandTwistExplicit.cc

+8-3
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ static void GlutIdle() {
206206
gStopped = true;
207207
}
208208

209+
if (gSteps > 2'000) {
210+
gMesh->der->rightAngularVelocity = 0;
211+
gMesh->der->leftAngularVelocity = 0;
212+
}
213+
209214
if (gSteps % 10 == 0 && gAnimating) {
210215
if (gSaveFrame) {
211216
char filename[512];
@@ -249,16 +254,16 @@ auto main(int argc, char **argv) -> int {
249254
gCamera->Zoom(10);
250255

251256
// Set the mesh
252-
Mat<Real> v(5, 3);
257+
Mat<Real> v(8, 3);
253258
for (int ii = 0; ii < v.rows(); ++ii) {
254259
v.row(ii) = Vec3<Real>(ii, 0, 0);
255260
}
256261

257262
gMesh = std::make_shared<StrandMesh>(v);
258263
gMesh->pinned(0) = 1;
259264
gMesh->pinned(v.rows() - 1) = 1;
260-
gMesh->der->rightAngularVelocity = -3.14;
261-
gMesh->der->leftAngularVelocity = 3.14;
265+
gMesh->der->rightAngularVelocity = 50;
266+
gMesh->der->leftAngularVelocity = 0;
262267
gIntegrator = std::make_shared<ForwardEulerStrand>(gMesh, nullptr,
263268
1.0 / 1'000.0, 5, 0.3);
264269

0 commit comments

Comments
 (0)