#include "gl_window.h" #include #include "gfx.h" #include "program.h" #include "shader.h" namespace chisel { const std::string vertex_shader = "#version 330\n" "in vec3 pos;\n" "out vec3 pos_barycentric;\n" "\n" "void main() {\n" "gl_Position = vec4(pos, 1.0);\n" "int m = int(mod(gl_VertexID, 3));\n" "float t[3] = float[](0.0, 0.0, 0.0);\n" "t[m] = 1.0;\n" "pos_barycentric = vec3(t[0], t[1], t[2]);\n" "}\n"; const std::string fragment_shader = "#version 330\n" "out vec4 color;\n" "in vec3 pos_barycentric;\n" "void main() {\n" "float trigger = pos_barycentric.x/2 + pos_barycentric.y;\n" "float criteria = trigger*trigger - pos_barycentric.y;\n" "float delta = 3*fwidth(criteria)/5;\n" "float alpha = 1 - smoothstep(-delta, delta, criteria);\n" "color = vec4(pos_barycentric, alpha);\n" "}\n"; void glfw_init() { // Initialize glfw glfwInit(); // Set OpenGL version glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); } void glfw_finish() { glfwTerminate(); } int GLWindow::gl_window_count = 0; GLWindow::GLWindow() { if (gl_window_count++ == 0) glfw_init(); // Create the window glfw_window = glfwCreateWindow(600, 400, "gl", nullptr, nullptr); // Create OpenGL context glfwMakeContextCurrent(glfw_window); gladLoadGLLoader((GLADloadproc)glfwGetProcAddress); auto program = gfx::Program(); { auto vs = gfx::Shader(vertex_shader, gfx::VERTEX_SHADER); auto fs = gfx::Shader(fragment_shader, gfx::FRAGMENT_SHADER); program.attach(vs); program.attach(fs); program.link(); } program.use(); while (true) { glfwPollEvents(); if (glfwWindowShouldClose(glfw_window)) break; int w, h; glfwGetWindowSize(glfw_window, &w, &h); width = (float)w; height = (float)h; glViewport(0, 0, w, h); glClearColor(1.0, 1.0, 1.0, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); float b[] = { }; glfwSwapInterval(1); glfwSwapBuffers(glfw_window); } } GLWindow::~GLWindow() { glfwDestroyWindow(glfw_window); if (--gl_window_count == 0) glfw_finish(); } }