現在地: メニュー >> 実践編02 >> 実践編02::クォータニオン >> 実践編02::まとめコード2

問題


「trackball.h」、「trackball.c」を使って、クォータニオンによる、物体のマウス回転を実現せよ。

答え

#include <iostream>
#include <GL/glut.h>
#include "trackball.h"

//------------ 各種外部変数 -----------//
float curquat[4];  //現在のクォータニオン
float lastquat[4]; //更新用のクォータニオン
std::pair<int,int> WindowSize(640,480);

int moving = 0;
int beginx, beginy;



//--------- プロトタイプ宣言 -----------//
void display();
void reshape(int w, int h);
void myMouseFunc(int button,int state,int x,int y);
void myMouseMotion(int x,int y);

void DRAW_XYZ();


//------------- OpenGLの初期化 -------------//
void GLUT_CALL_FUNCs()
{
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutMouseFunc(myMouseFunc);
	glutMotionFunc(myMouseMotion);

}


void OtherMyInit()
{
	glClearColor(1.0, 1.0, 1.0, 1.0);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_LIGHTING);//光源の有効化
	glEnable(GL_LIGHT0);

	glEnable(GL_NORMALIZE);
}


void GLUT_INITs(int *argcp, char **argv)
{
	glutInit(argcp,argv);
	glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE | GLUT_DEPTH);
	glutInitWindowSize(WindowSize.first,WindowSize.second);
	glutCreateWindow("Basic trackball");
	GLUT_CALL_FUNCs();
	OtherMyInit();
}


//-------------メイン関数-----------------//
int main(int argc, char **argv)
{
	trackball(curquat, 0.0, 0.0, 0.0, 0.0);
	GLUT_INITs(&argc,argv);
	glutMainLoop();
	return 0;
}


//------------- ここから各種コールバック -----------------//
void display()
{
	static float Light0Pos[]={-100.0, 100.0, 100.0,0}; //光源の位置
	static float m[4][4];//回転行列用

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glLoadIdentity();
	gluLookAt(0.0, 0.0, 5.0, 0,0,0, 0.0, 1.0, 0.0);

	build_rotmatrix(m, curquat);//クォータニオンから回転行列を作成する
	glMultMatrixf(&m[0][0]);

	glLightfv(GL_LIGHT0, GL_POSITION, Light0Pos);//位置だけ設定(あとはデフォルト)

	DRAW_XYZ();
	glutSolidTeapot(1);
	glutSwapBuffers();

}

void reshape(int w, int h)
{
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(30.0, (double)w / (double)h, 0.1, 1000.0);
	glMatrixMode(GL_MODELVIEW);

}



void myMouseFunc(int button,int state,int x,int y)
{
	if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
	{
		moving = 1;
		beginx = x;
		beginy = y;
	}

	if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) 
	{
		moving = 0;
	}

}

void myMouseMotion(int x,int y)
{

	if (moving) 
	{
		//マウスの視点と終点からクォータニオンを計算する(-1〜1の範囲に正規化した座標を指定する)
		trackball(lastquat,
			(2.0 * beginx - WindowSize.first) / WindowSize.first,
			(WindowSize.second - 2.0 * beginy) / WindowSize.second,
			(2.0 * x - WindowSize.first) / WindowSize.first,
			(WindowSize.second - 2.0 * y) / WindowSize.second
		);

		beginx = x;
		beginy = y;
		
		add_quats(lastquat, curquat, curquat);//クォータニオンを加算
	}
	
	glutPostRedisplay();
}


//------------ ここから各種関数 ---------------//
void DRAW_XYZ()
{
 
	glDisable(GL_LIGHTING);//光源の有効化
	glDisable(GL_LIGHT0);
	
	glBegin(GL_LINES);

	glColor3f(0,1,0);//x
	glVertex2f(-100,0);
	glVertex2f(100, 0);

	glColor3f(1,0,0);//y
	glVertex2f(0,0);
	glVertex2f(0,100);

	glColor3f(0,0,1);//z
	glVertex3f(0,0,-100);
	glVertex3f(0,0, 100);
	glEnd();
	
	glEnable(GL_LIGHTING);//光源の有効化
	glEnable(GL_LIGHT0);
}

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






▲よろしければ広告のクリックもお願いします


▲ランキングに参加しました

管理人/副管理人のみ編集できます