๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Graphic

OpenGL๋กœ ๋ฐฐ์šฐ๋Š” 3์ฐจ์› ์ปดํ“จํ„ฐ ๊ทธ๋ž˜ํ”ฝ์Šค::Project_1/4

by ๋„์บ๋ฆฌ๐Ÿฑ 2020. 7. 21.
๋ฐ˜์‘ํ˜•

โ— ํ”„๋กœ์ ํŠธ ๋ชฉํ‘œ

 

1) S ํด๋ฆญ์‹œ์— Solid ๋ Œ๋”๋ง

2) W ํด๋ฆญ์‹œ์— Wire ๋ Œ๋”๋ง

3) ๋งˆ์šฐ์Šค ์›€์ง์ž„์œผ๋กœ ์‹œ์  ๋ฐ”๊พธ๊ธฐ

4) ํƒ์ž๋ฅผ ๊ทธ๋ฆฐ ํ›„ ๊ทธ ์œ„์— ๋ฌผ์ฒด๋ฅผ ์˜ฌ๋ ค ๋†“๋Š”๋‹ค.

 

โ— ํ”„๋กœ์ ํŠธ ๋™์˜์ƒ ๊ฒฐ๊ณผ

 

 

โ— ํ”„๋กœ์ ํŠธ ์ฝ”๋“œ

#include <GL/glut.h>
#include <GL/GLU.h>
#include <GL/GL.h>
#define WIDTH 400
#define HEIGHT 400

GLboolean IsWire = true;
GLboolean IsSolid = true;
int ViewX = 0, ViewY = 0;				// ๋งˆ์šฐ์Šค ์›€์ง์ž„์— ๋”ฐ๋ผ ์‹œ์ ์„ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜


void InitLight()
{
	GLfloat mat_diffuse[] = { 0.5,0.4,0.3,1.0 };
	GLfloat mat_specular[] = { 1.0,1.0,1.0,1.0 };
	GLfloat mat_ambient[] = { 0.5,0.4,0.3,1.0 };
	GLfloat mat_shininess[] = { 15.0 };

	GLfloat light_specular[] = { 1.0,1.0,1.0,1.0 };
	GLfloat light_diffuse[] = { 0.8,0.8,0.8,1.0 };
	GLfloat light_ambient[] = { 0.3,0.3,0.3,1.0 };
	GLfloat light_position[] = { -3,6,3.0,0.0 };

	glShadeModel(GL_SMOOTH);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);

	glLightfv(GL_LIGHT0, GL_POSITION, light_position);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
	glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
	glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);

	glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
	glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
	glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
	glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
}

void MyMouseMove(GLint X, GLint Y)
{
	ViewX = X;
	ViewY = Y;
	glutPostRedisplay();
}

void MyDisplay()
{
	int X = ((ViewX) - (WIDTH / 2));
	int Y = -((ViewY) - (HEIGHT / 2));
	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	gluLookAt(0.0, 0.0, 0.0, X, Y, -1.0, 0.0, 1.0, 0.0);


	if (IsWire)
	{
		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(0.0, 0.57, 0.0);
		glutWireTeapot(0.15);
		glPopMatrix();

		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(0.3, 0.2, 0.175);
		glScalef(1.0, 8.0, 1.0);
		glutWireCube(0.05);
		glPopMatrix();

		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(-0.3, 0.2, 0.175);
		glScalef(1.0, 8.0, 1.0);
		glutWireCube(0.05);
		glPopMatrix();

		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(-0.3, 0.2, -0.175);
		glScalef(1.0, 8.0, 1.0);
		glutWireCube(0.05);
		glPopMatrix();

		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(-0.3, 0.2, -0.175);
		glScalef(1.0, 8.0, 1.0);
		glutWireCube(0.05);
		glPopMatrix();

		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(0.3, 0.2, -0.175);
		glScalef(1.0, 8.0, 1.0);
		glutWireCube(0.05);
		glPopMatrix();

		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(0.0, 0.37, 0.0);
		glScalef(13.0, 2.0, 8.0);
		glutWireCube(0.05);
		glPopMatrix();

	}
	else
	{
		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(0.0, 0.57, 0.0);
		glutSolidTeapot(0.15);
		glPopMatrix();


		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(0.3, 0.2, 0.175);
		glScalef(1.0, 8.0, 1.0);
		glutSolidCube(0.05);
		glPopMatrix();

		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(-0.3, 0.2, 0.175);
		glScalef(1.0, 8.0, 1.0);
		glutSolidCube(0.05);
		glPopMatrix();

		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(-0.3, 0.2, -0.175);
		glScalef(1.0, 8.0, 1.0);
		glutSolidCube(0.05);
		glPopMatrix();

		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(-0.3, 0.2, -0.175);
		glScalef(1.0, 8.0, 1.0);
		glutSolidCube(0.05);
		glPopMatrix();

		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(0.3, 0.2, -0.175);
		glScalef(1.0, 8.0, 1.0);
		glutSolidCube(0.05);
		glPopMatrix();

		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(0.0, 0.37, 0.0);
		glScalef(13.0, 2.0, 8.0);
		glutSolidCube(0.05);
		glPopMatrix();
	}
	if (IsSolid)
	{
		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(0.0, 0.57, 0.0);
		glutSolidTeapot(0.15);
		glPopMatrix();

		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(0.3, 0.2, 0.175);
		glScalef(1.0, 8.0, 1.0);
		glutSolidCube(0.05);
		glPopMatrix();

		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(-0.3, 0.2, 0.175);
		glScalef(1.0, 8.0, 1.0);
		glutSolidCube(0.05);
		glPopMatrix();

		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(-0.3, 0.2, -0.175);
		glScalef(1.0, 8.0, 1.0);
		glutSolidCube(0.05);
		glPopMatrix();

		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(-0.3, 0.2, -0.175);
		glScalef(1.0, 8.0, 1.0);
		glutSolidCube(0.05);
		glPopMatrix();

		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(0.3, 0.2, -0.175);
		glScalef(1.0, 8.0, 1.0);
		glutSolidCube(0.05);
		glPopMatrix();

		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(0.0, 0.37, 0.0);
		glScalef(13.0, 2.0, 8.0);
		glutSolidCube(0.05);
		glPopMatrix();

	}
	else
	{
		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(0.0, 0.57, 0.0);
		glutWireTeapot(0.15);
		glPopMatrix();

		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(0.3, 0.2, 0.175);
		glScalef(1.0, 8.0, 1.0);
		glutWireCube(0.05);
		glPopMatrix();

		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(-0.3, 0.2, 0.175);
		glScalef(1.0, 8.0, 1.0);
		glutWireCube(0.05);
		glPopMatrix();

		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(-0.3, 0.2, -0.175);
		glScalef(1.0, 8.0, 1.0);
		glutWireCube(0.05);
		glPopMatrix();

		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(-0.3, 0.2, -0.175);
		glScalef(1.0, 8.0, 1.0);
		glutWireCube(0.05);
		glPopMatrix();

		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(0.3, 0.2, -0.175);
		glScalef(1.0, 8.0, 1.0);
		glutWireCube(0.05);
		glPopMatrix();

		glPushMatrix();
		glTranslatef(-0.02, 0.0, -0.38);
		glTranslatef(0.0, 0.37, 0.0);
		glScalef(13.0, 2.0, 8.0);
		glutWireCube(0.05);
		glPopMatrix();
	}

	glFlush();
}

void MyKeyboard(unsigned char key, int x, int y)
{
	switch (key)
	{
	case'q':
	case 'Q':
		exit(0);
		break;
	case 's':
	case 'S':
		IsSolid = true;
		IsWire = false;
		glutPostRedisplay();
		break;
	case 'w':
	case'W':
		IsSolid = false;
		IsWire = true;
		glutPostRedisplay();
		break;
	}
}

int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH);
	glutInitWindowSize(400, 400);
	glutInitWindowPosition(0, 0);
	glutCreateWindow("PROJCET 1/4");
	glClearColor(0.4, 0.4, 0.4, 0.0);
	InitLight();

	glutDisplayFunc(MyDisplay);
	glutKeyboardFunc(MyKeyboard);		//??
	glutPassiveMotionFunc(MyMouseMove);

	glutMainLoop();
}
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€