Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added rotation capability in main.cpp . Runs but not fully debugged.

Rotation of cloud with ctrl-key. Moving cloud with shift-key.
Right now, cloud rotation does not stop when I release shift-key. I might
have to deal with deltas instead of absolutes.
  • Loading branch information...
commit a82392f84441ffaf62ddb33aaa7f1cb84954b369 1 parent 2357988
erlebach authored
View
4 rtpslib/CMakeLists.txt
@@ -60,7 +60,7 @@ set (rtpslib_INCLUDES
${rtpslib_SOURCE_DIR}/system/outer
${rtpslib_SOURCE_DIR}/system/common
${rtpslib_SOURCE_DIR}/render
- ${rtpslib_SOURCE_DIR}/rotations
+ #${rtpslib_SOURCE_DIR}/rotations
${rtpslib_SOURCE_DIR}/render/util
${rtpslib_SOURCE_DIR}/domain
${rtpslib_SOURCE_DIR}/opencl
@@ -80,7 +80,7 @@ set (rtpslib_SOURCES
${rtpslib_SOURCE_DIR}/rtps_common
${rtpslib_SOURCE_DIR}/render/Render
- ${rtpslib_SOURCE_DIR}/render/rotations
+ #${rtpslib_SOURCE_DIR}/render/rotations
${rtpslib_SOURCE_DIR}/render/SpriteRender
${rtpslib_SOURCE_DIR}/render/SSFRender
${rtpslib_SOURCE_DIR}/render/util/stb_image.c
View
8 rtpslib/RTPS.cpp
@@ -137,5 +137,13 @@ printf("created system in RTPS\n");
{
return cloud_translate;
}
+ void RTPS::setRotationAxis(float4& rotation_axis)
+ {
+ this->rotation_axis = rotation_axis;
+ }
+ float4& RTPS::getRotationAxis()
+ {
+ return rotation_axis;
+ }
};
View
3  rtpslib/RTPS.h
@@ -73,12 +73,15 @@ namespace rtps
void setCloudMovement(bool mvt);
bool getCloudMovement();
void setCloudTranslate(float cx, float cy, float cz);
+ void setRotationAxis(float4& rotation_axis);
+ float4& getRotationAxis();
float4& getCloudTranslate();
private:
bool cl_managed;
bool cloud_movement; // move or stop cloud 'S'
float4 cloud_translate;
+ float4 rotation_axis;
};
}
View
114 rtpslib/render/rotations.cpp
@@ -1,114 +0,0 @@
-
-#include "rotations.h"
-
-//----------------------------------------------------------------------
-Rotate::Rotate()
-{}
-//----------------------------------------------------------------------
-//Vectorize()
-// Create a 3D position vector on a unit sphere based on
-// mouse hitPt, sphere's screen origin, and sphere radius in
-// pixels. Return zero on success or non-zero if hit point
-// was outside the sphere. This routine uses Quickdraw[TM] 3D
-// and assumes that the relevant QD3D headers and library
-// files have been included. Also need to #include <math.h>.
-
-#define BOUNDS_ERR 1L
-
-long Rotate::Vectorize(
- Point *hit,
- Point *origin,
- float radius,
- TQ3Vector3D *vec )
-{
- float x,y,z, modulus;
-
- x = (float)(hit->h - origin->h)/radius;
- y = (float)(hit->v - origin->v)/radius;
-
- y *= -1.0; // compensate for "inverted" screen y-axis!
-
- modulus = x*x + y*y;
-
- if (modulus > 1.) // outside radius!
- return BOUNDS_ERR;
-
- z = sqrt(1. - (x*x + y*y) ); // compute fictitious 'z' value
-
- Q3Vector3D_Set( vec, x,y,z ); // compute pseudo-3D mouse position
-
- return 0L;
-}
-//----------------------------------------------------------------------
-Rotate::ZeroHysteresisRotation()
-// From two 3D vectors representing the positions of
-// points on a unit sphere, calculate an axis of rotation
-// and an amount of rotation such that Point A can be
-// moved along a geodesic to Point B.
-// CAUTION: Error-checking omitted for clarity.
-
-void ZeroHysteresisRotation( TQ3Vector3D v1, TQ3Vector3D v2 )
-{
- TQ3Vector3D cross;
- TQ3Matrix4x4 theMatrix;
- TQ3Point3D orig = { 0.,0.,0. };
- float dot,angle;
-
- dot = Q3Vector3D_Dot( &v1, &v2 );
-
- if (dot == 1.0)
- return; // nothing to do
-
- Q3Vector3D_Cross( &v1, &v2, &cross ); // axis of rotation
- Q3Vector3D_Normalize( &cross,&cross );
-
- angle = 2.*acos( dot ); // angle of rotation
-
- // set up a rotation around our chosen axis...
- Q3Matrix4x4_SetRotateAboutAxis(&theMatrix,
- &orig, &cross, angle);
-
- Q3Matrix4x4_Multiply( &gDocument.fRotation,
- &theMatrix,
- &gDocument.fRotation); // multiply
-
- DocumentDraw( &gDocument ) ; // draw
-}
-//----------------------------------------------------------------------
-//Rotate::FreeRotateWithMouse()
-// Call this function from the main event loop to
-// do free-rotations of 3D objects
-
-// the mouse action radius in pixels:
-#define RADIUS_VALUE 300.0
-
-void Rotate::FreeRotateWithMouse(void)
-{
- Point now, oldPt, center;
- WindowPtr win = FrontWindow();
- float radius = RADIUS_VALUE;
- TQ3Vector3D v1,v2;
- long err;
-
- GetMouse( &oldPt );
-
- center.h = (win->portRect.right - win->portRect.left)/2;
-
- center.v = (win->portRect.bottom - win->portRect.top)/2;
-
- while (StillDown())
- {
- GetMouse( &now );
-
- err = Vectorize(&oldPt, &center, RADIUS_VALUE, &v1 );
- err += Vectorize(&now, &center, RADIUS_VALUE, &v2 );
-
- if (!err)
- ZeroHysteresisRotation( v1,v2 );
-
- oldPt = now;
- }
-}
-//----------------------------------------------------------------------
-//----------------------------------------------------------------------
-//----------------------------------------------------------------------
View
17 rtpslib/render/rotations.h
@@ -1,17 +0,0 @@
-#ifndef _ROTATE_GE_H_
-#define _ROTATE_GE_H_
-
-// Derived from: (no use of quaternions)
-// http://www.mactech.com/articles/mactech/Vol.15/15.03/NaturalObjectRotation/index.html
-
-//----------------------------------------------------------------------
-class Rotate
-{
- Rotate();
- void Vectorize();
- void ZeroHysteresisRotation();
- void FreeRotateWithMouse();
-};
-//----------------------------------------------------------------------
-
-#endif
View
12 rtpslib/system/Cloud.cpp
@@ -61,8 +61,8 @@ namespace rtps
addCloud();
float scale = 30.;
- float cloud_omega_mag = 0.; // velocity
- cloud_omega = float4(1.,1.,10.,cloud_omega_mag);
+ float cloud_omega_mag = 0.2; // velocity
+ cloud_omega = float4(0.,1.,10.,cloud_omega_mag);
float nn = cloud_omega.x*cloud_omega.x +
cloud_omega.y*cloud_omega.y +
cloud_omega.z*cloud_omega.z;
@@ -120,14 +120,19 @@ namespace rtps
//----------------------------------------------------------------------
void CLOUD::cloudVelocityExecute()
{
- printf("**** BEFORE velocity execute *****\n");
//u.printDevArray(cl_position_s, "pos_s", 10, 10);
//u.printDevArray(cl_velocity_s, "vel_s", 10, 10);
cloud_cg_prev = cloud_cg;
cloud_cg = cloud_cg0 + ps->getCloudTranslate();
+
+ float4 cloud_omega = ps->getRotationAxis();
ps->getCloudTranslate().print("translate");
+ cloud_omega.w = 10.; // Angular rotation
+
+ printf("**** BEFORE vel exec, ");
cloud_cg.print("cloud_cg");
+ cloud_omega.print("*** BEFORE vel exec, rotation axis ***");
velocity.execute(
cloud_num,
@@ -208,6 +213,7 @@ namespace rtps
cl_normal_s,
cl_velocity_u,
cl_velocity_s,
+ cloud_cg,
cg_diff, // must make more efficient
cl_sort_indices,
*cl_sphp,
View
7 rtpslib/system/SPH.cpp
@@ -973,11 +973,14 @@ namespace rtps
cloud->cloudVelocityExecute(); // before collision?
}
- cloud->cloudVelocityExecute(); // before collision?
+ //cloud->cloudVelocityExecute(); // before collision?
+ printf("REMOVE COLLISIONS GE\n");
cloud->collision(cl_position_s, cl_velocity_s, cl_force_s, cl_sphp, num);
;
- cloud->integrate();
+ if (ps->getCloudMovement()) {
+ cloud->integrate();
+ }
}
//----------------------------------------------------------------------
void SPH::cloudCleanup()
View
4 rtpslib/system/sph/CloudEuler.cpp
@@ -21,6 +21,7 @@ namespace rtps
Buffer<float4>& velocity_u,
Buffer<float4>& velocity_s,
float4& pos_cg,
+ float4& diff_pos_cg,
//float4 vel,
Buffer<unsigned int>& indices,
//params
@@ -38,11 +39,14 @@ namespace rtps
k_cloud_euler.setArg(iargs++, velocity_u.getDevicePtr());
k_cloud_euler.setArg(iargs++, velocity_s.getDevicePtr());
k_cloud_euler.setArg(iargs++, pos_cg);
+ k_cloud_euler.setArg(iargs++, diff_pos_cg);
//k_cloud_euler.setArg(iargs++, vel);
k_cloud_euler.setArg(iargs++, indices.getDevicePtr());
k_cloud_euler.setArg(iargs++, sphp.getDevicePtr());
k_cloud_euler.setArg(iargs++, dt); //time step
+ pos_cg.print("*** pos_cg ***");
+
//printf("BEFORE k_cloud_euler.execute\n");
View
33 rtpslib/system/sph/CloudEuler.h
@@ -13,23 +13,22 @@ namespace rtps
CloudEuler() { cli = NULL; timer = NULL; };
CloudEuler(std::string path, CL* cli, EB::Timer* timer);
void execute(int num,
- float dt,
- Buffer<float4>& pos_u,
- Buffer<float4>& pos_s,
- Buffer<float4>& normal_u,
- Buffer<float4>& normal_s,
- Buffer<float4>& velocity_u,
- Buffer<float4>& velocity_s,
- float4& pos_cg,
- //float4 vel,
- Buffer<unsigned int>& indices,
- //params
- Buffer<SPHParams>& sphp,
- //debug
- Buffer<float4>& clf_debug,
- Buffer<int4>& cli_debug);
-
-
+ float dt,
+ Buffer<float4>& pos_u,
+ Buffer<float4>& pos_s,
+ Buffer<float4>& normal_u,
+ Buffer<float4>& normal_s,
+ Buffer<float4>& velocity_u,
+ Buffer<float4>& velocity_s,
+ float4& pos_cg,
+ float4& diff_pos_cg,
+ //float4 vel,
+ Buffer<unsigned int>& indices,
+ //params
+ Buffer<SPHParams>& sphp,
+ //debug
+ Buffer<float4>& clf_debug,
+ Buffer<int4>& cli_debug);
private:
CL* cli;
View
3  rtpslib/system/sph/cl_src/cloud_euler.cl
@@ -16,6 +16,7 @@ __kernel void cloudEuler(
__global float4* velocity_s,
// pos_cg: difference from previous time step
float4 pos_cg, // just the tnraslation from pos_cg0
+ float4 diff_pos_cg, // just the tnraslation from pos_cg0
__global int* sort_indices,
__constant struct SPHParams* sphp,
float dt)
@@ -29,7 +30,7 @@ __kernel void cloudEuler(
float4 p = pos_s[i] * ss;
float4 vel = velocity_s[i];
- p += dt*vel + pos_cg*ss;
+ p += dt*vel + diff_pos_cg*ss;
//p = pos_cg*sphp->simulation_scale + dt*vel;
//p = dt*vel;
View
2  rtpslib/system/sph/cl_src/cloud_velocity.cl
@@ -13,7 +13,7 @@ __kernel void kern_cloud_velocity(
if (i >= num) return;
// SHOULD NOT BE HARDCODED!! :-)
- float4 simulation_scale = 0.05; // pass sphp as argument
+ float simulation_scale = 0.05; // pass sphp as argument
float4 p = simulation_scale*(pos_s[i] - pos_cg);
vel_s[i] = (float4)(p.y*omega.z-p.z*omega.y, p.z*omega.x-p.x*omega.z,
View
207 test/main.cpp
@@ -29,6 +29,9 @@ int window_width = 640*2;
int window_height = 480*2;
int glutWindowHandle = 0;
+float4 mouse_old;
+float4 mouse_new;
+
#define DTR 0.0174532925
@@ -68,9 +71,14 @@ float cloud_velocity_x = 0.;
float cloud_velocity_y = 0.;
float cloud_velocity_z = 0.;
+float4 rotation_axis;
+void computeRotationAxis(float4& v1, float4& v2);
+
// track whether shift-key is down
bool shift_down = false;
-// if shift is down, left mouse tracks arm
+bool ctrl_down = false;
+// if shift is down, left mouse tracks arm (translation)
+// if ctrl is down, left mouse tracks arm (rotation)
bool cloud_movement = false;
@@ -96,6 +104,8 @@ void appMotion(int x, int y);
void resizeWindow(int w, int h);
void setShiftDown();
+long Vectorize(float4& hit, float4& origin, float radius, float4& vec);
+
void timerCB(int ms);
void drawString(const char *str, int x, int y, float color[4], void *font);
@@ -134,6 +144,9 @@ float4 color = float4(0.5, 0.3, 0.73, .05);
//float4 color = float4(1., 0.5, 0.0, 1.);
int hindex;
+// for rotation control via quaternion emulation
+float4 screen_center = float4(500.,500.,0.,1.);
+
//timers
@@ -536,6 +549,7 @@ void appDestroy()
}
+//----------------------------------------------------------------------
void appMouse(int button, int state, int x, int y)
{
// The shift must be down when I click the mouse. Then
@@ -559,12 +573,40 @@ void appMouse(int button, int state, int x, int y)
//glutPostRedisplay();
}
+//----------------------------------------------------------------------
void appMotion(int x, int y)
{
// If SHIFT was detected, then move arm.
// HOW TO DETECTED WHEN SHIFT IS RELEASED?
- if (!shift_down) {
+ if (shift_down) {
+ printf("***** SHIFT DOWN ****\n");
+ //cloud_movement();
+ if (mouse_buttons & 1)
+ {
+ // Change sensitivity of mouse
+ cloud_translate_x += 0.004 * (x - mouse_old_x);
+ // so that mouse negative y moves downward on screen
+ cloud_translate_z -= 0.004 * (y - mouse_old_y);
+ ps->setCloudTranslate(cloud_translate_x,
+ cloud_translate_y, cloud_translate_z);
+ }
+ printf("appMotion, shift down\n");
+ } else if (ctrl_down) {
+ printf("***** CTRL DOWN ****\n");
+ mouse_old = float4(x,y, 0.,1.);
+ mouse_new = float4(mouse_old_x,mouse_old_y, 0.,1.);
+ float4 vec_old, vec_new;
+ // 300: radius for mouse
+ Vectorize(mouse_old, screen_center, 300, vec_old);
+ Vectorize(mouse_new, screen_center, 300, vec_new);
+ computeRotationAxis(vec_old, vec_new);
+ rotation_axis.print("**** axis");
+ ps->setRotationAxis(rotation_axis);
+ //ZeroHysteresisRotation(vec_old, vec_new);
+ // cloud rotation();
+ } else {
+ // domain manipulation
printf("appMotion, shift up\n");
float dx, dy;
dx = x - mouse_old_x;
@@ -579,21 +621,8 @@ void appMotion(int x, int y)
{
translate_z -= dy * 0.1;
}
- } else {
- //cloud_movement();
- if (mouse_buttons & 1)
- {
- // Change sensitivity of mouse
- cloud_translate_x += 0.004 * (x - mouse_old_x);
- // so that mouse negative y moves downward on screen
- cloud_translate_z -= 0.004 * (y - mouse_old_y);
- ps->setCloudTranslate(cloud_translate_x,
- cloud_translate_y, cloud_translate_z);
- }
- printf("appMotion, shift down\n");
}
-
mouse_old_x = x;
mouse_old_y = y;
@@ -816,10 +845,150 @@ void draw_collision_boxes()
void setShiftDown()
{
int mod = glutGetModifiers();
- if (mod == GLUT_ACTIVE_SHIFT)
- shift_down = true;
- else
- shift_down = false;
+ shift_down = mod == GLUT_ACTIVE_SHIFT ? true : false;
+ ctrl_down = mod == GLUT_ACTIVE_CTRL ? true : false;
+
+ //printf("ctrl: %d, shift: %d\n", ctrl_down, shift_down);
+
+ //if (shift_down) exit(0);
+ //if (ctrl_down) exit(0);
+}
+//----------------------------------------------------------------------
+//Vectorize()
+// Create a 3D position vector on a unit sphere based on
+// mouse hitPt, sphere's screen origin, and sphere radius in
+// pixels. Return zero on success or non-zero if hit point
+// was outside the sphere. This routine uses Quickdraw[TM] 3D
+// and assumes that the relevant QD3D headers and library
+// files have been included. Also need to #include <math.h>.
+
+#define BOUNDS_ERR 1L
+
+long Vectorize(
+ float4& hit,
+ float4& origin,
+ float radius,
+ float4& vec )
+ //TQ3Vector3D *vec )
+{
+ float x,y,z, modulus;
+
+ x = (float)(hit.x - origin.x)/radius;
+ y = (float)(hit.y - origin.y)/radius;
+
+ //hit.print("hit");
+ //origin.print("origin");
+ //printf("before x,y= %f, %f\n", x, y);
+
+ y *= -1.0; // compensate for "inverted" screen y-axis!
+
+ modulus = sqrt(x*x + y*y);
+ //printf("modulus= %f\n", modulus);
+ //printf("radius(%f)/modulus= %f\n", radius, radius/modulus);
+
+ if (modulus > .95) { // outside radius!
+ x /= (modulus+.06); // avoid singularities
+ y /= (modulus+.06);
+ //return BOUNDS_ERR;
+ }
+
+ //printf("after x,y= %f, %f\n", x, y);
+ //printf("x^2+y^2= %f\n", x*x+y*y);
+
+ z = sqrt(1. - (x*x + y*y) ); // compute fictitious 'z' value
+ vec = float4(x,y,z,1.);
+ //vec.print("vec");
+
+ return 0L;
+}
+//----------------------------------------------------------------------
+//Rotate::ZeroHysteresisRotation()
+// From two 3D vectors representing the positions of
+// points on a unit sphere, calculate an axis of rotation
+// and an amount of rotation such that float4 A can be
+// moved along a geodesic to float4 B.
+// CAUTION: Error-checking omitted for clarity.
+
+void computeRotationAxis(float4& v1, float4& v2)
+{
+ float4 cross;
+ //TQ3Matrix4x4 theMatrix;
+ //TQ3float43D orig = { 0.,0.,0. };
+ float dot,angle;
+
+ dot = v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
+
+ if (dot == 1.0)
+ return; // nothing to do
+
+ cross = float4(v1.y*v2.z-v1.z*v2.y,
+ v1.z*v2.x-v1.x*v2.z,
+ v1.x*v2.y-v1.y*v2.x, 1.);
+
+ //Q3Vector3D_Cross( &v1, &v2, &cross ); // axis of rotation
+
+ // EVER ZERO?
+ float normi = sqrt(1./(cross.x*cross.x+cross.y*cross.y+cross.z*cross.z));
+ cross.x *= normi;
+ cross.y *= normi;
+ cross.z *= normi;
+ cross.w = 0.0;
+ //Q3Vector3D_Normalize( &cross,&cross );
+
+ angle = 2.*acos( dot ); // angle of rotation
+ printf("angle= %f\n", angle);
+ cross.w = angle;
+ cross.print("cross");
+ rotation_axis = cross;
+
+ #if 0
+ // set up a rotation around our chosen axis...
+ Q3Matrix4x4_SetRotateAboutAxis(&theMatrix,
+ &orig, &cross, angle);
+
+ Q3Matrix4x4_Multiply( &gDocument.fRotation,
+ &theMatrix,
+ &gDocument.fRotation); // multiply
+
+ DocumentDraw( &gDocument ) ; // draw
+ #endif
}
//----------------------------------------------------------------------
+#if 0
+//Rotate::FreeRotateWithMouse()
+// Call this function from the main event loop to
+// do free-rotations of 3D objects
+// the mouse action radius in pixels:
+#define RADIUS_VALUE 300.0
+
+void FreeRotateWithMouse(void)
+{
+ float4 now, oldPt, center;
+ WindowPtr win = FrontWindow();
+ float radius = RADIUS_VALUE;
+ TQ3Vector3D v1,v2;
+ long err;
+
+ GetMouse( &oldPt );
+
+ center.h = (win->portRect.right - win->portRect.left)/2;
+ center.v = (win->portRect.bottom - win->portRect.top)/2;
+
+ while (StillDown())
+ {
+ GetMouse( &now );
+
+ err = Vectorize(&oldPt, &center, RADIUS_VALUE, &v1 );
+ err += Vectorize(&now, &center, RADIUS_VALUE, &v2 );
+
+ if (!err)
+ getRotationAxis( v1,v2 );
+
+ oldPt = now;
+ }
+}
+#endif
+//----------------------------------------------------------------------
+//----------------------------------------------------------------------
+//----------------------------------------------------------------------
Please sign in to comment.
Something went wrong with that request. Please try again.