Commit 8981fe07 authored by Bastian Krayer's avatar Bastian Krayer
Browse files

added bspline ex. base

parent 63ca4329
#include "BSpline.h"
#include <cmath>
BSpline::BSpline(std::vector<glm::vec3> cpoints, int k)
: m_controlPoints(std::move(cpoints)), m_k(k) {}
BSpline::~BSpline() = default;
glm::vec3 BSpline::evaluate(float t) {
// TODO: Aufgabe 5 (c)
return glm::vec3(0.0f);
}
float BSpline::coxDeBoor(float t, int i, int k) {
// TODO: Aufgabe 5 (d)
return 0.0f;
}
void BSpline::setK(int val) { m_k = val; }
int BSpline::getK() const { return m_k; }
const std::vector<glm::vec3>& BSpline::getVertices() { return m_vertices; }
const std::vector<glm::vec3>& BSpline::getControlPoints() {
return m_controlPoints;
}
#ifndef BSPLINE_H
#define BSPLINE_H
#include <glm/glm.hpp>
#include <vector>
class BSpline {
public:
BSpline(std::vector<glm::vec3> cpoints, int k);
virtual ~BSpline();
virtual bool initialize(int num) = 0;
void setK(int val);
int getK() const;
const std::vector<glm::vec3>& getVertices();
const std::vector<glm::vec3>& getControlPoints();
protected:
float coxDeBoor(float t, int i, int k);
glm::vec3 evaluate(float t);
std::vector<glm::vec3> m_controlPoints;
std::vector<glm::vec3> m_vertices;
std::vector<float> m_knotVector;
int m_k = 0;
};
#endif
\ No newline at end of file
cmake_minimum_required(VERSION 3.1)
include(${CMAKE_MODULE_PATH}/DefaultExecutable.cmake)
\ No newline at end of file
#include "PeriodicBSpline.h"
// for std::iota
#include <numeric>
// for std::is_sorted
#include <algorithm>
PeriodicBSpline::PeriodicBSpline(std::vector<glm::vec3> cpoints, int k, int num)
: BSpline(std::move(cpoints), k) {
// TODO: Aufgabe 5 (a)
initialize(num);
}
PeriodicBSpline::~PeriodicBSpline() = default;
bool PeriodicBSpline::initialize(int num) {
// TODO: Aufgabe 5 (b)
return true;
}
\ No newline at end of file
#ifndef PERIODICBSPLINE_H
#define PERIODICBSPLINE_H
#include "BSpline.h"
class PeriodicBSpline : public BSpline {
public:
PeriodicBSpline(std::vector<glm::vec3> cpoints, int k, int num);
~PeriodicBSpline() override;
bool initialize(int num) override;
};
#endif
CVK_2
\ No newline at end of file
#include "PeriodicBSpline.h"
#include <CVK_2/CVK_Framework.h>
#include <glad/glad.h>
#include <memory>
constexpr int width = 800; // window width
constexpr int height = 800; // window height
class ExBSpline : public CVK::GlfwBase {
public:
ExBSpline();
~ExBSpline();
// run the actual loop and display
void display();
// render the spline
void renderSpline(BSpline& spline);
private:
// number of vertices
int numSamples = 1000;
// order
int k = 5;
int drawType = 0;
// render parameters
GLint m_colorHandle = 0;
GLuint m_vbos[2]{};
GLuint m_vaoHandle = 0;
std::unique_ptr<CVK::ShaderSet> m_shaderProgram;
};
int main() {
// Construct and display
ExBSpline demo;
demo.display();
return 0;
}
// Implementation
ExBSpline::ExBSpline() {
m_window = glfwCreateWindow(width, height, "BSpline", nullptr, nullptr);
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);
// compile a shader program
m_shaderProgram = std::make_unique<CVK::ShaderSet>(
VERTEX_SHADER_BIT | FRAGMENT_SHADER_BIT,
std::vector<std::string>{CVK::State::getInstance()->getShaderPath() +
"/BSplines/bsplines.vert",
CVK::State::getInstance()->getShaderPath() +
"/BSplines/bsplines.frag"});
// use the shader program
m_shaderProgram->useProgram();
m_colorHandle =
glGetUniformLocation(m_shaderProgram->getProgramID(), "color");
glGenVertexArrays(1, &m_vaoHandle);
glBindVertexArray(m_vaoHandle);
glGenBuffers(2, m_vbos);
}
ExBSpline::~ExBSpline() {
glDeleteBuffers(2, m_vbos);
glDeleteVertexArrays(1, &m_vaoHandle);
}
void ExBSpline::display() {
// controlpoints
std::vector<glm::vec3> cpoints;
// create control points
cpoints.emplace_back(-0.5, -0.25, 0.0);
cpoints.emplace_back(0.0, 0.5, 0.0);
cpoints.emplace_back(0.5, -0.25, 0.0);
PeriodicBSpline pSpline(cpoints, k, numSamples);
// render stuff
// Renderer lives only in this block and will clean up when it is
// destroyed
while (!glfwWindowShouldClose(m_window)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
renderSpline(pSpline);
// show what's been drawn
glfwSwapBuffers(m_window);
glfwPollEvents();
}
}
void ExBSpline::renderSpline(BSpline& spline) {
glm::vec3 black = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 red = glm::vec3(1.0f, 0.0f, 0.0f);
// draw control points
glPointSize(15.0f);
glUniform3fv(m_colorHandle, 1, glm::value_ptr(black));
glBindBuffer(GL_ARRAY_BUFFER, m_vbos[0]);
glBufferData(GL_ARRAY_BUFFER,
sizeof(glm::vec3) * spline.getControlPoints().size(),
spline.getControlPoints().data(), GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glDrawArrays(GL_POINTS, 0, spline.getControlPoints().size());
// draw curve points
glPointSize(5.0f);
glUniform3fv(m_colorHandle, 1, glm::value_ptr(red));
glBindBuffer(GL_ARRAY_BUFFER, m_vbos[1]);
glBufferData(GL_ARRAY_BUFFER,
sizeof(glm::vec3) * spline.getVertices().size(),
spline.getVertices().data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glDrawArrays(GL_LINE_STRIP, 0, spline.getVertices().size());
// unbind the VBO, we don't need it anymore
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
#version 330 core
uniform vec3 color;
out vec4 fragmentColor;
void main() { fragmentColor = vec4(color, 1); }
\ No newline at end of file
#version 330 core
layout(location = 0) in vec4 position;
void main() { gl_Position = position; }
\ No newline at end of file
#version 330 core
layout(location = 0) in vec4 position;
layout(location = 1) in vec4 normalAttribute;
uniform mat4 projection;
uniform mat4 view;
out vec3 passPosition;
out vec3 passNormal;
void main() {
passPosition = (view * position).xyz;
passNormal = normalize((transpose(inverse(view)) * normalAttribute).xyz);
gl_Position = projection * view * position;
}
\ No newline at end of file
#version 330
in vec3 passPosition;
in vec3 passNormal;
uniform mat4 view;
layout(location = 0) out vec4 fragColor;
void main() {
vec3 eye = normalize(-passPosition);
vec3 diffuse_color = vec3(0.4, 0.4, 0.4);
vec4 ambient_color = vec4(0.2, 0.2, 0.2, 1);
vec3 specular_color = vec3(0.5, 0.5, 0);
vec3 lightDir = vec3(1.0, 1.0, 0.0);
// Diffuse
vec3 lightVector = normalize(lightDir.xyz);
float cos_phi = max(dot(passNormal, lightVector), 0.0f);
// Specular
vec3 reflection = normalize(reflect(-lightVector, passNormal));
float cos_psi_n = pow(max(dot(reflection, eye), 0.0f), 3);
fragColor = ambient_color;
fragColor.rgb += diffuse_color * cos_phi;
fragColor.rgb += specular_color * cos_psi_n;
fragColor.a = 1;
}
Supports Markdown
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