OPENGL_KeyFunc

이승한·2024년 2월 7일
0

OpenGL

목록 보기
1/1

OPENGL _ PR

키보드 Func을 콜백 받아서 윈도우창의 색상을 변경하는 프로그램코드

  • glfw,glew32,glad.c 를 정상적으로 설치

기능 : ESC 버튼 누르면 창이 종료
opengl은 0.0f~1.0f까지의 색상을 가지는데
Q : 0.01F 만큼 Red 값 증가
W : 0.01F 만큼 Green 값 증가
E : 0.01F 만큼 Blue 값 증가
A : 0.01F 만큼 Red 값 감소
S : 0.01F 만큼 Green 값 감소
D : 0.01F 만큼 Blue 값 감소

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glew32.lib")
#pragma comment(lib, "glfw3.lib")
#pragma warning(disable: 4711 4710 4100)
#include <stdio.h>
#include <string.h> // for strrchr()

const unsigned int WIN_W = 300; // window size in pixels, (Width, Height)
const unsigned int WIN_H = 300;
const unsigned int WIN_X = 100; // window position in pixels, (X, Y) 
const unsigned int WIN_Y = 100;

GLfloat clr[4] = { 0.933F,0.779F,0.898F,1.0F };

void refreshFunc(GLFWwindow* window) {
	// refresh
	glClear(GL_COLOR_BUFFER_BIT);
	glFinish();
	// GLFW action
	glfwSwapBuffers(window);
}

void keyFunc(GLFWwindow* window, int key, int scancode, int action, int mods) {
	switch (key) {
	case GLFW_KEY_ESCAPE:
		if (action == GLFW_PRESS) {
			glfwSetWindowShouldClose(window, GL_TRUE);
		}
		break;
	case 'Q':clr[0] += 0.01F; if (clr[0] > 1.0F) clr[0] = 1.0F; break;
	case 'W':clr[1] += 0.01F; if (clr[1] > 1.0F) clr[1] = 1.0F; break;
	case 'E':clr[2] += 0.01F; if (clr[2] > 1.0F) clr[2] = 1.0F; break;
	case 'A':clr[0] -= 0.01F; if (clr[0] < 0.0F) clr[0] = 0.0F; break;
	case 'S':clr[1] -= 0.01F; if (clr[1] < 0.0F) clr[1] = 0.0F; break;
	case 'D':clr[2] -= 0.01F; if (clr[2] < 0.0F) clr[2] = 0.0F; break;
	}
	glClearColor(clr[0], clr[1], clr[2], clr[3]);
}

int main(int argc, char* argv[]) {
	// get your program name
#if defined(_WIN32) || defined(_WIN64)
	char* win_name = (strrchr(argv[0], '\\') == NULL) ? argv[0] : (strrchr(argv[0], '\\') + 1);
#else // Unix, Linux, MacOS
	char* win_name = (strrchr(argv[0], '/') == NULL) ? argv[0] : (strrchr(argv[0], '/') + 1);
#endif
	// start GLFW & GLEW
	glfwInit();
	GLFWwindow* window = glfwCreateWindow(WIN_W, WIN_H, win_name, NULL, NULL);
	glfwSetWindowPos(window, WIN_X, WIN_Y);
	glfwMakeContextCurrent(window);
	glewInit();
	// prepare
	glfwSetWindowRefreshCallback(window, refreshFunc);
	glfwSetKeyCallback(window, keyFunc);
	glClearColor(clr[0], clr[1],clr[2], clr[3]);
	// main loop
	while (!glfwWindowShouldClose(window)) {
		// draw
		glClear(GL_COLOR_BUFFER_BIT);
		glFinish();
		// GLFW actions
		glfwSwapBuffers(window);
		glfwPollEvents();
	}
	// done
	glfwTerminate();
	return 0;
}

p.s 1)
GLFWwindow* window = glfwCreateWindow(WIN_W, WIN_H, win_name, NULL, NULL); 하는부분에서 첫번째 NULL 값에 glfwGetPrimaryMoniter() 을 넣어주면 전체화면 모드로 실행이 된다.

0개의 댓글