Commit 012494fd authored by Bastian Krayer's avatar Bastian Krayer

second solution

parent 46c57c7e
......@@ -49,10 +49,6 @@ target_compile_features(${ProjectId} PUBLIC cxx_std_14)
target_compile_options(${ProjectId} PUBLIC
"$<$<CONFIG:Debug>:/bigobj>"
)
else()
target_compile_options(${ProjectId} PUBLIC
"$<$<CONFIG:Debug>:-Wa,-mbig-obj>"
)
endif()
add_dependencies(${ProjectId} COPY_RUNTIME_FILES)
......
......@@ -49,11 +49,6 @@ IF(SOURCES)
target_compile_options(${ProjectId} PUBLIC
"$<$<CONFIG:Debug>:/bigobj>"
)
else()
target_compile_options(${ProjectId} PUBLIC
"$<$<CONFIG:Debug>:-Wa,-mbig-obj>"
)
endif()
ELSEIF(HEADER)
......
cmake_minimum_required(VERSION 2.8)
include(${CMAKE_MODULE_PATH}/DefaultExecutable.cmake)
\ No newline at end of file
% This file contains the internal library dependencies in src/libraries
% Lines starting with % are comments and will be ignored
% To add a dependency, simply add a new line containing the name of the library
% Example: HelloLib
CVK_2
CVK_AnSim
\ No newline at end of file
#include <CVK_2/CVK_Framework.h>
#include <CVK_AnSim/CVK_AS_CoordinateSystem.h>
#include <CVK_AnSim/CVK_AS_HermiteSpline.h>
#include <CVK_AnSim/CVK_AS_Line.h>
#include <CVK_AnSim/CVK_AS_LineStrip.h>
#include <CVK_AnSim/CVK_AS_ShaderLineRender.h>
const int width = 640;
const int height = 480;
GLFWwindow* window;
// define Camera (Trackball)
CVK::Perspective projection(glm::radians(60.0f), width / float(height), 0.1f,
50.f);
CVK::Trackball* cam_trackball;
CVK::HermiteSpline* spline;
CVK::LineStrip* line;
//*************************************************************************************************************
float timeRunning = 0.0f;
float position = 0.0f;
float speed = 5.0f;
glm::vec3 renderPosition = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 tangent = glm::vec3(1.0f, 0.0f, 0.0f);
glm::vec3 normal = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 binormal = glm::vec3(0.0f, 0.0f, 1.0f);
//*************************************************************************************************************
void updateTeapot(float d_t) {
//*************************************************************************************************************
// TODO Aufgabe 5 (b)
// Berechnen Sie aus der verstrichenen Zeit und der Geschwindigkeit die
// Position des Teapots auf dem Spline und speichern Sie diese in der
// globalen Variable teapotPosition
//*************************************************************************************************************
timeRunning += d_t;
position = timeRunning * speed;
float u;
int patchNum;
spline->getParameterByArcLength(position, u, patchNum);
if (u < 0.0f) { // wenn der Teapot am Ende des Pfades angekommen ist wird er
// an die Ausgangsposition zurueckgesetzt
position = 0.0f;
timeRunning = 0.0f;
return;
}
// jetzt haben wir Parameter u und den index des entsprechenden Splines,
// nun koennen wir diesen evaluieren und erhalten die Position des
// Teekessels.
glm::vec3 t;
spline->evaluateHermiteSpline(
u, (*(spline->getControlPointsPtr()))[patchNum],
(*(spline->getControlPointsPtr()))[patchNum + 1], renderPosition, t);
//*************************************************************************************************************
// TODO Aufgabe 5 (c)
// Berechnen Sie die Orientierung des Teapots mittels der Frenet Frame
// Methode.
//*************************************************************************************************************
// Orientierung des Teapots mittels der Frenet Frame Methode
spline->calculateFrenetFrame(
u, (*(spline->getControlPointsPtr()))[patchNum],
(*(spline->getControlPointsPtr()))[patchNum + 1], tangent, binormal,
normal);
}
void resizeCallback(GLFWwindow* window, int w, int h) {
cam_trackball->setWidthHeight(w, h);
cam_trackball->getProjection()->updateRatio(w / (float)h);
glViewport(0, 0, w, h);
}
void init_camera() {
// Camera
cam_trackball = new CVK::Trackball(window, width, height, &projection);
glm::vec3 center(0.0f, 0.0f, 0.0f);
cam_trackball->setCenter(&center);
cam_trackball->setRadius(5.0f);
CVK::State::getInstance()->setCamera(cam_trackball);
}
void initSpline() {
// Create a sample spline
spline = new CVK::HermiteSpline();
spline->addControlPoint(new CVK::HermiteSplineControlPoint(
glm::vec3(-2.0, 0.0, 2.0), glm::vec3(6.0, 0.0, 0.0)));
spline->addControlPoint(new CVK::HermiteSplineControlPoint(
glm::vec3(-2.0, 0.0, -2.0), glm::vec3(-6.0, 0.0, 0.0)));
spline->addControlPoint(new CVK::HermiteSplineControlPoint(
glm::vec3(2.0, 2.0, 2.0), glm::vec3(6.0, 0.0, 0.0)));
spline->addControlPoint(new CVK::HermiteSplineControlPoint(
glm::vec3(2.0, 2.0, -2.0), glm::vec3(-6.0, 0.0, 0.0)));
spline->addControlPoint(new CVK::HermiteSplineControlPoint(
glm::vec3(2.0, 2.0, -2.0), glm::vec3(6.0, 0.0, 0.0)));
spline->generateRenderVertices();
// Create a line strip (in order to render the spline)
line = new CVK::LineStrip();
line->setColor(glm::vec4(1, 0, 0, 1));
for (int i = 0; i < spline->getVerticesPtr()->size(); i++) {
line->addPoint(spline->getVerticesPtr()->at(i));
}
//*************************************************************************************************************
renderPosition = (spline->getControlPointsPtr()->at(0))->getPosition();
spline->generateArcLengthTable(100);
//*************************************************************************************************************
}
int main() {
// Init GLFW and GLAD
glfwInit();
CVK::useOpenGL33CoreProfile();
window = glfwCreateWindow(width, height, "AnSim Exercise 2", 0, 0);
glfwSetWindowPos(window, 100, 50);
glfwMakeContextCurrent(window);
glfwSetWindowSizeCallback(window, resizeCallback);
if (!gladLoadGL()) {
printf("Something went wrong!\n");
exit(-1);
}
CVK::State::getInstance()->setBackgroundColor(WHITE);
glm::vec3 BgCol = CVK::State::getInstance()->getBackgroundColor();
glClearColor(BgCol.r, BgCol.g, BgCol.b, 0.0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// load, compile and link Shader
CVK::ShaderPhong phongShader(
VERTEX_SHADER_BIT | FRAGMENT_SHADER_BIT,
{CVK::State::getInstance()->getShaderPath() + "/CVK_AnSim/Phong.vert",
CVK::State::getInstance()->getShaderPath() + "/CVK_AnSim/Phong.frag"});
CVK::ShaderLineRender lineShader(
VERTEX_SHADER_BIT | FRAGMENT_SHADER_BIT,
{CVK::State::getInstance()->getShaderPath() +
"/CVK_AnSim/ColorMVPShader.vert",
CVK::State::getInstance()->getShaderPath() +
"/CVK_AnSim/ColorMVPShader.frag"});
// Camera
init_camera();
// Light Sources
CVK::Light light = CVK::Light(glm::vec4(0.0, 10.0, 0.0, 1.0), GREY,
glm::vec3(0, 0, 0), 1.0f, 0.0f);
CVK::State::getInstance()->addLight(&light);
CVK::State::getInstance()->updateSceneSettings(DARKGREY, 0, WHITE, 1, 10,
1);
// Teapot
CVK::Node teapot("Teapot");
CVK::Material material = CVK::Material(RED, WHITE, 100.0f);
teapot.setMaterial(&material);
CVK::Teapot geometry = CVK::Teapot();
teapot.setGeometry(&geometry);
// Coordinatesystem
CVK::CoordinateSystem coordinatesys(glm::vec3(0.0f));
// Init spline (controllpoints and arc length table)
initSpline();
glLineWidth(6);
float deltaTime = 0.0f;
float oldTime = glfwGetTime();
while (!glfwWindowShouldClose(window)) {
float currentTime = glfwGetTime();
deltaTime = currentTime - oldTime;
oldTime = currentTime;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Update Camera
cam_trackball->update(deltaTime);
updateTeapot(deltaTime);
// Use phong shader to render the teapot
CVK::State::getInstance()->setShader(&phongShader);
CVK::State::getInstance()->setLight(0, &light);
phongShader.update();
//*************************************************************************************************************
// TODO Aufgabe 5 (d)
// Passen Sie die Modelmatrix und rendern Sie den Teapot an gewuenschter
// Position und Orientierung.
//*************************************************************************************************************
glm::mat4 modelmatrix(1.0f);
// Nur Position
// modelmatrix = glm::translate(modelmatrix, renderPosition);
modelmatrix =
glm::mat4(glm::vec4(tangent, 0.0f), glm::vec4(binormal, 0.0f),
glm::vec4(normal, 0.0f), glm::vec4(renderPosition, 1.0f));
modelmatrix = glm::scale(modelmatrix, glm::vec3(0.5f, 0.5f, 0.5f));
teapot.setModelMatrix(modelmatrix);
teapot.render();
// Draw coordinate system and hermite spline as line strip
CVK::State::getInstance()->setShader(&lineShader);
lineShader.update();
coordinatesys.render(&lineShader);
line->render(&lineShader);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
// Clean up
delete line;
delete spline;
return 0;
}
......@@ -64,6 +64,44 @@ void HermiteSpline::generateArcLengthTable(int resolution) {
// TODO Aufgabe 5 (a)
// Hier muss die Erstellung der Bogenlaengentabelle programmiert werden.
//********************************************************************************
int numPatches = mControlPoints.size() - 1;
int stepsPerPatch = resolution / numPatches;
float u;
float uStep = 1.0f / stepsPerPatch;
glm::vec3 point, lastPoint;
for (int i = 0; i < numPatches; i++) {
u = 0.0f;
for (int j = 0; j <= stepsPerPatch; j++) {
if (i != 0 && j == 0) {
u += uStep;
continue;
}
glm::vec3 t;
evaluateHermiteSpline(u, mControlPoints[i], mControlPoints[i + 1],
point, t);
ArcLengthTableEntry ale;
ale.u = u;
ale.patchNumber = i;
if (i == 0 && j == 0) {
ale.arcLength = 0.0f;
} else {
ale.arcLength = (point - lastPoint).length() +
mArcLengthTable.back().arcLength;
}
lastPoint = point;
mArcLengthTable.push_back(ale);
u += uStep;
}
}
}
void HermiteSpline::getParameterByArcLength(float length, float& u,
......@@ -75,6 +113,88 @@ void HermiteSpline::getParameterByArcLength(float length, float& u,
// gegebene Bogenlaenge in der Tabelle gesucht. Ein binaeres
// Suchverfahren ist zu empfehlen.
//********************************************************************************
if (length >
mArcLengthTable.back().arcLength) { // given length exeeds path
// length, return -1 as error
u = -1.0;
patchNum = -1;
return;
}
bool found = false;
int index;
int indexLow = 0;
int indexHi = mArcLengthTable.size() - 1;
while (!found) {
index = static_cast<int>(indexLow + 0.5 * (indexHi - indexLow));
if (mArcLengthTable[index].arcLength == length) { // found
u = mArcLengthTable[index].u;
patchNum = mArcLengthTable[index].patchNumber;
found = true;
}
if (mArcLengthTable[index].arcLength > length) {
if (index - 1 >= 0) {
if (mArcLengthTable[index - 1].arcLength < length) {
float frac =
(length - mArcLengthTable[index - 1].arcLength) /
(mArcLengthTable[index].arcLength -
mArcLengthTable[index - 1].arcLength);
float interpolLow = mArcLengthTable[index - 1].u;
/*
Ueberprüfung: der kleinere Wert ist der letzte Wert des
vorherigen Patch In der Bogenlängentabelle vgl.
Vorlesungsskript fehlt der Wert für u = 0 Daher Wert für
u=1 des vorherigen Patches nehmen.
*/
// if(interpolLow >= 1.0f){
if (interpolLow >= 1.0f - THRESH &&
interpolLow <= 1.0f + THRESH) {
interpolLow = 0.0f;
}
float interpolHi = mArcLengthTable[index].u;
u = interpolLow + frac * (interpolHi - interpolLow);
patchNum = mArcLengthTable[index].patchNumber;
found = true;
} else {
indexHi = index - 1;
}
}
}
if (mArcLengthTable[index].arcLength < length) {
if (static_cast<unsigned int>(index + 1) <
mArcLengthTable.size()) { // bounds check
if (mArcLengthTable[index + 1].arcLength > length) {
float frac = (length - mArcLengthTable[index].arcLength) /
(mArcLengthTable[index + 1].arcLength -
mArcLengthTable[index].arcLength);
float interpolLow = mArcLengthTable[index].u;
// if(interpolLow >= 1.0f){
if (interpolLow >= 1.0f - THRESH &&
interpolLow <= 1.0f + THRESH) {
interpolLow = 0.0f;
}
float interpolHi = mArcLengthTable[index + 1].u;
u = interpolLow + frac * (interpolHi - interpolLow);
patchNum = mArcLengthTable[index + 1].patchNumber;
found = true;
} else {
indexLow = index + 1;
}
}
}
}
}
void HermiteSpline::calculateFrenetFrame(float u, HermiteSplineControlPoint* c0,
......@@ -89,6 +209,20 @@ void HermiteSpline::calculateFrenetFrame(float u, HermiteSplineControlPoint* c0,
// Funktionsargumenten gespeichert werden. Tipp: Sie brauchen noch die
// zweite Ableitung einer Bezier-Kurve ...
//********************************************************************************
std::vector<glm::vec3> bez;
bez.push_back(c0->getPosition());
bez.push_back(c0->getPosition() + ONE_THIRD * c0->getDirection());
bez.push_back(c1->getPosition() - ONE_THIRD * c1->getDirection());
bez.push_back(c1->getPosition());
glm::vec3 point;
deCasteljau(u, bez, point, tangent);
glm::vec3 secDer = 6.0f * (1.0f - u) * (bez[2] - 2.0f * bez[1] + bez[0]) +
6.0f * u * (bez[3] - 2.0f * bez[2] + bez[1]);
binormal = glm::normalize(glm::cross(tangent, secDer));
normal = glm::cross(binormal, tangent);
}
} // End namespace CVK
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