(2)一段目(羽のついてる所)を異なる速度で回転させよ。
(3)polarviewを用いて、斜め上30度から見た画面にせよ。
(4)マウス操作で視点の変更を可能とする。
・左ボタンの左右方向のドラッグで物体が垂直軸回りに回転。また同ボタンの
上下方向のドラッグで水平軸回りに回転。
・右ボタンのドラッグで、物体が拡大縮小する。
・ビューイング変換にはpolrviewを用いる。
(5)F1キーを押すと一段目が離れるようにせよ。
という宿題が出されました。
[1.1] (3)(5)の作成をしたいと思っております。
[1.2]
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <math.h>
#include "myShape.h"
#define KEY_ESC 27
void polarview( void );
void resetview( void );
unsigned char wireFlag = GL_TRUE;
unsigned char revolveFlag = GL_FALSE;
int xBegin, yBegin;
int mButton;
float distance, twist, elevation, azimuth;
float theta=0.0; /*物体の回転角度*/
float theta2=0.0; /*物体の回転角度*/
void display( void )
{
glClear( GL_COLOR_BUFFER_BIT );
glPushMatrix();
glTranslatef( 0.0, 0.0, -20.0 );
polarview(); /*ビューイング*/
glPushMatrix();
glRotatef( theta, 0.0, 1.0, 0.0 ); /*回転させたいところ*/
glTranslatef( 0.0, 1.0, 0.0 );
glColor3f(1.0,0.0,0.0);
myWireCylinder( 1.0, 2.0, 12 );
glTranslatef( 0.0, 1.0, 0.0);
glRotatef( -90.0, 1.0, 0.0, 0.0 );
glColor3f(1.0,0.5,0.0);
glutWireCone( 1.0, 2.0, 12, 3 );
glPopMatrix();
glRotatef( theta2, 0.0, 1.0, 0.0 ); /*回転させたいところ*/
glTranslatef( 0.0, -1.0, 0.0 );
glColor3f(0.0,1.0,0.0);
myWireCylinder( 1.0, 2.0, 12 );
glPopMatrix();
glPushMatrix();
glTranslatef( 0.0, -1.0, -20.0);
polarview(); /*ビューイング*/
glRotatef( theta2, 0.0, 1.0, 0.0 ); /*回転させたいところ*/
glTranslatef( 0.0, -1.5, 0.0);
glRotatef( -90.0, 1.0, 0.0, 0.0 );
glColor3f(0.0,1.0,1.0);
glutWireCone( 0.5, 0.8, 12, 1 );
glTranslatef(0.0,0.0,1.5);
glScalef(0.8,0.07,0.5);
glColor3f(1.0,0.0,1.0);
glutWireCube(4.0);
glTranslatef(0.0,0.0,0.0);
glScalef(0.08,11.0,1.0);
glColor3f(1.0,1.0,0.0);
glutWireCube(4.0);
glPopMatrix();
glutSwapBuffers(); /*バッファをスワップする*/
}
void idle(void) /*イベントがなければidleが常に実行される*/
{
theta=fmod(theta+0.2,360.0); /*回転角を0~360まで0.2ずつ増加*/
theta2=fmod(theta2+2.0,360.0); /*回転角を0~360まで2.0ずつ増加*/
glutPostRedisplay();
}
void myKbd( unsigned char key, int x, int y )
{
if( key == KEY_ESC ) exit( 0 );
}
void myMouse( int button, int state, int x, int y )
{
if (state == GLUT_DOWN) {
switch(button) {
case GLUT_LEFT_BUTTON:
mButton = button;
break;
case GLUT_MIDDLE_BUTTON:
revolveFlag = !revolveFlag;
if( revolveFlag == GL_TRUE )
glutIdleFunc( idle );
else
glutIdleFunc(NULL);
break;
case GLUT_RIGHT_BUTTON:
mButton = button;
break;
}
xBegin = x;
yBegin = y;
}
}
void myMotion( int x, int y )
{
int xDisp, yDisp;
xDisp = x - xBegin;
yDisp = y - yBegin;
switch (mButton) {
case GLUT_LEFT_BUTTON:
azimuth += (float) xDisp/2.0;
elevation -= (float) yDisp/2.0;
break;
case GLUT_RIGHT_BUTTON:
distance += (float) yDisp/40.0;
break;
}
xBegin = x;
yBegin = y;
glutPostRedisplay();
}
void myInit (char *progname)
{
int width = 300, height = 600;
float aspect = (float) width / (float) height;
glutInitWindowPosition(0, 0);
glutInitWindowSize( width, height );
glutInitDisplayMode( GLUT_RGBA );
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); /*ダブルバッファ宣言*/
glutCreateWindow(progname);
glClearColor (0.0, 0.0, 0.0, 1.0);
glutKeyboardFunc( myKbd );
glutMouseFunc(myMouse);
glutMotionFunc(myMotion);
resetview();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0, aspect, 1.0, 50.0);
glMatrixMode(GL_MODELVIEW);
}
void resetview( void )
{
distance = 5.0;
twist = 0.0;
elevation = 0.0;
azimuth = 0.0;
}
void polarview( void )
{
glTranslatef( 0.0, 0.0, -distance);
glRotatef( -twist, 0.0, 0.0, 1.0);
glRotatef( -elevation, 1.0, 0.0, 0.0);
glRotatef( -azimuth, 0.0, 1.0, 0.0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
myInit(argv[0]);
glutDisplayFunc(display);
glutIdleFunc(idle); /*イベントがない場合にはidleを繰り返す*/
glutMainLoop();
return( 0 );
}
[1.4]4枚の羽根が作れず、その上視点を変えると羽と噴射口の位置がずれます。
どのように直せばよいのか分からないため、修正をお願い致します。
[2] 環境
[2.1] OS : Windows
[2.2] コンパイラ名 : Microsoft Visual C++ 2010 Express
[3] その他
・初心者です。
・期限は12月5日までです。
どうぞ、よろしくお願いいたします。