Commit 987589be authored by Sylvain Thery's avatar Sylvain Thery

add environment mapping shader (cube mapping)

parent 249d9999
VARYING_FRAG vec3 EyeVector, Normal, LightDir,refl;
VARYING_FRAG vec3 Color;
uniform vec4 materialDiffuse;
uniform vec4 materialAmbient;
uniform samplerCube EnvMap;
uniform float blendCoef;
void main()
vec3 N = normalize (Normal);
vec3 L = normalize (LightDir);
float lambertTerm = dot(N,L);
vec4 finalColor = materialAmbient;
if (lambertTerm < 0.0)
N = -1.0*N;
lambertTerm = -1.0*lambertTerm;
if (lambertTerm > 0.0)
#ifndef WITH_COLOR
vec4 col = materialDiffuse;
vec4 col = vec4(Color,0.0) ;
vec3 R = reflect(-EyeVector,N);
finalColor += mix(col,texture(EnvMap,R),blendCoef) * lambertTerm;
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps *
* version 0.1 *
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your *
* option) any later version. *
* *
* This library 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 *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
* *
* Web site: *
* Contact information: *
* *
#include "Utils/GLSLShader.h"
#include "Utils/clippingShader.h"
#include "Geometry/vector_gen.h"
#include <string>
#ifdef WITH_QT
#include <QImage>
namespace CGoGN
namespace Utils
class ShaderEnvMap : public ClippingShader
// flag color per vertex or not
bool m_with_color;
// flag color per vertex or not
bool m_with_eyepos;
// shader sources OGL3
static std::string vertexShaderText;
static std::string fragmentShaderText;
// uniform locations
CGoGNGLuint m_unif_ambiant;
CGoGNGLuint m_unif_diffuse;
CGoGNGLuint m_unif_blend;
CGoGNGLuint m_unif_lightPos;
CGoGNGLuint m_unif_eyePos;
CGoGNGLuint m_unif_envMap;
Geom::Vec4f m_ambiant;
Geom::Vec4f m_diffuse;
float m_blend;
Geom::Vec3f m_lightPos;
Geom::Vec3f m_eyePos;
CGoGNGLuint m_texId;
VBO* m_vboPos;
VBO* m_vboNormal;
VBO* m_vboColor;
void getLocations();
void sendParams();
void restoreUniformsAttribs();
ShaderEnvMap(bool doubleSided = false, bool withEyePosition=false);
// inviduals parameter setting functions
void setAmbiant(const Geom::Vec4f& ambiant);
void setDiffuse(const Geom::Vec4f& diffuse);
void setSpecular(const Geom::Vec4f& specular);
void setBlendCoef(float blend);
void setLightPosition(const Geom::Vec3f& lp);
/// set eye position for VR environement
void setEyePosition(const Geom::Vec3f& ep);
const Geom::Vec4f& getAmbiant() const { return m_ambiant; }
const Geom::Vec4f& getDiffuse() const { return m_diffuse; }
float getBlendCoef() const { return m_blend; }
const Geom::Vec3f& getLightPosition() const { return m_lightPos; }
* set all parameter in on call (one bind also)
void setParams(const Geom::Vec4f& ambiant, const Geom::Vec4f& diffuse, float blend, const Geom::Vec3f& lightPos);
// attributes
unsigned int setAttributePosition(VBO* vbo);
unsigned int setAttributeNormal(VBO* vbo);
// optional attributes
unsigned int setAttributeColor(VBO* vbo);
void unsetAttributeColor();
void setCubeMap(unsigned int sz, unsigned char* Xpos, unsigned char* Ypos, unsigned char* Zpos, unsigned char* Xneg, unsigned char* Yneg, unsigned char* Zneg);
* set envmap texture image
* image looks likre:
* Y
* Y
* @parameter sz size of edge of cube in pixel
* @parameter ptr pointer on image data (RGB RGB RGB ....)
bool setCubeMapImg(unsigned int sz, unsigned char *ptr);
* set colored plane for testing
bool setCubeMapPipo();
#ifdef WITH_QT
bool setCubeMap(const std::string& filename);
void predraw();
void postdraw();
} // namespace Utils
} // namespace CGoGN
ATTRIBUTE vec3 VertexPosition, VertexNormal;
ATTRIBUTE vec3 VertexColor;
uniform mat4 ModelViewProjectionMatrix;
uniform mat4 ModelViewMatrix;
uniform mat4 NormalMatrix;
uniform vec3 lightPosition;
VARYING_VERT vec3 EyeVector, Normal, LightDir;
VARYING_VERT vec3 Color;
uniform vec3 eyePosition;
void main ()
Normal = vec3 (NormalMatrix * vec4 (VertexNormal, 0.0));
vec3 Position = vec3 (ModelViewMatrix * vec4 (VertexPosition, 1.0));
LightDir = Position - lightPosition;
EyeVector = eyePosition-Position;
EyeVector = -Position;
Color = VertexColor;
gl_Position = ModelViewProjectionMatrix * vec4 (VertexPosition, 1.0);
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment