最終更新: mikk_ni3_92 2007年09月06日(木) 11:16:40履歴
現在地 >> メニュー >> サンプルコード::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();
}
#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();
}