現在地 >> メニュー >> サンプルコード::OpenGL >> 点のアニメーション >> 点のアニメーション::その2 >> 点のアニメーション::その3 >> 点のアニメーション::改


問題


3D空間上に点を打って、立方体を描画せよ。
その他条件:
 「↑」、「↓」キーで点の数を変更。
 「→」、「←」キーで立方体のサイズ変更。
 「p」、「o」キーで点のサイズ変更。

答え


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

void display();
void reshape(int w, int h);
void SpecialKey(int key, int x, int y);
void keyboard(unsigned char key, int x, int y);


void DRAW_XYZ();


using namespace std;


/*******[OpenGLの初期設定]********/
inline void GLUT_INIT()
{
glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(200,200);
}

inline void GLUT_CALL_FUNC()
{
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutSpecialFunc(SpecialKey);
glutKeyboardFunc(keyboard);
}

inline void MY_INIT()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
}


int main(int argc, char **argv)
{
glutInit(&argc,argv);
GLUT_INIT();
glutCreateWindow("window name");
GLUT_CALL_FUNC();
MY_INIT();
glutMainLoop();

return 0;
}

/********[ここからコールバック]****************************************/
int num =2;
double size=1;
float p_size=1;

void display()
{
static int r = 0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

DRAW_XYZ();

glEnable(GL_DEPTH_TEST);

glPushMatrix();

glPointSize(p_size);
glBegin(GL_POINTS);

double x = -(size/2.0);
double y = x;
double z = -x;

for(int k = 0; k < num ;k++)
{
for(int j = 0; j < num ;j++)
{
for(int i = 0; i < num ;i++)
{
glVertex3d( x,y,z);
x += ( double(size)/double(num-1) );
}
y += ( double(size)/double(num-1) );
x = -(size/2.0);
}
z -= ( double(size)/double(num-1) );
y = x;
}


glEnd();

glPopMatrix();


glDisable(GL_DEPTH_TEST);

glutSwapBuffers();

r = r + 1;
if(r > 360)
{
r= 0;
}
}


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 SpecialKey(int key, int x, int y)
{
switch (key) {
case GLUT_KEY_UP:
num ++;
break;

case GLUT_KEY_DOWN:
num--;
if(num<2)num = 2;
break;


case GLUT_KEY_RIGHT:
size+=0.1;
break;

case GLUT_KEY_LEFT:
size-=0.1;
if(size<0)size = 0;
break;

}

glutPostRedisplay();
}

void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 'p':
p_size ++;
break;

case 'o':
p_size --;
if(p_size < 1)p_size = 1;
break;
}
glutPostRedisplay();
}



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

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();

}

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






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


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

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