model 은 삼각형 버텍스를 사각형보다 많이 사용 => 곡면 표현에 유리 (오차가 줄음)

일반적으로는 삼각형 버텍스를 사용

 

Mesh 요소

- Vertex

position(x,y,z)

color

vertex normal vector

texture coordinates

 

neighbor vertices

neighbor faces

 

- face

vertices

face normal vector

color

 

neighbor vertices

neighbor faces

 

vertex normal vector 구하기 -> 인접한 삼각형의 normal vector의 모든 normal vector를 더하여 n값(삼각형 개수)로 나누기

단점 : 조건이 까다롭다(조건 : 삼각형 크기와 같은 가중치가 모두 같아야한다.)

-> 가중치를 이용한 평균 계산을 한다(상대적으로 정확해 진다)

 

#include <stdio.h>
#include <stdlib.h>
#include "GL\glut.h"

float _zoom = 30.0f;
float _rot_x = 0.0f;
float _rot_y = 0.01f;
float _trans_x = 0.0f;
float _trans_y = 0.0f;
int _last_x = 0;
int _last_y = 0;
unsigned char _buttons[3] = { 0 }; //왼쪽, 오른쪽, 휠

void Init(void)
{
	glEnable(GL_DEPTH_TEST);
}

void Motion(int x, int y)
{
	int diff_x = x - _last_x;
	int diff_y = y - _last_y;
	_last_x = x;
	_last_y = y;

	if (_buttons[2])
	{
		_zoom -= (float)0.03f * diff_x;
	}
	else if (_buttons[1])
	{
		_trans_x += (float)0.02f * diff_x;
		_trans_y -= (float)0.02f * diff_y;
	}
	else if (_buttons[0])
	{
		_rot_x += (float)0.2f * diff_y;
		_rot_y += (float)0.2f * diff_x;
	}

	glutPostRedisplay();
}

/**
* button은 GLUT_LEFT_BUTTON과 같이 어떤 버튼이 눌려는지를 판단.
* state는 GLUT_DOWN 같이 눌렸는지를 판단.
*/
void Mouse(int button, int state, int x, int y)
{
	_last_x = x;
	_last_y = y;

	switch (button)
	{
	case GLUT_LEFT_BUTTON:
		_buttons[0] = state == GLUT_DOWN ? 1 : 0; // 삼항 연산자 state가 맞다면 1 틀리면 0을 _button 배열에 전달
		break;
	case GLUT_MIDDLE_BUTTON:
		_buttons[1] = state == GLUT_DOWN ? 1 : 0;
		break;
	case GLUT_RIGHT_BUTTON:
		_buttons[2] = state == GLUT_DOWN ? 1 : 0;
		break;
	default:
		break;
	}
	glutPostRedisplay();
}

/**
* key : ASCII
*/
void Keyboard(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 'Q':
	case 'q':
		exit(0);
	default:
		break;
	}
	glutPostRedisplay();
}

void Reshape(int w, int h)
{
	if (w == 0)	h = 1;
	
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0, (float)w / h, 0.1, 100);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void Draw(void)
{
	glEnable(GL_LIGHTING); //조명 활성화
	glEnable(GL_LIGHT0); // 첫번째 (0번) 조명 활성화
	glutSolidTeapot(3.0f);
	glDisable(GL_LIGHTING);
}

void Display(void)
{
	glClearColor(0.6f, 0.6f, 0.6f, 1.0f); // 지울 때 기본 색상
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //지울때 마다 어떤 것을 지울지 
	glLoadIdentity(); //행렬 초기화(단위 행렬)

	glTranslatef(0, 0, -_zoom);
	glTranslatef(_trans_x, _trans_y,0);
	glRotatef(_rot_x, 1, 0, 0); // degree, x, y, z
	glRotatef(_rot_y, 0, 1, 0);

	Draw();
	glutSwapBuffers(); //double buffer
}

void main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
	glutInitWindowSize(800, 800);
	glutInitWindowPosition(100, 100);
	glutCreateWindow("Fighting!");
	glutDisplayFunc(Display);
	glutReshapeFunc(Reshape);
	glutKeyboardFunc(Keyboard);
	glutMotionFunc(Motion); //드래그
	glutMouseFunc(Mouse);
	Init(); // 각종 클래스, 변수 초기화
	glutMainLoop();
}

 

'학교수업' 카테고리의 다른 글

데이터베이스 3주차  (0) 2022.09.20
데이터 베이스 2주차  (0) 2022.09.15
컴퓨터 그래픽스 응용 1주차  (0) 2022.09.07
인공지능 13주차  (0) 2022.06.06
인공지능 12주차  (0) 2022.06.06

+ Recent posts