1~3번 호출 경우는 reshape 이벤트와 동일: reshape 콜백 함수가 실행되면 새로 조정된 뷰포트 및 투상범위를 기준으로
자동으로 디스플레이 콜백함수가 실행
void glutDisplayFunc(void (*func)(int width, int height) );
void glutReshapeFunc(void (*func)(int width, int height) );
//visual stdio 기준
//#include <glut.h>
//#include <stdlib.h>
//#include <GL/glut.h>
#define GL_SILENCE_DEPRECATION //버전 오류 해결
#include <iostream>
#include <OpenGL/gl.h>
#include <OpenGl/glu.h>
#include <GLUT/glut.h>
void init(void) {
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH); //flat 보다 시간이 많이 걸림.
}
void triangle(void) {
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(-0.5, -0.5);
glColor3f(0.0, 1.0, 0.0);
glVertex2f(0.5, -0.5);
glColor3f(0.0, 0.0, 1.0);
glVertex2f(-0.5, 0.5);
glEnd();
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
triangle();
glFlush();
}
void reshape(int new_w, int new_h) {
glViewport(0, 0, new_w, new_h);
float WidthFactor = (float) new_w / 300.0;
float HeightFactor = (float)new_h / 300.0;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0 * WidthFactor, 1.0 * WidthFactor, -1.0 * HeightFactor, 1.0 * HeightFactor);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(300, 300); //초기 창의 크기를 결정한다.
glutInitWindowPosition(100, 100); //화면 기준 창이 나타나는 공간을 설정한다. 좌상단 원점 기준
glutCreateWindow(argv[0]); //glutMainLoop()가 호출되면 보여줌.
init();
glutDisplayFunc(display); //콜백함수
glutReshapeFunc(reshape); //콜백함수
glutMainLoop();
return 0;
}
정점 기준
지정된 하나의 색
으로 렌더링void glutKeyboardFunc(void(*func)(unsigned char key, int x, int y));
특수키 입력
void glutSpecialFunc(void(*func)(int key, int x, int y));
glutMouseFunc
은 윈도우 안에서 마우스 버튼을 누르거나 놓을 때 사용
void glutMouseFunc(void(*func)(int button, int state, int x, int y));
x,y 는 마우스의 현재 위치
: 윈도우 안에서 하나 또는 그 이상의 마우스 버튼이 눌린 상태로 움직일 때 호출
void glutPassiveMotionFunc(void (*func)(int x, int y));
: 아무런 마우스 버튼이 눌리지 않은 상태로 움직일 때 호출 됨
void glutPassiveMotionFunc(void (*func)(int x, int y));
#define GL_SILENCE_DEPRECATION //버전 오류 해결
#include <iostream>
#include <OpenGL/gl.h>
#include <OpenGl/glu.h>
#include <GLUT/glut.h>
void init(void) {
glClearColor(0.0, 0.0, 0.0, 0.0);
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 0.0);
glFlush();
}
void mouseProcess(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
printf("Left mouse button is pressed.. \n\n");
}
else if (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) {
printf("Middle mouse button is pressed..\n\n");
}
else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
printf("Right mouse button is pressed..\n\n");
}
else if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
printf("Left mouse button is released..\n\n");
}
else if (button == GLUT_MIDDLE_BUTTON && state == GLUT_UP) {
printf("Middle mouse button is released..\n\n");
}
else if (button == GLUT_RIGHT_BUTTON && state == GLUT_UP) {
printf("Right mouse button is released..\n\n");
}
}
void mousePassiveMotion(int x, int y){
printf("Current mouse position: %d, %d\n", x, y);
}
void mouseActiveMotion(int x, int y) {
printf("Pressed mouse position: %d, %d\n", x, y);
}
void mouseEntry(int state) {
if (state == GLUT_LEFT) {
printf("Mouse is outside of window..\n\n");
}
else if (state == GLUT_ENTERED) {
printf("Mouse is inside of window..\n\n");
}
}
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 'a':
printf("a is pressed .. %d, %d\a\n", x, y);
break;
case 'b':
printf("b is pressed .. %d, %d\a\n", x, y);
break;
case 27:
exit(0);
break;
}
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Mouse and keyboard");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouseProcess);
glutPassiveMotionFunc(mousePassiveMotion);
glutMotionFunc(mouseActiveMotion);
glutEntryFunc(mouseEntry);
glutMainLoop();
return 0;
}
마우스와 키보드의 입력에 따라 callback 함수가 호출되어 값 return