| @@ -0,0 +1,34 @@ | ||
| # Blender v2.67 (sub 0) OBJ File: '' | ||
| # www.blender.org | ||
| v -10.000000 -10.000000 10.000000 | ||
| v -10.000000 -10.000000 -10.000000 | ||
| v 10.000000 -10.000000 -10.000000 | ||
| v 10.000000 -10.000000 10.000000 | ||
| v -10.000000 10.000000 10.000000 | ||
| v -10.000000 10.000000 -10.000000 | ||
| v 10.000000 10.000000 -10.000000 | ||
| v 10.000000 10.000000 10.000000 | ||
| vt 0.000000 0.000000 | ||
| vt 10.000000 0.000000 | ||
| vt 0.000000 10.000000 | ||
| vt 10.000000 10.000000 | ||
| vn -10.000000 0.000000 0.000000 | ||
| vn 0.000000 0.000000 -10.000000 | ||
| vn 10.000000 -0.000000 0.000000 | ||
| vn 0.000000 0.000000 10.000000 | ||
| vn 0.000000 -10.000000 0.000000 | ||
| vn 0.000000 10.000000 0.000000 | ||
| s off | ||
| f 5/1/1 1/3/1 6/2/1 | ||
| f 6/1/2 2/3/2 7/2/2 | ||
| f 7/1/3 4/4/3 8/2/3 | ||
| # hier | ||
| f 8/1/4 1/4/4 5/2/4 | ||
| f 1/1/5 3/4/5 2/2/5 | ||
| f 8/1/6 6/4/6 7/2/6 | ||
| f 6/2/1 1/3/1 2/4/1 | ||
| f 7/2/2 2/3/2 3/4/2 | ||
| f 3/3/3 4/4/3 7/1/3 | ||
| f 4/3/4 1/4/4 8/1/4 | ||
| f 4/3/5 3/4/5 1/1/5 | ||
| f 5/3/6 6/4/6 8/1/6 |
| @@ -0,0 +1,162 @@ | ||
| //LIT TEXTURE FRAGMENT SHADER | ||
| #version 330 | ||
| #define LIGHTBUFFERSIZE 5 | ||
|
|
||
| uniform mat4 modelMatrix; | ||
| uniform vec3 modelColor; | ||
| uniform vec3 cameraPos; | ||
| uniform vec3 lightCount; | ||
| uniform float shininess; | ||
|
|
||
| uniform sampler2D dTexture; | ||
| uniform sampler2D highlight; | ||
|
|
||
| in vec2 tCoord; | ||
| in vec3 fNormal; | ||
| in vec3 fPos; | ||
|
|
||
| out vec4 fColor; | ||
| out vec4 bColor; | ||
|
|
||
| struct DirLight | ||
| { | ||
| vec3 direction; | ||
|
|
||
| vec3 ambient; | ||
| vec3 diffuse; | ||
| vec3 specular; | ||
|
|
||
| }; uniform DirLight dirLight[LIGHTBUFFERSIZE]; | ||
|
|
||
| struct PointLight | ||
| { | ||
| vec3 position; | ||
|
|
||
| vec3 ambient; | ||
| vec3 diffuse; | ||
| vec3 specular; | ||
|
|
||
| float constant; | ||
| float linear; | ||
| float quadratic; | ||
|
|
||
| }; uniform PointLight pointLight[LIGHTBUFFERSIZE]; | ||
|
|
||
| struct SpotLight | ||
| { | ||
| vec3 position; | ||
| vec3 direction; | ||
|
|
||
| vec3 ambient; | ||
| vec3 diffuse; | ||
| vec3 specular; | ||
|
|
||
| float constant; | ||
| float linear; | ||
| float quadratic; | ||
|
|
||
| float cutOff; | ||
| float outerCutOff; | ||
|
|
||
| }; uniform SpotLight spotLight[LIGHTBUFFERSIZE]; | ||
|
|
||
|
|
||
| vec3 CalcDirLight(DirLight, vec3, vec3, vec3); | ||
| vec3 CalcPointLight(PointLight, vec3, vec3, vec3); | ||
| vec3 CalcSpotLight(SpotLight, vec3, vec3, vec3); | ||
|
|
||
|
|
||
| void main( void ) | ||
| { | ||
| vec3 wNormal = vec3 (modelMatrix * vec4(fNormal, 0)); | ||
| vec3 viewDir = normalize(cameraPos - fPos); | ||
| vec3 tColor = texture(dTexture, tCoord).xyz; | ||
|
|
||
| vec3 color; | ||
|
|
||
| for (int i = 0; i < lightCount.x; i++) | ||
| color += CalcDirLight(dirLight[i], wNormal, viewDir, tColor); | ||
|
|
||
| for (int i = 0; i < lightCount.y; i++) | ||
| color += CalcPointLight(pointLight[i], wNormal, viewDir, tColor); | ||
|
|
||
| for (int i = 0; i < lightCount.z; i++) | ||
| color += CalcSpotLight(spotLight[i], wNormal, viewDir, tColor); | ||
|
|
||
|
|
||
| if (modelColor != vec3(0)) | ||
| { | ||
| vec4 hColor = texture(highlight, tCoord); | ||
|
|
||
| if (hColor.a != 0) | ||
| color = mix(hColor.rgb * modelColor, color, 0.2f); | ||
| } | ||
| fColor = vec4(color, 1); | ||
|
|
||
| //float brightness = dot(fColor.rgb, vec3(0.2126, 0.7152, 0.0722) * 0.6); | ||
| //if(brightness > 1.0) | ||
| // bColor = vec4(fColor.rgb, 1.0); | ||
| } | ||
|
|
||
|
|
||
| vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir, vec3 tColor) | ||
| { | ||
| float diff = dot(-light.direction, normal) * 0.5f + 0.5f; | ||
| vec3 ref = reflect(light.direction, normal); | ||
| float spec = pow(max(dot(viewDir, ref), 0), shininess); | ||
|
|
||
| vec3 ambient = tColor * light.ambient; | ||
| vec3 diffuse = tColor * light.diffuse * diff; | ||
| vec3 specular = light.specular * spec; | ||
|
|
||
| return (ambient + diffuse + specular); | ||
| } | ||
|
|
||
|
|
||
| vec3 CalcPointLight(PointLight light, vec3 normal, vec3 viewDir, vec3 tColor) | ||
| { | ||
| vec3 lightDir = normalize(fPos - light.position); | ||
|
|
||
| float diff = max(0, dot(-lightDir, normal)); | ||
| vec3 ref = reflect(lightDir, normal); | ||
| float spec = pow(max(dot(viewDir, ref), 0), shininess); | ||
|
|
||
|
|
||
| float distance = length(light.position - fPos); | ||
| float attenuation = 1.0f / (light.constant + light.linear * distance + | ||
| light.quadratic * (distance * distance)); | ||
|
|
||
| vec3 ambient = light.ambient * modelColor; | ||
| vec3 diffuse = light.diffuse * diff * modelColor * attenuation; | ||
| vec3 specular = light.specular * spec * modelColor * attenuation; | ||
|
|
||
| return (ambient + diffuse + specular); | ||
| } | ||
|
|
||
|
|
||
| vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 viewDir, vec3 tColor) | ||
| { | ||
| vec3 lightDir = normalize(fPos - light.position); | ||
|
|
||
| float angle = dot(light.direction, lightDir); | ||
|
|
||
| float distance = length(light.position - fPos); | ||
| float attenuation = 1.0f / (light.constant + light.linear * distance + | ||
| light.quadratic * (distance * distance)); | ||
|
|
||
| vec3 ambient = light.ambient * modelColor; | ||
|
|
||
| if (angle < light.cutOff) | ||
| return ambient; | ||
|
|
||
| float factor = clamp((angle - light.cutOff) / light.outerCutOff, 0, 1); | ||
|
|
||
| float diff = max(0, dot(-lightDir, normal)); | ||
| vec3 ref = reflect(lightDir, normal); | ||
| float spec = pow(max(dot(viewDir, ref), 0), shininess); | ||
|
|
||
| vec3 diffuse = light.diffuse * diff * modelColor * attenuation; | ||
| vec3 specular = light.specular * spec * modelColor * attenuation; | ||
|
|
||
| return ambient + (diffuse + specular) * factor; | ||
| } |
| @@ -0,0 +1,22 @@ | ||
| //LIT TEXTURE VERTEX SHADER | ||
| #version 330 | ||
|
|
||
| uniform mat4 mvpMatrix; | ||
| uniform mat4 modelMatrix; | ||
|
|
||
| in vec3 vertex; | ||
| in vec3 normal; | ||
| in vec2 uv; | ||
|
|
||
| out vec3 fPos; | ||
| out vec3 fNormal; | ||
| out vec2 tCoord; | ||
|
|
||
| void main( void ) | ||
| { | ||
| gl_Position = mvpMatrix * vec4(vertex, 1.f); | ||
|
|
||
| fPos = (modelMatrix * vec4(vertex, 1.f)).xyz; | ||
| fNormal = normal; | ||
| tCoord = uv; | ||
| } |
| @@ -6,6 +6,6 @@ uniform sampler2D skybox; | ||
|
|
||
| void main() | ||
| { | ||
| fColor = texture(skybox, tCoord); | ||
| //fColor = vec4(1,1,1,1); | ||
| } | ||
| @@ -1,12 +1,13 @@ | ||
| #version 330 | ||
|
|
||
| in vec3 vertex; | ||
| in vec2 normal; | ||
| in vec2 uv; | ||
|
|
||
| uniform mat4 MVmatrix; | ||
|
|
||
| out vec2 tCoord; | ||
|
|
||
| void main( void ) | ||
| { | ||
| gl_Position = MVmatrix * vec4(vertex, 1); | ||
| @@ -25,7 +25,6 @@ void Board::ResetBoard() | ||
| { | ||
| Tile* tile = _boardArray[j][i]; | ||
| tile->setOwner(Id::empty); | ||
| } | ||
| } | ||
| } | ||
| @@ -120,7 +120,7 @@ void AbstractGame::run() | ||
| //Bloom::renderToFBO(); | ||
| Skybox::render(); | ||
| _render(); | ||
| //Bloom::blur(4); | ||
| _window->display(); | ||
|
|
||
| float timeSinceLastRender = renderClock.restart().asSeconds(); | ||
| @@ -9,7 +9,6 @@ | ||
|
|
||
| Skybox* Skybox::_skybox = NULL; | ||
|
|
||
| Skybox::Skybox() : GameObject("skybox") | ||
| { | ||
| setMesh(Mesh::load(config::MGE_MODEL_PATH + "skybox_cube.obj")); | ||
| @@ -0,0 +1,163 @@ | ||
| #include "mge/materials/ChangeColorMaterial.hpp" | ||
| #include "mge/core/Mesh.hpp" | ||
| #include "mge/core/GameObject.hpp" | ||
| #include "mge/config.hpp" | ||
|
|
||
| #include "mge/behaviours/DirectionalLight.hpp" | ||
| #include "mge/behaviours/PointLight.hpp" | ||
| #include "mge/behaviours/SpotLight.hpp" | ||
|
|
||
| #include <string> | ||
|
|
||
| //shader | ||
| ShaderProgram* ChangeColorMaterial::_shader = NULL; | ||
|
|
||
| //vertex uniforms | ||
| GLint ChangeColorMaterial::_uMVPMatrix = 0; | ||
| GLint ChangeColorMaterial::_uModelMatrix = 0; | ||
|
|
||
| //fragment uniforms | ||
| GLint ChangeColorMaterial::_uModelColor = 0; | ||
| GLint ChangeColorMaterial::_uShininess = 0; | ||
| GLint ChangeColorMaterial::_uCameraPos = 0; | ||
| GLint ChangeColorMaterial::_uTexture = 0; | ||
| GLint ChangeColorMaterial::_uHighlight = 0; | ||
|
|
||
| //vertex attributes | ||
| GLint ChangeColorMaterial::_aVertex = 0; | ||
| GLint ChangeColorMaterial::_aNormal = 0; | ||
| GLint ChangeColorMaterial::_aUv = 0; | ||
|
|
||
| ChangeColorMaterial::ChangeColorMaterial(Texture* pTexture, Texture* pHighlight, glm::vec3 pModelColor, float pShininess, std::vector<AbstractLight*>* pLights) | ||
| : _texture(pTexture), _highlight(pHighlight), _modelColor(pModelColor), _shininess(pShininess), _lights(pLights) | ||
| { | ||
| _lazyInitializeShader(); | ||
| } | ||
|
|
||
| void ChangeColorMaterial::_lazyInitializeShader() | ||
| { | ||
| if (!_shader) | ||
| { | ||
| _shader = new ShaderProgram(); | ||
|
|
||
| _shader->addShader(GL_VERTEX_SHADER, config::MGE_SHADER_PATH + "litColorChange.vs"); | ||
| _shader->addShader(GL_FRAGMENT_SHADER, config::MGE_SHADER_PATH + "litColorChange.fs"); | ||
| _shader->finalize(); | ||
|
|
||
| //vertex uniforms | ||
| _uMVPMatrix = _shader->getUniformLocation("mvpMatrix"); | ||
| _uModelMatrix = _shader->getUniformLocation("modelMatrix"); | ||
|
|
||
| //fragment uniforms | ||
| _uModelColor = _shader->getUniformLocation("modelColor"); | ||
| _uShininess = _shader->getUniformLocation("shininess"); | ||
| _uCameraPos = _shader->getUniformLocation("cameraPos"); | ||
| _uTexture = _shader->getUniformLocation("dTexture"); | ||
| _uHighlight = _shader->getUniformLocation("highlight"); | ||
|
|
||
| //vertex attributes | ||
| _aVertex = _shader->getAttribLocation("vertex"); | ||
| _aNormal = _shader->getAttribLocation("normal"); | ||
| _aUv = _shader->getAttribLocation("uv"); | ||
| } | ||
| } | ||
|
|
||
| glm::vec3 ChangeColorMaterial::getColor() { | ||
| return _modelColor; | ||
| } | ||
| void ChangeColorMaterial::setColor(glm::vec3 newColor) { | ||
| _modelColor = newColor; | ||
| } | ||
|
|
||
| void ChangeColorMaterial::render(Mesh* pMesh, const glm::mat4& pModelMatrix, const glm::mat4& pViewMatrix, const glm::mat4& pProjectionMatrix) | ||
| { | ||
| _shader->use(); | ||
|
|
||
| //uniforms | ||
| glm::mat4 mvpMatrix = pProjectionMatrix * pViewMatrix * pModelMatrix; | ||
| glUniformMatrix4fv(_uMVPMatrix, 1, GL_FALSE, glm::value_ptr(mvpMatrix)); | ||
| glUniformMatrix4fv(_uModelMatrix, 1, GL_FALSE, glm::value_ptr(pModelMatrix)); | ||
|
|
||
| glUniform3fv(_uCameraPos, 1, glm::value_ptr(((GameObject*)(World::get()->getMainCamera()))->getWorldPosition())); | ||
| glUniform1f(_uShininess, _shininess); | ||
| glUniform3fv(_uModelColor, 1, glm::value_ptr(_modelColor)); | ||
|
|
||
| renderLights(); | ||
| renderTextures(); | ||
| pMesh->streamToOpenGL(_aVertex, _aNormal, _aUv); | ||
| } | ||
|
|
||
| void ChangeColorMaterial::renderTextures() | ||
| { | ||
| glActiveTexture(GL_TEXTURE0); | ||
| glBindTexture(GL_TEXTURE_2D, _texture->getId()); | ||
| glUniform1i(_uTexture, 0); | ||
|
|
||
| glActiveTexture(GL_TEXTURE1); | ||
| glBindTexture(GL_TEXTURE_2D, _highlight->getId()); | ||
| glUniform1i(_uHighlight, 1); | ||
| } | ||
|
|
||
| void ChangeColorMaterial::renderLights() | ||
| { | ||
| int dLights = 0; | ||
| int pLights = 0; | ||
| int sLights = 0; | ||
|
|
||
| for each (AbstractLight* abstractLight in *_lights) | ||
| { | ||
| std::string lType = ""; | ||
| std::string num = ""; | ||
|
|
||
| if (typeid(*abstractLight) == typeid(DirectionalLight)) | ||
| { | ||
| num = std::to_string(dLights); | ||
| lType = "dirLight"; | ||
|
|
||
| glUniform3fv(_shader->getUniformLocation("dirLight[" + num + "].direction"), 1, glm::value_ptr(abstractLight->getDirection())); | ||
|
|
||
| dLights++; | ||
| } | ||
| else if (typeid(*abstractLight) == typeid(PointLight)) | ||
| { | ||
| num = std::to_string(pLights); | ||
| lType = "pointLight"; | ||
|
|
||
| PointLight* light = static_cast<PointLight*>(abstractLight); | ||
|
|
||
| glUniform3fv(_shader->getUniformLocation("pointLight[" + num + "].position"), 1, glm::value_ptr(light->getWorldPosition())); | ||
|
|
||
| glUniform1f(_shader->getUniformLocation("pointLight[" + num + "].constant"), light->_constant); | ||
| glUniform1f(_shader->getUniformLocation("pointLight[" + num + "].linear"), light->_linear); | ||
| glUniform1f(_shader->getUniformLocation("pointLight[" + num + "].quadratic"), light->_quadratic); | ||
| pLights++; | ||
| } | ||
| else if (typeid(*abstractLight) == typeid(SpotLight)) | ||
| { | ||
| num = std::to_string(sLights); | ||
| lType = "spotLight"; | ||
|
|
||
| SpotLight* light = static_cast<SpotLight*>(abstractLight); | ||
|
|
||
| glUniform3fv(_shader->getUniformLocation("spotLight[" + num + "].direction"), 1, glm::value_ptr(light->getDirection())); | ||
| glUniform3fv(_shader->getUniformLocation("spotLight[" + num + "].position"), 1, glm::value_ptr(light->getWorldPosition())); | ||
|
|
||
| glUniform1f(_shader->getUniformLocation("spotLight[" + num + "].constant"), light->_constant); | ||
| glUniform1f(_shader->getUniformLocation("spotLight[" + num + "].linear"), light->_linear); | ||
| glUniform1f(_shader->getUniformLocation("spotLight[" + num + "].quadratic"), light->_quadratic); | ||
|
|
||
| glUniform1f(_shader->getUniformLocation("spotLight[" + num + "].cutOff"), 1 - glm::sin(light->_cutOff * (glm::pi<float>() / 180.0f))); | ||
| glUniform1f(_shader->getUniformLocation("spotLight[" + num + "].outerCutOff"), glm::sin(light->_outerCutOff * (glm::pi<float>() / 180.0f))); | ||
|
|
||
| sLights++; | ||
| } | ||
|
|
||
| //All lights have these values, so it's unnececary to cast | ||
| glUniform3fv(_shader->getUniformLocation(lType + "[" + num + "].ambient"), 1, glm::value_ptr(abstractLight->_ambient)); | ||
| glUniform3fv(_shader->getUniformLocation(lType + "[" + num + "].diffuse"), 1, glm::value_ptr(abstractLight->_diffuse)); | ||
| glUniform3fv(_shader->getUniformLocation(lType + "[" + num + "].specular"), 1, glm::value_ptr(abstractLight->_specular)); | ||
| } | ||
| glUniform3fv(_shader->getUniformLocation("lightCount"), 1, glm::value_ptr(glm::vec3(dLights, pLights, sLights))); | ||
| } | ||
|
|
||
| ChangeColorMaterial::~ChangeColorMaterial() { } |
| @@ -0,0 +1,58 @@ | ||
| #ifndef CHANGECOLORMATERIAL_H | ||
| #define CHANGECOLORMATERIAL_H | ||
|
|
||
| #include <vector> | ||
| #include <glm.hpp> | ||
|
|
||
| #include "mge/materials/AbstractMaterial.hpp" | ||
| #include "mge/core/ShaderProgram.hpp" | ||
| #include "mge/behaviours/AbstractLight.hpp" | ||
| #include "mge/core/World.hpp" | ||
| #include "mge/core/Texture.hpp" | ||
|
|
||
| class ChangeColorMaterial : public AbstractMaterial | ||
| { | ||
| public: | ||
| ChangeColorMaterial(Texture* pTexture, Texture* pHighlight, glm::vec3 pModelColor = glm::vec3(0), float pShininess = 10.0f, std::vector<AbstractLight*>* pLights = World::get()->GetLights()); | ||
|
|
||
| virtual ~ChangeColorMaterial(); | ||
|
|
||
| virtual void render(Mesh* pMesh, const glm::mat4& pModelMatrix, const glm::mat4& pViewMatrix, const glm::mat4& pProjectionMatrix) override; | ||
|
|
||
| glm::vec3 getColor(); | ||
| void setColor(glm::vec3 pColor); | ||
|
|
||
| private: | ||
| static void _lazyInitializeShader(); | ||
| void renderLights(); | ||
| void renderTextures(); | ||
|
|
||
| static ShaderProgram* _shader; | ||
|
|
||
| //vertex uniforms | ||
| static GLint _uMVPMatrix; | ||
| static GLint _uModelMatrix; | ||
|
|
||
| //fragment uniforms | ||
| static GLint _uModelColor; | ||
| static GLint _uShininess; | ||
| static GLint _uCameraPos; | ||
| static GLint _uTexture; | ||
| static GLint _uHighlight; | ||
|
|
||
| //vertex attributes | ||
| static GLint _aVertex; | ||
| static GLint _aNormal; | ||
| static GLint _aUv; | ||
|
|
||
| //lights | ||
| std::vector<AbstractLight*>* _lights; | ||
|
|
||
| //settings | ||
| glm::vec3 _modelColor; | ||
| Texture* _texture; | ||
| Texture*_highlight; | ||
| float _shininess; | ||
| }; | ||
|
|
||
| #endif // LITMATERIAL_H |