Commit 056f48bd authored by Bastian Krayer's avatar Bastian Krayer

ex3

parent 012494fd
cmake_minimum_required(VERSION 2.8)
include(${CMAKE_MODULE_PATH}/DefaultExecutable.cmake)
\ No newline at end of file
#include "ShaderSkybox.h"
ShaderSkybox::ShaderSkybox(GLuint shader_mask, const char** shaderPaths)
: CVK::ShaderMinimal(shader_mask, shaderPaths) {
m_colorTextureID = glGetUniformLocation(m_ProgramID, "colortexture");
}
void ShaderSkybox::update() {
CVK::ShaderMinimal::update();
glUniform1i(m_colorTextureID, 0);
glActiveTexture(GL_TEXTURE0);
}
ShaderSkybox::ShaderSkybox(GLuint shader_mask,
const std::vector<std::string>& shaderNames)
: ShaderMinimal(shader_mask, shaderNames) {
m_colorTextureID = glGetUniformLocation(m_ProgramID, "colortexture");
}
#ifndef __SHADER_SKYBOX_H
#define __SHADER_SKYBOX_H
#include <CVK_2/CVK_Framework.h>
class ShaderSkybox : public CVK::ShaderMinimal {
public:
ShaderSkybox(GLuint shader_mask, const char** shaderPaths);
ShaderSkybox(GLuint shader_mask,
const std::vector<std::string>& shaderNames);
virtual void update();
private:
GLuint m_colorTextureID;
};
#endif /*__SHADER_SIMPLE_TEXTURE_H*/
\ No newline at end of file
#include "Skybox.h"
Skybox::Skybox(float size) {
GLfloat vertices[] = {
// Front face
-size,
-size,
-size,
size,
-size,
-size,
size,
size,
-size,
size,
size,
-size,
-size,
size,
-size,
-size,
-size,
-size,
// Right face
size,
-size,
-size,
size,
-size,
size,
size,
size,
size,
size,
size,
size,
size,
size,
-size,
size,
-size,
-size,
// Back face
size,
-size,
size,
-size,
-size,
size,
-size,
size,
size,
-size,
size,
size,
size,
size,
size,
size,
-size,
size,
// Left face
-size,
-size,
size,
-size,
-size,
-size,
-size,
size,
-size,
-size,
size,
-size,
-size,
size,
size,
-size,
-size,
size,
// Bottom face
-size,
-size,
size,
size,
-size,
size,
size,
-size,
-size,
size,
-size,
-size,
-size,
-size,
-size,
-size,
-size,
size,
// Top Face
-size,
size,
-size,
size,
size,
-size,
size,
size,
size,
size,
size,
size,
-size,
size,
size,
-size,
size,
-size,
};
GLfloat uvCoordinates[] = {
// Front face
1.0f / 4.0f, 1.0f / 3.0f, 2.0f / 4.0f, 1.0f / 3.0f, 2.0f / 4.0f,
2.0f / 3.0f, 2.0f / 4.0f, 2.0f / 3.0f, 1.0f / 4.0f, 2.0f / 3.0f,
1.0f / 4.0f, 1.0f / 3.0f,
// Right face
2.0f / 4.0f, 1.0f / 3.0f, 3.0f / 4.0f, 1.0f / 3.0f, 3.0f / 4.0f,
2.0f / 3.0f, 3.0f / 4.0f, 2.0f / 3.0f, 2.0f / 4.0f, 2.0f / 3.0f,
2.0f / 4.0f, 1.0f / 3.0f,
// Back face
3.0f / 4.0f, 1.0f / 3.0f, 1.0f, 1.0f / 3.0f, 1.0f, 2.0f / 3.0f, 1.0f,
2.0f / 3.0f, 3.0f / 4.0f, 2.0f / 3.0f, 3.0f / 4.0f, 1.0f / 3.0f,
// Left face
0.0f, 1.0f / 3.0f, 1.0f / 4.0f, 1.0f / 3.0f, 1.0f / 4.0f, 2.0f / 3.0f,
1.0f / 4.0f, 2.0f / 3.0f, 0.0f, 2.0f / 3.0f, 0.0f, 1.0f / 3.0f,
// Bottom face
1.0f / 4.0f, 0.0f, 2.0f / 4.0f, 0.0f, 2.0f / 4.0f, 1.0f / 3.0f,
2.0f / 4.0f, 1.0f / 3.0f, 1.0f / 4.0f, 1.0f / 3.0f, 1.0f / 4.0f, 0.0f,
// Top face
1.0f / 4.0f, 2.0f / 3.0f, 2.0f / 4.0f, 2.0f / 3.0f, 2.0f / 4.0f, 1.0f,
2.0f / 4.0f, 1.0f, 1.0f / 4.0f, 1.0f, 1.0f / 4.0f, 2.0f / 3.0f};
glGenVertexArrays(1, &m_vaoHandle);
glBindVertexArray(m_vaoHandle);
// we generate multiple buffers at a time
glGenBuffers(2, m_vboHandles);
// bind the first buffer and store geometry into the VBO
glBindBuffer(GL_ARRAY_BUFFER, m_vboHandles[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
// uses the second buffer as the source for uv coordinates
glBindBuffer(GL_ARRAY_BUFFER, m_vboHandles[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(uvCoordinates), uvCoordinates,
GL_STATIC_DRAW);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
// unbind the VBO, we don't need it anymore
glBindBuffer(GL_ARRAY_BUFFER, 0);
// load a skybox texture
m_texture = new CVK::Texture(CVK::State::getInstance()->getResourcesPath() +
"/AS/skybox_space.png");
}
Skybox::~Skybox() {
// cleanup vao, vbos and texture
glDeleteVertexArrays(1, &m_vaoHandle);
glDeleteBuffers(2, &m_vboHandles[0]);
delete m_texture;
}
void Skybox::render() {
// bind the ID texture specified by the 2nd parameter
m_texture->bind();
glBindVertexArray(m_vaoHandle);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
\ No newline at end of file
#ifndef _SKYBOX_H
#define _SKYBOX_H
#include <CVK_2/CVK_Framework.h>
#include <CVK_2/CVK_Texture.h>
class Skybox {
public:
/**
* Constructor
*/
Skybox(float size);
/**
* Destructor
*/
~Skybox();
/**
* Render the skybox.
*/
void render();
inline GLint getTextureHandle() { return m_textureHandle; }
private:
CVK::Texture* m_texture;
GLuint m_vaoHandle{}; // vao
GLuint m_vboHandles[2]{}; // vbo handels (vertices and uv coordinates)
GLuint m_textureHandle{}; // texture handle
};
#endif
\ 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_MassPoint.h>
#include <CVK_AnSim/CVK_AS_ShaderSpace.h>
#include "ShaderSkybox.h"
#include "Skybox.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,
500.f);
CVK::Trackball* cam_trackball;
//*************************************************************************************************************
// space ship
CVK::MassPoint spaceShipMassPoint;
float spaceShipEngineForce = 10.0f;
float spaceShipRotAngle = 0.0f; // angle in degree
// pigs
std::vector<CVK::MassPoint> pigsMassPoints;
// fish
std::vector<glm::quat> quaternions;
//*************************************************************************************************************
void keyCallback(GLFWwindow* window, int key, int scancode, int action,
int mods) {
if (action == GLFW_RELEASE)
return;
switch (key) {
case GLFW_KEY_ESCAPE:
glfwSetWindowShouldClose(window, GL_TRUE);
break;
case GLFW_KEY_S:
spaceShipMassPoint.applyForce(
spaceShipEngineForce *
glm::vec3(glm::cos(glm::radians(spaceShipRotAngle)), 0.0f,
-glm::sin(glm::radians(spaceShipRotAngle))));
break;
case GLFW_KEY_LEFT:
spaceShipRotAngle += 10;
break;
case GLFW_KEY_RIGHT:
spaceShipRotAngle -= 10;
break;
}
}
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);
}
int main() {
// Init GLFW and glad
glfwInit();
CVK::useOpenGL33CoreProfile();
window = glfwCreateWindow(width, height, "AnSim Exercise 3", 0, 0);
glfwSetWindowPos(window, 100, 50);
glfwMakeContextCurrent(window);
glfwSetWindowSizeCallback(window, resizeCallback);
glfwSetKeyCallback(window, keyCallback);
if (!gladLoadGL()) {
printf("Something went wrong!\n");
exit(-1);
}
auto cvkState = CVK::State::getInstance();
cvkState->setBackgroundColor(WHITE);
glm::vec3 BgCol = cvkState->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::ShaderSpace spaceShader(
VERTEX_SHADER_BIT | FRAGMENT_SHADER_BIT,
{cvkState->getShaderPath() + "/CVK_AnSim/Toon.vert",
cvkState->getShaderPath() + "/CVK_AnSim/Toon.frag"});
ShaderSkybox skyboxShader(
VERTEX_SHADER_BIT | FRAGMENT_SHADER_BIT,
{cvkState->getShaderPath() + "/CVK_AnSim/Minimal.vert",
cvkState->getShaderPath() + "/CVK_AnSim/SimpleTexture.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);
cvkState->addLight(&light);
cvkState->updateSceneSettings(DARKGREY, 0, WHITE, 1, 10, 1);
// Create skybox
Skybox* skybox = new Skybox(50.0f);
// Init scene nodes and mass points
CVK::Node spaceship("Spaceship",
cvkState->getResourcesPath() + "/AS/spaceship.obj");
CVK::Node fish("Fish", cvkState->getResourcesPath() + "/AS/fish.obj");
CVK::Node pig("Pig", cvkState->getResourcesPath() + "/AS/piggy.obj");
// First mass point for the spaceship
spaceShipMassPoint = CVK::MassPoint(glm::vec3(0.0f, 2.3f, 0.0f),
glm::vec3(0.0f, 0.0f, 0.0f), 1.0);
// Next ones for the pigs
pigsMassPoints.push_back(CVK::MassPoint(
glm::vec3(-2.0f, 2.3f, 2.0f), glm::vec3(-0.3f, 0.0f, -0.23f), 1.0));
pigsMassPoints.push_back(CVK::MassPoint(glm::vec3(2.0f, 2.3f, -2.0f),
glm::vec3(0.0f, 0.0f, 0.4f), 2.0));
pigsMassPoints.push_back(CVK::MassPoint(
glm::vec3(4.0f, 2.3f, 3.0f), glm::vec3(-0.23f, 0.0f, -0.1f), 3.0));
pigsMassPoints.push_back(CVK::MassPoint(
glm::vec3(-5.0f, 0.0f, 6.0f), glm::vec3(-0.3f, 0.0f, -0.4f), 1.0));
pigsMassPoints.push_back(CVK::MassPoint(glm::vec3(5.0f, 2.3f, -3.0f),
glm::vec3(0.2f, 0.0f, 0.2f), 2.0));
pigsMassPoints.push_back(CVK::MassPoint(
glm::vec3(4.0f, 4.3f, 2.5f), glm::vec3(-0.4f, 0.0f, -0.2f), 3.0));
// TODO 4 (b)
// Fuellen Sie den global definierten Vektor (quaternions) mit den
// gewünschten Orientierungen.
float deltaTime = 0.0f;
float oldTime = glfwGetTime();
float t = 0.0f;
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);
// Use the skybox shader program
cvkState->setShader(&skyboxShader);
cvkState->getShader()->update();
skybox->render();
// Use phong shader to render the scene
cvkState->setShader(&spaceShader);
cvkState->setLight(0, &light);
spaceShader.update();
// Update and render spaceship
spaceShipMassPoint.numericIntegration(deltaTime);
glm::mat4 modelmatrix = glm::mat4(1.0f);
glm::mat4 shipS = glm::scale(glm::mat4(1.0f), glm::vec3(1.5, 1.5, 1.5));
glm::mat4 shipT =
glm::translate(glm::mat4(1.0f), spaceShipMassPoint.getPosition());
glm::mat4 shipR =
glm::rotate(glm::mat4(1.0f), glm::radians(spaceShipRotAngle),
glm::vec3(0.0f, 1.0f, 0.0f));
modelmatrix = shipT * shipS * shipR;
spaceship.setModelMatrix(modelmatrix);
spaceship.render();
// Update and render pigs
for (CVK::MassPoint& mp : pigsMassPoints) {
mp.numericIntegration(deltaTime);
modelmatrix = glm::translate(glm::mat4(1.0f), mp.getPosition());
modelmatrix = glm::scale(modelmatrix, glm::vec3(0.5, 0.5, 0.5));
pig.setModelMatrix(modelmatrix);
pig.render();
}
t += deltaTime;
// TODO 4 (b)
// Erweitern Sie die Modelmatrix um Translation und Rotation.
// Anmerkung: Verwenden Sie die Variable t.
modelmatrix = glm::scale(glm::mat4(1.0f), glm::vec3(0.1f, 0.1f, 0.1f));
fish.setModelMatrix(modelmatrix);
fish.render();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
......@@ -8,17 +8,17 @@ namespace CVK {
class MassPoint {
//---------------CONSTRUCTORS & DESTRUCTOR---------------
public:
MassPoint(void);
MassPoint();
MassPoint(glm::vec3 pos, glm::vec3 vel, float m);
~MassPoint(void);
~MassPoint();
//---------------MEMBER VARIABLES------------------------
private:
glm::vec3 mPosition;
glm::vec3 mVelocity;
float mMass;
glm::vec3 mPosition{};
glm::vec3 mVelocity{};
float mMass = 1.0f;
glm::vec3 mCurrentForce;
glm::vec3 mCurrentForce{};
//---------------MEMBER FUNCTIONS------------------------
public:
......@@ -28,13 +28,13 @@ class MassPoint {
inline void setVelocity(glm::vec3& v) { mVelocity = v; }
inline void setMass(float m) { mMass = m; }
inline glm::vec3 getPosition(void) { return mPosition; }
inline glm::vec3 getVelocity(void) { return mVelocity; }
inline float getMass(void) { return mMass; }
inline glm::vec3 getCurrentForce(void) { return mCurrentForce; }
inline glm::vec3 getPosition() { return mPosition; }
inline glm::vec3 getVelocity() { return mVelocity; }
inline float getMass() { return mMass; }
inline glm::vec3 getCurrentForce() { return mCurrentForce; }
inline void applyForce(glm::vec3 f) { mCurrentForce += f; }
inline void zeroForce(void) { mCurrentForce = glm::vec3(0.0f); }
inline void zeroForce() { mCurrentForce = glm::vec3(0.0f); }
};
} // End namespace CVK
......
......@@ -16,10 +16,10 @@ class ShaderSpace : public ShaderMinimal {
private:
void init();
GLuint m_colorID;
glm::vec3 m_color;
GLuint m_colorID{};
glm::vec3 m_color{};
GLuint m_useColorTexture, m_colorTextureID;
GLuint m_useColorTexture{}, m_colorTextureID{};
};
} // End namespace CVK
......
......@@ -3,7 +3,7 @@
namespace CVK {
MassPoint::MassPoint(void) { mMass = 1.0f; }
MassPoint::MassPoint() = default;
MassPoint::MassPoint(glm::vec3 pos, glm::vec3 vel, float m) {
mPosition = pos;
......@@ -11,23 +11,11 @@ MassPoint::MassPoint(glm::vec3 pos, glm::vec3 vel, float m) {
mMass = m;
}
MassPoint::~MassPoint(void) {}
MassPoint::~MassPoint() = default;
void MassPoint::numericIntegration(float d_t) {
// TODO Aufgabe 4 (a)
// Implementieren Sie das Euler Integrationsverfahren.
// Einfaches Euler Verfahren updated zuerst die Position, dann die
// Geschwindigkeit
mPosition = mPosition + mVelocity * d_t;
// F = ma -> a = F/m
// oft auch Speicherung von 1/m statt m, damit unendliche Massen dargestellt
// werden k�nnen
mVelocity = mVelocity + mCurrentForce / mMass * d_t;
// Kraft wieder auf 0 setzen
zeroForce();
}
} // 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