Commit 94fdf762 authored by Jan Scheffczyk's avatar Jan Scheffczyk
Browse files

Parsing of xml files now working. The export script for Blender was

written in Phython and exports with z-up coordinate System. The Parser has
to convert all vertices and matrices to a OpenGl conform Y-Up coordinate
System.
parent 9218f264
......@@ -23,7 +23,7 @@ file(GLOB HELPER
)
set(SOURCE_FILES main.cpp game/generation/TunnelGeneration.h game/generation/TunnelSegment.h game/gameLoop/Player.h game/StageManager.h game/gameLoop/PlayerCollisionManager.h game/gameLoop/InputHandler.h helper/MovmentTypes.h game/generation/TunnelSegment.cpp game/generation/TunnelMesh.h game/generation/TunnelSegmentParser.h)
set(SOURCE_FILES main.cpp game/generation/TunnelGeneration.h game/generation/TunnelSegment.h game/gameLoop/Player.h game/StageManager.h game/gameLoop/PlayerCollisionManager.h game/gameLoop/InputHandler.h helper/MovmentTypes.h game/generation/TunnelSegment.cpp game/generation/TunnelMesh.h game/generation/TunnelSegmentParser.h helper/GLOBAL_VARS.cpp helper/DrawAxis.h)
add_executable(CP16 ${SOURCE_FILES} ${HELPER} )
......
# Blender MTL File: 'upDown.blend'
# Material Count: 1
newmtl None
Ns 0
Ka 0.000000 0.000000 0.000000
Kd 0.8 0.8 0.8
Ks 0.8 0.8 0.8
d 1
illum 2
<TunnelSegment>
<next>
<vec4><x>3.652747392654419</x><y>0.0</y><z>0.0</z><w>-3.111236095428467</w></vec4>
<vec4><x>0.0</x><y>3.652747392654419</y><z>0.0</z><w>41.79875946044922</w></vec4>
<vec4><x>0.0</x><y>0.0</y><z>3.652747392654419</z><w>-9.059103012084961</w></vec4>
<vec4><x>0.0</x><y>0.0</y><z>0.0</z><w>1.0</w></vec4>
</next>
<start>
<vec4><x>3.652747392654419</x><y>0.0</y><z>0.0</z><w>-3.0623278617858887</w></vec4>
<vec4><x>0.0</x><y>3.652747392654419</y><z>0.0</z><w>-6.913697242736816</w></vec4>
<vec4><x>0.0</x><y>0.0</y><z>3.652747392654419</z><w>4.5981903076171875</w></vec4>
<vec4><x>0.0</x><y>0.0</y><z>0.0</z><w>1.0</w></vec4>
</start>
<plane>
<transform>
<vec4><x>3.652747392654419</x><y>0.0</y><z>0.0</z><w>-3.0623278617858887</w></vec4>
<vec4><x>0.0</x><y>6.3267436027526855</y><z>1.8263741731643677</z><w>-0.5092248916625977</w></vec4>
<vec4><x>0.0</x><y>-3.6527483463287354</y><z>3.1633718013763428</z><w>0.9570140242576599</w></vec4>
<vec4><x>0.0</x><y>0.0</y><z>0.0</z><w>1.0</w></vec4>
</transform>
<vec3><x>-1.0</x><y>-1.0</y><z>0.0</z></vec3>
<vec3><x>1.0</x><y>-1.0</y><z>0.0</z></vec3>
<vec3><x>-1.0</x><y>1.0</y><z>0.0</z></vec3>
<vec3><x>1.0</x><y>1.0</y><z>0.0</z></vec3>
</plane>
<plane>
<transform>
<vec4><x>3.652747392654419</x><y>0.0</y><z>0.0</z><w>-3.0623278617858887</w></vec4>
<vec4><x>0.0</x><y>7.305494785308838</y><z>0.0</z><w>13.099353790283203</w></vec4>
<vec4><x>0.0</x><y>0.0</y><z>3.652747392654419</z><w>-2.628068685531616</w></vec4>
<vec4><x>0.0</x><y>0.0</y><z>0.0</z><w>1.0</w></vec4>
</transform>
<vec3><x>-1.0</x><y>-1.0</y><z>0.0</z></vec3>
<vec3><x>1.0</x><y>-1.0</y><z>0.0</z></vec3>
<vec3><x>-1.0</x><y>1.0</y><z>0.0</z></vec3>
<vec3><x>1.0</x><y>1.0</y><z>0.0</z></vec3>
</plane>
<plane>
<transform>
<vec4><x>3.652747392654419</x><y>0.0</y><z>0.0</z><w>-3.0623278617858887</w></vec4>
<vec4><x>0.0</x><y>7.305494785308838</y><z>0.0</z><w>34.427101135253906</w></vec4>
<vec4><x>0.0</x><y>0.0</y><z>3.652747392654419</z><w>-9.059122085571289</w></vec4>
<vec4><x>0.0</x><y>0.0</y><z>0.0</z><w>1.0</w></vec4>
</transform>
<vec3><x>-1.0</x><y>-1.0</y><z>0.0</z></vec3>
<vec3><x>1.0</x><y>-1.0</y><z>0.0</z></vec3>
<vec3><x>-1.0</x><y>1.0</y><z>0.0</z></vec3>
<vec3><x>1.0</x><y>1.0</y><z>0.0</z></vec3>
</plane>
</TunnelSegment>
\ No newline at end of file
<TunnelSegment>
<next>
<vec4><x>3.652747392654419</x><y>0.0</y><z>0.0</z><w>-3.111236095428467</w></vec4>
<vec4><x>0.0</x><y>3.652747392654419</y><z>0.0</z><w>41.79875946044922</w></vec4>
<vec4><x>0.0</x><y>0.0</y><z>3.652747392654419</z><w>-9.059103012084961</w></vec4>
<vec4><x>0.0</x><y>0.0</y><z>0.0</z><w>1.0</w></vec4>
</next>
<start>
<vec4><x>3.652747392654419</x><y>0.0</y><z>0.0</z><w>-3.0623278617858887</w></vec4>
<vec4><x>0.0</x><y>3.652747392654419</y><z>0.0</z><w>-6.913697242736816</w></vec4>
<vec4><x>0.0</x><y>0.0</y><z>3.652747392654419</z><w>4.5981903076171875</w></vec4>
<vec4><x>0.0</x><y>0.0</y><z>0.0</z><w>1.0</w></vec4>
</start>
<plane>
<transform>
<vec4><x>3.652747392654419</x><y>0.0</y><z>0.0</z><w>-3.0623278617858887</w></vec4>
<vec4><x>0.0</x><y>6.3267436027526855</y><z>1.8263741731643677</z><w>-0.5092248916625977</w></vec4>
<vec4><x>0.0</x><y>-3.6527483463287354</y><z>3.1633718013763428</z><w>0.9570140242576599</w></vec4>
<vec4><x>0.0</x><y>0.0</y><z>0.0</z><w>1.0</w></vec4>
</transform>
<vec3><x>-1.0</x><y>-1.0</y><z>0.0</z></vec3>
<vec3><x>1.0</x><y>-1.0</y><z>0.0</z></vec3>
<vec3><x>-1.0</x><y>1.0</y><z>0.0</z></vec3>
<vec3><x>1.0</x><y>1.0</y><z>0.0</z></vec3>
</plane>
<plane>
<transform>
<vec4><x>3.652747392654419</x><y>0.0</y><z>0.0</z><w>-3.0623278617858887</w></vec4>
<vec4><x>0.0</x><y>7.305494785308838</y><z>0.0</z><w>13.099353790283203</w></vec4>
<vec4><x>0.0</x><y>0.0</y><z>3.652747392654419</z><w>-2.628068685531616</w></vec4>
<vec4><x>0.0</x><y>0.0</y><z>0.0</z><w>1.0</w></vec4>
</transform>
<vec3><x>-1.0</x><y>-1.0</y><z>0.0</z></vec3>
<vec3><x>1.0</x><y>-1.0</y><z>0.0</z></vec3>
<vec3><x>-1.0</x><y>1.0</y><z>0.0</z></vec3>
<vec3><x>1.0</x><y>1.0</y><z>0.0</z></vec3>
</plane>
<plane>
<transform>
<vec4><x>3.652747392654419</x><y>0.0</y><z>0.0</z><w>-3.0623278617858887</w></vec4>
<vec4><x>0.0</x><y>7.305494785308838</y><z>0.0</z><w>34.427101135253906</w></vec4>
<vec4><x>0.0</x><y>0.0</y><z>3.652747392654419</z><w>-9.059122085571289</w></vec4>
<vec4><x>0.0</x><y>0.0</y><z>0.0</z><w>1.0</w></vec4>
</transform>
<vec3><x>-1.0</x><y>-1.0</y><z>0.0</z></vec3>
<vec3><x>1.0</x><y>-1.0</y><z>0.0</z></vec3>
<vec3><x>-1.0</x><y>1.0</y><z>0.0</z></vec3>
<vec3><x>1.0</x><y>1.0</y><z>0.0</z></vec3>
</plane>
</TunnelSegment>
\ No newline at end of file
conversionMatrix = axis_conversion(from_forward='Y', from_up='Z', to_forward='Z', to_up='Y').to_4x4()
\ No newline at end of file
#version 330 core
in vec3 fColor;
out vec4 coler;
void main() {
coler = vec4(fColor,1.0f);
}
......@@ -9,6 +9,7 @@ uniform sampler2D texture_specular1;
void main()
{
color = vec4(texture(texture_diffuse1, TexCoords));
//color = vec4(texture(texture_diffuse1, TexCoords));
color = vec4(0.5,0.5,0.5,1.0);
}
#version 330 core
layout (location = 0) in vec3 pos;
layout (location = 1) in vec3 col;
out vec3 fColor;
uniform mat4 projection;
uniform mat4 view;
void main() {
gl_Position = projection * view * vec4(pos, 1.0f);
fColor = col;
}
......@@ -8,6 +8,7 @@ out vec2 TexCoords;
uniform mat4 projection;
uniform mat4 view;
void main()
{
gl_Position = projection * view * instanceMatrix * vec4(position, 1.0f);
......
......@@ -17,7 +17,7 @@
class PlayerCollisionManager{
std::vector<CollionPlane> planes;
std::vector<CollisionPlane> planes;
// Atrributes of the Player
// the player only needs to know it's Position
......@@ -45,8 +45,8 @@ class PlayerCollisionManager{
public:
PlayerCollisionManager(GLfloat radius){
position = glm::vec3(-5.0f,10.0f,-5.0f);
gravity= glm::vec3(0.0f,2.0f,0.0f);
position = glm::vec3(0.0f,10.0f,0.0f);
gravity= glm::vec3(0.0f,1.5f,0.0f);
velocity= glm::vec3(0.0f,0.0f,0.0f);
force= glm::vec3(0.0f,0.0f,0.0f);
this->radius = radius;
......@@ -56,7 +56,7 @@ public:
}
void add(CollionPlane cp){
void add(CollisionPlane cp){
planes.push_back(cp);
}
......@@ -64,7 +64,7 @@ public:
void doCollsion(){
CollionPlane *cp;
CollisionPlane *cp;
// what will be added to position for the next frame
glm::vec3 additionalForce =(force+(-(gravity*gravity)))*game::DELTATIME;
......@@ -75,10 +75,20 @@ public:
for(size_t i = 0;i<planes.size();i++){
cp = &planes[i];
// order
glm::vec4 tmp = (glm::vec4(nextPos,1.0f)) ;
glm::vec4 nextPosBoxCoords = cp->inverseTransfrom *glm::vec4(nextPos,1.0f);
// std::cout << std::endl << glm::to_string(tmp) << std::endl;
// std::cout << std::endl << glm::to_string(planes[i].inverseTransfrom[0]) << std::endl;
// std::cout << glm::to_string(planes[i].inverseTransfrom[1]) << std::endl;
// std::cout << glm::to_string(planes[i].inverseTransfrom[2]) << std::endl;
// std::cout << glm::to_string(planes[i].inverseTransfrom[3]) << std::endl<<std::endl;
//
glm::vec4 nextPosBoxCoords = (planes[i].inverseTransfrom) * tmp ;
//
// std::cout << std::endl << glm::to_string(nextPosBoxCoords) << std::endl;
if(nextPosBoxCoords.y > radius){
if(nextPosBoxCoords.y > radius*planes[i].inverseTransfrom[1][1]){
continue;
}
......@@ -98,8 +108,19 @@ public:
// small bounce factor
velocity += bounce*negImpact ;
additionalForce+= glm::vec3(0.0f);
// std::cout << "collision with " << i << std::endl;
////
// std::cout << glm::to_string(nextPos) << i << std::endl;
// std::cout << glm::to_string(nextPosBoxCoords) << i << std::endl;
//
//
// std::cout << std::endl << glm::to_string(tmp) << std::endl;
// std::cout << std::endl << glm::to_string(planes[i].inverseTransfrom) << std::endl;
}
......
......@@ -18,6 +18,7 @@
#include <rapidXML/rapidxml.hpp>
#include <rapidXML/rapidxml_print.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "TunnelSegmentParser.h"
TunnelSegment::TunnelSegment(GLchar *path) {
......@@ -104,7 +105,7 @@ void TunnelSegment::processMeshNode(aiNode *node, const aiScene *scene) {
vector.y = mesh->mVertices[i].y;
vector.z = mesh->mVertices[i].z;
std::cout << glm::to_string(vector) << std::endl;
// std::cout << glm::to_string(vector) << std::endl;
}
......@@ -119,6 +120,8 @@ void TunnelSegment::processMeshNode(aiNode *node, const aiScene *scene) {
void TunnelSegment::setup() {
std::cout << glm::to_string(modelMatrices[0]) << std::endl;
for (std::size_t i = 0; i < meshes.size(); i++) {
meshes[i]->setUpModelMatrices(modelMatrices.size(), &modelMatrices[0][0][0]);
}
......@@ -128,16 +131,23 @@ void TunnelSegment::setup() {
void TunnelSegment::createInstance(glm::mat4 transform,PlayerCollisionManager* pcm) {
modelMatrices.push_back(transform);
// create a new Collision Plane instance
// important! (A * B ) ^-1 = B^-1 * A^-1 !
glm::mat4 inverse = glm::inverse(transform);
glm::mat4 inverseTranspose = glm::inverse(glm::transpose(transform));
for (size_t i = 0; i < collisionPlanes.size(); i++) {
inverse = collisionPlanes[i].inverseTransfrom * inverse;
pcm->add(CollionPlane{collisionPlanes[i].normal, collisionPlanes[i].xmin, collisionPlanes[i].xmax,
collisionPlanes[i].zmin, collisionPlanes[i].zmax, inverse});
glm::vec3 norm = glm::normalize(glm::vec3(inverseTranspose * glm::vec4(collisionPlanes[i].normal,0.0f)));
// create a new Collision Plane instance
// important! (A * B )^-1 = B^-1 * A^-1 !
glm::mat4 curInverse = collisionPlanes[i].inverseTransfrom * inverse ;
// std::cout << glm::to_string(norm) <<std::endl;
// std::cout << collisionPlanes[i].xmin << " " << collisionPlanes[i].xmax << " " << collisionPlanes[i].zmin << " " << collisionPlanes[i].zmax << std::endl;
// std::cout << glm::to_string(inverse) <<std::endl;
pcm->add(CollisionPlane{norm, collisionPlanes[i].xmin, collisionPlanes[i].xmax,
collisionPlanes[i].zmin, collisionPlanes[i].zmax, curInverse});
}
......@@ -149,6 +159,7 @@ void TunnelSegment::draw(Shader *shader) {
ASSERT(shader != NULL, "Shader is NULL");
for (std::size_t i = 0; i < meshes.size(); i++) {
// glUniformMatrix4fv(glGetUniformLocation(shader->Program, "model"), 1, GL_FALSE, glm::value_ptr(modelMatrices[i]));
meshes[i]->draw(shader);
}
}
......@@ -309,7 +320,7 @@ void TunnelSegment::parseColFile(std::string path) {
start = TSParser::parseMat4(cur);
std::cout << glm::to_string(start) << std::endl;
// std::cout << glm::to_string(start) << std::endl;
cur = root_node->first_node("next");
......
......@@ -37,7 +37,7 @@ class TunnelSegment{
std::vector<TunnelMesh*> meshes;
std::string directory;
std::vector<Texture> textures_loaded;
std::vector<CollionPlane> collisionPlanes;
std::vector<CollisionPlane> collisionPlanes;
......
......@@ -10,11 +10,73 @@
using namespace rapidxml;
namespace TSParser{
inline glm::vec3 toYup(glm::vec3 in){
GLfloat tmp = in.y;
in.y=in.z;
in.z=tmp;
return in;
}
inline glm::vec4 toYup(glm::vec4 in) {
GLfloat tmp = in.y;
in.y = in.z;
in.z = tmp;
return in;
}
inline glm::mat4 toYup(glm::mat4 in){
glm::mat4 out = in;
// change the inner 3x3 matrix (rotations and scale)
// GLM is COL MAJOR!?!? why?!?
out[0][0] = in[0][0];
out[0][1] = in[0][2];
out[0][2] = in[0][1];
out[1][0] = in[2][0];
out[1][1] = in[2][2];
out[1][2] = in[2][1];
out[2][0] = in[1][0];
out[2][1] = in[1][2];
out[2][2] = in[1][1];
// change the translation part
out[3][0] = in [3][0];
out[3][1] = in [3][2];
out[3][2] = in [3][1];
// not sure about the botton row ...
out[0][3] = in[0][3];
out[1][3] = in[2][3];
out[2][3] = in[1][3];
out[3][3] = in[3][3];
return out;
}
inline glm::vec3 parseVec3(xml_node<>* vec3Node){
xml_node<>* vec = vec3Node->first_node("x");
GLfloat x = ::atof(vec->value());
vec = vec3Node->first_node("y");
GLfloat y = ::atof(vec->value());
......@@ -23,7 +85,7 @@ namespace TSParser{
return glm::vec3(x,y,z);
return toYup(glm::vec3(x,y,z));
}
......@@ -31,6 +93,7 @@ namespace TSParser{
xml_node<>* vec = vec4Node->first_node("x");
GLfloat x = ::atof(vec->value());
vec = vec4Node->first_node("y");
GLfloat y = ::atof(vec->value());
......@@ -40,21 +103,36 @@ namespace TSParser{
vec = vec4Node->first_node("w");
GLfloat w = ::atof(vec->value());
return glm::vec4(x,y,z,w);
return toYup(glm::vec4(x,y,z,w));
}
inline glm::mat4 parseMat4(xml_node<>* mat4Node){
xml_node<>* vec = mat4Node->first_node("vec4");
glm::vec4 t1 = parseVec4(vec);
vec = vec->next_sibling("vec4");
glm::vec4 t2 = parseVec4(vec);
vec = vec->next_sibling("vec4");
glm::vec4 t3 = parseVec4(vec);
vec = vec->next_sibling("vec4");
glm::vec4 t4 = parseVec4(vec);
return glm::mat4(t1,t2,t3,t4);
xml_node<>* entryNode;
glm::mat4 out;
// GLM matrix is COL major!!
for(size_t i =0;i<4;i++){
entryNode = vec->first_node("x");
out[0][i] = ::atof(entryNode->value());
entryNode = vec->first_node("y");
out[1][i] = ::atof(entryNode->value());
entryNode = vec->first_node("z");
out[2][i] = ::atof(entryNode->value());
entryNode = vec->first_node("w");
out[3][i] = ::atof(entryNode->value());
vec = vec->next_sibling("vec4");
}
return toYup(out);
}
......@@ -66,7 +144,7 @@ namespace TSParser{
return std::fmax(std::fmax(c,d),std::fmax(a,b));
}
inline CollionPlane parsePlane(xml_node<>* planeNode){
inline CollisionPlane parsePlane(xml_node<>* planeNode){
glm::mat4 transform = parseMat4(planeNode->first_node("transform"));
glm::vec3 a,b,c,d;
......@@ -96,14 +174,27 @@ namespace TSParser{
glm::mat4 transposeInverse = glm::inverse(glm::transpose(transform));
// calculate the normal in worldspace
glm::vec3 normal = glm::vec3(transposeInverse * glm::vec4(0.0f,1.0f,0.0f,0.0f));
glm::vec3 normal = glm::normalize( glm::vec3(transposeInverse * glm::vec4(0.0f,1.0f,0.0f,0.0f)));
std::cout << glm::to_string(normal) << std::endl;
glm::mat4 inverse = glm::inverse(transform);
return CollionPlane{normal,xmin,xmax,zmin,zmax,inverse};
}
// std::cout << glm::to_string(inverse[0]) << std::endl;
// std::cout << glm::to_string(inverse[1]) << std::endl;
// std::cout << glm::to_string(inverse[2]) << std::endl;
// std::cout << glm::to_string(inverse[3]) << std::endl<<std::endl;
//std::cout << std::endl << " transform " <<glm::to_string(transform) << std::endl;
//std::cout << glm::to_string(transposeInverse) << std::endl << std::endl;
CollisionPlane cp{normal,xmin,xmax,zmin,zmax,inverse};
return cp;
}
}
......
......@@ -11,15 +11,18 @@
#include <glm/glm.hpp>
struct CollionPlane{
// D = -p_0 * n
glm::vec3 normal;
struct CollisionPlane{
// normalized!
glm::vec3 normal;
// GLfloat D;
// for range checks
GLfloat xmin,xmax,zmin,zmax;
glm::mat4 inverseTransfrom;
};
struct CollionBox{
glm::vec3 origin,a,b,c;
};
......
//
// Created by Knork on 19.07.2016.
//
#ifndef CP16_DRAWAXIS_H
#define CP16_DRAWAXIS_H
#include <GL/glew.h>
#include <glm/vec3.hpp>
class DrawAxis{
public:
DrawAxis(){
setupMesh();
}
void draw(){
// draw mesh
glBindVertexArray(this->VAO);
glLineWidth(2.0f);
glDrawArrays(GL_LINES, 0,12 );
glLineWidth(1.0f);
glBindVertexArray(0);
}
private:
GLuint VAO, VBO;
void setupMesh()
{
glm::vec3 vertices[12]={glm::vec3(0.0f,0.0f,0.0f),glm::vec3(0.7f,0.0f,0.0f),
glm::vec3(3.0f,0.0f,0.0f),glm::vec3(0.7f,0.0f,0.0f),
glm::vec3(0.0f,0.0f,0.0f),glm::vec3(0.0f,0.7f,0.0f),
glm::vec3(0.0f,3.0f,0.0f),glm::vec3(0.0f,0.7f,0.0f),
glm::vec3(0.0f,0.0f,0.0f),glm::vec3(0.0f,0.0f,0.7f),
glm::vec3(0.0f,0.0f,3.0f),glm::vec3(0.0f,0.0f,0.7f)};
// Create buffers/arrays
glGenVertexArrays(1, &this->VAO);
glGenBuffers(1, &this->VBO);
glBindVertexArray(this->VAO);
// Load data into vertex buffers
glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
// A great thing about structs is that their memory layout is sequential for all its items.
// The effect is that we can simply pass a pointer to the struct and it translates perfectly to a glm::vec3/2 array which
// again translates to 3/2 floats which translates to a byte array.
glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(glm::vec3), vertices, GL_STATIC_DRAW);
// Set the vertex attribute pointers