現在地 >> メニュー >> サンプルコード::OpenGL >> 波の描画::sin関数

問題


sin関数で時刻tにおけるsin波の要素を描画せよ

その他条件:
 マウスで視点変更可能
 「↑」「↓」で周波数変更
 「→」「←」で表示幅変更

波の描画::パルス波

答え


#include <iostream>
#include <cstdio>
#include <cmath>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>

using namespace std;

#define POINT_NUM 100
#define POINT_SIZE 2


/**** データ構造 ****/
typedef struct
{
double x,y,z;
}POINT_3D;

POINT_3D Point[POINT_NUM];


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

void DRAW_XYZ();
void CALC_WAVE();
void POINT_SHIFT_RIGHT(POINT_3D *point,int num);


/************** OpenGLの各種設定 ***************************/
inline void GLUT_INIT()
{
glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(300,300);
}

inline void GLUT_CALL_FUNC()
{
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(1,timer,0);
glutMouseFunc(myMouseFunc);
glutMotionFunc(myMouseMotion);
glutSpecialFunc(SpecialKey);
}


double _lamda = 1.0;
double movement;
inline void MY_INIT()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glEnable(GL_DEPTH_TEST);
movement=(POINT_NUM /_lamda);
}


/************** ここからメイン関数 *****************/
int main(int argc, char **argv)
{
glutInit(&argc,argv);
GLUT_INIT();
glutCreateWindow("window name");
GLUT_CALL_FUNC();
MY_INIT();
glutMainLoop();

return 0;
}


/********[ここからコールバック]****************************************/
double xAngle = 0.0, yAngle = 0.0;
double xAngle2 = 0.0, yAngle2 = 0.0;
void display()
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();


glRotated(xAngle2,1,0,0);
glRotated(yAngle2,0,1,0);

gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

glRotated(xAngle,1,0,0);
glRotated(yAngle,0,1,0);

DRAW_XYZ();

CALC_WAVE();

glutSwapBuffers();

}


void reshape(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);
glLoadIdentity();
}

void timer(int value)
{
glutPostRedisplay();
glutTimerFunc(1,timer,0); //タイマー関数
}


int _Hz = 5;
void SpecialKey(int key, int x, int y)
{
switch (key) {
case GLUT_KEY_UP:
_Hz+=1.0;
cout << _Hz << "step\n";
break;

case GLUT_KEY_DOWN:
_Hz-=1.0;
if(_Hz < 0.0)
{
_Hz = 0.0;
}
cout << _Hz << "step\n";
break;


case GLUT_KEY_RIGHT:
_lamda += 0.1;
movement=(POINT_NUM /_lamda);
cout << _lamda << " width\n";
break;

case GLUT_KEY_LEFT:
_lamda -= 0.1;
if(_lamda < 0.0)
{
_lamda = 0.001;
}
movement=(POINT_NUM /_lamda);
cout << _lamda << " width\n";
break;

}
}


int xStart,yStart;
bool mouseFlag = GL_FALSE;
bool mouseFlag2 = GL_FALSE;
void myMouseFunc(int button,int state,int x,int y)
{

if(button == GLUT_LEFT_BUTTON &&state == GLUT_DOWN)//左ボタン
{
xStart = x;
yStart = y;
mouseFlag = GL_TRUE;
}
else
{
mouseFlag = GL_FALSE;
}

if(button == GLUT_RIGHT_BUTTON &&state == GLUT_DOWN)//右ボタン
{
xStart = x;
yStart = y;
mouseFlag2 = GL_TRUE;

}
else
{
mouseFlag2 = GL_FALSE;
}

}

void myMouseMotion(int x,int y)
{
int xdis,ydis;
double a = 0.5;

if*1 return;


if(mouseFlag == GL_TRUE)
{
xdis = x - xStart;
ydis = y - yStart;

xAngle += (double)ydis * a;
yAngle += (double)xdis * a;

xStart = x;
yStart = y;
}

if(mouseFlag2 == GL_TRUE)
{
xdis = x - xStart;
ydis = y - yStart;

xAngle2 += (double)ydis * a;
yAngle2 += (double)xdis * a;

xStart = x;
yStart = y;
}


}

/**********[ここから各種関数]***********************/

void DRAW_XYZ()
{
glBegin(GL_LINES);

glColor3d(0,1,0);//x
glVertex2d(-100,0);
glVertex2d(100, 0);

glColor3d(1,0,0);//y
glVertex2d(0,0);
glVertex2d(0,100);

glColor3d(0,0,1);//z
glVertex3d(0,0,-100);
glVertex3d(0,0, 100);
glEnd();

}


void CALC_WAVE()
{
#define PI_OVER_180 0.0174532925
static double t = 0;

Point[0].y = sin(t*PI_OVER_180);

POINT_SHIFT_RIGHT(Point,POINT_NUM);

static int i;
glPointSize(POINT_SIZE);
glColor3f(1,0,1);
glBegin(GL_POINTS);
for(i = 0;i < POINT_NUM; ++i)
{
glVertex3d(i/movement,Point[i].y,0);
}
glEnd();
glColor3f(1,1,1);

t+= _Hz;

}


void POINT_SHIFT_RIGHT(POINT_3D *point,int num)
{
static int i;
for(i = num - 2;i >= 0;--i)
{
point[i+1] = point[i];
}
}

メモ


わりとてきとう。

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






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


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

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