main.cpp 3.43 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
#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 Fragezeichen : public CVK::GlfwBase {
  public:
    Fragezeichen();

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

  private:
};

int main() {

    Fragezeichen demo;

    demo.display();

    return 0;
}

// Implementation

Fragezeichen::Fragezeichen() {
    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);

    // 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 Fragezeichen::display() {

    glClearColor(1, 1, 1, 1);

    // create cone vao

    // TODO Use appropriate constructor to create a cone with 45° opening angle,
    // oriented along the z-axis with its apex located at (0,0,0). The height
    // an be chosen to mach the camera's far value
    // You can use the model matrix to move the geometry
    CVK::Cone coneGeometry;

    // TODO Generate random points and colors

    // 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();

    // get uniform handles
    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.0);

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

    // set color of the cone
    glm::vec3 color(1.0, 0.0, 0.0);
    glUniform3fv(colorHandle, 1, glm::value_ptr(color));

    glEnable(GL_DEPTH_TEST);

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

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // TODO Render a cone for each point with the respective color
        // render cone
        coneGeometry.render();

        glfwSwapBuffers(m_window);
        glfwPollEvents();
    }
}