From 511a605e872a80725c12c7c98f8a1a4524c80f91 Mon Sep 17 00:00:00 2001 From: Peter Schaefer Date: Wed, 18 Mar 2015 14:08:46 +0100 Subject: [PATCH] added old Project started to enable and rewrite old code --- Weave/Average.h | 66 +++++++ Weave/Events.cpp | 239 ++++++++++++++++++++++++++ Weave/Events.h | 58 +++++++ Weave/Fps.cpp | 59 +++++++ Weave/Fps.h | 33 ++++ Weave/Game.cpp | 165 ++++++++++++++++++ Weave/Game.h | 36 ++++ Weave/Graphix/Act.cpp | 37 ++++ Weave/Graphix/Act.h | 32 ++++ Weave/Graphix/Drawable.cpp | 24 +++ Weave/Graphix/Drawable.h | 15 ++ Weave/Graphix/GLM.cpp | 6 + Weave/Graphix/GLM.h | 24 +++ Weave/Graphix/Graphix.cpp | 116 +++++++++++++ Weave/Graphix/Graphix.h | 48 ++++++ Weave/Graphix/Model.cpp | 231 +++++++++++++++++++++++++ Weave/Graphix/Model.h | 52 ++++++ Weave/Graphix/Performer.cpp | 303 +++++++++++++++++++++++++++++++++ Weave/Graphix/Performer.h | 62 +++++++ Weave/Graphix/Scene.cpp | 220 ++++++++++++++++++++++++ Weave/Graphix/Scene.h | 54 ++++++ Weave/Graphix/SceneObject.cpp | 259 ++++++++++++++++++++++++++++ Weave/Graphix/SceneObject.h | 99 +++++++++++ Weave/Graphix/Setting.cpp | 101 +++++++++++ Weave/Graphix/Setting.h | 54 ++++++ Weave/Graphix/Shader.cpp | 128 ++++++++++++++ Weave/Graphix/Shader.h | 28 +++ Weave/Graphix/Texture.cpp | 81 +++++++++ Weave/Graphix/Texture.h | 30 ++++ Weave/Graphix/ViewPort.cpp | 38 +++++ Weave/Graphix/ViewPort.h | 29 ++++ Weave/Graphix/idNcount.cpp | 33 ++++ Weave/Graphix/idNcount.h | 21 +++ Weave/Message.cpp | 43 +++++ Weave/Message.h | 17 ++ Weave/PlayerI.cpp | 42 +++++ Weave/PlayerI.h | 16 ++ Weave/Weave.vcxproj | 4 + Weave/Weave.vcxproj.filters | 12 ++ Weave/main.cpp | 53 ++++++ shader/basicTexture_FS.hlsl | 14 ++ shader/basicTexture_VS.hlsl | 42 +++++ shader/basic_FS.hlsl | 7 + shader/basic_VS.hlsl | 12 ++ shader/lightingTexture_FS.hlsl | 56 ++++++ shader/perspective_VS.hlsl | 12 ++ 46 files changed, 3111 insertions(+) create mode 100644 Weave/Average.h create mode 100644 Weave/Events.cpp create mode 100644 Weave/Events.h create mode 100644 Weave/Fps.cpp create mode 100644 Weave/Fps.h create mode 100644 Weave/Game.cpp create mode 100644 Weave/Game.h create mode 100644 Weave/Graphix/Act.cpp create mode 100644 Weave/Graphix/Act.h create mode 100644 Weave/Graphix/Drawable.cpp create mode 100644 Weave/Graphix/Drawable.h create mode 100644 Weave/Graphix/GLM.cpp create mode 100644 Weave/Graphix/GLM.h create mode 100644 Weave/Graphix/Graphix.cpp create mode 100644 Weave/Graphix/Graphix.h create mode 100644 Weave/Graphix/Model.cpp create mode 100644 Weave/Graphix/Model.h create mode 100644 Weave/Graphix/Performer.cpp create mode 100644 Weave/Graphix/Performer.h create mode 100644 Weave/Graphix/Scene.cpp create mode 100644 Weave/Graphix/Scene.h create mode 100644 Weave/Graphix/SceneObject.cpp create mode 100644 Weave/Graphix/SceneObject.h create mode 100644 Weave/Graphix/Setting.cpp create mode 100644 Weave/Graphix/Setting.h create mode 100644 Weave/Graphix/Shader.cpp create mode 100644 Weave/Graphix/Shader.h create mode 100644 Weave/Graphix/Texture.cpp create mode 100644 Weave/Graphix/Texture.h create mode 100644 Weave/Graphix/ViewPort.cpp create mode 100644 Weave/Graphix/ViewPort.h create mode 100644 Weave/Graphix/idNcount.cpp create mode 100644 Weave/Graphix/idNcount.h create mode 100644 Weave/Message.cpp create mode 100644 Weave/Message.h create mode 100644 Weave/PlayerI.cpp create mode 100644 Weave/PlayerI.h create mode 100644 Weave/main.cpp create mode 100644 shader/basicTexture_FS.hlsl create mode 100644 shader/basicTexture_VS.hlsl create mode 100644 shader/basic_FS.hlsl create mode 100644 shader/basic_VS.hlsl create mode 100644 shader/lightingTexture_FS.hlsl create mode 100644 shader/perspective_VS.hlsl diff --git a/Weave/Average.h b/Weave/Average.h new file mode 100644 index 0000000..3945f5d --- /dev/null +++ b/Weave/Average.h @@ -0,0 +1,66 @@ +#pragma once + +/* + Definiert eine gemittelte Variable + hierbei muss der Template Datentyp T + und * float unterstützen oder in einen Float konvertierbar sein +*/ + +template +class Average +{ +public: + Average(float const = 1); + Average(float const,T const&); + ~Average(); + + Average& operator=(T const&); + + operator const T&() const; + + void setAlpha(T const&); + +private: + float alpha; + T average; + +}; + + + +template +Average::Average(float const _alpha) : alpha(_alpha), average(0) +{ + +} + +template +Average::Average(float const _alpha, T const& _average) : alpha(_alpha), average(_average) +{ + +} + +template +Average::~Average() +{ +} + +template +Average& Average::operator=(T const& _input) +{ + average = (T)(alpha * _input + (1 - alpha) * average); + return *this; +} + +template +Average::operator const T&() const +{ + return average; +} + +template +void Average::setAlpha(T const& _alpha) +{ + alpha = _alpha; +} + + diff --git a/Weave/Events.cpp b/Weave/Events.cpp new file mode 100644 index 0000000..c6908af --- /dev/null +++ b/Weave/Events.cpp @@ -0,0 +1,239 @@ +#include "Events.h" +#include + +#include "Graphix\Graphix.h" + +#include +using std::cout; +using std::endl; + +int returnSet(int& value, int action2 = 0); + + +void Events::processEvents() +{ + SDL_Event sdl_event; + while (SDL_PollEvent(&sdl_event)){ + //cout << sdl_event.type << endl; + switch (sdl_event.type){ + case SDL_QUIT:{ + quit = true; + break; + } + case SDL_WINDOWEVENT: + { + switch (sdl_event.window.event) + { + case SDL_WINDOWEVENT_FOCUS_GAINED: + { + window_focus = true; + Graphix::catchMouse(); + break; + } + case SDL_WINDOWEVENT_FOCUS_LOST: + { + window_focus = false; + Graphix::freeMouse(); + break; + } + case SDL_WINDOWEVENT_RESIZED: + { + Graphix::setWindowSize(sdl_event.window.data1, sdl_event.window.data2); + break; + } + default: + break; + } + break; + } + case SDL_KEYDOWN:{ + if (sdl_event.key.keysym.sym == SDLK_ESCAPE) + { + quit = true; + } + key_held[sdl_event.key.keysym.sym]=true; + break; + } + case SDL_KEYUP:{ + key_held[sdl_event.key.keysym.sym]=false; + fKey(sdl_event.key.keysym.sym); + break; + } + case SDL_MOUSEMOTION:{ + if (window_focus) + mousemotion(sdl_event.motion); + break; + } + case SDL_MOUSEBUTTONDOWN:{ + if (sdl_event.button.button == SDL_BUTTON_LEFT) + { + action1++; + } + if (sdl_event.button.button == SDL_BUTTON_RIGHT) + { + action2++; + } + break; + } + default: + break; + } + } + keymotion(); +} + + +void Events::mousemotion(SDL_MouseMotionEvent sdl_motion) +{ + //cout << sdl_motion.x << " +++ " << sdl_motion.y << endl; + //if (sdl_motion.x!=400 && sdl_motion.y!=200) + { + + mouse_x = sdl_motion.xrel; + mouse_y = sdl_motion.yrel; + + SDL_WarpMouseInWindow(NULL, Graphix::getWindowWidth() / 2, Graphix::getWindowHeight() / 2); + + //cout << move_x << " --- " << move_y << endl; + } +} + +void Events::fKey(int _key) +{ + switch (_key) + { + case SDLK_F1: + //Help + break; + case SDLK_F2: + //FPS + break; + case SDLK_F3: + //WIREFRAME + break; + case SDLK_F4: + //TEXTURE SAMPLING QUALITY + break; + case SDLK_F5: + //MIPMAP QUALITY + break; + case SDLK_F6: + //??? BLOOM + break; + case SDLK_F7: + //??? + break; + case SDLK_F8: + //VIEW FRUSTUMCULLING + break; + case SDLK_F9: + //TRANSPARENCY + break; + case SDLK_F10: + //??? + break; + default: + break; + } +} + +void Events::keymotion() +{ + axis(view_x, SDLK_w, SDLK_s); + axis(view_y, SDLK_a, SDLK_d); + axis(view_z, SDLK_e, SDLK_q); +} + +void Events::padmotion() +{ + +} + +void Events::axis(int& axis, int keyUP, int keyDOWN) +{ + if (key_held[keyUP]) + { + //cout << "w"; + axis += 1; + } + else if (axis > 0) + { + //cout << "0"; + axis = 0; + } + + if (key_held[keyDOWN]) + { + axis -= 1; + } + else if (axis < 0) + { + axis = 0; + } +} + +int Events::getViewX() +{ + return view_x; +} + +int Events::getViewY() +{ + return view_y; +} + +int Events::getViewZ() +{ + return view_z; +} + +int Events::getMouseX() +{ + return returnSet(mouse_x); +} + +int Events::getMouseY() +{ + return returnSet(mouse_y); +} + +int Events::getAction1() +{ + return returnSet(action1); +} + +int Events::getAction2() +{ + return returnSet(action2); +} + +bool Events::isHeld(int const _key) +{ + return key_held[_key]; +} + +int returnSet(int& value, int action2) +{ + int tmp = value; + value = action2; + return tmp; +} + +map Events::key_held; +map Events::mouse_held; +map Events::pad_held; + +int Events::view_x = 0; +int Events::view_y = 0; +int Events::view_z = 0; + +int Events::mouse_x = 0; +int Events::mouse_y = 0; + +int Events::action1 = 0; +int Events::action2 = 0; + +bool Events::quit = false; + +bool Events::window_focus = true; + diff --git a/Weave/Events.h b/Weave/Events.h new file mode 100644 index 0000000..16d7248 --- /dev/null +++ b/Weave/Events.h @@ -0,0 +1,58 @@ +#pragma once + +#include "SDL2\SDL.h" +#include + +using std::map; + +class Events +{ +public: + + void static processEvents(); + + //virtual void keydown(SDL_Keycode sdl_event); + + bool static quit; + + bool static window_focus; + + int static getViewX(); + int static getViewY(); + int static getViewZ(); + + int static getMouseX(); + int static getMouseY(); + + int static getAction1(); + int static getAction2(); + + bool static isHeld(int const); + +private: + + void static mousemotion(SDL_MouseMotionEvent sdl_motion); + void static keymotion(); + void static padmotion(); + + void static fKey(int); + + void static axis(int&, int, int); + + //const Uint8* keymap; + map static key_held; + map static mouse_held; + map static pad_held; + + int static view_x; + int static view_y; + int static view_z; + + int static mouse_x; + int static mouse_y; + + int static action1; + int static action2; + + +}; diff --git a/Weave/Fps.cpp b/Weave/Fps.cpp new file mode 100644 index 0000000..4091535 --- /dev/null +++ b/Weave/Fps.cpp @@ -0,0 +1,59 @@ +#include "Fps.h" + +#include "SDL2\SDL.h" +#include "Average.h" + +/* + Übernimmt die FPS berechnungen +*/ + +Fps::Fps(const float _alpha) : +lasttime(SDL_GetTicks()), +delta(0), +av_delta(_alpha) +{ +} + +Fps::~Fps() +{ +} + + +Uint32 Fps::getFPS() const +{ + return (Uint32)(1000.f / delta); +} + +Uint32 Fps::getAvFPS() const +{ + return (Uint32)(1000.f / av_delta); +} + +float Fps::getDelta() const +{ + return delta / 1000.f; +} + +float Fps::getAvDelta() const +{ + return av_delta / 1000.f; +} + +Uint32 Fps::getTicks() const +{ + return delta; +} + +Fps::operator float()const +{ + return delta / 1000.f; +} + +void Fps::step() +{ + Uint32 current = SDL_GetTicks(); + delta = current - lasttime; + lasttime = current; + av_delta = delta; +} + diff --git a/Weave/Fps.h b/Weave/Fps.h new file mode 100644 index 0000000..8c610c0 --- /dev/null +++ b/Weave/Fps.h @@ -0,0 +1,33 @@ +#pragma once + +#include "Average.h" +#include + +class Fps +{ +public: + Fps(const float=0.6f); + ~Fps(); + + /* FrameZeit in Millisekunden */ + operator float() const; + + /* FrameZeit in Millisekunden*/ + float getDelta() const; + /* Gewichtete FrameZeit in Millisekunden */ + float getAvDelta() const; + /* Anzahl Frames Pro Sekunde */ + Uint32 getFPS() const; + /* Gewichtete Anzahl Frames Pro Sekunde*/ + Uint32 getAvFPS() const; + /* Anzahl der Ticks pro Frame */ + Uint32 getTicks() const; + + /* Nächster Frame Step */ + void step(); + +private: + Average av_delta; + Uint32 delta; + Uint32 lasttime; +}; diff --git a/Weave/Game.cpp b/Weave/Game.cpp new file mode 100644 index 0000000..0b9d3be --- /dev/null +++ b/Weave/Game.cpp @@ -0,0 +1,165 @@ +#include "Game.h" + +#include + +#include "Graphix\GLM.h" +#include + +#include +#include "Graphix\Scene.h" +#include "Graphix\SceneObject.h" +#include "Graphix\Shader.h" +#include "Graphix\Drawable.h" +#include "Graphix\Graphix.h" +#include "Fps.h" +#include "Events.h" + +#include "Message.h" + +#include "PlayerI.h" + +#include +#include + +#define FRAMERATE_FIX 60 + +using std::cout; +using std::endl; + +using std::string; + + +Game::Game() +{ + 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"); + + //Player + SceneObject* tmp_playerObject = new SceneObject(shader1, glm::mat4(1.0f), "../models/sphere/sphere.dae", "../Textures/simple_trans.png"); + + 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); + tmp_world->setCollision(false); + + SceneObject::setTextureDanger("../Textures/enemy.png"); + SceneObject::setTextureSave("../Textures/enemy_save.png"); + + tmp_Scene->addObject(tmp_world); + + int size = 5; + int steps = 10; + + vec3 pos; + + SceneObject* tmp_enemy; + for (auto i = 0; i < 10;++i) + { + pos = vec3(((float)(rand() % (size*steps * 2)) / steps - size), ((float)(rand() % (size*steps * 2)) / steps - size), ((float)(rand() % (size*steps * 2)) / steps - size)); + if (VektorAbs(pos) < 2) + { + --i; + continue; + } + tmp_enemy = new SceneObject(shader1, translate(pos), "../models/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; + } + //ein Gegner + //tmp_Scene->addObject(new SceneObject(shader1, translate(vec3(1.f, -2.f, 2.f)), "../models/sphere/sphere.dae", "../Textures/game_box.png")); + //tmp_Scene->addObject(new SceneObject(shader1, translate(vec3(-1.f, 2.f, -2.f)), "../models/box/box.dae", "../Textures/sky_withstars.png")); + //tmp_Scene->addObject(new SceneObject(shader1, translate(vec3(1.f, 3.f, -2.f)), "../models/cow/cow.dae", "../models/cow/texture.jpg")); +} + + +Game::~Game() +{ + for (auto i = layer.begin(); i != layer.end(); ++i){ + delete i->second; + } + +} + + +void Game::play() +{ + Fps fps(0.3f); + int sleep_time=0; + 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(); + + update(fps); + + Graphix::clear(); + + draw(); + + Graphix::swap(); + + GLenum error = glGetError(); + + if (error != GL_NO_ERROR) { + switch (error) { + case GL_INVALID_ENUM: + Message::error((string)"GL: enum argument out of range."); + break; + case GL_INVALID_VALUE: + Message::error((string)"GL: Numeric argument out of range."); + break; + case GL_INVALID_OPERATION: + Message::error((string)"GL: Operation illegal in current state."); + break; + case GL_INVALID_FRAMEBUFFER_OPERATION: + Message::error((string)"GL: Framebuffer object is not complete."); + break; + case GL_OUT_OF_MEMORY: + Message::error((string)"GL: Not enough memory left to execute command."); + break; + default: + Message::error((string)"GL: Unknown error."); + } + } + } +} + +void Game::update(float deltaT) +{ + + for (auto i = layer.begin(); i != layer.end(); ++i){ + (*i).second->update(deltaT); + } +} + +void Game::draw() const +{ + + for (auto i = layer.begin(); i != layer.end(); ++i){ + (*i).second->draw(); + } + +} diff --git a/Weave/Game.h b/Weave/Game.h new file mode 100644 index 0000000..4508931 --- /dev/null +++ b/Weave/Game.h @@ -0,0 +1,36 @@ +#pragma once + + +//#include "Graphix\Scene.h" +#include "Graphix\Drawable.h" +//#include "Player.h" + +#include + +using std::map; + + +class Game +{ +public: + Game(); + ~Game(); + + //void init(); + + void play(); + + //void loadLVL(); + + +private: + + map layer; + //Player player1; + + + void update(float); + void draw() const; + +}; + diff --git a/Weave/Graphix/Act.cpp b/Weave/Graphix/Act.cpp new file mode 100644 index 0000000..13b49ad --- /dev/null +++ b/Weave/Graphix/Act.cpp @@ -0,0 +1,37 @@ +#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 new file mode 100644 index 0000000..614a688 --- /dev/null +++ b/Weave/Graphix/Act.h @@ -0,0 +1,32 @@ +#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/Drawable.cpp b/Weave/Graphix/Drawable.cpp new file mode 100644 index 0000000..7fd8457 --- /dev/null +++ b/Weave/Graphix/Drawable.cpp @@ -0,0 +1,24 @@ +#include "Drawable.h" + + +Drawable::Drawable() +{ +} + + +Drawable::~Drawable() +{ +} + + +void Drawable::update(float _deltaT) +{ +} + +void Drawable::draw() const +{ +} + +void Drawable::collisions() +{ +} diff --git a/Weave/Graphix/Drawable.h b/Weave/Graphix/Drawable.h new file mode 100644 index 0000000..c466e25 --- /dev/null +++ b/Weave/Graphix/Drawable.h @@ -0,0 +1,15 @@ +#pragma once + +class Drawable +{ +public: + Drawable(); + virtual ~Drawable(); + + virtual void draw() const; + virtual void update(float); + + virtual void collisions(); + +}; + diff --git a/Weave/Graphix/GLM.cpp b/Weave/Graphix/GLM.cpp new file mode 100644 index 0000000..8ee8974 --- /dev/null +++ b/Weave/Graphix/GLM.cpp @@ -0,0 +1,6 @@ +#include "GLM.h" + +float VektorAbs(const vec3& vek) +{ + return sqrt(vek.x*vek.x + vek.y*vek.y + vek.z*vek.z); +} \ No newline at end of file diff --git a/Weave/Graphix/GLM.h b/Weave/Graphix/GLM.h new file mode 100644 index 0000000..c556038 --- /dev/null +++ b/Weave/Graphix/GLM.h @@ -0,0 +1,24 @@ +#pragma once + +#include "glm\glm.hpp" +#include "glm\gtx\transform.hpp" +#include + +using glm::vec3; +using glm::mat4; +using glm::vec4; + +using glm::translate; +using glm::rotate; +using glm::scale; +using glm::perspective; +using glm::normalize; +using glm::inverse; +using glm::dot; + +using glm::value_ptr; + +using glm::sign; + + +float VektorAbs(const vec3& vek); diff --git a/Weave/Graphix/Graphix.cpp b/Weave/Graphix/Graphix.cpp new file mode 100644 index 0000000..b62781a --- /dev/null +++ b/Weave/Graphix/Graphix.cpp @@ -0,0 +1,116 @@ +#include "Graphix.h" + +#include "../Message.h" +#include + +using std::string; + + +void Graphix::setWindowSize(unsigned int _width, unsigned int _height) +{ + width = _width; + height = _height; +} + +void Graphix::setWindowPos(unsigned int _xpos, unsigned int _ypos) +{ + xpos = _xpos; + ypos = _ypos; +} + +unsigned int Graphix::getWindowHeight() +{ + return height; +} +unsigned int Graphix::getWindowWidth() +{ + return width; +} + +void Graphix::swap() +{ + SDL_GL_SwapWindow(sdl_window); +} + + +void Graphix::init() +{ + + if (SDL_Init(sdlFlags) != 0) + { + Message::error((string)"SDL_Init: " + SDL_GetError()); + } + + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + + sdl_window = SDL_CreateWindow(windowTitle.c_str(), xpos, ypos, width, height, windowFlags); + + if (sdl_window == nullptr) + { + Message::error((string)"SDL_CreateWindow: " + SDL_GetError()); + } + + sdl_glcontext = SDL_GL_CreateContext(sdl_window); + + GLenum err = glewInit(); + if (err != GLEW_OK) + { + Message::error((string)"glewInit: " + (char*)glewGetErrorString(err)); + } + + glEnable(GL_DEPTH_TEST); + + //SDL_SetWindowGrab(sdl_window, SDL_TRUE); + //SDL_ShowCursor(SDL_DISABLE); + glEnable(GL_COLOR_MATERIAL); + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + //glEnable(GL_CULL_FACE); + glShadeModel(GL_SMOOTH); + + //glEnable(GL_BLEND); + //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); +} + +void Graphix::catchMouse() +{ + SDL_SetWindowGrab(sdl_window, SDL_TRUE); + SDL_ShowCursor(SDL_DISABLE); //TODO eventuell nicht nötig +} + +void Graphix::freeMouse() +{ + SDL_SetWindowGrab(sdl_window, SDL_FALSE); + SDL_ShowCursor(SDL_ENABLE); //TODO eventuell nicht nötig +} + +void Graphix::clear() +{ + glClearColor(0, 0, 0.3f, 1); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); +} + +void Graphix::cleanup() +{ + SDL_SetWindowGrab(sdl_window, SDL_FALSE); + + SDL_GL_DeleteContext(sdl_glcontext); + SDL_DestroyWindow(sdl_window); + + SDL_Quit(); +} + +unsigned int Graphix::width = 1024; +unsigned int Graphix::height = 768; + +unsigned int Graphix::xpos = 200; +unsigned int Graphix::ypos = 100; + +string Graphix::windowTitle = "Weave"; + +Uint32 Graphix::windowFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE; +Uint32 Graphix::sdlFlags = SDL_INIT_VIDEO | SDL_INIT_TIMER; + +SDL_Window* Graphix::sdl_window; + +SDL_GLContext Graphix::sdl_glcontext; \ No newline at end of file diff --git a/Weave/Graphix/Graphix.h b/Weave/Graphix/Graphix.h new file mode 100644 index 0000000..a1cebb8 --- /dev/null +++ b/Weave/Graphix/Graphix.h @@ -0,0 +1,48 @@ +#pragma once + +#include +#include +#include +//#include + +//#include "Drawable.h" +//#include "Scene.h" + +using std::string; +//using std::unordered_map; + +class Graphix +{ +public: + static void setWindowSize(unsigned int _width, unsigned int _height); + static void setWindowPos(unsigned int _xpos, unsigned int _ypos); + static unsigned int getWindowHeight(); + static unsigned int getWindowWidth(); + + static void catchMouse(); + static void freeMouse(); + + static void swap(); + + static void init(); + static void cleanup(); + static void clear(); + +private: + static unsigned int width; + static unsigned int height; + + static unsigned int xpos; + static unsigned int ypos; + + static string windowTitle; + + static Uint32 windowFlags; + static Uint32 sdlFlags; + + static SDL_Window* sdl_window; + + static SDL_GLContext sdl_glcontext; + +}; + diff --git a/Weave/Graphix/Model.cpp b/Weave/Graphix/Model.cpp new file mode 100644 index 0000000..6a4b31a --- /dev/null +++ b/Weave/Graphix/Model.cpp @@ -0,0 +1,231 @@ +#include "Model.h" + +#include +#include +#include +#include + +#include "Shader.h" +#include "../Message.h" + +typedef unsigned int uint; + + +Model::Model(const string& _modelpath, Shader* _shader, bool invert) : + shader(_shader), + modelpath(_modelpath), + vao(-1), + numfaces(0), + numvertices(-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); + + 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() +{ + /* + glDeleteBuffers(1, &vertexBuffer); + glDeleteBuffers(1, &normalBuffer); + glDeleteBuffers(1, &uvBuffer); + glDeleteBuffers(1, &indexBuffer); + glDeleteVertexArrays(1, &vao); + */ +} + +void Model::useModel() const +{ + glBindVertexArray(vao); + + shader->useShader(); +} + +void Model::drawModel() const +{ + + glDrawElements(GL_TRIANGLES, numfaces * 3, GL_UNSIGNED_INT, 0); + + glBindVertexArray(0); +} + + + +void Model::genBuffer(uint &buffer, uint size, void* value) +{ + glGenBuffers(1, &buffer); + glBindBuffer(GL_ARRAY_BUFFER, buffer); + glBufferData(GL_ARRAY_BUFFER, size, value, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); +} + +void Model::bindBuffer(uint &buffer, string name, 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); + 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) +{ + 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."); + } + + 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 new file mode 100644 index 0000000..05dd017 --- /dev/null +++ b/Weave/Graphix/Model.h @@ -0,0 +1,52 @@ +#pragma once + +#include +#include + +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; + + +class Shader; + + +class Model +{ +public: + Model(const string& modelpath,Shader* _shader,bool invert=false); + + virtual ~Model(); + + void useModel() const; + void drawModel() const; + + Shader* getShader() const; + +protected: + unsigned int vao; + Shader* shader; + string modelpath; + unsigned int numvertices, numfaces; + +private: + + bool import(const string& path, unsigned int& numvertices, unsigned int& numfaces, float*& vertex, float*& uvs, float*& normals, unsigned int*& index, bool invert=false); + 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; + +}; + diff --git a/Weave/Graphix/Performer.cpp b/Weave/Graphix/Performer.cpp new file mode 100644 index 0000000..24d5346 --- /dev/null +++ b/Weave/Graphix/Performer.cpp @@ -0,0 +1,303 @@ +#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 new file mode 100644 index 0000000..2f6623c --- /dev/null +++ b/Weave/Graphix/Performer.h @@ -0,0 +1,62 @@ +#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 new file mode 100644 index 0000000..669268c --- /dev/null +++ b/Weave/Graphix/Scene.cpp @@ -0,0 +1,220 @@ +#include "Scene.h" + +#include "GLM.h" +#include + +#include "../Message.h" +#include + +#include "../Events.h" + +#include "Shader.h" + +#include "Setting.h" + +/* +Scene::Scene() : + SceneObjects(new list), + ShaderSet(new set), + newObjects(true) +{ +}*/ + +Scene::Scene(unsigned int x, unsigned int y, unsigned int width, unsigned int height, float fovy, float zNear, float zFar, vec3 pos, SceneObject* _lookat) : + viewPort(new ViewPort(x, y, width, height)), + projection(perspective(fovy, (float)width / height, zNear, zFar)), + view(translate(pos)), + SceneObjects(new list), + ShaderSet(new set), + newObjects(true), + lookat(_lookat) +{ + + +} + +Scene::Scene(Scene* _Scene, unsigned int x, unsigned int y, unsigned int width, unsigned int height, float fovy, float zNear, float zFar, vec3 pos, SceneObject* _lookat) : + viewPort(new ViewPort(x, y, width, height)), + projection(perspective(fovy, (float)width / height, zNear, zFar)), + view(translate(pos)), + SceneObjects(_Scene->SceneObjects), + ShaderSet((_Scene->ShaderSet)), + newObjects(false), + lookat(_lookat) +{ +} + +Scene::Scene(Scene* _Scene) : + SceneObjects(_Scene->SceneObjects), + ShaderSet((_Scene->ShaderSet)), + newObjects(false) +{ +} + + +Scene::~Scene() +{ + if (newObjects){ + for (auto i = SceneObjects->begin(); i != SceneObjects->end(); ++i) + { + delete (*i); + } + delete SceneObjects; + delete ShaderSet; + } + + + if (viewPort != nullptr) + delete viewPort; +} + + + + +void Scene::update(float deltaT) +{ + vec3 pos; + //Focus auf Player + + // XYAchse um den Player + if (Events::getViewX() || Events::getViewY()) + { + if (lookat!=NULL) + view = translate(view,lookat->getPosition()); + pos = (vec3)view[3]; + view = rotate(deltaT * 70, vec3(sign(Events::getViewX()), sign(Events::getViewY()), 0.0f))*view; + view = translate(pos - (vec3)view[3])*view; + if (lookat != NULL) + view = translate(view, -lookat->getPosition()); + // TODO Kamera wieder an die richtige Stelle schieben + } + + + // Zoom auf Player + if (Events::getViewZ()) + { + // float dist = 0; + // if (lookat!=NULL) + // dist = VektorAbs(getViewerPos() - lookat->getPosition()); + // if (true) + view = translate( vec3(0.f,0.f,(float)(5*deltaT *sign(Events::getViewZ()))))*view; + //view = view*scale(vec3((float)(1 + deltaT * sign(Events::getViewZ())))); + } + + if (lookat != NULL) + pos = lookat->getPosition(); + // Alle Objekte der Scene aktualisieren + if (newObjects) + { + for (auto i = SceneObjects->begin(); i != SceneObjects->end(); ++i) + { + (*i)->update(deltaT); + } + + + for (auto i = SceneObjects->begin(); i != SceneObjects->end(); ++i) + { + auto j = i; + for (j++; j != SceneObjects->end(); ++j) + { + (*i)->collisions(*j); + } + } + for (auto i = SceneObjects->begin(); i != SceneObjects->end(); ++i) + { + if ((*i)->isEnemy() && (*i)->getMatter() < lookat->getMatter()) + (*i)->useTextureSave(); + else if ((*i)->isEnemy() && (*i)->getMatter() >= lookat->getMatter()) + (*i)->useTextureDanger(); + + if ((*i)->getMatter() < 1.e-3 && newObjects) + { + //SceneObjects->erase(i); +// delete (*i); + } + } + } + + if (lookat != NULL) + view = translate(view,pos - lookat->getPosition()); +} + +void Scene::draw() const +{ + viewPort->useViewPort(); + + 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)); + } + + + + //don't know if this is really the right place to set the lighting + //add ambient light + GLfloat ambientcolor[] = { 0.5f, 0.5f, 0.5f, 1.0f }; + glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientcolor); + + //add positioned light + GLfloat lightcolor0[] = { 1.5f, 1.5f, 1.5f, 1.0f }; + GLfloat lightposition0[] = { 1.0f, 1.0f, 1.0f, 1.0f }; + glLightfv(GL_LIGHT0, GL_DIFFUSE, lightcolor0); + glLightfv(GL_LIGHT0, GL_POSITION, lightposition0); + + + + for (auto i = SceneObjects->cbegin(); i != SceneObjects->cend(); ++i) + { + (*i)->draw(); + } +} + +void Scene::addObject(SceneObject* _add) +{ + SceneObjects->push_back(_add); + ShaderSet->insert(_add->getShader()); + _add->setMainScene(this); +} + +void Scene::deleteObject(SceneObject* _del) +{ + SceneObjects->remove(_del); +} + +const vec3 Scene::getViewerPos() const +{ + return vec3(projection * vec4(0.0f)); +} + +list* Scene::getSceneObjects() +{ + return SceneObjects; +} + +void Scene::setProjection(mat4 _projection) +{ + projection = _projection; +} + +void Scene::setProjection(float fovy, float aspect, float zNear, float zFar) +{ + projection = perspective(fovy, aspect, zNear, zFar); +} + +/* +void Scene::updateProjection(mat4 _projection) +{ + projection = _projection * projection; +}*/ + +void Scene::setViewPort(unsigned int x, unsigned int y, unsigned int width, unsigned int height) +{ + viewPort = new ViewPort(x, y, width, height); +} + +void Scene::setLookAt(SceneObject* _lookat) +{ + lookat = _lookat; +} \ No newline at end of file diff --git a/Weave/Graphix/Scene.h b/Weave/Graphix/Scene.h new file mode 100644 index 0000000..e322287 --- /dev/null +++ b/Weave/Graphix/Scene.h @@ -0,0 +1,54 @@ +#pragma once + +#include "Drawable.h" +#include "ViewPort.h" +#include "SceneObject.h" +#include "GLM.h" + +#include +#include + +using std::list; +using std::set; + +typedef list SceneList; + +class Scene : + public Drawable +{ +public: + //Scene(); + Scene(unsigned int x, unsigned int y, unsigned int width, unsigned int height, float fovy, float zNear, float zFar, vec3 pos, SceneObject* lookat = NULL); + Scene(Scene*); + Scene(Scene* _Scene, unsigned int x, unsigned int y, unsigned int width, unsigned int height, float fovy, float zNear, float zFar, vec3 pos, SceneObject* lookat = NULL); + virtual ~Scene(); + + virtual void update(float); + virtual void draw() const; + + void addObject(SceneObject*); + void deleteObject(SceneObject*); + + const vec3 getViewerPos() const; + + SceneList* getSceneObjects(); + + void setViewPort(unsigned int, unsigned int, unsigned int, unsigned int); + + void setProjection(mat4 _projection); + void setProjection(float fovy, float aspect, float zNear, float zFar); + //void updateProjection(mat4); + + void setLookAt(SceneObject* _lookat); + +protected: + ViewPort* viewPort; + mat4 projection; + mat4 view; + + SceneObject* lookat; + + bool newObjects; + SceneList* SceneObjects; + set* ShaderSet; +}; diff --git a/Weave/Graphix/SceneObject.cpp b/Weave/Graphix/SceneObject.cpp new file mode 100644 index 0000000..8c3b8bf --- /dev/null +++ b/Weave/Graphix/SceneObject.cpp @@ -0,0 +1,259 @@ +#include "SceneObject.h" + +#include +#include "GLM.h" + +#include "../Message.h" +#include +#include "../ArtificialI.h" + +#include + +#include "../Intelligenz.h" +#include "Shader.h" +#include "Scene.h" +#include "Texture.h" + +#define MODEL_contractionspeed 8.f +#define MassSpeed_relation 3.f +#define BOX_size 6 + +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), +model(_model), +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"); + model = scale(model, vec3(pow(matter,1./3))); + if (texturepath!="") + texture = new Texture(texturepath); +} + + +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; + + //Matter Difference + if (abs(matterDiff)>1e-3) + { + float nmatter = (deltaT * matterDiff * MODEL_contractionspeed + matter); + //cout << faktor << endl; + model = scale(model, vec3(pow(nmatter / matter, 1. / 3))); + matterDiff -= deltaT * matterDiff * MODEL_contractionspeed; + matter = nmatter; + } + else if (matterDiff != 0) + { + float nmatter = (matterDiff + matter); + model = scale(model, vec3(pow(nmatter / matter, 1. / 3))); + matterDiff = 0; + matter = nmatter; + } + + //model = scale(model, vec3(matter)); + //model = (deltaT * spin) * model; //TODO drehen scheint nicht zu funktionieren + + // Im Ramen bleiben + vec3 pos = getPosition(); + if (fabs(pos.x) > BOX_size && direction.x*pos.x > 0){ + direction.x *= -1; + } + else if (fabs(pos.y) > BOX_size && direction.y*pos.y > 0){ + direction.y *= -1; + } + else if (fabs(pos.z) > BOX_size && direction.z*pos.z > 0){ + direction.z *= -1; + } +} + +void SceneObject::draw() const +{ + useModel(); + + if (texture!=nullptr) + { + int unit = 0; + texture->bind(unit); + glUniform1i(glGetUniformLocation(*shader, "ColorTexture"), unit); + } + glUniformMatrix4fv(modelID, 1, GL_FALSE, value_ptr(model)); + + glUniform1i(glGetUniformLocation(*shader, "inv"), invert ? -1 : 1); + + drawModel(); +} + +void SceneObject::collisions(SceneObject* _other) +{ + if (collision_ignore || _other->collision_ignore) + return; + + float otherM = _other->getMatter(); + float diff = VektorAbs(getPosition() - _other->getPosition()) - pow(matter, 1. / 3) - pow(otherM, 1. / 3); + if (diff < 0) + { + float mass = matter - pow(pow(matter, 1. / 3) + diff / 2, 3) + otherM - pow(pow(otherM, 1. / 3) + diff / 2, 3); + + if (matter>_other->getMatter()) + { + _other->setMatter(otherM - mass); + setMatter(matter + mass); + } + else + { + _other->setMatter(otherM + mass); + setMatter(matter - mass); + } + } +} + +vec3 SceneObject::getPosition() const{ + return vec3(model[3]); +} + +vec3 SceneObject::push(vec3& _direction, float _matter) +{ + vec3 direct = normalize(vec3(model * vec4(_direction, 0.f))); + 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); + + delete copy->texture; + copy->texture = new Texture("../Textures/enemy.png"); + + direction += direct * dir_len; + matterDiff -= _matter * matter; + } + return direct; +} + +void SceneObject::stop() +{ + push(vec3(inverse(model)*vec4(-direction,0))); + direction = vec3(0.f); +} + +void SceneObject::setModel(mat4& _model){ + model = _model; +} + +void SceneObject::turn(float degree, vec3& axis){ + //vec3 pos = getPosition(); + //model = rotate(degree, axis)*model; + //model = model*translate(pos - getPosition()); + model = model * rotate(degree, axis); +} + +void SceneObject::setDirection(vec3& _direction) +{ + direction = _direction; +} + +void SceneObject::setCollision(bool _col) +{ + collision_ignore = !_col; +} + + +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; +} + +void SceneObject::setMainScene(Scene* _scene) +{ + mainScene = _scene; +} + +void SceneObject::setMatter(float _matter) +{ + model = scale(model, vec3(pow(_matter / matter,1./3))); + matter = _matter; +} + + +void SceneObject::setTextureSave(string texture) +{ + default_save = new Texture(texture); +} +void SceneObject::setTextureDanger(string texture) +{ + default_danger = new Texture(texture); +} + +void SceneObject::useTextureSave() +{ + texture = default_save; +} +void SceneObject::useTextureDanger() +{ + texture = default_danger; +} + +void SceneObject::setEnemy(bool _isenemy) +{ + isenemy = _isenemy; +} + +bool SceneObject::isEnemy() +{ + return isenemy; +} + +Texture* SceneObject::default_danger = nullptr; +Texture* SceneObject::default_save = nullptr; + diff --git a/Weave/Graphix/SceneObject.h b/Weave/Graphix/SceneObject.h new file mode 100644 index 0000000..030010e --- /dev/null +++ b/Weave/Graphix/SceneObject.h @@ -0,0 +1,99 @@ +#pragma once + +#include "GLM.h" + +#include "Drawable.h" +#include "Model.h" + +#include + +using std::string; + +class Scene; +class Intelligenz; +class Texture; + +//class string; + +class SceneObject : + public Drawable, + public Model +{ +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); + virtual ~SceneObject(); + + virtual void update(float); + virtual void draw() const; + + virtual void collisions(SceneObject*); + + virtual vec3 getPosition() const; + virtual void setDirection(vec3&); + + virtual vec3 push(vec3&, float = 0.1f); + + virtual void stop(); + + virtual void setModel(mat4&); + virtual void turn(float, vec3&); + + virtual bool operator==(SceneObject); + + void setIntelligenz(Intelligenz*); + + void setMainScene(Scene *); + + float getMatter(); + + void setCollision(bool); + + static void setTextureSave(string texture); + static void setTextureDanger(string texture); + + void useTextureSave(); + void useTextureDanger(); + + void setEnemy(bool isenemy); + bool isEnemy(); + +protected: + + void setMatter(float); + + Intelligenz* will; + + mat4 model; + unsigned int modelID; + + Scene* mainScene; + + bool collision_ignore; + + virtual SceneObject* copy(); + + float matter; + float speed; + float atack; + + bool invert; + + float matterDiff; + + vec3 direction; + mat4 spin; + + Texture* texture; + + bool isenemy; + +private: + + static Texture* default_danger; + static Texture* default_save; + + +}; + diff --git a/Weave/Graphix/Setting.cpp b/Weave/Graphix/Setting.cpp new file mode 100644 index 0000000..83336dd --- /dev/null +++ b/Weave/Graphix/Setting.cpp @@ -0,0 +1,101 @@ +#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 new file mode 100644 index 0000000..ee17664 --- /dev/null +++ b/Weave/Graphix/Setting.h @@ -0,0 +1,54 @@ +#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 new file mode 100644 index 0000000..96de52a --- /dev/null +++ b/Weave/Graphix/Shader.cpp @@ -0,0 +1,128 @@ +#include "Shader.h" + +#include "../Message.h" + +#include + +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); + + handle = loadProgram(handleS1, handleS2); + + glDeleteShader(handleS1); + glDeleteShader(handleS2); + +} + + +Shader::~Shader() +{ + glDeleteProgram(handle); +} + + +void Shader::useShader() const +{ + glUseProgram(handle); +} + +GLuint Shader::getHandle() const +{ + return handle; +} + +Shader::operator GLuint() const +{ + return handle; +} + + +GLuint Shader::loadShader(string& _shaderPath) +{ + GLuint 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; +} + + +GLuint Shader::loadProgram(GLuint _shader1, GLuint _shader2){ + GLuint 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; +} diff --git a/Weave/Graphix/Shader.h b/Weave/Graphix/Shader.h new file mode 100644 index 0000000..34d9bdf --- /dev/null +++ b/Weave/Graphix/Shader.h @@ -0,0 +1,28 @@ +#pragma once + +#include +#include + +using std::string; + +class Shader +{ +public: + Shader(string _shader1, string _shader2); + virtual ~Shader(); + + void useShader() const; + + unsigned int getHandle() const; + operator unsigned int() const; + +private: + unsigned int handle; + + unsigned int loadShader(string& _shaderPath); + unsigned int loadProgram(unsigned int _shader1, unsigned int _shader2); + + + +}; + diff --git a/Weave/Graphix/Texture.cpp b/Weave/Graphix/Texture.cpp new file mode 100644 index 0000000..0c1974d --- /dev/null +++ b/Weave/Graphix/Texture.cpp @@ -0,0 +1,81 @@ +#include "Texture.h" +#include + +using std::string; +using std::cout; +using std::endl; + + +Texture::Texture(const string& _path) : path(_path) +{ + + auto i = texture_map.find(_path); + if (i != texture_map.end()) + { + handle = i->second; + return; + } + + glGenTextures(1, &handle); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, handle); + + FREE_IMAGE_FORMAT format = FreeImage_GetFileType(path.c_str());//Automatocally detects the format + if (format == FIF_UNKNOWN) + { + cout << "Couldn't detect texture file format." << endl; + system("pause"); + exit(-1); + } + FIBITMAP* data = FreeImage_Load(format, path.c_str()); + if (!data) + { + cout << "Couldn't read texture file." << endl; + system("pause"); + exit(-1); + } + + //FIBITMAP* temp = data; + data = FreeImage_ConvertTo32Bits(data); + if (!data) + { + cout << "Couldn't convert texture file to bit format." << endl; + system("pause"); + exit(-1); + } + //FreeImage_Unload(temp); + + unsigned int width = FreeImage_GetWidth(data); + unsigned int height = FreeImage_GetHeight(data); + + //unsigned int width, height; + + //FIBITMAP *data = FreeImage_Load(FIF_PNG, path.c_str(), PNG_DEFAULT); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, (GLvoid*)FreeImage_GetBits(data)); + glGenerateMipmap(GL_TEXTURE_2D); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + texture_map[_path] = handle; +} + +Texture::~Texture() +{ + //glDeleteTextures(1, &handle); +} + +void Texture::bind(int unit) +{ + glActiveTexture(GL_TEXTURE0 + unit); + glBindTexture(GL_TEXTURE_2D, handle); +} + +Texture::operator string() const +{ + return path; +} + +str2int_map Texture::texture_map; \ No newline at end of file diff --git a/Weave/Graphix/Texture.h b/Weave/Graphix/Texture.h new file mode 100644 index 0000000..1962a1c --- /dev/null +++ b/Weave/Graphix/Texture.h @@ -0,0 +1,30 @@ +#pragma once + +#include +#include +#include "FreeImage.h" + +#include + +using std::unordered_map; + +using std::string; + +typedef unordered_map str2int_map; + +class Texture +{ +public: + Texture(const string& path); + ~Texture(); + + void bind(int unit); + + operator string() const; + +private: + GLuint handle; + string path; + + static str2int_map texture_map; +}; \ No newline at end of file diff --git a/Weave/Graphix/ViewPort.cpp b/Weave/Graphix/ViewPort.cpp new file mode 100644 index 0000000..4791e42 --- /dev/null +++ b/Weave/Graphix/ViewPort.cpp @@ -0,0 +1,38 @@ +#include "ViewPort.h" + +#include + + +ViewPort::ViewPort(unsigned int _x, unsigned int _y, unsigned int _width, unsigned int _height) : xpos(_x), ypos(_y), width(_width), height(_height) +{ +} + + +ViewPort::~ViewPort() +{ +} + +void ViewPort::useViewPort() const +{ + glViewport(xpos, ypos, width, height); +} + +unsigned int ViewPort::getXPos() const +{ + return xpos; +} + +unsigned int ViewPort::getYPos() const +{ + return ypos; +} + +unsigned int ViewPort::getWidth() const +{ + return width; +} + +unsigned int ViewPort::getHeight() const +{ + return height; +} diff --git a/Weave/Graphix/ViewPort.h b/Weave/Graphix/ViewPort.h new file mode 100644 index 0000000..efc53a3 --- /dev/null +++ b/Weave/Graphix/ViewPort.h @@ -0,0 +1,29 @@ +#pragma once + +#include "Drawable.h" + +class ViewPort +{ +public: + ViewPort(unsigned int, unsigned int, unsigned int, unsigned int); + ~ViewPort(); + + void useViewPort() const; + + unsigned int getXPos() const; + unsigned int getYPos() const; + unsigned int getWidth() const; + unsigned int getHeight() const; + +// Drawable* content; + + +protected: + unsigned int width; + unsigned int height; + + unsigned int xpos; + unsigned int ypos; + +}; + diff --git a/Weave/Graphix/idNcount.cpp b/Weave/Graphix/idNcount.cpp new file mode 100644 index 0000000..e2d0ce3 --- /dev/null +++ b/Weave/Graphix/idNcount.cpp @@ -0,0 +1,33 @@ +#include "idNcount.h" + +idNcount::idNcount(unsigned int _id) : +id(_id), +count(0) +{ +} + +idNcount::~idNcount() +{ +} + +idNcount::operator unsigned int() const +{ + return id; +} + +idNcount::operator bool() const +{ + return count <= 0; +} + +idNcount& idNcount::operator++() +{ + count++; + return *this; +} + +idNcount& idNcount::operator--() +{ + count--; + return *this; +} \ No newline at end of file diff --git a/Weave/Graphix/idNcount.h b/Weave/Graphix/idNcount.h new file mode 100644 index 0000000..bae2bb5 --- /dev/null +++ b/Weave/Graphix/idNcount.h @@ -0,0 +1,21 @@ + +class idNcount +{ +public: + idNcount(unsigned int); + ~idNcount(); + + //handleID + operator unsigned int() const; + + //handleID wird gebraucht + idNcount& operator ++(); + //handleID wird nicht mehr gebraucht + idNcount& operator --(); + //ist die handleID ungenutzt? + operator bool() const; + +private: + unsigned int id; + unsigned int count; +}; \ No newline at end of file diff --git a/Weave/Message.cpp b/Weave/Message.cpp new file mode 100644 index 0000000..82af28e --- /dev/null +++ b/Weave/Message.cpp @@ -0,0 +1,43 @@ +#include "Message.h" +#include "time.h" + +#include + +using namespace std; + +string currentDateTime(); + +Message::Message() +{ +} + +Message::~Message() +{ +} + +void Message::error(string _msg) +{ + cerr << currentDateTime() << " E " << _msg << endl; + system("pause"); + exit(-1); +} + +void Message::warning(string _msg) +{ + cout << currentDateTime() << " W " << _msg << endl; +} + +void Message::info(string _msg) +{ + cout << currentDateTime() << " I " << _msg << endl; +} + +string currentDateTime() { + time_t now = time(0); + struct tm tstruct; + char buf[80]; + tstruct = *localtime(&now); + strftime(buf, sizeof(buf), "[%X]", &tstruct); + + return buf; +} \ No newline at end of file diff --git a/Weave/Message.h b/Weave/Message.h new file mode 100644 index 0000000..f58e697 --- /dev/null +++ b/Weave/Message.h @@ -0,0 +1,17 @@ +#pragma once + +#include +using std::string; + +class Message +{ +public: + Message(); + ~Message(); + + static void error(string _msg); + static void Message::warning(string _msg); + static void Message::info(string _msg); + +}; + diff --git a/Weave/PlayerI.cpp b/Weave/PlayerI.cpp new file mode 100644 index 0000000..eadd870 --- /dev/null +++ b/Weave/PlayerI.cpp @@ -0,0 +1,42 @@ +#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 new file mode 100644 index 0000000..b77f6b6 --- /dev/null +++ b/Weave/PlayerI.h @@ -0,0 +1,16 @@ +#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 0658123..7cd19c7 100644 --- a/Weave/Weave.vcxproj +++ b/Weave/Weave.vcxproj @@ -86,14 +86,18 @@ + + + + diff --git a/Weave/Weave.vcxproj.filters b/Weave/Weave.vcxproj.filters index 026f3d3..3268ef8 100644 --- a/Weave/Weave.vcxproj.filters +++ b/Weave/Weave.vcxproj.filters @@ -27,6 +27,12 @@ Source Files + + Source Files + + + Source Files + @@ -41,5 +47,11 @@ Header Files + + Header Files + + + Header Files + \ No newline at end of file diff --git a/Weave/main.cpp b/Weave/main.cpp new file mode 100644 index 0000000..8797643 --- /dev/null +++ b/Weave/main.cpp @@ -0,0 +1,53 @@ + +#include +#include + +//#include "Graphix\GLM.h" + +//#include //glew32.dll + +//#include //SDL2.dll + +//#include //freetype2.dll, zlib1.dll +//#include FT_FREETYPE_H + +//#include "Graphix\SceneObject.h" +//#include "Graphix\Shader.h" +//#include "Message.h" + +#include "Graphix\Graphix.h" +//#include "Graphix\Scene.h" +//#include "Events.h" + +//#include "Average.h" + +//#include "Fps.h" + +//#include "Game.h" + +//using std::cout; +//using std::endl; +//using std::string; + + + +/* +void logSDLerror(ostream &os, const char *msg) +{ + os << msg << " error: " << SDL_GetError() << endl; +} +*/ +//Events events; + +int main(int argc, char *argv[]) +{ + Graphix::init(); + +// Game spiel1; + +// spiel1.play(); + + Graphix::cleanup(); + + return 0; +} diff --git a/shader/basicTexture_FS.hlsl b/shader/basicTexture_FS.hlsl new file mode 100644 index 0000000..daaefbc --- /dev/null +++ b/shader/basicTexture_FS.hlsl @@ -0,0 +1,14 @@ +//Fragment Shader +#version 330 +//in worldNormal; +in vec2 fUVs; + +out vec4 color; + +uniform sampler2D ColorTexture; + +void main() +{ + vec3 uvColor = texture(ColorTexture, fUVs).rgb; + color = vec4(uvColor, 0.7); +} \ No newline at end of file diff --git a/shader/basicTexture_VS.hlsl b/shader/basicTexture_VS.hlsl new file mode 100644 index 0000000..0b5bc86 --- /dev/null +++ b/shader/basicTexture_VS.hlsl @@ -0,0 +1,42 @@ +//Vertex Shader +#version 330 + +in vec3 normal, position; +//in vec3 lightposition +in vec2 uv; + +out vec3 worldNormal, worldLightPoint1; +out vec2 fUVs; +out float visNormal, SpecularCosPoint1, SpecularCosDirection1; + +uniform mat4 projection, view, model; +uniform int inv = 1; + +uniform int bloom = 1; +uniform int transp = 1; + +void main() +{ + //for every point light source define PointLightPosition and SpecularCos + //for every directional light source define Direction and SpecularCos + //everything else is in the Fragment Shader + + vec3 PointLightPosition1 = vec3(0.0f, 0.0f, 0.0f); + vec3 DirectionalLightDirection1 = normalize(vec3(-2.0f, -2.0f, -2.0f)); + + + fUVs = uv; + vec4 world_position = model * vec4(position, 1); + worldLightPoint1 = PointLightPosition1 - world_position.xyz; + + gl_Position = projection * view * world_position; + worldNormal = (model * inv * vec4(normal, 0.0f)).xyz; + visNormal = (view * vec4(worldNormal, 0.0f)).z; + + + SpecularCosPoint1 = clamp(dot(vec3(0.0f, 0.0f, -1.0f), normalize((projection * view * vec4(reflect(-PointLightPosition1, worldNormal), 0.0f)).xyz)), 0, 1); + SpecularCosDirection1 = clamp(dot(vec3(0.0f, 0.0f, -1.0f), normalize((projection * view * vec4(reflect(-DirectionalLightDirection1, worldNormal), 0.0f)).xyz)), 0, 1); + + + //worldNormal = vec3(SpecularCos, SpecularCos, SpecularCos); +} \ No newline at end of file diff --git a/shader/basic_FS.hlsl b/shader/basic_FS.hlsl new file mode 100644 index 0000000..fd96435 --- /dev/null +++ b/shader/basic_FS.hlsl @@ -0,0 +1,7 @@ +//Fragment Shader +#version 330 +out vec4 color; + +void main(){ + color = vec4(0,0.3,0,0.6); +} \ No newline at end of file diff --git a/shader/basic_VS.hlsl b/shader/basic_VS.hlsl new file mode 100644 index 0000000..15e757e --- /dev/null +++ b/shader/basic_VS.hlsl @@ -0,0 +1,12 @@ +//Vertex Shader +#version 330 + +in vec3 position; +uniform mat4 projection; +uniform mat4 model; +uniform mat4 view; + +void main(){ + //gl_Position = projection * model * vec4(position, 1); + gl_Position = projection * view * model * vec4(position, 1); +} \ No newline at end of file diff --git a/shader/lightingTexture_FS.hlsl b/shader/lightingTexture_FS.hlsl new file mode 100644 index 0000000..daaa655 --- /dev/null +++ b/shader/lightingTexture_FS.hlsl @@ -0,0 +1,56 @@ +//Fragment Shader +#version 330 +in vec3 worldNormal, worldLightPoint1; +in vec2 fUVs; +in float visNormal, SpecularCosPoint1, SpecularCosDirection1; + +out vec4 color; + +uniform sampler2D ColorTexture; + +void main() +{ + if (visNormal < 0) + { + discard; + } + float specularConst = 5.0f; + vec3 normal = normalize(worldNormal); + vec4 uvColor = texture(ColorTexture, fUVs); + + + + + vec3 PointLightDirection1 = normalize(worldLightPoint1); + vec3 PointLightColor1 = vec3(30.0f, 30.0f, 30.0f); + + vec3 DirectionalLightDirection1 = normalize(vec3(-2.0f, -2.0f, -2.0f)); + vec3 DirectionalLightColor1 = vec3(1.0f, 1.0f, 1.0f); + + vec3 AmbientLightColor = vec3(0.1f, 0.1f, 0.1f); + + + float cosThetaPoint1 = clamp(dot(normal, PointLightDirection1), 0, 1); + float cosThetaDirection1 = clamp(dot(normal, DirectionalLightDirection1), 0, 1); + + float squaredist1 = worldLightPoint1.x * worldLightPoint1.x + worldLightPoint1.y * worldLightPoint1.y + worldLightPoint1.z * worldLightPoint1.z; + + + + //if (uvColor.a < 0.3) + // discard; + + + + color = vec4(uvColor.rgb * (AmbientLightColor + + PointLightColor1 * (cosThetaPoint1 + pow(SpecularCosPoint1, specularConst)) / squaredist1 + + DirectionalLightColor1 * (cosThetaDirection1 + pow(SpecularCosDirection1, specularConst)) + ), uvColor.a); + + + //color = vec4(worldNormal, 1.0f); + //color = vec4(length(DirectionalLightDirection1) - 1.0f, length(DirectionalLightDirection1) - 1.0f, length(DirectionalLightDirection1) - 1.0f, 1.0f); + + //vec3 uvColor = texture(ColorTexture, fUVs).rgb; + //color = vec4(uvColor, 1.0); +} diff --git a/shader/perspective_VS.hlsl b/shader/perspective_VS.hlsl new file mode 100644 index 0000000..032eeef --- /dev/null +++ b/shader/perspective_VS.hlsl @@ -0,0 +1,12 @@ +//Vertex Shader +#version 330 + +in vec3 position; +uniform mat4 projection; +uniform mat4 view; +uniform mat4 model; + +void main(){ + gl_Position = projection * view * model * vec4(position, 1); + //gl_Position = projection * view * model * vec4(vertexPosition, 1); +} \ No newline at end of file -- 2.47.3