最終更新: mikk_ni3_92 2007年11月19日(月) 15:16:11履歴
現在地 >> メニュー >> サンプルコード::OpenGL >> 点のアニメーション >> 点のアニメーション::その2 >> 点のアニメーション::その3
1×1×1の空間に点を8000個打って、立方体を描画せよ。
その他条件:
→ 点のアニメーション::改
#include <cstdio>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
void display();
void reshape(int w, int h);
void timer(int value);
void DRAW_XYZ();
void DRAW_POINT();
void myMouseFunc(int button,int state,int x,int y);
void myMouseMotion(int x,int y);
using namespace std;
inline void GLUT_INIT()
{
glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(250,250);
}
inline void GLUT_CALL_FUNC()
{
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(10,timer,0);
glutMouseFunc(myMouseFunc);
glutMotionFunc(myMouseMotion);
}
inline void MY_INIT()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glHint(GL_POINT_SMOOTH_HINT,GL_NICEST);
glEnable(GL_POINT_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT,GL_NICEST);
glEnable(GL_LINE_SMOOTH);
}
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;
void display()
{
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);
glRotated(xAngle,1,0,0);
glRotated(yAngle,0,1,0);
DRAW_XYZ();
glEnable(GL_DEPTH_TEST);
glPushMatrix();
DRAW_POINT();
glPopMatrix();
glDisable(GL_DEPTH_TEST);
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);
gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
void timer(int value)
{
glutPostRedisplay();
glutTimerFunc(1,timer,0);
}
int xStart,yStart;
bool mouseFlag = 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;
}
}
void myMouseMotion(int x,int y)
{
int xdis,ydis;
double a = 0.5;
if(mouseFlag == GL_FALSE)return;
xdis = x - xStart;
ydis = y - yStart;
xAngle += (double)ydis * a;
yAngle += (double)xdis * a;
xStart = x;
yStart = y;
glutPostRedisplay();
}
/**********[ここから各種関数]***********************/
void DRAW_XYZ()
{
glLineWidth(0.5);
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 DRAW_POINT()
{
glPointSize(1);
glBegin(GL_POINTS);
for(int z = 0;z<20;z++)
{
for(int y = 0;y<20;y++)
{
for(int x = 0;x<20;x++)
{
glVertex3d((double)x/19,(double)y/19,(double)z/19);
}
}
}
glEnd();
}
1×1×1の空間に点を8000個打って、立方体を描画せよ。
その他条件:
- 点の大きさは1、線の太さは0.5
- マウスで視点を変えられるようにする。
- わかりやすいように、XYZ軸を描画する。
- 点、線には、アンチエイリアス処理をすること。(品質はGL_NICESTとする。)
→ 点のアニメーション::改
#include <cstdio>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
void display();
void reshape(int w, int h);
void timer(int value);
void DRAW_XYZ();
void DRAW_POINT();
void myMouseFunc(int button,int state,int x,int y);
void myMouseMotion(int x,int y);
using namespace std;
inline void GLUT_INIT()
{
glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(250,250);
}
inline void GLUT_CALL_FUNC()
{
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(10,timer,0);
glutMouseFunc(myMouseFunc);
glutMotionFunc(myMouseMotion);
}
inline void MY_INIT()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glHint(GL_POINT_SMOOTH_HINT,GL_NICEST);
glEnable(GL_POINT_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT,GL_NICEST);
glEnable(GL_LINE_SMOOTH);
}
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;
void display()
{
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);
glRotated(xAngle,1,0,0);
glRotated(yAngle,0,1,0);
DRAW_XYZ();
glEnable(GL_DEPTH_TEST);
glPushMatrix();
DRAW_POINT();
glPopMatrix();
glDisable(GL_DEPTH_TEST);
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);
gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
void timer(int value)
{
glutPostRedisplay();
glutTimerFunc(1,timer,0);
}
int xStart,yStart;
bool mouseFlag = 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;
}
}
void myMouseMotion(int x,int y)
{
int xdis,ydis;
double a = 0.5;
if(mouseFlag == GL_FALSE)return;
xdis = x - xStart;
ydis = y - yStart;
xAngle += (double)ydis * a;
yAngle += (double)xdis * a;
xStart = x;
yStart = y;
glutPostRedisplay();
}
/**********[ここから各種関数]***********************/
void DRAW_XYZ()
{
glLineWidth(0.5);
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 DRAW_POINT()
{
glPointSize(1);
glBegin(GL_POINTS);
for(int z = 0;z<20;z++)
{
for(int y = 0;y<20;y++)
{
for(int x = 0;x<20;x++)
{
glVertex3d((double)x/19,(double)y/19,(double)z/19);
}
}
}
glEnd();
}