現在地 >> メニュー >> サンプルコード::OpenGL >> 引数として関数を渡して描画

問題


引数として関数を渡して物体を描画せよ。

答え


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

inline void SPHERE();
inline void CUBE();
inline void DRAW(void (*func)(void));

using namespace std;


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);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}


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

return 0;
}

GLfloat light_pos[] = { 0.0, 3.0, 5.0, 1.0 };
GLfloat red[] = { 0.8, 0.2, 0.2, 1.0 };
GLfloat green[] = { 0.2, 0.8, 0.2, 1.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);

glLightfv(GL_LIGHT0, GL_POSITION, light_pos);

glEnable(GL_DEPTH_TEST);

DRAW(CUBE);
DRAW(SPHERE);

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

}

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

/**********[ここから各種関数]***********************/
inline void DRAW(void (*func)(void))
{
func();
}

inline void CUBE()
{

static int r = 0;

glPushMatrix();
glRotated((double)r, 0.0, 1.0, 0.0);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, red);
glutSolidCube(1);
glPopMatrix();

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

}


inline void SPHERE()
{
glPushMatrix();
glTranslated(0.5,0,0);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, green);
glutSolidSphere(0.5,30,30);
glPopMatrix();
}

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






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


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

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