[OpenGL] 2.시작하기 Hello Window

힐링힐링·2025년 1월 17일
0

OpenGL

목록 보기
2/2

결과

Hello Window

  1. index.cpp 파일 생성 및 코드 작성
    glfw초기화 및 Window 화면 관련 설정
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>


int main()
{
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

    return 0;
}
  1. 해상도 설정
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
    std::cout << "Failed to create GLFW window" << std::endl;
    glfwTerminate();
    return -1;
}
glfwMakeContextCurrent(window);

GLAD

  1. GLAD
    GLAD가 OpenGL 함수의 포인터를 관리해야되므로 OpenGL 함수 호출전 GLAD를 초기화
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
    std::cout << "Failed to initialize GLAD" << std::endl;
    return -1;
}    

Viewport

4.Viewport 설정
Main 상단에 윈도우 사이즈 설정 함수 선정

void framebuffer_size_callback(GLFWwindow* window, int width, int height);

렌더링 윈도우 사이즈 설정

void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
    glViewport(0, 0, width, height);
}  

창 크기가 변할때마다 GLFW에게 알려줘야함

glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);  

창을 처음 표시할때 초기 그대로 framebuffer_size_callback 호출

Engine 준비

프로그램을 계속 작동시켜야되므로, GLFW에게 그만하라고 할때까지 실행

while(!glfwWindowShouldClose(window))
{
    glfwSwapBuffers(window);
    glfwPollEvents();    
}

마지막

렌더링 루프가 종료되면 할당된 자원 정리/삭제

glfwTerminate();
return 0;

중간결과

이어서...

입력

glfwGetKey로 키입력 확인
GLFW_KEY_ESCAPE로 ESC입력을 확인하여, 창을 닫는다.
Main상단에 함수 선언

void processInput(GLFWwindow* window);
void processInput(GLFWwindow *window)
{
    if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
}

지속적으로 읽을수 있도록 while문에 processInput(window); 추가한다.

    while (!glfwWindowShouldClose(window))
    {
    	//input
        processInput(window);

        glfwSwapBuffers(window);
        glfwPollEvents();
        
    }

렌더링 관련코드

화면의 색을 변경해주기 위한 코드이며,
지속적으로 보여야되기에, While에 추가한다.

glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

전체코드

#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>

using namespace std;

void processInput(GLFWwindow* window);
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
    glViewport(0, 0, width, height);
}
int main()
{
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

    GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
    if (window == NULL)
    {
        cout << "Failed to create GLFW window" << endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);

    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        cout << "Failed to initialize GLAD" << endl;
        return -1;
    }
    // 뷰포트 설정
    glViewport(0, 0, 800, 600);

    // 콜백 함수 등록
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    while (!glfwWindowShouldClose(window))
    {
        //input
        processInput(window);
        //rendering
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        // 버퍼 스왑 및 이벤트 처리

        glfwSwapBuffers(window);
        glfwPollEvents();
        
    }
    glfwTerminate();

    return 0;
}


출처

https://learnopengl.com/Getting-started/Hello-Window
https://heinleinsgame.tistory.com/5

profile
블로그 이전합니다 https://james-kim-tech.tistory.com/

0개의 댓글