개인 블로그에서 21.05.14에 작성된 글입니다. 참고 바랍니다.
https://kyoungwhankim.github.io/ko/blog/opengl_triangle1_2/
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdlib.h>
int main(void)
{
if (!glfwInit())
exit(EXIT_FAILURE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
GLFWwindow* window = glfwCreateWindow(1080, 720, "title", NULL, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
int framebuf_width, framebuf_height;
glfwGetFramebufferSize(window, &framebuf_width, &framebuf_height);
glViewport(0, 0, framebuf_width, framebuf_height);
if (glewInit() != GLEW_OK)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
float vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
GLuint VAO;
GLuint VBO;
glGenVertexArrays(1, &VAO); // VAO 생성
glBindVertexArray(VAO); // VAO를 OpenGL context에 연결(bind)
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GL_FLOAT), (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
while (!glfwWindowShouldClose(window))
{
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(ShaderProgramThatWeWillMakeLater); // 아직 모르셔도 됩니다
glBindVertexArray(VAO); // 한 줄로 끝!
glDrawArrays(GL_TRIANGLES, 0, 3); // 아직 모르셔도 됩니다
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
코드가 좀 길어졌습니다. 삼각형을 생성하는 코드를 밖으로 빼서 하나의 함수로 만들도록 하겠습니다. 전체 코드 중 아래 부분을 CreateTriangle이라는 이름의 함수로 빼도록 하겠습니다.
float vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
GLuint VAO;
GLuint VBO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GL_FLOAT), (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
이 과정에서 VAO와 VBO는 전역 변수로 빼도록 하겠습니다.
그럼 다음과 같이 main 함수가 정리됩니다.
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdlib.h>
GLuint VAO, VBO;
void CreateTriangle()
{
float vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
GLuint VAO;
GLuint VBO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GL_FLOAT), (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
int main(void)
{
if (!glfwInit())
exit(EXIT_FAILURE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
GLFWwindow* window = glfwCreateWindow(1080, 720, "title", NULL, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
int framebuf_width, framebuf_height;
glfwGetFramebufferSize(window, &framebuf_width, &framebuf_height);
glViewport(0, 0, framebuf_width, framebuf_height);
if (glewInit() != GLEW_OK)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
CreateTriangle();
while (!glfwWindowShouldClose(window))
{
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(ShaderProgramThatWeWillMakeLater);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
main 함수가 조금 짧아졌습니다.
다음 글에서 셰이더를 작성해보겠습니다.