2014年3月12日水曜日

似非マウス回転

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(eye.fovy, (float)eye.width/(float)eye.height, 0.1, 100000);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eye.eye[0], eye.eye[1], eye.eye[2],
eye.center[0], eye.center[1], eye.center[2],
eye.up[0], eye.up[1], eye.up[2]);

eye.calcuratePosition(beforePoint,beforeMouse[0],beforeMouse[1]);
eye.calcuratePosition(nowPoint,x,y);
eye.rotateEye(beforePoint,nowPoint);

void Eye::calcuratePosition(double result[3],int x,int y)
{
x -= this->width/2;
y -= this->height/2;
printf("%d,%d\n",x,y);
double rate =  (2.0 * 50.0 * tan(PI/180*this->fovy/2))/height;

printf("%lf\n",rate);
double rateX = x * rate;
double rateY = y * rate;

for (int i = 0;i < 3; i++){
result[i] = 50.0 * this->center[i] + rateX * this->left[i] + rateY * this->up[i];
}
}

void Eye::rotateEye(double start[3],double end[3])
{
double axis[3];
double radius;
vectorNormarise(start);
vectorNormarise(end);

exteriorProduct(start ,end, axis);
vectorNormarise(axis);
radius = acos(innerrProduct(start ,end));

printf("%lf,%lf,%lf\n",start[0],start[1],start[2]);
printf("%lf,%lf,%lf\n",end[0],end[1],end[2]);
printf("%lf,%lf,%lf,%lf\n",axis[0],axis[1],axis[2],radius);

rotateVector(axis,radius,center);
rotateVector(axis,radius,up);
rotateVector(axis,radius,left);
}
void Eye::rotateBy(double axis[3],double radius)
{
rotateVector(axis,radius,center);
rotateVector(axis,radius,up);
rotateVector(axis,radius,left);
printf("%lf,%lf,%lf\n",center[0],center[1],center[2]);
printf("%lf,%lf,%lf\n",up[0],up[1],up[2]);

printf("%lf,%lf,%lf\n",up[0],up[1],up[2]);
}

void exteriorProduct(double vec1[3],double vec2[3],double resullt[3])
{
resullt[0] = vec1[1] * vec2[2] - vec1[2] * vec2[1];
resullt[1] = vec1[2] * vec2[0] - vec1[0] * vec2[2];
resullt[2] = vec1[0] * vec2[1] - vec1[1] * vec2[0];
}
double vectorSize(double vec[3])
{
return sqrt(vec[0]*vec[0]+vec[1]*vec[1]+vec[2]*vec[2]);
}
void vectorNormarise(double vec1[3])
{
double size = vectorSize(vec1);
vec1[0] /= size;
vec1[1] /= size;
vec1[2] /= size;
}

double innerrProduct(double vec1[3],double vec2[3])
{
return vec1[0]*vec2[0]+vec1[1]*vec2[1]+vec1[2]*vec2[2];
}

void rotateVector(double axis[3],double radius, double vec[3])
{
double matrix[3][3];
matrix[0][0] = axis[0] * axis[0] * (1 - cos(radius)) + cos(radius);
matrix[0][1] = axis[0] * axis[1] * (1 - cos(radius)) - axis[2] * sin(radius);
matrix[0][2] = axis[2] * axis[0] * (1 - cos(radius)) + axis[1] * sin(radius);


matrix[1][0] = axis[0] * axis[1] * (1 - cos(radius)) + axis[2] * sin(radius);
matrix[1][1] = axis[1] * axis[1] * (1 - cos(radius)) + cos(radius);
matrix[1][2] = axis[1] * axis[2] * (1 - cos(radius)) - axis[0] * sin(radius);

matrix[2][0] = axis[2] * axis[0] * (1 - cos(radius)) - axis[1] * sin(radius);
matrix[2][1] = axis[1] * axis[2] * (1 - cos(radius)) + axis[0] * sin(radius);
matrix[2][2] = axis[2] * axis[2] * (1 - cos(radius)) + cos(radius);

double tmp[3];
for (int i = 0;i < 3;i++){
tmp[i] = vec[i];
}
for (int i = 0;i < 3;i++){
vec[i] = tmp[0] * matrix[i][0] + tmp[1] * matrix[i][1] + tmp[2] * matrix[i][2];
}
}

0 件のコメント:

コメントを投稿