Last active
April 30, 2017 00:45
-
-
Save fushime2/efe9b440067653af42e3fac51f78ed9b to your computer and use it in GitHub Desktop.
Color House
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <cstdlib> | |
#include "GL/glut.h" | |
using namespace std; | |
// 直方体の表面 | |
int face[][4] = { | |
{0, 3, 2, 1}, | |
{1, 2, 6, 5}, | |
{5, 6, 7, 4}, | |
{4, 7, 3, 0}, | |
{4, 0, 1, 5}, | |
{3, 7, 6, 2} | |
}; | |
// 三角形の表面 | |
int face1[][4] = { | |
{1, 4, 0, 0}, | |
{0, 4, 3, 0}, | |
{3, 4, 2, 0}, | |
{2, 4, 1, 0}, | |
{0, 3, 2, 1} | |
}; | |
GLdouble color[][3] = { | |
{1.0, 0.0, 0.0}, // red | |
{0.0, 1.0, 0.0}, // green | |
{0.0, 0.0, 1.0}, // blue | |
{1.0, 1.0, 0.0}, // yellow | |
{1.0, 0.0, 1.0}, // magenta | |
{0.0, 1.0, 1.0} // cyan | |
}; | |
// 直方体の頂点 | |
GLdouble vertex[][3] = { | |
{0.0, 0.0, 0.0}, | |
{1.0, 0.0, 0.0}, | |
{1.0, 1.0, 0.0}, | |
{0.0, 1.0, 0.0}, | |
{0.0, 0.0, 1.0}, | |
{1.0, 0.0, 1.0}, | |
{1.0, 1.0, 1.0}, | |
{0.0, 1.0, 1.0} | |
}; | |
// 三角形の頂点 | |
GLdouble vertex1[][3] = { | |
{1.3, 1.0, -0.3}, // A | |
{-0.3, 1.0, -0.3}, // B | |
{-0.3, 1.0, 1.3}, // C | |
{1.3, 1.0, 1.3}, // D | |
{0.5, 1.4, 0.5} // E | |
}; | |
// 四角形を表示する | |
void disp_square(){ | |
for(int i=0; i<6; ++i){ | |
glColor3dv(color[i]); | |
for(int j=0; j<4; ++j){ | |
glVertex3dv(vertex[face[i][j]]); | |
} | |
} | |
// 三角の底面の四角形 | |
glColor3dv(color[0]); | |
for(int j=0; j<4; ++j) | |
glVertex3dv(vertex1[face1[4][j]]); | |
} | |
// 三角形を表示する | |
void disp_triangle() { | |
for(int i=0; i<4; ++i){ | |
glColor3dv(color[3-i]); | |
for(int j=0; j<3; ++j){ | |
glVertex3dv(vertex1[face1[i][j]]); | |
} | |
} | |
} | |
void display(){ | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glColor3d(1.0, 1.0, 1.0); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
// 視点位置、注視点、上 | |
gluLookAt(1.5, 2.5, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); | |
//gluLookAt(1.5, -2.5, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); | |
glRotated(45, 0.0, 1.0, 0.0); | |
glTranslated(-0.5, -1.0, -0.3); | |
glRotated(30.0, -1.0, 1.0, 1.0); | |
// 四角の表示 | |
glBegin(GL_QUADS); | |
disp_square(); | |
glEnd(); | |
// 三角の表示 | |
glBegin(GL_TRIANGLES); | |
disp_triangle(); | |
glEnd(); | |
glFlush(); | |
} | |
void resize(int w, int h){ | |
glViewport(0, 0, w, h); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
gluPerspective(45.0, 1.0 * w / h, 1.0, 100.0); | |
} | |
void init(){ | |
glClearColor(0.7, 0.7, 0.7, 1.0); | |
glEnable(GL_DEPTH_TEST); | |
// 片面表示 | |
glEnable(GL_CULL_FACE); | |
// 裏面消去 | |
glCullFace(GL_BACK); | |
} | |
int main(int argc, char* argv[]){ | |
glutInit(&argc, argv); | |
glutInitWindowPosition(100,100); | |
glutInitWindowSize(320, 320); | |
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH); | |
glutCreateWindow("Test"); | |
glutDisplayFunc(display); | |
glutReshapeFunc(resize); | |
init(); | |
glutMainLoop(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment