読者です 読者をやめる 読者になる 読者になる

個人的自由ノート

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

OpenGLでマウスの視点移動プログラム?

いろいろ調べてやってみてる最中ですが、なにかおかしいような…。数式が間違えてるんでしょうか。もう少しやってみますが、時間がかかりすぎたので今日はここまでにします。

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

static int flag_ld = 0;
static int flag_rd = 0;
static const double t = 0.001;
static const double PI = 3.141592653589793;
static int w0 = 0;
static int h0 = 0;
static double v_abs;
static double angle;
static double a;

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;
  static double p[2];
  static 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] = v_abs * sin(r * PI / 180.0);
    v[1] = v_abs * 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];
  }
  if(flag_rd) {
    v[0] = v_abs * sin(r * PI / 180.0);
    v[1] = v_abs * cos(r * PI / 180.0);
  }
}

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){
  v_abs = sqrt(pow(abs(x - w0), 2) + pow(abs(y - h0), 2));
  angle = acos(abs(x - w0) / v_abs);
  a = angle / 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;
}

%e3%82%b9%e3%82%af%e3%83%aa%e3%83%bc%e3%83%b3%e3%82%b7%e3%83%a7%e3%83%83%e3%83%88-2016-11-15-20-30-02