1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| #define GLEW_STATIC #include <GL/glew.h> #include <GL/GL.h> #include <GLFW/glfw3.h> #include <Shader.h> #include <iostream>
#pragma region BaseSetting
static GLFWwindow* window; const unsigned int SCR_WIDTH = 800; const unsigned int SCR_HEIGHT = 600; const unsigned int MAX_COUNT = 800 * 600;
static void InitializeWindow() { glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Test", NULL, NULL); glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, [](GLFWwindow* window, int width, int height){ glViewport(0, 0, width, height); });
glewExperimental = GL_TRUE; glewInit(); }
static void ProcessInput(GLFWwindow* window) { if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); } #pragma endregion
#pragma region InitializeVertex
static float vertices[] = { 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f }; static unsigned int VAO, VBO;
static void Initialize() { glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO);
glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float))); glEnableVertexAttribArray(1);
}
#pragma endregion
void Render() { glClearColor(0.5, 0.5, 0.5, 1); glClear(GL_COLOR_BUFFER_BIT);
float time = glfwGetTime(); static Shader shader01("Shader01.vs", "Shader01.fs");
shader01.setVec3("posOffset", sin(time) / 2.0, sin(time) / 2.0, 0.0f); shader01.setVec4("colorOffset", sin(time) / 2.0, sin(time) / 2.0, sin(time) / 2.0, 0.0f);
shader01.use();
glBindVertexArray(VAO); glDrawArrays(GL_TRIANGLES, 0, 3); }
int main() { InitializeWindow();
Initialize();
while (!glfwWindowShouldClose(window)) { ProcessInput(window);
Render();
glfwSwapBuffers(window); glfwPollEvents(); }
glfwTerminate(); return 0; }
|