main.cpp 6.82 KB
Newer Older
Bastian Krayer's avatar
Bastian Krayer committed
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <CVK_2/CVK_Framework.h>

#include <glad/glad.h>

#include <memory>
#include <random>

const int WIDTH = 600;
const int HEIGHT = 600;
const int NUM_POINTS = 50;

class FragezeichenSolution : public CVK::GlfwBase {
  public:
    FragezeichenSolution();

    // run the actual loop and display
    void display();

    static void mouseCallback(GLFWwindow* window, int button, int action,
                              int mods);

  private:
    std::vector<glm::vec3> colors, points;
    int selectIndex = -1;
};

void mouseCallback(GLFWwindow* window, int button, int action, int mods);

int main() {

    FragezeichenSolution demo;

    demo.display();

    return 0;
}

// Implementation

FragezeichenSolution::FragezeichenSolution() {
    m_window = glfwCreateWindow(WIDTH, HEIGHT, "Voronoi", nullptr, nullptr);

    // Allows us to associate data with a window
    // We store a pointer to this object so we can use it in the static methods
    glfwSetWindowUserPointer(m_window, this);
    glfwSetWindowPos(m_window, 600, 50);
    glfwMakeContextCurrent(m_window);
    glfwSetMouseButtonCallback(m_window, mouseCallback);

    // init opengl
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
        std::cerr << "Failed to initialize OpenGL context" << std::endl;
        throw std::runtime_error("Failed to initialize OpenGL context");
    }
}

void FragezeichenSolution::mouseCallback(GLFWwindow* window, int button,
                                         int action, int mods) {
    // Since glfw callbacks come from C, we can only pass static member
    // functions. But at the beginning, we store a pointer to our object with
    // this window and we can retrieve it. "demo" effectively acts as "this"
    // here
    auto demo =
        static_cast<FragezeichenSolution*>(glfwGetWindowUserPointer(window));

    if (action == GLFW_RELEASE) {
        demo->selectIndex = -1;
    }
    if (action == GLFW_PRESS) {
        double x, y;
        glfwGetCursorPos(window, &x, &y);
        glm::vec2 mousePos;
        mousePos.x = static_cast<int>(((x / WIDTH) * 2.0f) - 1.0f);
        mousePos.y =
            static_cast<int>(((((HEIGHT - y) / HEIGHT) * 2.0f) - 1.0f));

        float minDist = 100.0f;
        for (int i = 0; i < NUM_POINTS; i++) {
            glm::vec2 point_pos(demo->points[i]);
            float dist = glm::length(point_pos - mousePos);
            if (dist < minDist) {
                minDist = dist;
                demo->selectIndex = i;
            }
        }
    }
}

void FragezeichenSolution::display() {

    glClearColor(1, 1, 1, 1);

    float coneRadius = 10.0f;
    CVK::Cone coneGeometry(glm::vec3(0.0f, 0.0f, -coneRadius),
                           glm::vec3(0.0f, 0.0f, 0.0f), coneRadius, 0.0f, 100);

    // create points
    // random device for generating random numbers
    std::random_device gen;
    std::uniform_real_distribution<float> dis(-1.0f, 1.0f);
    std::uniform_real_distribution<float> disColor(0.0f, 1.0f);

    for (int i = 0; i < NUM_POINTS; i++) {
        glm::vec3 point;
        glm::vec3 color;

        point.x = dis(gen);
        point.y = dis(gen);
        point.z = 0.0f;
        points.push_back(point);

        for (int j = 0; j < 3; j++) {
            color[j] = disColor(gen);
        }
        colors.push_back(color);
    }

    GLuint vaoHandlePoints;
    glGenVertexArrays(1, &vaoHandlePoints);
    glBindVertexArray(vaoHandlePoints);

    GLuint vboHandle;
    glGenBuffers(1, &vboHandle);

    // bind the first buffer and store geometry into the VBO
    glBindBuffer(GL_ARRAY_BUFFER, vboHandle);
    glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * points.size(), &points[0],
                 GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), nullptr);

    glm::vec3 black(0.0f, 0.0f, 0.0f);

    // compile a shader program
    CVK::ShaderSet shaderProgram(VERTEX_SHADER_BIT | FRAGMENT_SHADER_BIT,
                                 {CVK::State::getInstance()->getShaderPath() +
                                      "/Fragezeichen/fragezeichen.vert",
                                  CVK::State::getInstance()->getShaderPath() +
                                      "/Fragezeichen/fragezeichen.frag"});
    // use the shader program
    shaderProgram.useProgram();

    GLint viewMatrixHandle =
        glGetUniformLocation(shaderProgram.getProgramID(), "viewMatrix");
    GLint projectionMatrixHandle =
        glGetUniformLocation(shaderProgram.getProgramID(), "projectionMatrix");
    GLint modelMatrixHandle =
        glGetUniformLocation(shaderProgram.getProgramID(), "modelMatrix");
    GLint colorHandle =
        glGetUniformLocation(shaderProgram.getProgramID(), "color");

    // setting up the camera parameters
    glm::mat4 viewMatrix =
        glm::lookAt(glm::vec3(0.0f, 0.0f, 0.001f), glm::vec3(0.0f, 0.0f, 0.0f),
                    glm::vec3(0.0f, 1.0f, 0.0f));
    glm::mat4 projectionMatrix =
        glm::ortho(-1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 10.0f);
    glm::mat4 modelMatrix = glm::mat4(1.0f);

    glUniformMatrix4fv(viewMatrixHandle, 1, GL_FALSE,
                       glm::value_ptr(viewMatrix));
    glUniformMatrix4fv(projectionMatrixHandle, 1, GL_FALSE,
                       glm::value_ptr(projectionMatrix));

    glEnable(GL_DEPTH_TEST);
    glPointSize(5.0f);

    // renderloop
    while (!glfwWindowShouldClose(m_window)) {

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        if (selectIndex >= 0 && selectIndex < points.size()) {
            // get mouse position
            double x, y;
            glfwGetCursorPos(m_window, &x, &y);
            glm::vec2 mousePos;
            mousePos.x = ((x / WIDTH) * 2.0f) - 1.0f;
            mousePos.y = ((((HEIGHT - y) / HEIGHT) * 2.0f) - 1.0f);

            // update selected point
            points[selectIndex] = glm::vec3(mousePos.x, mousePos.y, 0);
            glBindBuffer(GL_ARRAY_BUFFER, vboHandle);
            glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * points.size(),
                         points.data(), GL_STATIC_DRAW);
        }

        for (int i = 0; i < NUM_POINTS; i++) {
            // set color
            glUniform3fv(colorHandle, 1, glm::value_ptr(colors[i]));
            // update modelmatrix
            modelMatrix = glm::translate(glm::mat4(1.0f), points.at(i));
            glUniformMatrix4fv(modelMatrixHandle, 1, GL_FALSE,
                               glm::value_ptr(modelMatrix));
            coneGeometry.render();
        }

        modelMatrix = glm::mat4(1.0f);
        glUniformMatrix4fv(modelMatrixHandle, 1, GL_FALSE,
                           glm::value_ptr(modelMatrix));
        glUniform3fv(colorHandle, 1, glm::value_ptr(black));

        glBindVertexArray(vaoHandlePoints);
        glDrawArrays(GL_POINTS, 0, points.size());

        // show what's been drawn
        glfwSwapBuffers(m_window);
        glfwPollEvents();
    }
}