最終更新: mikk_ni3_92 2007年10月09日(火) 13:47:52履歴
現在地 >> メニュー >> サンプルコード::OpenGL >> 頂点配列による描画
頂点配列を用いて、陰面消去とアニメーションのプログラムを実装せよ。
→頂点配列は、
double points[3][2]=
{
{0,1,0},
{-0.5,0,0},
{0.5,0,0}
};
と指定する。
→頂点配列による描画2
#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_TRI();
using namespace std;
double points[3][3]=
{
{0,1,0},
{-0.5,0,0},
{0.5,0,0}
};
inline void GLUT_INIT()
{
glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(200,200);
}
inline void GLUT_CALL_FUNC()
{
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(10,timer,0);
}
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;
}
/********[ここからコールバック]****************************************/
void display()
{
static int r = 0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
DRAW_XYZ();
glEnable(GL_DEPTH_TEST);
glPushMatrix();
glRotated((double)r, 0.0, 1.0, 0.0);
glColor3d(1,1,0);
DRAW_TRI();
glPopMatrix();
glPushMatrix();
glRotated((double)r+90, 0.0, 1.0, 0.0);
glColor3d(0,1,0);
DRAW_TRI();
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);
glLoadIdentity();
gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0);
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); //タイマー関数
}
/**********[ここから各種関数]***********************/
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 DRAW_TRI()
{
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3 , GL_DOUBLE , 0 , &points[0]);
glBegin(GL_TRIANGLES);
for(int i = 0 ; i < 3 ; i++)
{
glArrayElement(i);
}
glEnd();
glDisableClientState(GL_VERTEX_ARRAY);
}
頂点配列を用いて、陰面消去とアニメーションのプログラムを実装せよ。
→頂点配列は、
double points[3][2]=
{
{0,1,0},
{-0.5,0,0},
{0.5,0,0}
};
と指定する。
→頂点配列による描画2
#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_TRI();
using namespace std;
double points[3][3]=
{
{0,1,0},
{-0.5,0,0},
{0.5,0,0}
};
inline void GLUT_INIT()
{
glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(200,200);
}
inline void GLUT_CALL_FUNC()
{
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(10,timer,0);
}
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;
}
/********[ここからコールバック]****************************************/
void display()
{
static int r = 0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
DRAW_XYZ();
glEnable(GL_DEPTH_TEST);
glPushMatrix();
glRotated((double)r, 0.0, 1.0, 0.0);
glColor3d(1,1,0);
DRAW_TRI();
glPopMatrix();
glPushMatrix();
glRotated((double)r+90, 0.0, 1.0, 0.0);
glColor3d(0,1,0);
DRAW_TRI();
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);
glLoadIdentity();
gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0);
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); //タイマー関数
}
/**********[ここから各種関数]***********************/
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 DRAW_TRI()
{
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3 , GL_DOUBLE , 0 , &points[0]);
glBegin(GL_TRIANGLES);
for(int i = 0 ; i < 3 ; i++)
{
glArrayElement(i);
}
glEnd();
glDisableClientState(GL_VERTEX_ARRAY);
}