메인 루프 외부 코드
// Include standard headers
#include <stdio.h>
#include <stdlib.h>
// Include GLEW
#include <GL/glew.h>
// Include GLFW
#include <GLFW/glfw3.h>
// Include GLM
#include <glm/glm.hpp>
using namespace glm;
// Import the LoadShaders function
#include <common/shader.hpp>
메인 루프 외부, but 메인 함수 내부 코드 ( int main() { ... } )
glewExperimental = true;
if (!glfwInit()) {
fprintf(stderr, "Failed to initalize GLFW\n");
return -1;
}
glfwWindowHint(GLFW_SAMPELS, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // for MacOS
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // not old OpenGL
- Open a window & create OpenGL context
GLFWwindow* window;
window = glfwCreateWindow(1024, 768, "Title", NULL, NULL);
if (window == NULL) {
fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version\n");
glfwTerminate();
}
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
- Create a Vertex Array Object & set it as the current one
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f
};
- Create and compile the GLSL program for the shaders
GLuint programID = LoadShaders("SimpleVertexShader.vertexshader", "SimpleFragmentShader.fragmentshader");
GLuint vertexbuffer; // identify the vertex buffer
glGenBuffers(1, &vertexbuffer); // generate 1 buffer, put the resulting identifier in vertexbuffer
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); // talk about the 'vertexbuffer' buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); // give the verticles to OpenGL
메인 루프 내부 코드 ( do { ... } while { ... } )
glfwSetInputMode(window, GLFW_STICK_KEYS, GL_TRUE);
glUseProgram(programID);
- 1st attribute buffer : verticles
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized (T/F)
0, // stride (다음 요소까지의 간격)
(void*)0 // array buffer offset (배열 버퍼의 옮기는 값)
);
glDrawArrays(GL_TRIANGLES, 0, 3); // starting from the vertex 0, 3 verticles total (1 triangle)
glDisableVertexAttribArray(0);
glfwSwapBuffers(window);
glfwPollEvents();