summaryrefslogtreecommitdiff
path: root/chisel/backends/opengl/gl_window.cpp
blob: 66b86819fdc8e602add69b8c7f726cdda98a8504 (plain)
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
#include "gl_window.h"

#include <string>
#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();
    }
}