Browse Source

PhongShader: fall back to GLSL 1.20 if 3.30/3.20 is not supported.

pull/7/head
Vladimír Vondruš 14 years ago
parent
commit
5ad3844626
  1. 11
      src/Shaders/PhongShader.cpp
  2. 7
      src/Shaders/PhongShader.frag
  3. 4
      src/Shaders/PhongShader.h
  4. 11
      src/Shaders/PhongShader.vert

11
src/Shaders/PhongShader.cpp

@ -17,14 +17,21 @@
#include <Utility/Resource.h>
#include "Extensions.h"
#include "Shader.h"
namespace Magnum { namespace Shaders {
PhongShader::PhongShader() {
Corrade::Utility::Resource rs("MagnumShaders");
attachShader(Shader::fromData(Version::GL330, Shader::Type::Vertex, rs.get("PhongShader.vert")));
attachShader(Shader::fromData(Version::GL330, Shader::Type::Fragment, rs.get("PhongShader.frag")));
Version v = Context::current()->isVersionSupported(Version::GL320) ? Version::GL320 : Version::GL210;
attachShader(Shader::fromData(v, Shader::Type::Vertex, rs.get("PhongShader.vert")));
attachShader(Shader::fromData(v, Shader::Type::Fragment, rs.get("PhongShader.frag")));
if(!Context::current()->isExtensionSupported<Extensions::GL::ARB::explicit_attrib_location>()) {
bindAttributeLocation(Position::Location, "position");
bindAttributeLocation(Normal::Location, "normal");
}
link();

7
src/Shaders/PhongShader.frag

@ -1,3 +1,8 @@
#if __VERSION__ == 120
#define in varying
#define color gl_FragColor
#endif
uniform vec3 ambientColor = vec3(0.0, 0.0, 0.0);
uniform vec3 diffuseColor;
uniform vec3 specularColor = vec3(1.0, 1.0, 1.0);
@ -8,7 +13,9 @@ in vec3 transformedNormal;
in vec3 lightDirection;
in vec3 cameraDirection;
#if __VERSION__ != 120
out vec4 color;
#endif
void main() {
/* Ambient color */

4
src/Shaders/PhongShader.h

@ -30,8 +30,8 @@ namespace Magnum { namespace Shaders {
/**
@brief Phong shader
@requires_gl33 The shader is written in GLSL 3.3, although it should be trivial
to port it to older versions.
If supported, uses GLSL 3.20 and @extension{ARB,explicit_attrib_location},
otherwise falls back to GLSL 1.20.
*/
class SHADERS_EXPORT PhongShader: public AbstractShaderProgram {
public:

11
src/Shaders/PhongShader.vert

@ -1,9 +1,20 @@
#if __VERSION__ == 120
#define in attribute
#define out varying
#endif
uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform vec3 light;
#if __VERSION__ != 120 && defined(GL_ARB_explicit_attrib_location)
#extension GL_ARB_explicit_attrib_location: enable
layout(location = 0) in vec4 position;
layout(location = 1) in vec3 normal;
#else
in vec4 position;
in vec3 normal;
#endif
out vec3 transformedNormal;
out vec3 lightDirection;

Loading…
Cancel
Save