#pragma comment(linker, "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup")
#include <GL/freeglut/freeglut.h>
#define WIDTH 320
#define HEIGHT 240
//回転用
float angle = 0.0f;
//ピンク
GLfloat Pink[] = { 1.0, 0.5, 0.5, 1.0 };
//ライトの位置
GLfloat lightpos[] = { 200.0, 500.0, 0.0, 1.0 };
//直方体
void cuboid(float width,float height,float depth)
{
glBegin(GL_QUADS);
//前
glNormal3f(0.0,0.0,-1.0);
glVertex3f(width/2,height/2,depth/2);
glVertex3f(-width/2,height/2,depth/2);
glVertex3f(-width/2,-height/2,depth/2);
glVertex3f(width/2,-height/2,depth/2);
//左
glNormal3f(1.0,0.0,0.0);
glVertex3f(width/2,height/2,depth/2);
glVertex3f(width/2,height/2,-depth/2);
glVertex3f(width/2,-height/2,-depth/2);
glVertex3f(width/2,-height/2,depth/2);
//右
glNormal3f(-1.0,0.0,0.0);
glVertex3f(-width/2,height/2,-depth/2);
glVertex3f(-width/2,height/2,depth/2);
glVertex3f(-width/2,-height/2,depth/2);
glVertex3f(-width/2,-height/2,-depth/2);
//後
glNormal3f(0.0,0.0,1.0);
glVertex3f(width/2,height/2,-depth/2);
glVertex3f(-width/2,height/2,-depth/2);
glVertex3f(-width/2,-height/2,-depth/2);
glVertex3f(width/2,-height/2,-depth/2);
//上
glNormal3f(0.0,1.0,0.0);
glVertex3f(width/2,height/2,depth/2);
glVertex3f(-width/2,height/2,depth/2);
glVertex3f(-width/2,height/2,-depth/2);
glVertex3f(width/2,height/2,-depth/2);
//下
glNormal3f(0.0,-1.0,0.0);
glVertex3f(width/2,-height/2,depth/2);
glVertex3f(-width/2,-height/2,depth/2);
glVertex3f(-width/2,-height/2,-depth/2);
glVertex3f(width/2,-height/2,-depth/2);
glEnd();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, WIDTH, HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//視野角,アスペクト比(ウィンドウの幅/高さ),描画する範囲(最も近い距離,最も遠い距離)
gluPerspective(30.0, (double)WIDTH / (double)HEIGHT, 1.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//視点の設定
gluLookAt(150.0,100.0,-200.0, //カメラの座標
0.0,0.0,0.0, // 注視点の座標
0.0,1.0,0.0); // 画面の上方向を指すベクトル
//ライトの設定
glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
//マテリアルの設定
glMaterialfv(GL_FRONT, GL_DIFFUSE, Pink);
//回転
glRotatef(angle,1.0f,0.5f,0.0f);//回転
cuboid(50.0,100.0,30.0);
glutSwapBuffers();
}
void idle(void)
{
angle+=0.2f;
Sleep(1);
glutPostRedisplay();
}
void Init(){
glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
int main(int argc, char *argv[])
{
glutInitWindowPosition(100, 100);
glutInitWindowSize(WIDTH, HEIGHT);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutCreateWindow("直方体を描画");
glutDisplayFunc(display);
glutIdleFunc(idle);
Init();
glutMainLoop();
return 0;
}
|