Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // main.cpp ==============================================================================================================
- #include "engine.h"
- int main(int argc, char* args[])
- {
- try
- {
- Engine* e = new Engine(800, 600, 1);
- e->init();
- e->mainLoop();
- e->close();
- delete e;
- }
- catch(std::string &message)
- {
- std::cout << message << std::endl;
- return -1;
- }
- return 0;
- }
- // main.cpp ==============================================================================================================
- // engine.h ==============================================================================================================
- #ifndef ENGINE_H
- #define ENGINE_H
- #include "scene.h"
- class Engine
- {
- public:
- Engine(int _screenWidth, int _screenHeight, int _delay = 1);
- virtual ~Engine();
- void init();
- void mainLoop();
- void close();
- protected:
- void draw();
- void animate(float dt);
- void createWindow();
- void setOpenGL();
- SDL_Window* window = nullptr;
- SDL_GLContext glContext;
- SDL_TimerID timerID;
- const int screenWidth;
- const int screenHeight;
- int delay;
- float deltaTime = 0.0f;
- std::vector<std::unique_ptr<Scene> > scene;
- unsigned int activeScene = 0;
- private:
- };
- // engine.h ==============================================================================================================
- // engine.cpp ==============================================================================================================
- #include "engine.h"
- #include <sstream>
- #include <gl/gl.h>
- #include <SDL2/SDL_image.h>
- #include <SDL2/SDL_ttf.h>
- Engine::Engine(int _screenWidth, int _screenHeight, int _delay)
- : screenWidth(_screenWidth), screenHeight(_screenHeight), delay(_delay)
- {
- deltaTime = static_cast<float>(delay)*0.001f;
- }
- Engine::~Engine()
- {
- //dtor
- }
- void Engine::init()
- {
- createWindow();
- setOpenGL();
- glewExperimental = GL_TRUE;
- glewInit();
- scene.push_back(std::make_unique<Scene>(screenWidth, screenHeight));
- for (const auto & s : scene) s->init();
- }
- void Engine::mainLoop()
- {
- bool quit = false;
- SDL_Event event;
- while (!quit)
- {
- while (SDL_PollEvent(&event) != 0)
- {
- if (event.type == SDL_QUIT)
- {
- quit = true;
- }
- else if (event.type == SDL_USEREVENT)
- {
- animate(deltaTime);
- }
- scene[activeScene]->eventLoop(event);
- }
- draw();
- }
- }
- void Engine::close()
- {
- SDL_GL_DeleteContext(glContext);
- SDL_DestroyWindow(window);
- window = nullptr;
- SDL_RemoveTimer(timerID);
- TTF_Quit();
- IMG_Quit();
- SDL_Quit();
- }
- void Engine::draw()
- {
- scene[activeScene]->draw();
- SDL_GL_SwapWindow(window);
- }
- void Engine::animate(float dt)
- {
- scene[activeScene]->animate(dt);
- }
- void Engine::createWindow()
- {
- std::stringstream err;
- if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
- {
- err << "Engine::init: Blad przy inicjalizacji SDL: " << SDL_GetError();
- throw err.str();
- }
- if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
- {
- std::cout << "Engine::init: Filtrowanie liniowe nie zostalo wlaczone" << std::endl;
- }
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
- SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
- SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
- SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
- SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);
- window = SDL_CreateWindow("OpenGL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screenWidth,
- screenHeight, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
- if( window == NULL )
- {
- err << "Engine::init: Nie mozna utworzyc okna " << SDL_GetError();
- throw err.str();
- }
- glContext = SDL_GL_CreateContext(window);
- timerID = SDL_AddTimer(delay, callback, (void*)"");
- int imgFlags = IMG_INIT_PNG;
- if (!(IMG_Init(imgFlags) & imgFlags))
- {
- err << "Engine::init: SDL_image nie zostal zainicjowany: " << IMG_GetError();
- throw err.str();
- }
- if (TTF_Init() == -1)
- {
- err << "Engine::init: SDL_TTF nie zostal zainicjowany: " << TTF_GetError();
- throw err.str();
- }
- }
- void Engine::setOpenGL()
- {
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_FRAMEBUFFER_SRGB);
- glEnable(GL_CULL_FACE);
- glCullFace(GL_FRONT);
- glFrontFace(GL_CW);
- }
- Uint32 callback(Uint32 interval, void *param)
- {
- SDL_Event event;
- SDL_UserEvent userevent;
- userevent.type = SDL_USEREVENT;
- userevent.code = 0;
- userevent.data1 = NULL;
- userevent.data2 = NULL;
- event.type = SDL_USEREVENT;
- event.user = userevent;
- SDL_PushEvent(&event);
- return(interval);
- }
- // engine.cpp ==============================================================================================================
- // scene.h ==============================================================================================================
- #ifndef SCENE_H
- #define SCENE_H
- #include "program.h"
- #include "camera.h"
- #include "model.h"
- #include <memory>
- #include <vector>
- #include <iostream>
- #include <SDL2/SDL.h>
- #include "lightning.h"
- class Scene
- {
- public:
- Scene(int _width, int _height);
- virtual ~Scene();
- void init();
- void draw();
- void eventLoop(SDL_Event &event);
- void animate(float dt);
- protected:
- void loadSceneProgram(std::string vertName, std::string fragName, std::string geoName = "");
- void loadModelProgram(std::string vertName, std::string fragName, std::string geoName = "");
- void createFrameBuffer();
- void createDrawSurface();
- void drawSurface();
- int width;
- int height;
- std::vector<std::shared_ptr<Program> > sceneProgram;
- std::vector<std::shared_ptr<Program> > modelProgram;
- unsigned int activeSceneProgram = 0;
- unsigned int activeModelProgram = 0;
- std::unique_ptr<Camera> camera;
- std::vector<std::shared_ptr<Model> > model;
- std::unique_ptr<Lightning> lightning;
- GLuint frameBuffer;
- GLuint frameTex;
- GLuint renderBuffer;
- GLuint surfaceVao;
- GLuint surfaceVbo;
- GLuint surfaceEbo;
- int x = 0, y = 0, old_x = 0, old_y = 0;
- bool stop = false;
- private:
- };
- #endif // SCENE_H
- // scene.h ==============================================================================================================
- // scene.cpp ==============================================================================================================
- #include "scene.h"
- #include <cmath>
- #include <glm/glm.hpp>
- #include <glm/gtc/matrix_transform.hpp>
- #include <glm/gtc/type_ptr.hpp>
- Scene::Scene(int _width, int _height)
- : width(_width), height(_height)
- {
- //ctor
- }
- Scene::~Scene()
- {
- //dtor
- }
- void Scene::init()
- {
- loadSceneProgram("data/shaders/scene.glvs", "data/shaders/scene.glfs");
- loadModelProgram("data/shaders/model.glvs", "data/shaders/model.glfs");
- activeModelProgram = 0;
- createFrameBuffer();
- createDrawSurface();
- model.push_back(std::make_shared<Cube>());
- //model.push_back(std::make_shared<Cone>());
- model.push_back(std::make_shared<Mirror>());
- model.push_back(std::make_shared<Mirror>());
- model.push_back(std::make_shared<Mirror>());
- model.push_back(std::make_shared<Mirror>());
- model.push_back(std::make_shared<Mirror>());
- model.push_back(std::make_shared<Mirror>());
- for (const auto &m: model) m->init(modelProgram[activeModelProgram]);
- camera = std::make_unique<Camera>(glm::vec3(0.0, 0.0, 10.0), glm::vec3(0.0, 0.0, 0.0), glm::vec3(0.0, 1.0, 0.0));
- camera->setProgram(modelProgram[activeModelProgram]);
- lightning = std::make_unique<Lightning>(modelProgram[activeModelProgram]);
- lightning->setAmbient(glm::vec3(0.0f, 0.0f, 0.0f));
- lightning->addLight(glm::vec3(0.0f, 6.0f, 0.0f), glm::vec3(10.0f, 10.0f, 0.0f), 0.05f);
- lightning->addLight(glm::vec3(0.0f, -6.0f, 0.0f), glm::vec3(10.0f, 10.0f, 0.0f), 0.05f);
- lightning->addLight(glm::vec3(6.0f, 0.0f, 0.0f), glm::vec3(2.0f, 0.0f, 0.0f), 0.05f);
- lightning->addLight(glm::vec3(-6.0f, 0.0f, 0.0f), glm::vec3(0.0f, 2.0f, 0.0f), 0.05f);
- lightning->addLight(glm::vec3(0.0f, 0.0f, 6.0f), glm::vec3(0.0f, 0.0f, 2.0f), 0.05f);
- lightning->addLight(glm::vec3(0.0f, 0.0f, -6.0f), glm::vec3(2.0f, 2.0f, 2.0f), 0.05f);
- glUseProgram(modelProgram[activeModelProgram]->getID());
- glm::mat4 proj = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 1.0f, 10000.0f);
- glUniformMatrix4fv(modelProgram[activeModelProgram]->getUniform("proj"), 1, GL_FALSE, glm::value_ptr(proj));
- glUseProgram(0);
- glUseProgram(sceneProgram[activeSceneProgram]->getID());
- glUniform1f(sceneProgram[activeSceneProgram]->getUniform("gamma"), 0.95f);
- glUseProgram(0);
- model[0]->setPosition(glm::vec3(0.0, 0.0, 0.0));
- model[1]->setPosition(glm::vec3(0.0, 0.0, -4.0));
- model[1]->setYaw(0.0f);
- model[1]->setScale(4.0f);
- model[2]->setPosition(glm::vec3(4.0, 0.0, 0.0));
- model[2]->setYaw(-90.0f);
- model[2]->setScale(4.0f);
- model[3]->setPosition(glm::vec3(0.0, -4.0, 0.0));
- model[3]->setPitch(-90.0f);
- model[3]->setScale(4.0f);
- model[4]->setPosition(glm::vec3(0.0, 4.0, 0.0));
- model[4]->setPitch(90.0f);
- model[4]->setScale(4.0f);
- model[5]->setPosition(glm::vec3(0.0, 0.0, 4.0));
- model[5]->setYaw(180.0f);
- model[5]->setScale(4.0f);
- model[6]->setPosition(glm::vec3(-4.0, 0.0, 0.0));
- model[6]->setYaw(90.0f);
- model[6]->setScale(4.0f);
- std::static_pointer_cast<Mirror>(model[1])->addMirroredModel(model[0]);
- std::static_pointer_cast<Mirror>(model[2])->addMirroredModel(model[0]);
- std::static_pointer_cast<Mirror>(model[3])->addMirroredModel(model[0]);
- std::static_pointer_cast<Mirror>(model[4])->addMirroredModel(model[0]);
- std::static_pointer_cast<Mirror>(model[5])->addMirroredModel(model[0]);
- std::static_pointer_cast<Mirror>(model[6])->addMirroredModel(model[0]);
- }
- void Scene::draw()
- {
- glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
- glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- for(const auto &m : model) m->draw();
- drawSurface();
- }
- void Scene::eventLoop(SDL_Event &event)
- {
- if (event.type == SDL_KEYDOWN)
- {
- switch(event.key.keysym.sym)
- {
- case SDLK_SPACE:
- stop = !stop;
- break;
- case SDLK_LEFT:
- model[0]->changeYaw(-10.0f);
- break;
- case SDLK_RIGHT:
- model[0]->changeYaw(10.0f);
- break;
- case SDLK_UP:
- model[0]->changePitch(10.0f);
- break;
- case SDLK_DOWN:
- model[0]->changePitch(-10.0f);
- break;
- }
- }
- else if (event.type == SDL_MOUSEMOTION)
- {
- SDL_GetMouseState(&x, &y);
- if (event.button.button == 1)
- {
- camera->changeGamma(0.01f*(y - old_y));
- camera->changeTeta(0.01f*(x - old_x));
- }
- else if (event.button.button == 4)
- {
- camera->changeLength(0.1f*(y - old_y));
- }
- old_x = x;
- old_y = y;
- }
- }
- void Scene::animate(float dt)
- {
- if (!stop) for(const auto &m : model) m->animate(dt);
- }
- void Scene::loadSceneProgram(std::string vertName, std::string fragName, std::string geoName)
- {
- sceneProgram.push_back(std::make_shared<Program>(vertName, fragName, geoName));
- }
- void Scene::loadModelProgram(std::string vertName, std::string fragName, std::string geoName)
- {
- modelProgram.push_back(std::make_shared<Program>(vertName, fragName, geoName));
- }
- void Scene::createFrameBuffer()
- {
- glGenFramebuffers(1, &frameBuffer);
- glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
- glGenTextures(1, &frameTex);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, frameTex);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST_MIPMAP_NEAREST);
- glGenRenderbuffers(1, &renderBuffer);
- glBindRenderbuffer(GL_RENDERBUFFER, renderBuffer);
- glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
- glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, renderBuffer);
- glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, frameTex, 0);
- GLuint attachments[2] = {GL_COLOR_ATTACHMENT0, GL_DEPTH_STENCIL_ATTACHMENT};
- glDrawBuffers(1, attachments);
- GLenum e = glCheckFramebufferStatus(GL_FRAMEBUFFER);
- switch (e)
- {
- case GL_FRAMEBUFFER_UNDEFINED:
- std::cout << "FBO Undefined\n";
- break;
- case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT :
- std::cout << "FBO Incomplete Attachment\n";
- break;
- case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT :
- std::cout << "FBO Missing Attachment\n";
- break;
- case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER :
- std::cout << "FBO Incomplete Draw Buffer\n";
- break;
- case GL_FRAMEBUFFER_UNSUPPORTED :
- std::cout << "FBO Unsupported\n";
- break;
- }
- }
- void Scene::createDrawSurface()
- {
- GLfloat vertices[] =
- {
- -1.0f, 1.0f, 0.0f, 1.0f,
- 1.0f, 1.0f, 1.0f, 1.0f,
- 1.0f, -1.0f, 1.0f, 0.0f,
- -1.0f, -1.0f, 0.0f, 0.0f
- };
- GLuint elements[] =
- {
- 1, 3, 2,
- 1, 0, 3
- };
- glGenVertexArrays(1, &surfaceVao);
- glGenBuffers(1, &surfaceVbo);
- glGenBuffers(1, &surfaceEbo);
- glBindVertexArray(surfaceVao);
- glBindBuffer(GL_ARRAY_BUFFER, surfaceVbo);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, surfaceEbo);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
- GLint v1 = glGetAttribLocation(sceneProgram[activeSceneProgram]->getID(), "position");
- glEnableVertexAttribArray(v1);
- glVertexAttribPointer(v1, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), 0);
- GLint v2 = glGetAttribLocation(sceneProgram[activeSceneProgram]->getID(), "texcoord");
- glEnableVertexAttribArray(v2);
- glVertexAttribPointer(v2, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void*)(2*sizeof(GLfloat)));
- }
- void Scene::drawSurface()
- {
- glUseProgram(sceneProgram[activeSceneProgram]->getID());
- glBindFramebuffer(GL_FRAMEBUFFER, 0);
- glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glBindTexture(GL_TEXTURE_2D, frameBuffer);
- glGenerateTextureMipmap(frameBuffer);
- glBindVertexArray(surfaceVao);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, frameTex);
- glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
- glUseProgram(sceneProgram[activeSceneProgram]->getID());
- }
- // scene.cpp ==============================================================================================================
- // program.h ==============================================================================================================
- #ifndef PROGRAM_H
- #define PROGRAM_H
- #include <gl/glew.h>
- #include <gl/gl.h>
- #include <string>
- #include <vector>
- #include <map>
- class Program
- {
- public:
- Program(std::string _vertexName, std::string _fragmentName = "", std::string _geometryName = "",
- bool _is_framebuffer = false);
- virtual ~Program();
- GLuint getID() const { return id; }
- GLint getUniform(std::string name);
- protected:
- void createShader(GLenum type, std::string fileName);
- std::string loadFile(std::string fileName);
- bool is_framebuffer = false;
- GLuint id;
- std::vector<GLuint> shader;
- std::map<std::string, GLint> uniform;
- private:
- };
- #endif // PROGRAM_H
- // program.h ==============================================================================================================
- // program.cpp ==============================================================================================================
- #include "program.h"
- #include <GL/gl.h>
- #include <fstream>
- #include <iostream>
- #include <sstream>
- #include <glm/glm.hpp>
- #include <glm/gtc/matrix_transform.hpp>
- #include <glm/gtc/type_ptr.hpp>
- Program::Program(std::string _vertexName, std::string _fragmentName, std::string _geometryName,
- bool _is_framebuffer)
- : is_framebuffer(_is_framebuffer)
- {
- id = glCreateProgram();
- createShader(GL_VERTEX_SHADER, _vertexName);
- if (!_fragmentName.empty()) createShader(GL_FRAGMENT_SHADER, _fragmentName);
- if (!_geometryName.empty()) createShader(GL_GEOMETRY_SHADER, _geometryName);
- if (_fragmentName.empty())
- {
- const GLchar *feedback[] = {"outValue"};
- glTransformFeedbackVaryings(id, 1, feedback, GL_INTERLEAVED_ATTRIBS);
- }
- glLinkProgram(id);
- GLint programSuccess = GL_TRUE;
- glGetProgramiv(id, GL_LINK_STATUS, &programSuccess);
- if(programSuccess != GL_TRUE)
- {
- std::stringstream err;
- err << "Nie mozna utworzyc programu: " << glGetError();
- throw err.str();
- }
- glBindFragDataLocation(id, 0, "outColor");
- glUseProgram(id);
- if (!is_framebuffer)
- {
- glUniform1i(getUniform("tex1"), 0);
- glUniform1i(getUniform("tex2"), 1);
- glUniform1f(getUniform("scale"), 1.0f);
- glUniform1f(getUniform("overlap"), 1.0f);
- }
- else
- glUniform1i(getUniform("framebuffer"), 0);
- glUseProgram(0);
- }
- Program::~Program()
- {
- for (const auto &s: shader) glDeleteShader(s);
- glDeleteProgram(id);
- }
- GLint Program::getUniform(std::string name)
- {
- auto it = uniform.find(name);
- if (it == uniform.end())
- {
- GLint value = glGetUniformLocation(id, name.c_str());
- if (value != -1) uniform[name] = value;
- else return -1;
- }
- return uniform[name];
- }
- void Program::createShader(GLenum type, std::string fileName)
- {
- GLuint s;
- if (type == GL_VERTEX_SHADER) s = glCreateShader(GL_VERTEX_SHADER);
- else if (type == GL_FRAGMENT_SHADER) s = glCreateShader(GL_FRAGMENT_SHADER);
- else if (type == GL_GEOMETRY_SHADER) s = glCreateShader(GL_GEOMETRY_SHADER);
- const GLchar *source = loadFile(fileName).c_str();
- glShaderSource(s, 1, &source, nullptr);
- glCompileShader(s);
- GLint vShaderCompiled = GL_FALSE;
- glGetShaderiv(s, GL_COMPILE_STATUS, &vShaderCompiled);
- if (vShaderCompiled != GL_TRUE)
- {
- std::stringstream err;
- err << "Nie mozna skompilowac shadera: " << fileName << ". Blad: " << glGetError();
- throw err.str();
- }
- glAttachShader(id, s);
- shader.push_back(s);
- }
- std::string Program::loadFile(std::string fileName)
- {
- std::fstream file;
- file.open(fileName.c_str(), std::ios::in);
- std::stringstream buffer;
- buffer << file.rdbuf();
- std::string data(buffer.str());
- return data;
- }
- // program.cpp ==============================================================================================================
- // model.h ==============================================================================================================
- #ifndef MODEL_H
- #define MODEL_H
- #include <GL/glew.h>
- #include <SDL2/SDL_opengl.h>
- #include <vector>
- #include <memory>
- #include <glm/glm.hpp>
- #include "program.h"
- enum Type
- {
- CUBE = 1,
- MIRROR = 2,
- CONE = 3
- };
- //============================================================================================================
- // Model
- class Model
- {
- public:
- Model(Type _type, unsigned int vaoCount = 1, unsigned int vboCount = 1, unsigned int eboCount = 1,
- unsigned int texCount = 1);
- virtual ~Model();
- virtual void init(std::shared_ptr<Program> &_program) = 0;
- void draw(bool is_reflect = false);
- virtual void animate(float dt) = 0;
- virtual void setProgram(std::shared_ptr<Program> &_program) { program = _program; }
- void setPosition(glm::vec3 _position) { position = _position; }
- void changePosition(glm::vec3 _dp) { position += _dp; }
- glm::vec3 getPosition() const { return position; }
- void setPitch(float _pitch) { pitch = _pitch; }
- void changePitch(float dp) { pitch += dp; }
- float getPitch() const { return pitch; }
- void setYaw(float _yaw) { yaw = _yaw; }
- void changeYaw(float dy) { yaw += dy; }
- float getYaw() const { return yaw; }
- void setRoll(float _roll) { roll = _roll; }
- void changeRoll(float dr) { roll += dr; }
- float getRoll() const { return roll; }
- void setScale(float _scale) { scale = _scale; }
- void changeScale(float _ds) { scale += _ds; }
- float getScale() const { return scale; }
- Type getType() const { return type; }
- void setMatrix(glm::mat4 _matrix) { matrix = _matrix; }
- void changeMatrix(glm::mat4 _matrix);
- glm::mat4 getMatrix() const { return matrix; }
- protected:
- virtual void setAttrib() = 0;
- virtual void drawModel() = 0;
- std::vector<GLuint> vao;
- std::vector<GLuint> vbo;
- std::vector<GLuint> ebo;
- std::vector<GLuint> tex;
- std::shared_ptr<Program> program;
- glm::vec3 position;
- float pitch = 0.0f;
- float yaw = 0.0f;
- float roll = 0.0f;
- float scale = 1.0f;
- glm::mat4 matrix;
- glm::vec3 ambient;
- glm::vec3 diffuse;
- glm::vec3 specular;
- float shininess;
- glm::vec3 emission;
- private:
- Type type;
- };
- //============================================================================================================
- // Cube
- class Cube: public Model
- {
- public:
- Cube();
- ~Cube();
- void init(std::shared_ptr<Program> &_program);
- void animate(float dt);
- protected:
- void setAttrib();
- void drawModel();
- private:
- };
- //============================================================================================================
- // Mirror
- class Mirror: public Model
- {
- public:
- Mirror();
- ~Mirror();
- void init(std::shared_ptr<Program> &_program);
- void animate(float dt);
- void setProgram(std::shared_ptr<Program> &_program);
- void addMirroredModel(std::shared_ptr<Model> &_model);
- void removeMirroredModel(std::shared_ptr<Model> &_model);
- protected:
- void setAttrib();
- void drawModel();
- std::vector<std::shared_ptr<Model> > model;
- private:
- };
- //============================================================================================================
- // Cone
- class Cone: public Model
- {
- public:
- Cone();
- ~Cone();
- void init(std::shared_ptr<Program> &_program);
- void animate(float dt);
- protected:
- void setAttrib();
- void drawModel();
- private:
- };
- #endif // MODEL_H
- // model.h ==============================================================================================================
- // model.cpp ==============================================================================================================
- #include "model.h"
- #include <GL/gl.h>
- #include <SDL2/SDL_image.h>
- #include <string>
- #include <sstream>
- #include <iostream>
- #include <algorithm>
- #include <cmath>
- #include <glm/gtc/matrix_transform.hpp>
- #include <glm/gtc/type_ptr.hpp>
- #include <fstream>
- //============================================================================================================
- // Model
- Model::Model(Type _type, unsigned int vaoCount, unsigned int vboCount, unsigned int eboCount, unsigned int texCount)
- : type(_type)
- {
- vao.resize(vaoCount);
- vbo.resize(vboCount);
- ebo.resize(eboCount);
- tex.resize(texCount);
- glGenVertexArrays(vao.size(), vao.data());
- glGenBuffers(vbo.size(), vbo.data());
- glGenBuffers(ebo.size(), ebo.data());
- glGenTextures(tex.size(), tex.data());
- }
- Model::~Model()
- {
- glDeleteBuffers(ebo.size(), ebo.data());
- glDeleteBuffers(vbo.size(), vbo.data());
- glDeleteVertexArrays(vao.size(), vao.data());
- glDeleteTextures(tex.size(), tex.data());
- }
- void Model::draw(bool is_reflect)
- {
- glUseProgram(program->getID());
- matrix = glm::translate(matrix, position);
- matrix = glm::rotate(matrix, glm::radians(pitch), glm::vec3(1, 0, 0));
- matrix = glm::rotate(matrix, glm::radians(yaw), glm::vec3(0, 1, 0));
- matrix = glm::rotate(matrix, glm::radians(roll), glm::vec3(0, 0, 1));
- glUniform1f(program->getUniform("scale"), scale);
- glUniformMatrix4fv(program->getUniform("model"), 1, GL_FALSE, glm::value_ptr(matrix));
- glUniform4f(program->getUniform("ambient"), ambient.r, ambient.g, ambient.b, 1.0);
- glUniform4f(program->getUniform("diffuse"), diffuse.r, diffuse.g, diffuse.b, 1.0);
- glUniform4f(program->getUniform("specular"), specular.r, specular.g, specular.b, 1.0);
- glUniform1f(program->getUniform("shininess"), shininess);
- glUniform4f(program->getUniform("emission"), emission.r, emission.g, emission.b, 1.0);
- if (is_reflect)
- {
- changeMatrix(glm::scale(getMatrix(), glm::vec3(1.0f, -1.0f, 1.0f)));
- glUniform4f(program->getUniform("ambient"), 0.1, 0.1, 0.1, 1.0);
- glUniform4f(program->getUniform("diffuse"), 0.2, 0.2, 0.2, 1.0);
- }
- setAttrib();
- drawModel();
- matrix = glm::mat4();
- glUseProgram(0);
- }
- void Model::changeMatrix(glm::mat4 _matrix)
- {
- matrix = _matrix;
- glUniformMatrix4fv(program->getUniform("model"), 1, GL_FALSE, glm::value_ptr(matrix));
- }
- //============================================================================================================
- // Cube
- Cube::Cube()
- : Model(CUBE, 1, 1, 1, 2)
- {
- ambient = glm::vec3(0.3, 0.3, 0.3);
- diffuse = glm::vec3(0.4, 0.4, 0.4);
- specular = glm::vec3(0.3, 0.3, 0.3);
- shininess = 12;
- emission = glm::vec3(0.0, 0.0, 0.0);
- }
- Cube::~Cube()
- {
- }
- void Cube::init(std::shared_ptr<Program> &_program)
- {
- program = _program;
- glBindVertexArray(vao[0]);
- GLfloat vertices[] =
- {
- 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, // 0
- -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 0.0, 0.0, // 1
- -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 0.0, 1.0, // 2
- 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, // 3
- 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 0.0, 1.0, // 4
- -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0, // 5
- -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 0.0, // 6
- 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 0.0, 0.0, // 7
- };
- glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
- GLuint elements[] =
- {
- 0, 1, 2,
- 0, 2, 3,
- 5, 6, 4,
- 4, 6, 7,
- 4, 0, 3,
- 4, 7, 0,
- 5, 2, 1,
- 5, 1, 6,
- 0, 6, 1,
- 0, 7, 6,
- 2, 5, 3,
- 5, 4, 3,
- };
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo[0]);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
- SDL_Surface *img;
- std::string file;
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, tex[0]);
- file = "data/textures/tex1.png";
- img = IMG_Load(file.c_str());
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_BYTE, img->pixels);
- SDL_FreeSurface(img);
- glGenerateMipmap(GL_TEXTURE_2D);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST_MIPMAP_NEAREST);
- glActiveTexture(GL_TEXTURE1);
- glBindTexture(GL_TEXTURE_2D, tex[1]);
- file = "data/textures/tex2.png";
- img = IMG_Load(file.c_str());
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_BYTE,
- img->pixels);
- SDL_FreeSurface(img);
- glGenerateMipmap(GL_TEXTURE_2D);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST_MIPMAP_NEAREST);
- }
- void Cube::animate(float dt)
- {
- }
- void Cube::setAttrib()
- {
- glBindVertexArray(vao[0]);
- glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
- GLint value1 = glGetAttribLocation(program->getID(), "position");
- glEnableVertexAttribArray(value1);
- glVertexAttribPointer(value1, 3, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), 0);
- GLint value2 = glGetAttribLocation(program->getID(), "normal");
- glEnableVertexAttribArray(value2);
- glVertexAttribPointer(value2, 3, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), (void*)(3*sizeof(GLfloat)));
- GLint value3 = glGetAttribLocation(program->getID(), "texcoord");
- glEnableVertexAttribArray(value3);
- glVertexAttribPointer(value3, 2, GL_FLOAT, GL_FALSE, 8*sizeof(float), (void*)(6*sizeof(float)));
- }
- void Cube::drawModel()
- {
- glBindVertexArray(vao[0]);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, tex[0]);
- glActiveTexture(GL_TEXTURE1);
- glBindTexture(GL_TEXTURE_2D, tex[1]);
- glDrawElements(GL_TRIANGLES, 3*12, GL_UNSIGNED_INT, 0);
- }
- //============================================================================================================
- // Mirror
- Mirror::Mirror()
- : Model(MIRROR, 1, 1, 1, 2)
- {
- ambient = glm::vec3(0.1, 0.1, 0.1);
- diffuse = glm::vec3(0.5, 0.5, 0.5);
- specular = glm::vec3(0.8, 0.8, 0.8);
- shininess = 52;
- emission = glm::vec3(0.2, 0.2, 0.2);
- }
- Mirror::~Mirror()
- {
- }
- void Mirror::init(std::shared_ptr<Program> &_program)
- {
- program = _program;
- glBindVertexArray(vao[0]);
- glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
- GLfloat vertices[] =
- {
- -1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, // 0
- 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, // 1
- 1.0, -1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, // 2
- -1.0, -1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, // 3
- };
- glBufferData(GL_ARRAY_BUFFER, 32*sizeof(GLfloat), vertices, GL_STATIC_DRAW);
- GLint elements[] =
- {
- 1, 3, 2,
- 1, 0, 3
- };
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo[0]);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6*sizeof(GLint), elements, GL_STATIC_DRAW);
- SDL_Surface *img;
- std::string file;
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, tex[0]);
- file = "data/textures/tex3.png";
- img = IMG_Load(file.c_str());
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_BYTE, img->pixels);
- SDL_FreeSurface(img);
- glGenerateMipmap(GL_TEXTURE_2D);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST_MIPMAP_NEAREST);
- glActiveTexture(GL_TEXTURE1);
- glBindTexture(GL_TEXTURE_2D, tex[1]);
- file = "data/textures/tex4.png";
- img = IMG_Load(file.c_str());
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_BYTE, img->pixels);
- SDL_FreeSurface(img);
- glGenerateMipmap(GL_TEXTURE_2D);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST_MIPMAP_NEAREST);
- }
- void Mirror::animate(float dt)
- {
- }
- void Mirror::setAttrib()
- {
- glBindVertexArray(vao[0]);
- glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
- GLint value1 = glGetAttribLocation(program->getID(), "position");
- glEnableVertexAttribArray(value1);
- glVertexAttribPointer(value1, 3, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), 0);
- GLint value2 = glGetAttribLocation(program->getID(), "normal");
- glEnableVertexAttribArray(value2);
- glVertexAttribPointer(value2, 3, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), (void*)(3*sizeof(GLfloat)));
- GLint value3 = glGetAttribLocation(program->getID(), "texcoord");
- glEnableVertexAttribArray(value3);
- glVertexAttribPointer(value3, 2, GL_FLOAT, GL_FALSE, 8*sizeof(float), (void*)(6*sizeof(float)));
- }
- void Mirror::drawModel()
- {
- glClear(GL_STENCIL_BUFFER_BIT);
- glEnable(GL_STENCIL_TEST);
- glStencilFunc(GL_ALWAYS, 1, 1);
- glStencilOp(GL_ZERO, GL_ZERO, GL_REPLACE);
- glStencilMask(1);
- glDepthMask(GL_FALSE);
- glBindVertexArray(vao[0]);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, tex[0]);
- glActiveTexture(GL_TEXTURE1);
- glBindTexture(GL_TEXTURE_2D, tex[1]);
- glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
- glDepthMask(GL_TRUE);
- glStencilFunc(GL_EQUAL, 1, 1);
- for (const auto &m: model)
- {
- glm::vec3 p = position - m->getPosition();
- float lenp = std::sqrt(p.x*p.x + p.y*p.y + p.z*p.z);
- glm::vec3 n = glm::vec3(sin(glm::radians(getYaw())*cos(glm::radians(getPitch()))),
- sin(glm::radians(getPitch())),
- cos(glm::radians(getYaw()))*cos(glm::radians(getPitch())));
- float dot = p.x*n.x + p.y*n.y + p.z*n.z;
- glm::vec3 d = dot*n;
- float alfa = acos(std::fabs(dot)/lenp);
- m->changeMatrix(glm::translate(m->getMatrix(), 2.0f*d));
- m->changeMatrix(glm::rotate(m->getMatrix(), glm::radians(180.0f) - 2.0f*alfa, glm::vec3(
- sin(glm::radians(getYaw()))*cos(glm::radians(getPitch())),
- sin(glm::radians(getPitch())),
- cos(glm::radians(getYaw()))*cos(glm::radians(getPitch()))
- )));
- glDisable(GL_CULL_FACE);
- m->draw(true);
- glEnable(GL_CULL_FACE);
- }
- glDisable(GL_STENCIL_TEST);
- }
- void Mirror::setProgram(std::shared_ptr<Program> &_program)
- {
- program = _program;
- for (const auto &m: model) m->setProgram(_program);
- }
- void Mirror::addMirroredModel(std::shared_ptr<Model> &_model)
- {
- model.push_back(_model);
- }
- void Mirror::removeMirroredModel(std::shared_ptr<Model> &_model)
- {
- auto it = std::find(model.begin(), model.end(), _model);
- if (it != model.end()) model.erase(it);
- }
- //============================================================================================================
- // Cone
- Cone::Cone()
- : Model(CONE, 1, 1, 1, 2)
- {
- ambient = glm::vec3(0.3, 0.3, 0.3);
- diffuse = glm::vec3(0.4, 0.4, 0.4);
- specular = glm::vec3(0.3, 0.3, 0.3);
- shininess = 12;
- emission = glm::vec3(0.0, 0.0, 0.0);
- }
- Cone::~Cone()
- {
- }
- void Cone::init(std::shared_ptr<Program> &_program)
- {
- program = _program;
- glBindVertexArray(vao[0]);
- glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
- GLfloat vertices[] =
- {
- // vertices // normal // texcoord
- 1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 0.0, 0.0, // 0
- 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, // 2
- 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 0.0, 1.0, // 1
- -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 0.0, 0.0, // 0
- -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 0.0, // 3
- 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, // 2
- 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 0.0, // 1
- 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.5, 0.5, // 4
- -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 0.0, 0.0, // 0
- 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 0.0, 0.0, // 1
- 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 0.0, // 2
- 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.5, 0.5, // 4
- 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.5, 0.5, // 4
- 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 0.0, 0.0, // 2
- -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 0.0, // 3
- -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 0.0, // 0
- 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.5, 0.5, // 4
- -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 0.0, 0.0 // 3
- };
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, tex[0]);
- SDL_Surface *img = IMG_Load("data/textures/tex1.png");
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_BYTE, img->pixels);
- SDL_FreeSurface(img);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glActiveTexture(GL_TEXTURE1);
- glBindTexture(GL_TEXTURE_2D, tex[1]);
- img = IMG_Load("data/textures/tex4.png");
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_BYTE, img->pixels);
- SDL_FreeSurface(img);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- }
- void Cone::animate(float dt)
- {
- }
- void Cone::setAttrib()
- {
- glBindVertexArray(vao[0]);
- glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
- GLint value1 = glGetAttribLocation(program->getID(), "position");
- glEnableVertexAttribArray(value1);
- glVertexAttribPointer(value1, 3, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), 0);
- GLint value2 = glGetAttribLocation(program->getID(), "normal");
- glEnableVertexAttribArray(value2);
- glVertexAttribPointer(value2, 3, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), (void*)(3*sizeof(GLfloat)));
- GLint value3 = glGetAttribLocation(program->getID(), "texcoord");
- glEnableVertexAttribArray(value3);
- glVertexAttribPointer(value3, 2, GL_FLOAT, GL_FALSE, 8*sizeof(float), (void*)(6*sizeof(float)));
- }
- void Cone::drawModel()
- {
- glBindVertexArray(vao[0]);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, tex[0]);
- glDrawArrays(GL_TRIANGLES, 0, 18);
- }
- // model.cpp ==============================================================================================================
- // camera.h ==============================================================================================================
- #ifndef CAMERA_H
- #define CAMERA_H
- #include <glm/glm.hpp>
- #include <memory>
- #include "program.h"
- class Camera
- {
- public:
- Camera(glm::vec3 _pos, glm::vec3 _dir, glm::vec3 _up);
- virtual ~Camera();
- void setProgram(std::shared_ptr<Program> &_program);
- void setPos(glm::vec3 _pos);
- void changePos(glm::vec3 _dp);
- glm::vec3 getPos() const { return pos; }
- void setDir(glm::vec3 _dir);
- void changeDir(glm::vec3 _dd);
- glm::vec3 getDir() const { return dir; }
- void setUp(glm::vec3 _up);
- void changeUp(glm::vec3 _du);
- glm::vec3 getUp() const { return up; }
- void setGamma(float _gamma);
- void changeGamma(float _dg);
- float getGamma() const { return gamma; }
- void setTeta(float _teta);
- void changeTeta(float _dt);
- float getTeta() const { return teta; }
- void setLength(float _length);
- void changeLength(float _dl);
- float getLength() const { return length; }
- protected:
- void update();
- glm::vec3 pos;
- glm::vec3 dir;
- glm::vec3 up;
- float gamma;
- float teta;
- float length;
- private:
- std::shared_ptr<Program> program;
- };
- #endif // CAMERA_H
- // camera.h ==============================================================================================================
- // camera.cpp ==============================================================================================================
- #include "camera.h"
- #include <glm/gtc/matrix_transform.hpp>
- #include <glm/gtc/type_ptr.hpp>
- #include <cmath>
- Camera::Camera(glm::vec3 _pos, glm::vec3 _dir, glm::vec3 _up)
- : pos(_pos), dir(_dir), up(_up)
- {
- length = std::sqrt(pos.x*pos.x + pos.y*pos.y + pos.z*pos.z);
- gamma = asin(pos.y/length);
- teta = acos(pos.x/(length*cos(gamma)));
- }
- Camera::~Camera()
- {
- //dtor
- }
- void Camera::setProgram(std::shared_ptr<Program> &_program)
- {
- program = _program;
- update();
- }
- void Camera::setPos(glm::vec3 _pos)
- {
- pos = _pos;
- length = std::sqrt(pos.x*pos.x + pos.y*pos.y + pos.z*pos.z);
- update();
- }
- void Camera::changePos(glm::vec3 _dp)
- {
- pos += _dp;
- setPos(pos);
- update();
- }
- void Camera::setDir(glm::vec3 _dir)
- {
- dir = _dir;
- update();
- }
- void Camera::changeDir(glm::vec3 _dd)
- {
- dir += _dd;
- update();
- }
- void Camera::setUp(glm::vec3 _up)
- {
- up = _up;
- update();
- }
- void Camera::changeUp(glm::vec3 _du)
- {
- up += _du;
- update();
- }
- void Camera::setGamma(float _gamma)
- {
- gamma = _gamma;
- if (gamma > 1.5707963f) gamma = 1.5707963f;
- else if (gamma < -1.5707963f) gamma = -1.5707963f;
- pos.x = length*cos(gamma)*cos(teta);
- pos.y = length*sin(gamma);
- pos.z = length*cos(gamma)*sin(teta);
- update();
- }
- void Camera::changeGamma(float _dg)
- {
- gamma += _dg;
- setGamma(gamma);
- }
- void Camera::setTeta(float _teta)
- {
- teta = _teta;
- if (teta > 6.2831853f) teta = 0.0f;
- else if (teta < 0.0f) teta = 6.2831853f;
- pos.x = length*cos(gamma)*cos(teta);
- pos.y = length*sin(gamma);
- pos.z = length*cos(gamma)*sin(teta);
- update();
- }
- void Camera::changeTeta(float _dt)
- {
- teta += _dt;
- setTeta(teta);
- }
- void Camera::setLength(float _length)
- {
- length = _length;
- pos.x = length*cos(gamma)*cos(teta);
- pos.y = length*sin(gamma);
- pos.z = length*cos(gamma)*sin(teta);
- update();
- }
- void Camera::changeLength(float _dl)
- {
- length += _dl;
- setLength(length);
- }
- void Camera::update()
- {
- glUseProgram(program->getID());
- glm::mat4 view;
- view = glm::lookAt(pos, dir, up);
- glUniformMatrix4fv(program->getUniform("view"), 1, GL_FALSE, glm::value_ptr(view));
- glUseProgram(0);
- }
- // camera.cpp ==============================================================================================================
- // lightning.h ==============================================================================================================
- #ifndef LIGHTNING_H
- #define LIGHTNING_H
- #include <program.h>
- #include <glm/glm.hpp>
- #include <memory>
- class Lightning
- {
- public:
- Lightning(std::shared_ptr<Program> _program);
- virtual ~Lightning();
- void setAmbient(glm::vec3 _ambient);
- void addLight(glm::vec3 _position, glm::vec3 _color, float _attenuation = 1.0f);
- void removeLight(unsigned int number);
- unsigned int getLightsCount() const { return lightsCount; }
- protected:
- void update();
- std::shared_ptr<Program> program;
- unsigned int lightsCount = 0;
- const unsigned int maxLightsCount = 20;
- glm::vec3 ambient;
- std::vector<float> position;
- std::vector<float> color;
- std::vector<float> attenuation;
- private:
- };
- #endif // LIGHTNING_H
- // lightning.h ==============================================================================================================
- // lightning.cpp ==============================================================================================================
- #include "lightning.h"
- #include <iostream>
- Lightning::Lightning(std::shared_ptr<Program> _program)
- : program(_program)
- {
- //ctor
- }
- Lightning::~Lightning()
- {
- //dtor
- }
- void Lightning::setAmbient(glm::vec3 _ambient)
- {
- ambient = _ambient;
- glUseProgram(program->getID());
- glUniform4f(program->getUniform("lightAmbient"), ambient.x, ambient.y, ambient.z, 1.0f);
- glUseProgram(0);
- }
- void Lightning::addLight(glm::vec3 _position, glm::vec3 _color, float _attenuation)
- {
- lightsCount++;
- if (lightsCount > maxLightsCount)
- {
- lightsCount--;
- std::cout << "Lightning::addLight: Operacja anulowana, nie moze byc wiecej niz " << maxLightsCount << " zrodel swiatla\n";
- }
- position.push_back(_position.x);
- position.push_back(_position.y);
- position.push_back(_position.z);
- color.push_back(_color.r);
- color.push_back(_color.g);
- color.push_back(_color.b);
- color.push_back(1.0f);
- attenuation.push_back(_attenuation);
- update();
- }
- void Lightning::removeLight(unsigned int number)
- {
- lightsCount--;
- unsigned int pos = 3*(number - 1);
- position.erase(position.begin() + pos, position.begin() + pos + 2);
- pos = 4*(number - 1);
- color.erase(color.begin() + pos, color.begin() + pos + 3);
- attenuation.erase(attenuation.begin() + number);
- update();
- }
- void Lightning::update()
- {
- glUseProgram(program->getID());
- glUniform1i (program->getUniform("lightsCount"), lightsCount);
- glUniform3fv(program->getUniform("lightPos"), lightsCount, position.data());
- glUniform4fv(program->getUniform("lightColor"), lightsCount, color.data());
- glUniform1fv(program->getUniform("lightAttenuation"), lightsCount, attenuation.data());
- glUseProgram(0);
- }
- // lightning.cpp ==============================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement