Commit 50f1b4c0 authored by Bastian Krayer's avatar Bastian Krayer

added Fragezeichen solution

parent 468b5521
cmake_minimum_required(VERSION 2.8)
include(${CMAKE_MODULE_PATH}/DefaultExecutable.cmake)
\ No newline at end of file
#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;
std::vector<glm::vec3> colors, points;
int selectIndex = -1;
void mouseCallback(GLFWwindow* window, int button, int action, int mods);
void renderMain(GLFWwindow* window) {
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);
glm::vec3 point, color;
for (int i = 0; i < NUM_POINTS; i++) {
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.0, 0.0, 0.0);
// 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.0);
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(window)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (selectIndex >= 0 && selectIndex < points.size()) {
// get mouse position
double x, y;
glfwGetCursorPos(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(window);
glfwPollEvents();
}
}
int main() {
glfwInit();
CVK::useOpenGL33CoreProfile();
GLFWwindow* window =
glfwCreateWindow(WIDTH, HEIGHT, "Voronoi", nullptr, nullptr);
glfwSetWindowPos(window, 600, 50);
glfwMakeContextCurrent(window);
glfwSetMouseButtonCallback(window, mouseCallback);
// init opengl
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize OpenGL context" << std::endl;
return -1;
}
renderMain(window);
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
void mouseCallback(GLFWwindow* window, int button, int action, int mods) {
if (action == GLFW_RELEASE) {
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.0;
for (int i = 0; i < NUM_POINTS; i++) {
glm::vec2 point_pos(points.at(i).x, points.at(i).y);
float dist = glm::length(point_pos - mousePos);
if (dist < minDist) {
minDist = dist;
selectIndex = i;
}
}
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment