mirror of https://github.com/mosra/magnum.git
8 changed files with 230 additions and 1 deletions
@ -0,0 +1,12 @@ |
|||||||
|
find_package(Qt4 REQUIRED) |
||||||
|
|
||||||
|
corrade_add_resource(Shaders shaders PhongShader.frag PhongShader.vert) |
||||||
|
set(Shaders_SRCS |
||||||
|
PhongShader.cpp |
||||||
|
${Shaders} |
||||||
|
) |
||||||
|
|
||||||
|
add_library(MagnumShaders SHARED ${Shaders_SRCS}) |
||||||
|
target_link_libraries(MagnumShaders ${MAGNUM_LIBRARY}) |
||||||
|
|
||||||
|
install(TARGETS MagnumShaders DESTINATION ${MAGNUM_LIBRARY_INSTALL_DIR}) |
||||||
@ -0,0 +1,50 @@ |
|||||||
|
/*
|
||||||
|
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||||
|
|
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Magnum is free software: you can redistribute it and/or modify |
||||||
|
it under the terms of the GNU Lesser General Public License version 3 |
||||||
|
only, as published by the Free Software Foundation. |
||||||
|
|
||||||
|
Magnum is distributed in the hope that it will be useful, |
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
GNU Lesser General Public License version 3 for more details. |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "PhongShader.h" |
||||||
|
|
||||||
|
#include "Utility/Resource.h" |
||||||
|
|
||||||
|
using namespace Corrade::Utility; |
||||||
|
|
||||||
|
namespace Magnum { namespace Shaders { |
||||||
|
|
||||||
|
PhongShader::PhongShader() { |
||||||
|
Resource rs("shaders"); |
||||||
|
Shader* vertexShader = Shader::fromData(Shader::Vertex, rs.get("PhongShader.vert")); |
||||||
|
Shader* fragmentShader = Shader::fromData(Shader::Fragment, rs.get("PhongShader.frag")); |
||||||
|
attachShader(vertexShader); |
||||||
|
attachShader(fragmentShader); |
||||||
|
|
||||||
|
bindAttribute(Vertex, "vertex"); |
||||||
|
bindAttribute(Normal, "normal"); |
||||||
|
|
||||||
|
link(); |
||||||
|
delete vertexShader; |
||||||
|
delete fragmentShader; |
||||||
|
|
||||||
|
ambientColorUniform = uniformLocation("ambientColor"); |
||||||
|
diffuseColorUniform = uniformLocation("diffuseColor"); |
||||||
|
specularColorUniform = uniformLocation("specularColor"); |
||||||
|
shininessUniform = uniformLocation("shininess"); |
||||||
|
transformationMatrixUniform = uniformLocation("transformationMatrix"); |
||||||
|
projectionMatrixUniform = uniformLocation("projectionMatrix"); |
||||||
|
lightUniform = uniformLocation("light"); |
||||||
|
lightAmbientColorUniform = uniformLocation("lightAmbientColor"); |
||||||
|
lightDiffuseColorUniform = uniformLocation("lightDiffuseColor"); |
||||||
|
lightSpecularColorUniform = uniformLocation("lightSpecularColor"); |
||||||
|
} |
||||||
|
|
||||||
|
}} |
||||||
@ -0,0 +1,34 @@ |
|||||||
|
#version 330 |
||||||
|
|
||||||
|
uniform vec3 ambientColor; |
||||||
|
uniform vec3 diffuseColor; |
||||||
|
uniform vec3 specularColor; |
||||||
|
uniform float shininess; |
||||||
|
|
||||||
|
uniform vec3 lightAmbientColor; |
||||||
|
uniform vec3 lightDiffuseColor; |
||||||
|
uniform vec3 lightSpecularColor; |
||||||
|
|
||||||
|
in vec3 transformedNormal; |
||||||
|
in vec3 lightDirection; |
||||||
|
|
||||||
|
out vec4 color; |
||||||
|
|
||||||
|
void main() { |
||||||
|
/* Ambient color */ |
||||||
|
color.rgb = ambientColor*lightAmbientColor; |
||||||
|
|
||||||
|
/* Add diffuse color */ |
||||||
|
float intensity = max(0.0, dot(transformedNormal, lightDirection)); |
||||||
|
color.rgb += diffuseColor*lightDiffuseColor*intensity; |
||||||
|
|
||||||
|
/* Add specular color, if needed */ |
||||||
|
if(intensity != 0) { |
||||||
|
vec3 reflection = reflect(-lightDirection, transformedNormal); |
||||||
|
float specularity = pow(max(0.0, dot(transformedNormal, reflection)), shininess); |
||||||
|
color.rgb += specularColor*lightSpecularColor*specularity; |
||||||
|
} |
||||||
|
|
||||||
|
/* Force alpha to 1 */ |
||||||
|
color.a = 1.0; |
||||||
|
} |
||||||
@ -0,0 +1,103 @@ |
|||||||
|
#ifndef Magnum_Shaders_PhongShader_h |
||||||
|
#define Magnum_Shaders_PhongShader_h |
||||||
|
/*
|
||||||
|
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||||
|
|
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Magnum is free software: you can redistribute it and/or modify |
||||||
|
it under the terms of the GNU Lesser General Public License version 3 |
||||||
|
only, as published by the Free Software Foundation. |
||||||
|
|
||||||
|
Magnum is distributed in the hope that it will be useful, |
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
GNU Lesser General Public License version 3 for more details. |
||||||
|
*/ |
||||||
|
|
||||||
|
/** @file
|
||||||
|
* @brief Class Magnum::Shaders::PhongShader |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "AbstractShaderProgram.h" |
||||||
|
|
||||||
|
namespace Magnum { namespace Shaders { |
||||||
|
|
||||||
|
/** @brief Phong shader */ |
||||||
|
class PhongShader: public AbstractShaderProgram { |
||||||
|
public: |
||||||
|
/** @brief Attribute */ |
||||||
|
enum Attribute { |
||||||
|
Vertex = 1, /**< @brief Vertex position (four-component vector) */ |
||||||
|
Normal = 2 /**< @brief Normal direction (three-component vector) */ |
||||||
|
}; |
||||||
|
|
||||||
|
/** @brief Constructor */ |
||||||
|
PhongShader(); |
||||||
|
|
||||||
|
/** @brief Object ambient color */ |
||||||
|
inline void setAmbientColorUniform(const Vector3& color) { |
||||||
|
setUniform(ambientColorUniform, color); |
||||||
|
} |
||||||
|
|
||||||
|
/** @brief Object diffuse color */ |
||||||
|
inline void setDiffuseColorUniform(const Vector3& color) { |
||||||
|
setUniform(diffuseColorUniform, color); |
||||||
|
} |
||||||
|
|
||||||
|
/** @brief Object specular color */ |
||||||
|
inline void setSpecularColorUniform(const Vector3& color) { |
||||||
|
setUniform(specularColorUniform, color); |
||||||
|
} |
||||||
|
|
||||||
|
/** @brief Object shininess */ |
||||||
|
inline void setShininessUniform(GLfloat shininess) { |
||||||
|
setUniform(shininessUniform, shininess); |
||||||
|
} |
||||||
|
|
||||||
|
/** @brief Transformation matrix */ |
||||||
|
inline void setTransformationMatrixUniform(const Matrix4& matrix) { |
||||||
|
setUniform(transformationMatrixUniform, matrix); |
||||||
|
} |
||||||
|
|
||||||
|
/** @brief Projection matrix */ |
||||||
|
inline void setProjectionMatrixUniform(const Matrix4& matrix) { |
||||||
|
setUniform(projectionMatrixUniform, matrix); |
||||||
|
} |
||||||
|
|
||||||
|
/** @brief Light position */ |
||||||
|
inline void setLightUniform(const Vector3& light) { |
||||||
|
setUniform(lightUniform, light); |
||||||
|
} |
||||||
|
|
||||||
|
/** @brief Light ambient color */ |
||||||
|
inline void setLightAmbientColorUniform(const Vector3& color) { |
||||||
|
setUniform(lightAmbientColorUniform, color); |
||||||
|
} |
||||||
|
|
||||||
|
/** @brief Light diffuse color */ |
||||||
|
inline void setLightDiffuseColorUniform(const Vector3& color) { |
||||||
|
setUniform(lightDiffuseColorUniform, color); |
||||||
|
} |
||||||
|
|
||||||
|
/** @brief Light specular color */ |
||||||
|
inline void setLightSpecularColorUniform(const Vector3& color) { |
||||||
|
setUniform(lightSpecularColorUniform, color); |
||||||
|
} |
||||||
|
|
||||||
|
private: |
||||||
|
GLint ambientColorUniform, |
||||||
|
diffuseColorUniform, |
||||||
|
specularColorUniform, |
||||||
|
shininessUniform, |
||||||
|
transformationMatrixUniform, |
||||||
|
projectionMatrixUniform, |
||||||
|
lightUniform, |
||||||
|
lightAmbientColorUniform, |
||||||
|
lightDiffuseColorUniform, |
||||||
|
lightSpecularColorUniform; |
||||||
|
}; |
||||||
|
|
||||||
|
}} |
||||||
|
|
||||||
|
#endif |
||||||
@ -0,0 +1,26 @@ |
|||||||
|
#version 330 |
||||||
|
|
||||||
|
uniform mat4 transformationMatrix; |
||||||
|
uniform mat4 projectionMatrix; |
||||||
|
uniform vec3 light; |
||||||
|
|
||||||
|
in vec4 vertex; |
||||||
|
in vec4 normal; |
||||||
|
|
||||||
|
out vec3 transformedNormal; |
||||||
|
out vec3 lightDirection; |
||||||
|
|
||||||
|
void main() { |
||||||
|
/* Transformed vertex position */ |
||||||
|
vec4 transformedVertex4 = transformationMatrix*vertex; |
||||||
|
vec3 transformedVertex = transformedVertex4.xyz/transformedVertex4.w; |
||||||
|
|
||||||
|
/* Transformed normal vector */ |
||||||
|
transformedNormal = normalize(mat3x3(transformationMatrix)*normal.xyz); |
||||||
|
|
||||||
|
/* Direction to the light */ |
||||||
|
lightDirection = normalize(light.xyz - transformedVertex); |
||||||
|
|
||||||
|
/* Transform the vertex */ |
||||||
|
gl_Position = projectionMatrix*transformationMatrix*vertex; |
||||||
|
} |
||||||
Loading…
Reference in new issue