본문 바로가기

프로그래밍/OpenGL

[OpenGL] 오픈지엘 콜벡 함수

이번 시간에는 오픈지엘에서 사용하는 콜벡 함수들을 정리해보려 한다.

콜벡 함수이란 무엇인지 알아보고 대표적인 콜벡 함수 몇 가지를 프로젝트에 사용해보자.

 

 

 

01. 콜벡 함수란?

 

일반 함수와 콜벡 함수

 

콜벡함수는 특정 이벤트나 메세지 발생 시, 그 이벤트를 처리하기 위해 호출하는 함수를 말한다.

 

오픈지엘에서 대표적으로 사용하는 콜벡함수는 다음과 같다.

 

glutDisplayFunc 디스플레이 콜벡 함수
glutReshapeFunc 윈도우 형태 콜벡 함수
glutKeybordFunc 키보드 입력 콜벡 함수
glutKeybordUpFunc 키보드 입력 Up 콜벡 함수
glutSpecialFunc 스페셜 키보드 입력 콜벡 함수
glutMouseFunc 마우스 클릭 콜벡 함수
glutMotionFunc 마우스 드래그 콜벡 함수
glutIdleFunc IDLE 콜벡 함수
glutTimerFunc 타이머 콜벡 함수
glutPostRedisplay 현재 화면 리렌더링

 

이 외에 콜벡 함수 종류는 많으니 궁금하다면 아래를 참고하면 되겠다.

 

https://www.opengl.org/resources/libraries/glut/spec3/node45.html

 

7 Callback Registration

Next: 7.1 glutDisplayFunc Up: GLUT APIversion 3 Previous: 6.9 glutAttachMenuglutDetachMenu GLUT supports a number of callbacks to respond to events. There are three types of callbacks: window, menu, and global. Window callbacks indicate when to redisplay o

www.opengl.org

 

 

 

02. 콜벡 함수 사용

 

대표적인 몇 가지 콜벡 함수 들을 사용한 코드는 다음과 같다.

 

#include <iostream>
#include <gl/glew.h> 
#include <gl/freeglut.h>
#include <gl/freeglut_ext.h>

#pragma comment(lib, "glew32.lib")
#pragma comment(lib, "freeglut.lib")

#define _CRT_SECURE_NO_WARNINGS

#define WIDTH 800
#define HEIGHT 600

using namespace std;

// 콜벡 함수
GLvoid Render(GLvoid);
GLvoid Reshape(int w, int h);
GLvoid KeyBoard(unsigned char key, int x, int y);
GLvoid keyBoardUp(unsigned char key, int x, int y);
GLvoid SpecialKeyBoard(int key, int x, int y);
GLvoid SpecialkeyBoardUp(int key, int x, int y);
GLvoid MouseClick(int button, int state, int x, int y);
GLvoid MouseDrag(int x, int y);

GLvoid TimerFunction(int value);
GLboolean timer = true;

void main(int argc, char** argv)
{
	// 윈도우 생성
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowPosition(0, 0);
	glutInitWindowSize(WIDTH, HEIGHT);
	glutCreateWindow("Make Window");

	glutDisplayFunc(Render);

	// 키보드
	glutKeyboardFunc(KeyBoard);
	glutKeyboardUpFunc(keyBoardUp);
	glutSpecialFunc(SpecialKeyBoard);
	glutSpecialUpFunc(SpecialkeyBoardUp);

	// 마우스
	glutMouseFunc(MouseClick);
	glutMotionFunc(MouseDrag);

	glutMainLoop();
}

GLvoid Render()
{
	glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT);
	glutSwapBuffers();
}

GLvoid Reshape(int w, int h)
{
	// 뷰포트 기본 WIDTH HEIGHT로 설정
	glViewport(0, 0, w, h);
}

GLvoid KeyBoard(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 'A':
	case 'a':
		// A 키 누를 시 TimerFuction 30초 밀리초마다 호출
		glutTimerFunc(30, TimerFunction, timer);
		break;
	case 'B':
	case 'b':
		// B키 누를 시 timer 종료
		timer = false;
	case 'q':
		// q 키 누를 시 프로그램 종료
		exit(EXIT_FAILURE);
		break;
	default:
		break;
	}
	glutPostRedisplay();
}

void keyBoardUp(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 'A':
	case 'a':
		// A 키에서 땔 시 수행
		break;
	default:
		break;
	}

	glutPostRedisplay();
}

void SpecialKeyBoard(int key, int x, int y)
{
	// 스페설 키보드 입력 처리
	switch (key) {
	case GLUT_KEY_LEFT: 
		break;
	case GLUT_KEY_RIGHT: 
		break;
	case GLUT_KEY_UP: 
		break;
	case GLUT_KEY_DOWN:
		break;
	}
	glutPostRedisplay();
}

void SpecialkeyBoardUp(int key, int x, int y)
{
	switch (key) {
	case GLUT_KEY_LEFT:
		break;
	case GLUT_KEY_RIGHT:
		break;
	case GLUT_KEY_UP:
		break;
	case GLUT_KEY_DOWN:
		break;
	}
	glutPostRedisplay();
}

GLvoid MouseClick(int button, int state, int x, int y)
{
	if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
	{
		// 마우스 왼쪽 버튼 누를 때 수행
	}

	if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
	{
		// 마우스 왼쪽 버튼 땔 때 수행
	}

	glutPostRedisplay();
}

GLvoid MouseDrag(int x, int y)
{
	// 마우스 드래그 시 수행
	glutPostRedisplay();
}

GLvoid TimerFunction(int value)
{
	// 30 밀리초 마다 수행
	glutPostRedisplay();
	if (timer) glutTimerFunc(30, TimerFunction, timer);
}