main.cpp 5.94 KB
Newer Older
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
#include "SplineSurface.h"

#include <CVK_2/CVK_GlfwBase.h>
#include <CVK_2/CVK_ShaderSet.h>
#include <CVK_2/CVK_Trackball.h>

#include <memory>

constexpr int num_samples = 200; // number of vertices
constexpr int k = 3;             // order
constexpr int width = 800;       // window width
constexpr int height = 800;      // window height

class ExSplineSurface : public CVK::GlfwBase {

  public:
    ExSplineSurface();
    ~ExSplineSurface();

    void display();

  private:
    void render(SplineSurface& surface, const glm::mat4& projection,
                const glm::mat4& cam);

    std::unique_ptr<CVK::ShaderSet> mShaderProgram;
    GLint mColorHandle;
    GLint mProjectionHandle;
    GLint mViewHandle;
    GLuint mVbos[3];
};

int main() {

    ExSplineSurface demo;

    demo.display();

    return 0;
}

// Implementation

ExSplineSurface::ExSplineSurface() {
    m_window = glfwCreateWindow(width, height, "Spline-Surface", 0, 0);
    glfwSetWindowPos(m_window, 600, 50);
    glfwMakeContextCurrent(m_window);
    glfwSetWindowUserPointer(m_window, this);

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

    glClearColor(1, 1, 1, 1);

    glCullFace(GL_NONE);
    glEnable(GL_DEPTH_TEST);

    // compile a shader program
    std::vector<std::string> shadernames = {
        CVK::State::getInstance()->getShaderPath() +
            "/BSplines/bsplines_cam.vert",
        CVK::State::getInstance()->getShaderPath() +
            "/BSplines/phong_bezier.frag"};
    mShaderProgram = std::make_unique<CVK::ShaderSet>(
        VERTEX_SHADER_BIT | FRAGMENT_SHADER_BIT, shadernames);
    // use the shader program
    mShaderProgram->useProgram();

    mColorHandle =
        glGetUniformLocation(mShaderProgram->getProgramID(), "color");
    mProjectionHandle =
        glGetUniformLocation(mShaderProgram->getProgramID(), "projection");
    mViewHandle = glGetUniformLocation(mShaderProgram->getProgramID(), "view");

    GLuint vaoHandle;
    glGenVertexArrays(1, &vaoHandle);
    glBindVertexArray(vaoHandle);
    glGenBuffers(3, mVbos);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

ExSplineSurface::~ExSplineSurface() { glDeleteBuffers(2, mVbos); }

void ExSplineSurface::display() {
    // controlpoints
    std::vector<glm::vec3> cpoints;
    // controlpoints
    std::vector<glm::vec4> cpointsTrim;

    // create control points beware that the order matters!
    cpoints.push_back(glm::vec3(-0.5, -0.25, -0.5));
    cpoints.push_back(glm::vec3(-0.5, 0.5, -0.25));
    cpoints.push_back(glm::vec3(-0.5, -0.25, 0.0));

    cpoints.push_back(glm::vec3(0, 0, -0.5));
    cpoints.push_back(glm::vec3(0, 0.5, -0.25));
    cpoints.push_back(glm::vec3(0, 0, 0.0));

    cpoints.push_back(glm::vec3(0.5, -0.25, -0.5));
    cpoints.push_back(glm::vec3(0.5, 0.5, -0.25));
    cpoints.push_back(glm::vec3(0.5, -0.25, 0.0));

    // create control points // z==0 make sure we are in range [0 1]
    cpointsTrim.push_back(glm::vec4(0.5, 0, 0.0, 1.0));
    cpointsTrim.push_back(glm::vec4(0, 0.3, 0.0, 3.0));
    cpointsTrim.push_back(glm::vec4(0.25, 0.5, 0.0, 3.0));

    cpointsTrim.push_back(glm::vec4(0.5, 0.25, 0.0, 2));
    cpointsTrim.push_back(glm::vec4(0.5, 0.25, 0.0, 2));
    cpointsTrim.push_back(glm::vec4(0.5, 0.25, 0.0, 2));

    cpointsTrim.push_back(glm::vec4(0.75, 0.5, 0.0, 3));
    cpointsTrim.push_back(glm::vec4(1, 0.3, 0.0, 3));
    cpointsTrim.push_back(glm::vec4(0.5, 0, 0.0, 1.0));

    SplineSurface splineSurface(cpoints, num_samples, num_samples);

    // trackball camera
    auto proj = std::make_shared<CVK::Perspective>(
        glm::radians(75.0f), float(width) / float(height), 0.1, 10);
    CVK::Trackball cam(m_window, width, height, proj);
    cam.setRadius(1);

    double lastTime = glfwGetTime();
    double deltaTime = 0.0;
    double nowTime = 0.0;

    while (!glfwWindowShouldClose(m_window)) {
        nowTime = glfwGetTime();
        deltaTime = nowTime - lastTime;
        lastTime = nowTime;

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        cam.update(deltaTime);

        render(splineSurface, cam.getProjection()->getProjMatrix(),
               cam.getView());

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

void ExSplineSurface::render(SplineSurface& surface,
                             const glm::mat4& projection,
                             const glm::mat4& cam) {
    glm::vec3 black = glm::vec3(0.0, 0.0, 0.0);
    glm::vec3 red = glm::vec3(1.0, 0.0, 0.0);

    mShaderProgram->useProgram();
    // update camera info
    glUniformMatrix4fv(mProjectionHandle, 1, false, glm::value_ptr(projection));
    glUniformMatrix4fv(mViewHandle, 1, false, glm::value_ptr(cam));

    // draw control points
    glPointSize(15.0f);
    glUniform3fv(mColorHandle, 1, glm::value_ptr(black));
    glBindBuffer(GL_ARRAY_BUFFER, mVbos[0]);
    glBufferData(GL_ARRAY_BUFFER,
                 sizeof(glm::vec3) * surface.getControlPoints().size(),
                 surface.getControlPoints().data(), GL_DYNAMIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glDrawArrays(GL_POINTS, 0, surface.getControlPoints().size());

    // draw curve points
    if (!surface.getVertices().empty()) {
        glPointSize(5.0f);
        glUniform3fv(mColorHandle, 1, glm::value_ptr(red));
        glBindBuffer(GL_ARRAY_BUFFER, mVbos[1]);
        glBufferData(GL_ARRAY_BUFFER,
                     sizeof(glm::vec3) * surface.getVertices().size(),
                     surface.getVertices().data(), GL_STATIC_DRAW);
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

        glDrawArrays(GL_POINTS, 0, surface.getVertices().size());

        // unbind the VBO, we don't need it anymore
        glBindBuffer(GL_ARRAY_BUFFER, 0);
    }
}