From: Peter Schaefer Date: Fri, 3 Apr 2015 13:38:11 +0000 (+0200) Subject: redesigned Model & Shader X-Git-Url: https://git.leopard-lacewing.eu/?a=commitdiff_plain;h=31d6a461374a137144a9bf7af9ab4e98e6c9cd5a;p=cgue_weave.git redesigned Model & Shader error! Assimp defekt? --- diff --git a/Weave/Fps.h b/Weave/Fps.h index 8c610c0..fdc805a 100644 --- a/Weave/Fps.h +++ b/Weave/Fps.h @@ -6,7 +6,7 @@ class Fps { public: - Fps(const float=0.6f); + Fps(const float=0.3f); ~Fps(); /* FrameZeit in Millisekunden */ diff --git a/Weave/Game.cpp b/Weave/Game.cpp index 0b9d3be..845e115 100644 --- a/Weave/Game.cpp +++ b/Weave/Game.cpp @@ -16,7 +16,7 @@ #include "Message.h" -#include "PlayerI.h" +//#include "PlayerI.h" #include #include @@ -29,33 +29,30 @@ using std::endl; using std::string; -Game::Game() +Game::Game() : playing(true) { srand((int)time(NULL)); // Hauptfenster Scene* tmp_Scene = new Scene(0, 0, Graphix::getWindowWidth(), Graphix::getWindowHeight(), 60, 0.1f, 100, vec3(0.0f, 0.0f, -4.0f)); layer[0] = tmp_Scene; - //Minimap - layer[1] = new Scene(tmp_Scene, Graphix::getWindowWidth() - 200, 0, 200, 100, 60, 0.1f, 100, vec3(0.0f, 0.0f, -8.0f)); - //Allg Shader - Shader* shader1 = new Shader("../shader/basicTexture_VS.hlsl", "../shader/lightingTexture_FS.hlsl"); + Shader* shader1 = new Shader("basicTexture_VS.hlsl", "lightingTexture_FS.hlsl"); //Player - SceneObject* tmp_playerObject = new SceneObject(shader1, glm::mat4(1.0f), "../models/sphere/sphere.dae", "../Textures/simple_trans.png"); + SceneObject* tmp_playerObject = new SceneObject(shader1, glm::mat4(1.0f), "sphere/sphere.dae", "simple_trans.png"); - tmp_playerObject->setIntelligenz(new PlayerI(tmp_playerObject)); + //tmp_playerObject->setIntelligenz(new PlayerI(tmp_playerObject)); tmp_Scene->addObject(tmp_playerObject); tmp_Scene->setLookAt(tmp_playerObject); //player1 = Player(tmp_playerObject); // World - SceneObject* tmp_world = new SceneObject(shader1, scale(vec3(15.f)), "../models/sphere/sphere.dae", "../Textures/sky_withstars.png",1.f,1.f,1.f,vec3(0.f),nullptr,true); + SceneObject* tmp_world = new SceneObject(shader1, scale(vec3(15.f)), "sphere/sphere.dae", "sky_withstars.png",1.f,1.f,1.f,vec3(0.f)); tmp_world->setCollision(false); - SceneObject::setTextureDanger("../Textures/enemy.png"); - SceneObject::setTextureSave("../Textures/enemy_save.png"); + SceneObject::setTextureDanger("enemy.png"); + SceneObject::setTextureSave("enemy_save.png"); tmp_Scene->addObject(tmp_world); @@ -73,7 +70,7 @@ Game::Game() --i; continue; } - tmp_enemy = new SceneObject(shader1, translate(pos), "../models/sphere/sphere.dae", "", (float)(rand() % 10 + 2) / 10.f); + tmp_enemy = new SceneObject(shader1, translate(pos), "sphere/sphere.dae", "", (float)(rand() % 10 + 2) / 10.f); tmp_enemy->setEnemy(true); tmp_Scene->addObject(tmp_enemy); //cout << ((float)(rand() % (size*steps * 2)) / steps - size) << ((float)(rand() % (size*steps * 2)) / steps - size) << ((float)(rand() % (size*steps * 2)) / steps - size) << endl; @@ -101,14 +98,11 @@ void Game::play() while (!Events::quit){ fps.step(); - //cout << "fps:" << fps.getAvFPS(); - sleep_time += (int)(1000./ FRAMERATE_FIX - fps.getTicks()); if (sleep_time < 0) sleep_time = 0; - //cout << " stime:" << sleep_time << endl; Sleep(sleep_time); Events::processEvents(); @@ -149,17 +143,25 @@ void Game::play() void Game::update(float deltaT) { + if (playing){ + for (auto i = layer.begin(); i != layer.end(); ++i){ + (*i).second->update(deltaT); + } - for (auto i = layer.begin(); i != layer.end(); ++i){ - (*i).second->update(deltaT); - } + // l_interface->update(deltaT); + }else + l_menu->update(deltaT); } void Game::draw() const { - - for (auto i = layer.begin(); i != layer.end(); ++i){ - (*i).second->draw(); + if (playing){ + for (auto i = layer.begin(); i != layer.end(); ++i){ + (*i).second->draw(); + } + // l_interface->draw(); } + else + l_menu->draw(); } diff --git a/Weave/Game.h b/Weave/Game.h index 4508931..1378706 100644 --- a/Weave/Game.h +++ b/Weave/Game.h @@ -25,6 +25,10 @@ public: private: + bool playing; + + Drawable *l_interface, *l_menu; + map layer; //Player player1; diff --git a/Weave/Graphix/Act.cpp b/Weave/Graphix/Act.cpp deleted file mode 100644 index 13b49ad..0000000 --- a/Weave/Graphix/Act.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "Act.h" -#include "Performer.h" - -#include - -using std::cout; -using std::endl; - -Act::Act(Performer* _performer, mat4 _model) : modelMatrix(_model), performer(_performer), boxPos(vec3(0)) -{ - -} - -Act::~Act() -{ - cout << "NICE!" << endl; -} - - -mat4 Act::getModelMatrix() const -{ - return modelMatrix; -} - -void Act::draw() const -{ - performer->draw(modelMatrix, -1); -} - -void Act::updateBoxPos(const mat4& _perspective) -{ - boxPos = (vec3)(_perspective*modelMatrix)[4]; -} -vec3 Act::getBoxPos() const -{ - return boxPos; -} \ No newline at end of file diff --git a/Weave/Graphix/Act.h b/Weave/Graphix/Act.h deleted file mode 100644 index 614a688..0000000 --- a/Weave/Graphix/Act.h +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once - -#include "GLM.h" - -class Performer; - -class Act -{ -public: - Act(Performer*,mat4); - ~Act(); - - //setModelMatrix(); - //setTexture(); - - mat4 getModelMatrix() const; - - void updateBoxPos(const mat4& _perspective); - vec3 Act::getBoxPos() const; - - void draw() const; - - -private: - Performer* performer; - mat4 modelMatrix; - - vec3 boxPos; - - //Texture -}; - diff --git a/Weave/Graphix/Model.cpp b/Weave/Graphix/Model.cpp index 6a4b31a..fcc705a 100644 --- a/Weave/Graphix/Model.cpp +++ b/Weave/Graphix/Model.cpp @@ -1,9 +1,9 @@ #include "Model.h" #include -#include -#include -#include +//#include +//#include +//#include #include "Shader.h" #include "../Message.h" @@ -11,112 +11,99 @@ typedef unsigned int uint; -Model::Model(const string& _modelpath, Shader* _shader, bool invert) : - shader(_shader), - modelpath(_modelpath), - vao(-1), - numfaces(0), - numvertices(-1) +Model::Model(const string& _modelpath) : + numfaces(-1), + numvertices(-1), + vertexBuffer(-1), + normalBuffer(-1), + uvBuffer(-1), + indexBuffer(-1) { - //falls Model mit Shader schon geladen wurde - auto i = vao_map.find(*shader); - if (i != vao_map.end()) - { - auto j = i->second.find(modelpath); - if (j != i->second.end()) - { - vao = vao_map[*shader][modelpath]; - numvertices = buffer_map[modelpath].numvertices; - numfaces = buffer_map[modelpath].numfaces; - return; - } - } - - { - - uint vertexBuffer = -1, normalBuffer = -1, uvBuffer = -1, indexBuffer = -1; - - //falls model aber noch nicht mit dem Shader geladen wurde - auto i = buffer_map.find(modelpath); - if (i != buffer_map.end()) - { - vertexBuffer = i->second.vertexBuffer; - normalBuffer = i->second.normalBuffer; - uvBuffer = i->second.uvBuffer; - indexBuffer = i->second.indexBuffer; - numvertices = i->second.numvertices; - numfaces = i->second.numfaces; - } - else - { - float *vertex = nullptr, *normals = nullptr, *uvs = nullptr; uint *index = nullptr; - import(modelpath, numvertices,numfaces,vertex,uvs,normals,index,invert); + import(_modelpath, numvertices, numfaces, vertex, uvs, normals, index); genBuffer(vertexBuffer, numvertices * 3 * sizeof(float), (void*)vertex); genBuffer(normalBuffer, numvertices * 3 * sizeof(float), (void*)normals); genBuffer(uvBuffer, numvertices * 2 * sizeof(float), (void*)uvs); genBuffer(indexBuffer, numfaces * 3 * sizeof(uint), (void*)index); - buffer_map[modelpath].vertexBuffer = vertexBuffer; - buffer_map[modelpath].normalBuffer = normalBuffer; - buffer_map[modelpath].uvBuffer = uvBuffer; - buffer_map[modelpath].indexBuffer = indexBuffer; - buffer_map[modelpath].numfaces = numfaces; - buffer_map[modelpath].numvertices = numvertices; - delete vertex, normals, uvs, index; - } - - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); - - bindBuffer(vertexBuffer, "position"); - bindBuffer(uvBuffer, "uv", 2); - bindBuffer(normalBuffer, "normal"); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); - - glBindVertexArray(0); - - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - - vao_map[*shader][modelpath] = vao; - } } Model::~Model() { - /* + for (auto i = shader_map.begin(); i != shader_map.end(); ++i){ + glDeleteVertexArrays(1, &(i->second)); + } glDeleteBuffers(1, &vertexBuffer); glDeleteBuffers(1, &normalBuffer); glDeleteBuffers(1, &uvBuffer); glDeleteBuffers(1, &indexBuffer); - glDeleteVertexArrays(1, &vao); - */ + + } -void Model::useModel() const +void Model::useModel(Shader* _shader) { + uint vao=-1; + auto i = shader_map.find(*_shader); + if (i == shader_map.end()) + vao = bindShader(_shader); + else + vao = i->second; + glBindVertexArray(vao); + _shader->useShader(); + +} + +void Model::useModel(Shader* _shader) const +{ + uint vao = -1; + auto i = shader_map.find(*_shader); + if (i == shader_map.end()) + Message::error("The Shader wasn't bind to the Model.\n"); + else + vao = i->second; + + glBindVertexArray(vao); + _shader->useShader(); - shader->useShader(); } void Model::drawModel() const { glDrawElements(GL_TRIANGLES, numfaces * 3, GL_UNSIGNED_INT, 0); - glBindVertexArray(0); } +unsigned int Model::bindShader(Shader* _shader) +{ + uint vao; + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + bindBuffer(vertexBuffer, _shader->getAttribLocation("position")); + bindBuffer(uvBuffer, _shader->getAttribLocation("uv"), 2); + bindBuffer(normalBuffer, _shader->getAttribLocation("normal")); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); + + glBindVertexArray(0); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + + shader_map[*_shader] = vao; + + return vao; +} void Model::genBuffer(uint &buffer, uint size, void* value) { @@ -126,106 +113,80 @@ void Model::genBuffer(uint &buffer, uint size, void* value) glBindBuffer(GL_ARRAY_BUFFER, 0); } -void Model::bindBuffer(uint &buffer, string name, uint dim) +void Model::bindBuffer(const uint &buffer,const uint index,const uint dim) { - GLint Index = glGetAttribLocation(*shader, name.c_str()); - if (Index >= 0) - { glBindBuffer(GL_ARRAY_BUFFER, buffer); - glEnableVertexAttribArray(Index); - glVertexAttribPointer(Index, dim, GL_FLOAT, GL_FALSE, 0, 0); + glEnableVertexAttribArray(index); + glVertexAttribPointer(index, dim, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); - } - else{ - Message::info("GL_GetAttLoc: Kann '" + name + "' nicht finden."); - } } -bool Model::import(const string& path,uint& numvertices, uint& numfaces, float*& vertex, float*& uvs, float*& normals, uint*& index, bool invert) +bool Model::import(const string& path,uint& numvertices, uint& numfaces, float*& vertex, float*& uvs, float*& normals, uint*& index) { - Assimp::Importer importer; - - const aiScene* scene = importer.ReadFile(path, aiProcess_GenUVCoords | aiProcess_Triangulate | aiProcess_JoinIdenticalVertices | aiProcess_SortByPType); - if (!scene) - { - Message::error("The file " + path + " couldn't be read.\n" + importer.GetErrorString()); - } - - if (scene->HasMeshes()) - { - aiMesh* mesh = scene->mMeshes[0]; - numvertices = mesh->mNumVertices; - numfaces = mesh->mNumFaces; - vertex = new float[numvertices * 3]; - uvs = new float[numvertices * 2]; - index = new uint[numfaces * 3]; - normals = new float[numvertices * 3]; - //aiFace* faces = mesh->mFaces; - - - //load vertices from file - for (uint i = 0; i < numvertices; i++) - { - vertex[3 * i] = mesh->mVertices[i].x; - vertex[3 * i + 1] = mesh->mVertices[i].y; - vertex[3 * i + 2] = mesh->mVertices[i].z; - } - - - - //TODO SHOULD BE IVERTED - //load UVs from file - for (uint i = 0; i < numvertices; i++) - { - uvs[2 * i] = mesh->mTextureCoords[0][i].x;//[i]->x; - uvs[2 * i + 1] = mesh->mTextureCoords[0][i].y;//[i]->y; - } - - - //load indices from file - for (uint i = 0; i < numfaces; i++) - { - index[3 * i] = mesh->mFaces[i].mIndices[0]; - index[3 * i + 1] = mesh->mFaces[i].mIndices[1]; - index[3 * i + 2] = mesh->mFaces[i].mIndices[2]; - } - - //TODO SHOULD BE IVERTED - //load normals from file - - if (invert) - { - for (uint i = 0; i < numvertices; i++) - { - normals[3 * i] = -mesh->mNormals[i].x; - normals[3 * i + 1] = -mesh->mNormals[i].y; - normals[3 * i + 2] = -mesh->mNormals[i].z; - } - } - else{ - for (uint i = 0; i < numvertices; i++) - { - normals[3 * i] = mesh->mNormals[i].x; - normals[3 * i + 1] = mesh->mNormals[i].y; - normals[3 * i + 2] = mesh->mNormals[i].z; - } - } - - } - else - { - Message::error("The file " + path + " doesn't contain any nodes."); - } + //Assimp::Importer importer; + + //const aiScene* scene = importer.ReadFile(path, aiProcess_GenUVCoords | aiProcess_Triangulate | aiProcess_JoinIdenticalVertices | aiProcess_SortByPType); + //if (!scene) + //{ + // Message::error("The file " + path + " couldn't be read.\n" + importer.GetErrorString()); + //} + + //if (scene->HasMeshes()) + //{ + // //aiMesh* mesh = scene->mMeshes[0]; + // numvertices = mesh->mNumVertices; + // numfaces = mesh->mNumFaces; + // vertex = new float[numvertices * 3]; + // uvs = new float[numvertices * 2]; + // index = new uint[numfaces * 3]; + // normals = new float[numvertices * 3]; + // //aiFace* faces = mesh->mFaces; + + + // //load vertices from file + // for (uint i = 0; i < numvertices; i++) + // { + // vertex[3 * i] = mesh->mVertices[i].x; + // vertex[3 * i + 1] = mesh->mVertices[i].y; + // vertex[3 * i + 2] = mesh->mVertices[i].z; + // } + + + + // //load UVs from file + // for (uint i = 0; i < numvertices; i++) + // { + // uvs[2 * i] = mesh->mTextureCoords[0][i].x;//[i]->x; + // uvs[2 * i + 1] = mesh->mTextureCoords[0][i].y;//[i]->y; + // } + + + // //load indices from file + // for (uint i = 0; i < numfaces; i++) + // { + // index[3 * i] = mesh->mFaces[i].mIndices[0]; + // index[3 * i + 1] = mesh->mFaces[i].mIndices[1]; + // index[3 * i + 2] = mesh->mFaces[i].mIndices[2]; + // } + + // //TODO SHOULD BE IVERTED + // //load normals from file + + + // for (uint i = 0; i < numvertices; i++) + // { + // normals[3 * i] = mesh->mNormals[i].x; + // normals[3 * i + 1] = mesh->mNormals[i].y; + // normals[3 * i + 2] = mesh->mNormals[i].z; + // } + + //} + //else + //{ + // Message::error("The file " + path + " doesn't contain any nodes."); + //} return true; } - -Shader* Model::getShader() const -{ - return shader; -} - -int2str2int_map Model::vao_map; -str2buffer_map Model::buffer_map; \ No newline at end of file diff --git a/Weave/Graphix/Model.h b/Weave/Graphix/Model.h index 05dd017..9435323 100644 --- a/Weave/Graphix/Model.h +++ b/Weave/Graphix/Model.h @@ -7,46 +7,32 @@ using std::string; using std::unordered_map; //VAO MAP -typedef unordered_map str2int_map; -typedef unordered_map int2str2int_map; - -//BUFFER MAP -typedef struct _myBuffer -{ - unsigned int vertexBuffer = -1, normalBuffer = -1, uvBuffer = -1, indexBuffer = -1, numfaces=0, numvertices=0; -} Buffer; -typedef unordered_map str2buffer_map; - +typedef unordered_map int2int_map; class Shader; - class Model { public: - Model(const string& modelpath,Shader* _shader,bool invert=false); + Model(const string& modelpath); virtual ~Model(); - void useModel() const; + void useModel(Shader* shader); + void useModel(Shader* shader) const; void drawModel() const; + unsigned int bindShader(Shader* shader); - Shader* getShader() const; - -protected: - unsigned int vao; - Shader* shader; - string modelpath; +private: unsigned int numvertices, numfaces; + unsigned int vertexBuffer, normalBuffer, uvBuffer, indexBuffer; -private: + int2int_map shader_map; - bool import(const string& path, unsigned int& numvertices, unsigned int& numfaces, float*& vertex, float*& uvs, float*& normals, unsigned int*& index, bool invert=false); + bool import(const string& path, unsigned int& numvertices, unsigned int& numfaces, float*& vertex, float*& uvs, float*& normals, unsigned int*& index); void genBuffer(unsigned int &buffer, unsigned int size, void* value); - void bindBuffer(unsigned int &buffer, string name, unsigned int dim = 3); - - static int2str2int_map vao_map; - static str2buffer_map buffer_map; + void bindBuffer(const unsigned int &buffer, const unsigned int index,const unsigned int dim = 3); + }; diff --git a/Weave/Graphix/Performer.cpp b/Weave/Graphix/Performer.cpp deleted file mode 100644 index 24d5346..0000000 --- a/Weave/Graphix/Performer.cpp +++ /dev/null @@ -1,303 +0,0 @@ -#include "Performer.h" - -#include "GLM.h" -#include -#include - -#include -#include -#include - - -#include "../Message.h" - -using std::ifstream; -using std::istreambuf_iterator; - -typedef unsigned int uint; - - -Performer::Performer(const string& _shader1, const string& _shader2, const string& _modelpath) -{ - //SHADER ---------------------------------------------------- - initShader(_shader1, _shader2); - - //MODEL ------------------------------------------------------ - initModel(_modelpath); -} - - -Performer::~Performer() -{ - - glDeleteBuffers(1, &vertexBuffer); - glDeleteBuffers(1, &normalBuffer); - glDeleteBuffers(1, &uvBuffer); - glDeleteBuffers(1, &indexBuffer); - glDeleteVertexArrays(1, &vao); - - glDeleteProgram(shader); -} - -void Performer::initShader(const string& _shader1, const string& _shader2) -{ - uint handleS1 = loadShader(_shader1); - uint handleS2 = loadShader(_shader2); - - shader = loadProgram(handleS1, handleS2); - - glDeleteShader(handleS1); - glDeleteShader(handleS2); - - modelID = glGetUniformLocation(shader, "model"); - viewID = glGetUniformLocation(shader, "view"); - projectionID = glGetUniformLocation(shader, "projection"); -} - -void Performer::initModel(const string& _modelpath) -{ - glUseProgram(shader); - - float *vertex = nullptr, *normals = nullptr, *uvs = nullptr; - uint *index = nullptr; - - import(_modelpath, numvertices, numfaces, vertex, uvs, normals, index); - - genBuffer(vertexBuffer, numvertices * 3 * sizeof(float), (void*)vertex); - genBuffer(normalBuffer, numvertices * 3 * sizeof(float), (void*)normals); - genBuffer(uvBuffer, numvertices * 2 * sizeof(float), (void*)uvs); - genBuffer(indexBuffer, numfaces * 3 * sizeof(uint), (void*)index); - - delete vertex, normals, uvs, index; - - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); - - bindBuffer(vertexBuffer, "position"); - bindBuffer(uvBuffer, "uv", 2); - bindBuffer(normalBuffer, "normal"); - - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); - - glBindVertexArray(0); - - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); -} - - -void Performer::upgradePerspective(const mat4& _view, const mat4& _projection) const -{ - glUseProgram(shader); - - glUniformMatrix4fv(viewID, 1, GL_FALSE, value_ptr(_view)); - glUniformMatrix4fv(projectionID, 1, GL_FALSE, value_ptr(_projection)); -} - -void Performer::draw(const mat4& _model, uint _texture) const -{ - bool invert = false; - - glBindVertexArray(vao); - - glUseProgram(shader); - - //other loading - - glUniformMatrix4fv(modelID, 1, GL_FALSE, value_ptr(_model)); - - glUniform1i(glGetUniformLocation(shader, "inv"), invert ? -1 : 1); - - glDrawElements(GL_TRIANGLES, numfaces * 3, GL_UNSIGNED_INT, 0); - - glBindVertexArray(0); - -} - - -void Performer::useShader() const -{ - glUseProgram(shader); -} - -uint Performer::getHandle() const -{ - return shader; -} - - -uint Performer::loadShader(const string& _shaderPath) const -{ - uint handle = -1; - auto type = _shaderPath.substr(_shaderPath.find_last_of('_') + 1); - GLenum shaderType; - if (type == "VS.hlsl"){ - shaderType = GL_VERTEX_SHADER; - } - else if (type == "FS.hlsl"){ - shaderType = GL_FRAGMENT_SHADER; - } - else{ - Message::error((string)"LoadShader: Typ <" + type + (string)"> nicht erkannt."); - } - - ifstream shaderFile(_shaderPath); - - if (shaderFile.good()){ - string code = string(istreambuf_iterator(shaderFile), istreambuf_iterator()); - shaderFile.close(); - - handle = glCreateShader(shaderType); - - if (handle == 0){ - Message::error("LoadShader: Shader konnte nicht erstellt werden."); - } - - auto codePtr = code.c_str(); - glShaderSource(handle, 1, &codePtr, NULL); - glCompileShader(handle); - - GLint succeed; - glGetShaderiv(handle, GL_COMPILE_STATUS, &succeed); - - if (succeed == GL_FALSE || !glIsShader(handle)){ - GLint logSize; - glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &logSize); - auto message = new char[logSize]; - - glGetShaderInfoLog(handle, logSize, NULL, message); - Message::error((string)"LoadShader: Shader konnte nicht kompeliert werden.\n " + message); - } - } - else{ - Message::error((string)"LoadShader: ShaderFile <" + _shaderPath + (string)"> konnte nicht geöffnet werden."); - } - - return handle; -} - - -uint Performer::loadProgram(uint _shader1, uint _shader2) const -{ - uint programHandle; - - programHandle = glCreateProgram(); - - if (programHandle == 0){ - Message::error("LoadProgram: Program konnte nicht erstellt werden."); - } - - glAttachShader(programHandle, _shader1); - glAttachShader(programHandle, _shader2); - - glBindFragDataLocation(programHandle, 0, "fragColor"); - - glLinkProgram(programHandle); - - GLint succeded; - - glGetProgramiv(programHandle, GL_LINK_STATUS, &succeded); - - if (!succeded){ - GLint logSize; - glGetProgramiv(programHandle, GL_INFO_LOG_LENGTH, &logSize); - auto message = new char[logSize]; - - glGetProgramInfoLog(programHandle, logSize, NULL, message); - Message::error((string)"LoadProgram: Program konnte nicht kompeliert werden.\n" + message); - } - return programHandle; -} - -void Performer::genBuffer(uint &buffer, uint size, void* value) const -{ - glGenBuffers(1, &buffer); - glBindBuffer(GL_ARRAY_BUFFER, buffer); - glBufferData(GL_ARRAY_BUFFER, size, value, GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); -} - -void Performer::bindBuffer(uint &buffer, const string& name, uint dim) const -{ - GLint Index = glGetAttribLocation(shader, name.c_str()); - if (Index >= 0) - { - glBindBuffer(GL_ARRAY_BUFFER, buffer); - glEnableVertexAttribArray(Index); - glVertexAttribPointer(Index, dim, GL_FLOAT, GL_FALSE, 0, 0); - glBindBuffer(GL_ARRAY_BUFFER, 0); - } - else{ - Message::info("GL_GetAttLoc: Kann '" + name + "' nicht finden."); - } -} - - - -bool Performer::import(const string& path, uint& numvertices, uint& numfaces, float*& vertex, float*& uvs, float*& normals, uint*& index) const -{ - Assimp::Importer importer; - - const aiScene* scene = importer.ReadFile(path, aiProcess_GenUVCoords | aiProcess_Triangulate | aiProcess_JoinIdenticalVertices | aiProcess_SortByPType); - if (!scene) - { - Message::error("The file " + path + " couldn't be read.\n" + importer.GetErrorString()); - } - - if (scene->HasMeshes()) - { - aiMesh* mesh = scene->mMeshes[0]; - numvertices = mesh->mNumVertices; - numfaces = mesh->mNumFaces; - vertex = new float[numvertices * 3]; - uvs = new float[numvertices * 2]; - index = new uint[numfaces * 3]; - normals = new float[numvertices * 3]; - //aiFace* faces = mesh->mFaces; - - - //load vertices from file - for (uint i = 0; i < numvertices; i++) - { - vertex[3 * i] = mesh->mVertices[i].x; - vertex[3 * i + 1] = mesh->mVertices[i].y; - vertex[3 * i + 2] = mesh->mVertices[i].z; - } - - - - //TODO SHOULD BE IVERTED - //load UVs from file - for (uint i = 0; i < numvertices; i++) - { - uvs[2 * i] = mesh->mTextureCoords[0][i].x;//[i]->x; - uvs[2 * i + 1] = mesh->mTextureCoords[0][i].y;//[i]->y; - } - - - //load indices from file - for (uint i = 0; i < numfaces; i++) - { - index[3 * i] = mesh->mFaces[i].mIndices[0]; - index[3 * i + 1] = mesh->mFaces[i].mIndices[1]; - index[3 * i + 2] = mesh->mFaces[i].mIndices[2]; - } - - //TODO SHOULD BE IVERTED - //load normals from file - for (uint i = 0; i < numvertices; i++) - { - normals[3 * i] = mesh->mNormals[i].x; - normals[3 * i + 1] = mesh->mNormals[i].y; - normals[3 * i + 2] = mesh->mNormals[i].z; - } - - - } - else - { - Message::error("The file " + path + " doesn't contain any nodes."); - } - - return true; -} \ No newline at end of file diff --git a/Weave/Graphix/Performer.h b/Weave/Graphix/Performer.h deleted file mode 100644 index 2f6623c..0000000 --- a/Weave/Graphix/Performer.h +++ /dev/null @@ -1,62 +0,0 @@ -#pragma once - -#include -#include "GLM.h" - -using std::string; - -class Shader; - -class Performer -{ -public: - Performer(const string& _shader1, const string& _shader2, const string& _modelpath); - - virtual ~Performer(); - - - void upgradePerspective(const mat4& _view, const mat4& _projection) const; - void upgradeTexture() const; - void draw(const mat4& _model, unsigned int _texture) const; - //update(deltaT) - - void useShader() const; - unsigned int getHandle() const; - -private: - //Animation - - //SHADER --------------------------------------------------- - unsigned int shader=-1; - - unsigned int loadShader(const string& _shaderPath) const; - unsigned int loadProgram(unsigned int _shader1, unsigned int _shader2) const; - - void initShader(const string& _shader1, const string& _shader2); - - - //MODEL --------------------------------------------------- - unsigned int modelID=-1; - unsigned int vertexBuffer = -1, normalBuffer = -1, uvBuffer = -1, indexBuffer = -1, vao=-1; - unsigned int numvertices=0, numfaces=0; - - bool import(const string& path, unsigned int& numvertices, unsigned int& numfaces, float*& vertex, float*& uvs, float*& normals, unsigned int*& index) const; - void genBuffer(unsigned int &buffer, unsigned int size, void* value) const; - void bindBuffer(unsigned int &buffer, const string&, unsigned int dim = 3) const; - - void initModel(const string& _modelpath); - - //VIEWPERSPECTIVE ----------------------------------------- - unsigned int viewID=-1, projectionID=-1; - -}; - - - -//DEFINE DEFAULT KONSTRUKTORS - -#define PERF_KUH "../shader/basicTexture_VS.hlsl", "../shader/lightingTexture_FS.hlsl","../models/cow/cow.dae" -#define PERF_SPHERE "../shader/basicTexture_VS.hlsl", "../shader/lightingTexture_FS.hlsl","../models/sphere/sphere.dae" -#define PERF_BOX "../shader/basicTexture_VS.hlsl", "../shader/lightingTexture_FS.hlsl","../models/box/box.dae" - -#define PERF_SKY \ No newline at end of file diff --git a/Weave/Graphix/Scene.cpp b/Weave/Graphix/Scene.cpp index 669268c..98e0997 100644 --- a/Weave/Graphix/Scene.cpp +++ b/Weave/Graphix/Scene.cpp @@ -10,8 +10,6 @@ #include "Shader.h" -#include "Setting.h" - /* Scene::Scene() : SceneObjects(new list), @@ -76,6 +74,30 @@ void Scene::update(float deltaT) vec3 pos; //Focus auf Player + + if (lookat->getMatter()<1e-2) + { + Message::error("GameOver"); + } + int move_x = Events::getMouseX(); + int move_y = Events::getMouseY(); + int shoot = Events::getAction1(); + int reset = Events::getAction2(); + + //cout << move_x << " " << move_y << endl; + + if (move_x) + lookat->turn(0.3f *move_x, vec3(0.f, 1.f, 0.f)); + if (move_y) + lookat->turn(0.3f *move_y, vec3(1.f, 0.f, 0.f)); + if (shoot) + { + vec3 direction = lookat->push(vec3(0.f, 0.f, shoot*0.1f)); + //SceneObjects->push_back(self->copy();) + } + if (reset) + lookat->stop(); + // XYAchse um den Player if (Events::getViewX() || Events::getViewY()) { @@ -137,6 +159,7 @@ void Scene::update(float deltaT) if (lookat != NULL) view = translate(view,pos - lookat->getPosition()); + } void Scene::draw() const @@ -146,8 +169,8 @@ void Scene::draw() const for (auto i = ShaderSet->cbegin(); i != ShaderSet->cend(); ++i) { (*i)->useShader(); - glUniformMatrix4fv(glGetUniformLocation((*i)->getHandle(), "projection"), 1, false, value_ptr(projection)); - glUniformMatrix4fv(glGetUniformLocation((*i)->getHandle(), "view"), 1, false, value_ptr(view)); + glUniformMatrix4fv((*i)->getUniformLocation("projection"), 1, false, value_ptr(projection)); + glUniformMatrix4fv((*i)->getUniformLocation("view"), 1, false, value_ptr(view)); } @@ -174,7 +197,7 @@ void Scene::draw() const void Scene::addObject(SceneObject* _add) { SceneObjects->push_back(_add); - ShaderSet->insert(_add->getShader()); +// ShaderSet->insert(_add->getShader()); _add->setMainScene(this); } @@ -193,10 +216,10 @@ list* Scene::getSceneObjects() return SceneObjects; } -void Scene::setProjection(mat4 _projection) -{ - projection = _projection; -} +//void Scene::setProjection(mat4 _projection) +//{ +// projection = _projection; +//} void Scene::setProjection(float fovy, float aspect, float zNear, float zFar) { diff --git a/Weave/Graphix/Scene.h b/Weave/Graphix/Scene.h index e322287..cb4ca4a 100644 --- a/Weave/Graphix/Scene.h +++ b/Weave/Graphix/Scene.h @@ -35,7 +35,7 @@ public: void setViewPort(unsigned int, unsigned int, unsigned int, unsigned int); - void setProjection(mat4 _projection); +// void setProjection(mat4 _projection); void setProjection(float fovy, float aspect, float zNear, float zFar); //void updateProjection(mat4); diff --git a/Weave/Graphix/SceneObject.cpp b/Weave/Graphix/SceneObject.cpp index 8c3b8bf..3835ffa 100644 --- a/Weave/Graphix/SceneObject.cpp +++ b/Weave/Graphix/SceneObject.cpp @@ -5,11 +5,9 @@ #include "../Message.h" #include -#include "../ArtificialI.h" #include -#include "../Intelligenz.h" #include "Shader.h" #include "Scene.h" #include "Texture.h" @@ -23,40 +21,37 @@ using std::string; using std::cout; using std::endl; -SceneObject::SceneObject(Shader* _shader, mat4& _model, string _modelpath, string texturepath, float _matter, float _speed, float _atack, vec3& _direction, Intelligenz* _will, bool _invert) : -Model(_modelpath, _shader, _invert), +SceneObject::SceneObject(Shader* _shader, mat4& _model, string _modelpath, string texturepath, float _matter, float _speed, float _atack, vec3& _direction) : +Model(_modelpath), model(_model), +shader(_shader), speed(_speed), matter(_matter), atack(_atack), spin(1.0f), direction(_direction), -will(_will), matterDiff(0), mainScene(NULL), collision_ignore(false), -invert(_invert), isenemy(false), texture(nullptr) { - modelID = glGetUniformLocation(shader->getHandle(), "model"); + modelID = _shader->getUniformLocation("model"); model = scale(model, vec3(pow(matter,1./3))); if (texturepath!="") texture = new Texture(texturepath); + + bindShader(shader); } SceneObject::~SceneObject() { - //if (will!=NULL) - delete will; // delete texture; } void SceneObject::update(float deltaT) { - if (will!=NULL) - will->update(deltaT); //Direction & Speed model = translate(deltaT * speed * direction)*model; @@ -96,17 +91,17 @@ void SceneObject::update(float deltaT) void SceneObject::draw() const { - useModel(); + useModel(shader); if (texture!=nullptr) { int unit = 0; texture->bind(unit); - glUniform1i(glGetUniformLocation(*shader, "ColorTexture"), unit); + glUniform1i(shader->getUniformLocation("ColorTexture"), unit); } glUniformMatrix4fv(modelID, 1, GL_FALSE, value_ptr(model)); - glUniform1i(glGetUniformLocation(*shader, "inv"), invert ? -1 : 1); +// glUniform1i(shader->getUniformLocation ("inv"), invert ? -1 : 1); drawModel(); } @@ -145,16 +140,16 @@ vec3 SceneObject::push(vec3& _direction, float _matter) float dir_len = VektorAbs(_direction); if (dir_len > 0) { - SceneObject* copy = this->copy(); - mainScene->addObject(copy); - copy->setModel(translate(getPosition() - direct * (float)(1.25*pow(matter*_matter, 1. / 3) + pow(matter, 1. / 3)))); - copy->direction = direct * dir_len / -_matter; - copy->setMatter(_matter*matter*.1); - copy->matterDiff = _matter*matter*.9; - copy->setEnemy(true); + //SceneObject* copy = this->copy(); + //mainScene->addObject(copy); + //copy->setModel(translate(getPosition() - direct * (float)(1.25*pow(matter*_matter, 1. / 3) + pow(matter, 1. / 3)))); + //copy->direction = direct * dir_len / -_matter; + //copy->setMatter(_matter*matter*.1); + //copy->matterDiff = _matter*matter*.9; + //copy->setEnemy(true); - delete copy->texture; - copy->texture = new Texture("../Textures/enemy.png"); + //delete copy->texture; + //copy->texture = new Texture("../Textures/enemy.png"); direction += direct * dir_len; matterDiff -= _matter * matter; @@ -190,25 +185,18 @@ void SceneObject::setCollision(bool _col) } -bool SceneObject::operator==(SceneObject _ref) -{ - return (this == &_ref); -} - -SceneObject* SceneObject::copy() -{ - return new SceneObject(shader, model,modelpath, *texture); -} +//bool SceneObject::operator==(SceneObject _ref) +//{ +// return (this == &_ref); +//} +//SceneObject* SceneObject::copy() +//{ +// return new SceneObject(shader, model, modelpath, *texture); +//} +// - - -void SceneObject::setIntelligenz(Intelligenz* _intelligenz) -{ - will = _intelligenz; -} - float SceneObject::getMatter() { return matter; diff --git a/Weave/Graphix/SceneObject.h b/Weave/Graphix/SceneObject.h index 030010e..aea26ab 100644 --- a/Weave/Graphix/SceneObject.h +++ b/Weave/Graphix/SceneObject.h @@ -10,8 +10,8 @@ using std::string; class Scene; -class Intelligenz; class Texture; +class Shader; //class string; @@ -22,7 +22,7 @@ class SceneObject : public: //SceneObject(Shader* _shader, mat4& model); - SceneObject(Shader* _shader, mat4& model, string modelpath, string texturepath, float matter= 1.f, float speed= 1.f, float atack= 1.f, vec3& direction= vec3(0.f), Intelligenz* _will = NULL,bool invert=false); + SceneObject(Shader* _shader, mat4& model, string modelpath, string texturepath, float matter= 1.f, float speed= 1.f, float atack= 1.f, vec3& direction= vec3(0.f)); virtual ~SceneObject(); virtual void update(float); @@ -40,9 +40,7 @@ public: virtual void setModel(mat4&); virtual void turn(float, vec3&); - virtual bool operator==(SceneObject); - - void setIntelligenz(Intelligenz*); + //virtual bool operator==(SceneObject); void setMainScene(Scene *); @@ -63,23 +61,21 @@ protected: void setMatter(float); - Intelligenz* will; - mat4 model; unsigned int modelID; + Shader* shader; + Scene* mainScene; bool collision_ignore; - virtual SceneObject* copy(); + //virtual SceneObject* copy(); float matter; float speed; float atack; - bool invert; - float matterDiff; vec3 direction; diff --git a/Weave/Graphix/Setting.cpp b/Weave/Graphix/Setting.cpp deleted file mode 100644 index 83336dd..0000000 --- a/Weave/Graphix/Setting.cpp +++ /dev/null @@ -1,101 +0,0 @@ -#include "Setting.h" - -#include "Act.h" -#include "Performer.h" - -#include "GLM.h" - -#include - -using std::list; - -Setting::Setting() :viewMatrix(mat4(1)), projectionMatrix(mat4(1)) -{ - -} - - -Setting::~Setting() -{ - cleanAct(); - //clearPerformers -} - - -void Setting::setView(mat4 _view) -{ - viewMatrix = _view; -} - -void Setting::setProjection(mat4 _projection) -{ - projectionMatrix = _projection; -} - -void Setting::setProjection(float fovy, float aspect, float zNear, float zFar) -{ - projectionMatrix = perspective(fovy, aspect, zNear, zFar); -} - -mat4 const Setting::getViewMatrix() const -{ - return viewMatrix; -} - - -Act* Setting::newAct() -{ - Act* _new = new Act(nullptr,mat4(1)); - // Act in actLIST einhängen - - actList.push_back(_new); - - return _new; -} - -void Setting::deleteAct(Act* _del) -{ - actList.remove(_del); - delete _del; -} - -void Setting::cleanAct() -{ - actList.clear(); -} - -void Setting::draw() -{ - - //update PerformerStuff - for (auto i = performerList.begin(); i == performerList.end(); ++i) - { - i->second->upgradePerspective(viewMatrix, projectionMatrix); - } - - //frustumCULLING - mat4 perspective = projectionMatrix*viewMatrix; - for (auto i = actList.begin(); i == actList.end(); ++i) - { - (*i)->updateBoxPos(perspective); - - } - - //sort ActLIST - actList.sort( - [this](const Act* _act1, const Act* _act2) - { - mat4 tmp = getViewMatrix(); - return dot((vec3)(tmp[4]) - (vec3)(_act1->getModelMatrix()[4]), (vec3)tmp[3]) - > dot((vec3)(tmp[4]) - (vec3)(_act2->getModelMatrix()[4]),(vec3)tmp[3]); - } - ); //sortiere nach Entfernung - - //draw ActLIST - for (auto i = actList.cbegin(); i == actList.cend(); ++i) - { - //filter.... - (*i)->draw(); - } - -} \ No newline at end of file diff --git a/Weave/Graphix/Setting.h b/Weave/Graphix/Setting.h deleted file mode 100644 index ee17664..0000000 --- a/Weave/Graphix/Setting.h +++ /dev/null @@ -1,54 +0,0 @@ -#pragma once - -#include "GLM.h" - -#include -#include -#include - -class Act; -class Performer; -class Texture; - -using std::string; -using std::unordered_map; -using std::list; - -typedef unordered_map id2performer; -typedef unordered_map id2texture; - -class Setting -{ -public: - Setting(); - ~Setting(); - - void setView(mat4 matrix); - void setProjection(mat4 matrix); - void setProjection(float fovy, float aspect, float zNear, float zFar); - - mat4 const getViewMatrix() const; - - //Prepare Setting for Draw - Act* newAct(); - void deleteAct(Act*); - void cleanAct(); - - void draw(); - -private: - - mat4 viewMatrix; - mat4 projectionMatrix; - - //lichtquellen - - //ShaderLIST - id2performer performerList; - - //Texturen - id2texture textureList; - - //ActLIST - list actList; -}; diff --git a/Weave/Graphix/Shader.cpp b/Weave/Graphix/Shader.cpp index 96de52a..dff77f4 100644 --- a/Weave/Graphix/Shader.cpp +++ b/Weave/Graphix/Shader.cpp @@ -6,14 +6,10 @@ using namespace std; -//GLuint loadShader(string& _shaderPath); -//GLuint loadProgram(GLuint _shader1, GLuint _shader2); - - Shader::Shader(string _shader1, string _shader2) { - GLuint handleS1 = loadShader(_shader1); - GLuint handleS2 = loadShader(_shader2); + GLuint handleS1 = loadShader("../shader/"+_shader1); + GLuint handleS2 = loadShader("../shader/"+_shader2); handle = loadProgram(handleS1, handleS2); @@ -34,6 +30,24 @@ void Shader::useShader() const glUseProgram(handle); } +GLuint Shader::getAttribLocation(const string& name) const +{ + GLuint ind = glGetAttribLocation(handle, name.c_str()); + if (ind < 0) + Message::info("GL_GetAttLoc: Kann '" + name + "' nicht finden."); + + return ind; +} + +GLuint Shader::getUniformLocation(const string& name) const +{ + GLuint ind = glGetUniformLocation(handle, name.c_str()); + if (ind < 0) + Message::info("GL_GetUniLoc: Kann '" + name + "' nicht finden."); + + return ind; +} + GLuint Shader::getHandle() const { return handle; diff --git a/Weave/Graphix/Shader.h b/Weave/Graphix/Shader.h index 34d9bdf..3c58030 100644 --- a/Weave/Graphix/Shader.h +++ b/Weave/Graphix/Shader.h @@ -13,6 +13,8 @@ public: void useShader() const; + unsigned int getAttribLocation(const string& name) const; + unsigned int getUniformLocation(const string& name) const; unsigned int getHandle() const; operator unsigned int() const; diff --git a/Weave/Graphix/Texture.cpp b/Weave/Graphix/Texture.cpp index 0c1974d..9b32961 100644 --- a/Weave/Graphix/Texture.cpp +++ b/Weave/Graphix/Texture.cpp @@ -28,7 +28,7 @@ Texture::Texture(const string& _path) : path(_path) system("pause"); exit(-1); } - FIBITMAP* data = FreeImage_Load(format, path.c_str()); + FIBITMAP* data = FreeImage_Load(format, ("../textures/" + path).c_str()); if (!data) { cout << "Couldn't read texture file." << endl; diff --git a/Weave/PlayerI.cpp b/Weave/PlayerI.cpp deleted file mode 100644 index eadd870..0000000 --- a/Weave/PlayerI.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "PlayerI.h" - -#include "Graphix\SceneObject.h" -#include "Events.h" -#include "Message.h" - - -PlayerI::PlayerI(SceneObject* _self) : Intelligenz(_self) -{ -} - - -PlayerI::~PlayerI() -{ -} - - -void PlayerI::update(float deltaT) -{ - if (self->getMatter()<1e-2) - { - Message::error("GameOver"); - } - int move_x = Events::getMoveX(); - int move_y = Events::getMoveY(); - int shoot = Events::getAction1(); - int reset = Events::getAction2(); - - //cout << move_x << " " << move_y << endl; - - if (move_x) - self->turn(0.3f *move_x, vec3(0.f, 1.f, 0.f)); - if (move_y) - self->turn(0.3f *move_y, vec3(1.f, 0.f, 0.f)); - if (shoot) - { - vec3 direction = self->push(vec3(0.f, 0.f, shoot*0.1f)); - //SceneObjects->push_back(self->copy();) - } - if (reset) - self->stop(); -} \ No newline at end of file diff --git a/Weave/PlayerI.h b/Weave/PlayerI.h deleted file mode 100644 index b77f6b6..0000000 --- a/Weave/PlayerI.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -class PlayerI : - public Intelligenz -{ -public: - PlayerI(SceneObject*); - ~PlayerI(); - - void update(float); - - SceneObject* self; - list* SceneObjects; - -}; - diff --git a/Weave/Weave.vcxproj b/Weave/Weave.vcxproj index 7cd19c7..58525c7 100644 --- a/Weave/Weave.vcxproj +++ b/Weave/Weave.vcxproj @@ -88,8 +88,16 @@ + + + + + + + + @@ -97,8 +105,16 @@ + + + + + + + + diff --git a/Weave/Weave.vcxproj.filters b/Weave/Weave.vcxproj.filters index 3268ef8..47bc040 100644 --- a/Weave/Weave.vcxproj.filters +++ b/Weave/Weave.vcxproj.filters @@ -33,6 +33,30 @@ Source Files + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + @@ -53,5 +77,29 @@ Header Files + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + \ No newline at end of file diff --git a/Weave/main.cpp b/Weave/main.cpp index 8797643..76cd9e7 100644 --- a/Weave/main.cpp +++ b/Weave/main.cpp @@ -23,7 +23,7 @@ //#include "Fps.h" -//#include "Game.h" +#include "Game.h" //using std::cout; //using std::endl; @@ -43,7 +43,7 @@ int main(int argc, char *argv[]) { Graphix::init(); -// Game spiel1; + Game spiel1; // spiel1.play(); diff --git a/bin/assimp.dll b/bin/assimp.dll index cdaec66..ccb9121 100644 Binary files a/bin/assimp.dll and b/bin/assimp.dll differ diff --git a/lib/x64/SDL2.dll b/lib/x64/SDL2.dll deleted file mode 100644 index a65b22c..0000000 Binary files a/lib/x64/SDL2.dll and /dev/null differ diff --git a/lib/x86/SDL2.dll b/lib/x86/SDL2.dll deleted file mode 100644 index 18d707f..0000000 Binary files a/lib/x86/SDL2.dll and /dev/null differ