落書きノート

ふと自分が気になった事を書いてます

OpenGLでマウスドラッグでの視点移動作成失敗っぽい…。

今回は失敗ですね。数学に精通してないと厳しいかも…。クォータニオンを使えば楽だと書いてありましたが、とりあえずここまでにしておきました。

#include <stdlib.h>
#include <math.h>
#include <stdlib.h>
#include <GL/glut.h>

int flag_ld = 0;
int flag_rd = 0;
const double t = 0.00001;
const double PI = 3.14;
int w0 = 0;
int h0 = 0;
int cx = 0;
int cy = 0;
double distance = 0.0;
double a = 0.0;

void scene(void){
  static GLfloat red[] = { 0.8, 0.2, 0.2, 1.0 };
  static GLfloat green[] = { 0.2, 0.8, 0.2, 1.0 };
  static GLfloat blue[] = { 0.2, 0.2, 0.8, 1.0 };
  static GLfloat yellow[] = { 0.8, 0.8, 0.2, 1.0 };
  static GLfloat ground[][4] = {
    { 0.6, 0.6, 0.6, 1.0 },
    { 0.3, 0.3, 0.3, 1.0 }
  };

  int i, j;

  glPushMatrix();
  glTranslated(0.0, 0.0, -3.0);
  glMaterialfv(GL_FRONT, GL_DIFFUSE, red);
  glutSolidCube(1.0);
  glPopMatrix();

  glPushMatrix();
  glTranslated(0.0, 0.0, 3.0);
  glMaterialfv(GL_FRONT, GL_DIFFUSE, green);
  glutSolidCube(1.0);
  glPopMatrix();

  glPushMatrix();
  glTranslated(-3.0, 0.0, 0.0);
  glMaterialfv(GL_FRONT, GL_DIFFUSE, blue);
  glutSolidCube(1.0);
  glPopMatrix();

  glPushMatrix();
  glTranslated(3.0, 0.0, 0.0);
  glMaterialfv(GL_FRONT, GL_DIFFUSE, yellow);
  glutSolidCube(1.0);
  glPopMatrix();

  glBegin(GL_QUADS);
  glNormal3d(0.0, 1.0, 0.0);
  for(j = -5; j < 5; ++j){
    for(i = -5; i < 5; ++i){
      glMaterialfv(GL_FRONT, GL_DIFFUSE, ground[(i + j) & 1]);
      glVertex3d((GLdouble)i, -0.5, (GLdouble)j);
      glVertex3d((GLdouble)i, -0.5, (GLdouble)(j + 1));
      glVertex3d((GLdouble)(i + 1), -0.5, (GLdouble)(j + 1));
      glVertex3d((GLdouble)(i + 1), -0.5, (GLdouble)j);
    }
  }
  glEnd();
}

void idle(void){
  glutPostRedisplay();
}

void display(void){
  static GLfloat lightpos[] = { 3.0, 4.0, 5.0, 1.0 };
  static double ex = 0.0, ez = 0.0;
  static double r = 0.0;
  double p[2];
  double v[2];

  p[0] = ex;
  p[1] = ez;

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glLoadIdentity();

  glRotated(r, 0.0, 1.0, 0.0);
  glTranslated(ex, 0.0, ez);

  glLightfv(GL_LIGHT0, GL_POSITION, lightpos);

  scene();

  glutSwapBuffers();

  if(flag_ld){
    r = a * t + r;
    v[0] = distance * sin(r * PI / 180.0);
    v[1] = distance * cos(r * PI / 180.0);
    p[0] = v[0] * t + p[0];
    p[1] = v[1] * t + p[1];
    ex = p[0];
    ez = p[1];
  }
}

void resize(int w, int h){
  glViewport(0, 0, w, h);
  glMatrixMode(GL_PROJECTION);

  glLoadIdentity();
  gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0);

  glMatrixMode(GL_MODELVIEW);

  w0 = w / 2;
  h0 = h / 2;
}

void keyboard(unsigned char key, int x, int y){
  if(key == '\033' || key == 'q'){
    exit(0);
  }
}

void mouse(int button, int state, int x, int y){
  switch(button){
    case GLUT_LEFT_BUTTON:
      if(state == GLUT_DOWN){
        flag_ld = 1;
        glutIdleFunc(idle);
      } else {
        flag_ld = 0;
        glutIdleFunc(0);
      }
      break;
    case GLUT_RIGHT_BUTTON:
      if(state == GLUT_DOWN){
        flag_rd = 1;
        glutIdleFunc(idle);
      } else {
        flag_rd = 0;
        glutIdleFunc(0);
      }
      break;
    default:
      break;
  }
}

void motion(int x, int y){
  int x_distance = x - w0;
  int y_distance = y - h0;
  distance = sqrt(pow(abs(x_distance), 2) + pow(abs(y_distance), 2));
  double radian = atan2(y_distance, x_distance);
  a = radian / t;
}

void init(void){
  glClearColor(1.0, 1.0, 1.0, 0.0);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_CULL_FACE);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
}

int main(int argc, char** argv){
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
  glutCreateWindow(argv[0]);
  glutMouseFunc(mouse);
  glutMotionFunc(motion);
  glutDisplayFunc(display);
  glutReshapeFunc(resize);
  glutKeyboardFunc(keyboard);
  init();
  glutMainLoop();
  return 0;
}