...
 
Commits (2)
#include "BSpline.h"
BSpline::BSpline(std::vector<glm::vec3> cpoints, int k)
: mControlPoints(std::move(cpoints)), mK(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) { mK = val; }
int BSpline::getK() const { return mK; }
std::vector<glm::vec3>& BSpline::getVertices() { return mVertices; }
std::vector<glm::vec3>& BSpline::getControlPoints() { return mControlPoints; }
#ifndef BSPLINE_H
#define BSPLINE_H
#include <glm/glm.hpp>
#include <iostream>
#include <list>
#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;
std::vector<glm::vec3>& getVertices();
std::vector<glm::vec3>& getControlPoints();
protected:
float coxDeBoor(float t, int i, int k);
glm::vec3 evaluate(float t);
std::vector<glm::vec3> mControlPoints;
std::vector<glm::vec3> mVertices;
std::vector<float> mKnotVector;
int mK;
};
#endif
\ No newline at end of file
cmake_minimum_required(VERSION 2.8)
include(${CMAKE_MODULE_PATH}/DefaultExecutable.cmake)
\ No newline at end of file
#include "PeriodicBSpline.h"
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
#include "Renderer.h"
#include <CVK_2/CVK_Framework.h>
#include <memory>
GLFWwindow* Renderer::initialize(int width, int height) {
glfwInit();
CVK::useOpenGL33CoreProfile();
GLFWwindow* window =
glfwCreateWindow(width, height, "BSpline", nullptr, nullptr);
glfwSetWindowPos(window, 600, 50);
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize OpenGL context" << std::endl;
return nullptr;
}
glClearColor(1, 1, 1, 1);
// compile a shader program
mShaderProgram = 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
mShaderProgram->useProgram();
mColorHandle =
glGetUniformLocation(mShaderProgram->getProgramID(), "color");
GLuint vaoHandle;
glGenVertexArrays(1, &vaoHandle);
glBindVertexArray(vaoHandle);
glGenBuffers(2, mVbos);
return window;
}
void Renderer::render(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(mColorHandle, 1, glm::value_ptr(black));
glBindBuffer(GL_ARRAY_BUFFER, mVbos[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(mColorHandle, 1, glm::value_ptr(red));
glBindBuffer(GL_ARRAY_BUFFER, mVbos[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);
}
Renderer::~Renderer() { glDeleteBuffers(2, mVbos); }
#ifndef RENDERER_H
#define RENDERER_H
#include "BSpline.h"
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <CVK_2/CVK_ShaderSet.h>
#include <memory>
class Renderer {
public:
Renderer() = default;
virtual ~Renderer();
GLFWwindow* initialize(int width, int height);
void render(BSpline& spline);
private:
GLint mColorHandle = 0;
GLuint mVbos[2]{};
std::unique_ptr<CVK::ShaderSet> mShaderProgram;
};
#endif
CVK_2
\ No newline at end of file
#include "PeriodicBSpline.h"
#include "Renderer.h"
std::vector<glm::vec3> cpoints; // controlpoints
int num_samples = 1000; // number of vertices
int k = 3; // order
const int width = 800; // window width
const int height = 800; // window height
int main() {
// 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 p_spline(cpoints, k, num_samples);
GLFWwindow* window = nullptr;
{
// render stuff
// Renderer lives only in this block and will clean up when it is
// destroyed
Renderer renderer;
window = renderer.initialize(width, height);
if (window == nullptr) {
std::cerr << "error initializing opengl" << std::endl;
return -1;
}
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
renderer.render(p_spline);
// show what's been drawn
glfwSwapBuffers(window);
glfwPollEvents();
}
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
......@@ -23,10 +23,10 @@ CubeMapTexture::~CubeMapTexture() {
void CubeMapTexture::createCubeMapTexture() {
glGenTextures(1, &m_textureID);
glBindTexture(GL_TEXTURE_CUBE_MAP, m_textureID);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
}
......
#include <CVK_2/CVK_Geometry.h>
#include <algorithm>
namespace CVK {
Geometry::Geometry() = default;
......@@ -112,6 +114,7 @@ void Geometry::render() {
void Geometry::computeTangents() {
m_tangents.resize(m_vertices.size());
std::fill(m_tangents.begin(), m_tangents.end(), glm::vec3(0.0f));
for (int i = 0; i < m_indices; i += 3) {
// Edges of the triangle : postion delta
......@@ -119,8 +122,8 @@ void Geometry::computeTangents() {
int index_v1 = m_index[i + 1];
int index_v2 = m_index[i + 2];
glm::vec4 deltaPos1 = m_vertices[index_v1] - m_vertices[index_v0];
glm::vec4 deltaPos2 = m_vertices[index_v2] - m_vertices[index_v0];
glm::vec3 deltaPos1 = m_vertices[index_v1] - m_vertices[index_v0];
glm::vec3 deltaPos2 = m_vertices[index_v2] - m_vertices[index_v0];
/*
glm::vec4 deltaPos1 = m_vertices[index_v0]-m_vertices[index_v1];
glm::vec4 deltaPos2 = m_vertices[index_v2]-m_vertices[index_v1];
......@@ -134,13 +137,19 @@ void Geometry::computeTangents() {
glm::vec2 deltaUV2 = m_uvs[index_v2]-m_uvs[index_v1];
*/
float r = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV1.y * deltaUV2.x);
glm::vec4 tmp = (deltaPos1 * deltaUV2.y - deltaPos2 * deltaUV1.y) * r;
glm::vec3 tmp = (deltaPos1 * deltaUV2.y - deltaPos2 * deltaUV1.y) * r;
glm::vec3 tangent = glm::vec3(tmp.x, tmp.y, tmp.z);
// Set the same tangent for all three vertices of the triangle
m_tangents[index_v0] = tangent;
m_tangents[index_v1] = tangent;
m_tangents[index_v2] = tangent;
// Adding, as multiple triangles share indices
// This will smooth out sudden changes
m_tangents[index_v0] += tangent;
m_tangents[index_v1] += tangent;
m_tangents[index_v2] += tangent;
}
// normalize at the end
for (auto& t : m_tangents) {
t = glm::normalize(t);
}
}
......
#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