From 5ad384462669f5d90c055651ef060252fc13d25c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 20 Oct 2012 21:59:47 +0200 Subject: [PATCH] PhongShader: fall back to GLSL 1.20 if 3.30/3.20 is not supported. --- src/Shaders/PhongShader.cpp | 11 +++++++++-- src/Shaders/PhongShader.frag | 7 +++++++ src/Shaders/PhongShader.h | 4 ++-- src/Shaders/PhongShader.vert | 11 +++++++++++ 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/Shaders/PhongShader.cpp b/src/Shaders/PhongShader.cpp index 382648f2c..8d79c69ba 100644 --- a/src/Shaders/PhongShader.cpp +++ b/src/Shaders/PhongShader.cpp @@ -17,14 +17,21 @@ #include +#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()) { + bindAttributeLocation(Position::Location, "position"); + bindAttributeLocation(Normal::Location, "normal"); + } link(); diff --git a/src/Shaders/PhongShader.frag b/src/Shaders/PhongShader.frag index 16a100336..c6d9c9be5 100644 --- a/src/Shaders/PhongShader.frag +++ b/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 */ diff --git a/src/Shaders/PhongShader.h b/src/Shaders/PhongShader.h index dfd4ab045..d334133a9 100644 --- a/src/Shaders/PhongShader.h +++ b/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: diff --git a/src/Shaders/PhongShader.vert b/src/Shaders/PhongShader.vert index 8fb57f05a..3872fcc74 100644 --- a/src/Shaders/PhongShader.vert +++ b/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;