////////////////draw XYZ
void DrawXYZ()
{
glDisable(GL_LIGHTING);
//x축 그리기 (빨간색)
glColor3f(1.f, 0.f, 0.f);
glBegin(GL_LINE_LOOP);
glVertex3f(10.0, 0.0, 0.0);
glVertex3f(-10.0, 0.0, 0.0);
glEnd();
//y축 그리기 (초록색)
glColor3f(0.f, 1.f, 0.f);
glBegin(GL_LINE_LOOP);
glVertex3f(0.0, 10.0, 0.0);
glVertex3f(0.0, -10.0, 0.0);
glEnd();
//z축 그리기 (파란색)
glColor3f(0.f, 0.f, 1.f);
glBegin(GL_LINE_LOOP);
glVertex3f(0.0, 0.0, 10.0);
glVertex3f(0.0, 0.0, -10.0);
glEnd();
}
삼각형을 그리는 함수를 만든 뒤 그 함수를 이용하서 사각형을 만드는 함수를 만든다. 사각형을 만드는 함수를 이용하여 정육면체를 그리면 된다.
void DrawTriangle(GLfloat I)
{
glColor3f(0.2f, 0.2f, 0.6f);
glBegin(GL_TRIANGLES);
glNormal3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glNormal3f(0.0, 0.0, 1.0);
glVertex3f(I, 0.0, 0.0);
glNormal3f(0.0, 0.0, 1.0);
glVertex3f(0.0, I, 0.0);
glEnd();
}
void DrawSquare(GLfloat I)
{
glPushMatrix();
glTranslatef(-0.5 * I, -0.5 * I, 0.5 * I);
DrawTriangle(I);
glPopMatrix();
glPushMatrix();
glRotatef(180.0, 0.0, 0.0, 1.0);
glTranslatef(-0.5 * I, -0.5 * I, 0.5 * I);
DrawTriangle(I);
glPopMatrix();
}
void DrawCube(GLfloat I)
{
glPushMatrix();
DrawSquare(I);
glRotatef(90.0, 0.0, 1.0, 0.0);
DrawSquare(I);
glRotatef(90.0, 0.0, 1.0, 0.0);
DrawSquare(I);
glRotatef(90.0, 0.0, 1.0, 0.0);
DrawSquare(I);
glPopMatrix();
glPushMatrix();
glRotatef(90.0, 1.0, 0.0, 0.0);
DrawSquare(I);
glRotatef(180.0, 1.0, 0.0, 0.0);
DrawSquare(I);
glPopMatrix();
}
DrawScene 함수 안에서 구현. 숫자 3을 누르면 실행되게 했다.
if (b_number_3)
{
glEnable(GL_LIGHTING);
glDisable(GL_LIGHT0);
glDisable(GL_LIGHT3);
glDisable(GL_LIGHT4);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
GLfloat light12_ambient[] = { 0.5f, 0.5f, 0.9f, 1.0f };
GLfloat light12_diffuse[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat light12_position[] = { 3.0f, 3.0f, 6.0f, 1.0f };
GLfloat mat0_noemission[] = { 0.0f, 0.0f, 0.0f, 1.0f };
GLfloat mat0_shininess[] = { 50.0 };
GLfloat mat0_properties[2][4] =
{
{0.3f, 0.3f, 0.3f, 1.0f}, //ambient
{0.2f, 0.2f, 0.2f, 1.0f}, //diffuse
};
glLightfv(GL_LIGHT1, GL_AMBIENT, light12_ambient);
glLightfv(GL_LIGHT2, GL_DIFFUSE, light12_diffuse);
glLightfv(GL_LIGHT2, GL_POSITION, light12_position);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat0_shininess);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat0_properties[mat_diffuse]);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat0_properties[mat_ambient]);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat0_noemission);
}
숫자 4를 누르면 실행되게 했다.
if (b_number_4)
{
glEnable(GL_LIGHTING);
glDisable(GL_LIGHT1);
glDisable(GL_LIGHT2);
glEnable(GL_LIGHT0);
GLfloat spotlight_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
GLfloat spotlight_diffuse[] = { 0.2f, 0.2f, 0.7f, 1.0f };
GLfloat spotlight_specular[] = { 0.3f, 0.3f, 0.3f, 1.0f };
GLfloat mat0_no[] = { 0.3f, 0.3f, 0.3f, 1.0f };
GLfloat spotlight_direction[] = { 0.0f, 0.0f, -1.0f, 1.0f };
GLfloat mat0_noemission[] = { 0.0f, 0.0f, 0.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_SPECULAR, spotlight_specular);
glLightfv(GL_LIGHT0, GL_DIFFUSE, spotlight_diffuse);
glLightfv(GL_LIGHT0, GL_AMBIENT, spotlight_ambient);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 80.0f);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 80.0f);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spotlight_direction);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat0_no);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat0_no);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat0_noemission);
spot_angle += 0.1f;
glPushMatrix();
glTranslatef(0.0, 0.0, 0.0);
glRotatef(spot_angle, 0, 0, 1);
GLfloat lightpos[4] = { 1.0f, 1.0f, 10.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
glPopMatrix();
glDisable(GL_LIGHT3);
glDisable(GL_LIGHT4);
}
숫자 5를 누르면 실행되게 함.
if (b_number_5)
{
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT3);
glEnable(GL_LIGHT4);
glDisable(GL_LIGHT0);
glDisable(GL_LIGHT1);
glDisable(GL_LIGHT2);
////light 3,4 설정
GLfloat light_emission[] = { 0.5f, 0.6f, 0.2f, 1.0f }; //emission
GLfloat light34_ambient[] = { 0.1f, 0.1f, 0.1f, 1.f };
GLfloat light34_diffuse[] = { 0.3f, 0.3f, 0.3f, 1.f };
GLfloat mat0_shininess[] = { 50.0 };
GLfloat mat0_no[] = { 0.3f, 0.3f, 0.3f, 1.0f };
glLightfv(GL_LIGHT3, GL_DIFFUSE, light34_ambient);
glLightfv(GL_LIGHT4, GL_AMBIENT, light34_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, light_emission);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat0_shininess);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat0_no);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat0_no);
}
void DrawGLScene() {
gluLookAt(0.f, 30.f, m_cameraDist, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f);
if (b_mouseMove) {
glRotatef(angleVelocity * (m_currentPoint.x - m_anchorPoint.x), 0.0, 1.0, 0.0);
glRotatef(angleVelocity * (m_currentPoint.y - m_anchorPoint.y), 1.0, 0.0, 0.0);
}
BuildCheckerImage();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, 3,
checkerImageWidth, checkerImageHeight, 0, GL_RGB,
GL_UNSIGNED_BYTE, &checkerImage[0][0][0]);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
//up
glEnable(GL_TEXTURE_2D);
CString fname("image\\up.bmp");
BindTextureFile((LPCSTR)fname, 0);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 1.0); glVertex3f(-500.0, 500.0, 500.0);
glTexCoord2f(0.0, 0.0); glVertex3f(-500.0, 500.0, -500.0);
glTexCoord2f(1.0, 0.0); glVertex3f(500.0, 500.0, -500.0);
glTexCoord2f(1.0, 1.0); glVertex3f(500.0, 500.0, 500.0);
glEnd();
glDisable(GL_TEXTURE_2D);
//down
glEnable(GL_TEXTURE_2D);
CString fname1("image\\down.bmp");
BindTextureFile((LPCSTR)fname1, 0);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 1.0); glVertex3f(-500.0, -500.0, 500.0);
glTexCoord2f(0.0, 0.0); glVertex3f(-500.0, -500.0, -500.0);
glTexCoord2f(1.0, 0.0); glVertex3f(500.0, -500.0, -500.0);
glTexCoord2f(1.0, 1.0); glVertex3f(500.0, -500.0, 500.0);
glEnd();
glDisable(GL_TEXTURE_2D);
glEnable(GL_TEXTURE_2D);
CString fname2("image\\left.bmp");
BindTextureFile((LPCSTR)fname2, 0);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 1.0); glVertex3f(-500.0, 500.0, 500.0);
glTexCoord2f(0.0, 0.0); glVertex3f(-500.0, 500.0, -500.0);
glTexCoord2f(1.0, 0.0); glVertex3f(-500.0, -500.0, -500.0);
glTexCoord2f(1.0, 1.0); glVertex3f(-500.0, -500.0, 500.0);
glEnd();
glDisable(GL_TEXTURE_2D);
glEnable(GL_TEXTURE_2D);
CString fname3("image\\right.bmp");
BindTextureFile((LPCSTR)fname3, 0);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 1.0); glVertex3f(500.0, 500.0, 500.0);
glTexCoord2f(0.0, 0.0); glVertex3f(500.0, 500.0, -500.0);
glTexCoord2f(1.0, 0.0); glVertex3f(500.0, -500.0, -500.0);
glTexCoord2f(1.0, 1.0); glVertex3f(500.0, -500.0, 500.0);
glEnd();
glDisable(GL_TEXTURE_2D);
glEnable(GL_TEXTURE_2D);
CString fname4("image\\front.bmp");
BindTextureFile((LPCSTR)fname4, 0);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 1.0); glVertex3f(-500.0, -500.0, 500.0);
glTexCoord2f(0.0, 0.0); glVertex3f(-500.0, 500.0, 500.0);
glTexCoord2f(1.0, 0.0); glVertex3f(500.0, 500.0, 500.0);
glTexCoord2f(1.0, 1.0); glVertex3f(500.0, -500.0, 500.0);
glEnd();
glDisable(GL_TEXTURE_2D);
glEnable(GL_TEXTURE_2D);
CString fname5("image\\back.bmp");
BindTextureFile((LPCSTR)fname5, 0);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 1.0); glVertex3f(-500.0, -500.0, -500.0);
glTexCoord2f(0.0, 0.0); glVertex3f(-500.0, 500.0, -500.0);
glTexCoord2f(1.0, 0.0); glVertex3f(500.0, 500.0, -500.0);
glTexCoord2f(1.0, 1.0); glVertex3f(500.0, -500.0, -500.0);
glEnd();
glDisable(GL_TEXTURE_2D);
//draw Box1
glEnable(GL_TEXTURE_2D);
CString fname7("image\\crate.bmp");
BindTextureFile((LPCSTR)fname7, 0);
glBegin(GL_QUADS);
//front
glTexCoord2f(0.0, 1.0); glVertex3f(0.0, 0.0, 10.0);
glTexCoord2f(0.0, 0.0); glVertex3f(0.0, 10.0, 10.0);
glTexCoord2f(1.0, 0.0); glVertex3f(10.0, 10.0, 10.0);
glTexCoord2f(1.0, 1.0); glVertex3f(10.0, 0.0, 10.0);
glEnd();
//back
glBegin(GL_QUADS);
glTexCoord2f(0.0, 1.0); glVertex3f(0.0, 10.0, 0.0);
glTexCoord2f(0.0, 0.0); glVertex3f(10.0, 10.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(10.0, 0.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 0.0, 0.0);
glEnd();
//up
glBegin(GL_QUADS);
glTexCoord2f(0.0, 1.0); glVertex3f(0.0, 10.0, 10.0);
glTexCoord2f(0.0, 0.0); glVertex3f(0.0, 10.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(10.0, 10.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(10.0, 10.0, 10.0);
glEnd();
//down
glBegin(GL_QUADS);
glTexCoord2f(0.0, 1.0); glVertex3f(0.0, 0.0, 0.0);
glTexCoord2f(0.0, 0.0); glVertex3f(10.0, 0.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(10.0, 0.0, 10.0);
glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 0.0, 10.0);
glEnd();
//left -> 1번씩 당긴거
glBegin(GL_QUADS);
glTexCoord2f(0.0, 1.0); glVertex3f(0.0, 0.0, 0.0);
glTexCoord2f(0.0, 0.0); glVertex3f(0.0, 10.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(0.0, 10.0, 10.0);
glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 0.0, 10.0);
glEnd();
//right
glBegin(GL_QUADS);
glTexCoord2f(0.0, 1.0); glVertex3f(10.0, 10.0, 0.0);
glTexCoord2f(0.0, 0.0); glVertex3f(10.0, 0.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(10.0, 0.0, 10.0);
glTexCoord2f(1.0, 1.0); glVertex3f(10.0, 10.0, 10.0);
glEnd();
glDisable(GL_TEXTURE_2D);
//draw Box1 finish
//draw Box2
glEnable(GL_TEXTURE_2D);
CString fname8("image\\crate.bmp");
BindTextureFile((LPCSTR)fname8, 0);
//down
glBegin(GL_QUADS);
glTexCoord2f(0.0, 1.0); glVertex3f(5.0, 10.0, 0.0);
glTexCoord2f(0.0, 0.0); glVertex3f(15.0, 10.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(15.0, 10.0, 10.0);
glTexCoord2f(1.0, 1.0); glVertex3f(5.0, 10.0, 10.0);
glEnd();
//up
glBegin(GL_QUADS);
glTexCoord2f(0.0, 1.0); glVertex3f(5, 20.0, 10.0);
glTexCoord2f(0.0, 0.0); glVertex3f(5, 20.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(15, 20.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(15, 20.0, 10.0);
glEnd();
//front
glBegin(GL_QUADS);
glTexCoord2f(0.0, 1.0); glVertex3f(5, 10.0, 10.0);
glTexCoord2f(0.0, 0.0); glVertex3f(5, 20.0, 10.0);
glTexCoord2f(1.0, 0.0); glVertex3f(15, 20.0, 10.0);
glTexCoord2f(1.0, 1.0); glVertex3f(15, 10.0, 10.0);
glEnd();
//back
glBegin(GL_QUADS);
glTexCoord2f(0.0, 1.0); glVertex3f(5.0, 20.0, 0.0);
glTexCoord2f(0.0, 0.0); glVertex3f(15.0, 20.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(15.0, 10.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(5.0, 10.0, 0.0);
glEnd();
//left
glBegin(GL_QUADS);
glTexCoord2f(0.0, 1.0); glVertex3f(5, 10.0, 0.0);
glTexCoord2f(0.0, 0.0); glVertex3f(5, 20.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(5, 20.0, 10.0);
glTexCoord2f(1.0, 1.0); glVertex3f(5, 10.0, 10.0);
glEnd();
//right
glBegin(GL_QUADS);
glTexCoord2f(0.0, 1.0); glVertex3f(15, 20.0, 0.0);
glTexCoord2f(0.0, 0.0); glVertex3f(15, 20.0, 10.0);
glTexCoord2f(1.0, 0.0); glVertex3f(15, 10.0, 10.0);
glTexCoord2f(1.0, 1.0); glVertex3f(15, 10.0, 0.0);
glEnd();
glDisable(GL_TEXTURE_2D);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
//draw grass
glEnable(GL_TEXTURE_2D);
CString fname9("image\\grass.bmp");
BindTextureFile((LPCSTR)fname9, 0);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 80.0); glVertex3f(-50.0, 0.0, -50.0);
glTexCoord2f(0.0, 0.0); glVertex3f(-50.0, 0.0, 50.0);
glTexCoord2f(80.0, 0.0); glVertex3f(50.0, 0.0, 50.0);
glTexCoord2f(80.0, 80.0); glVertex3f(50.0, 0.0, -50.0);
glEnd();
glDisable(GL_TEXTURE_2D);
}