shaderSimpleFlat.cpp 7.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*******************************************************************************
* 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: http://cgogn.unistra.fr/                                           *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/
Sylvain Thery's avatar
Sylvain Thery committed
24
#define CGoGN_UTILS_DLL_EXPORT 1
25 26 27 28 29 30 31 32 33 34 35
#include <GL/glew.h>
#include "Utils/Shaders/shaderSimpleFlat.h"

namespace CGoGN
{

namespace Utils
{

#include "shaderSimpleFlat.vert"
#include "shaderSimpleFlat.frag"
Sylvain Thery's avatar
Sylvain Thery committed
36 37
#include "shaderSimpleFlatClip.vert"
#include "shaderSimpleFlatClip.frag"
38 39


Sylvain Thery's avatar
Sylvain Thery committed
40 41

ShaderSimpleFlat::ShaderSimpleFlat(bool withClipping, bool doubleSided):
42
	m_with_color(false),
43
	m_doubleSided(doubleSided),
44 45 46
	m_ambiant(Geom::Vec4f(0.05f,0.05f,0.1f,0.0f)),
	m_diffuse(Geom::Vec4f(0.1f,1.0f,0.1f,0.0f)),
	m_lightPos(Geom::Vec3f(10.0f,10.0f,1000.0f)),
Sylvain Thery's avatar
Sylvain Thery committed
47
	m_backColor(0.0f,0.0f,0.0f,0.0f),
48
	m_vboPos(NULL),
Sylvain Thery's avatar
Sylvain Thery committed
49 50
	m_vboColor(NULL),
	m_planeClip(Geom::Vec4f(0.0f,0.0f,0.0f,0.0f))
51
{
52 53
	std::string glxvert(GLSLShader::defines_gl());
	std::string glxfrag(GLSLShader::defines_gl());
54

Sylvain Thery's avatar
Sylvain Thery committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
	if (withClipping)
	{
		m_nameVS = "ShaderSimpleFlatClip_vs";
		m_nameFS = "ShaderSimpleFlatClip_fs";
		glxvert.append(vertexShaderClipText);
		if (doubleSided)
			glxfrag.append("#define DOUBLE_SIDED\n");
		glxfrag.append(fragmentShaderClipText);
	}
	else
	{
		m_nameVS = "ShaderSimpleFlat_vs";
		m_nameFS = "ShaderSimpleFlat_fs";
		// get choose GL defines (2 or 3)
		// ans compile shaders
		glxvert.append(vertexShaderText);
		if (doubleSided)
			glxfrag.append("#define DOUBLE_SIDED\n");
		glxfrag.append(fragmentShaderText);
	}
75

Sylvain Thery's avatar
Sylvain Thery committed
76
	loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str());
77 78 79 80 81
	// and get and fill uniforms
	getLocations();
	sendParams();
}

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
void ShaderSimpleFlat::setDoubleSided(bool doubleSided)
{
	if (doubleSided == m_doubleSided)
		return;

	std::string glxvert(GLSLShader::defines_gl());
	std::string glxfrag(GLSLShader::defines_gl());

	if (m_nameVS == "ShaderSimpleFlatClip_vs")
	{
		glxvert.append(vertexShaderClipText);
		if (doubleSided)
			glxfrag.append("#define DOUBLE_SIDED\n");
		glxfrag.append(fragmentShaderClipText);
	}
	else
	{
		glxvert.append(vertexShaderText);
		if (doubleSided)
			glxfrag.append("#define DOUBLE_SIDED\n");
		glxfrag.append(fragmentShaderText);
	}

	loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str());
	// and get and fill uniforms
	getLocations();
	sendParams();
}


112 113 114 115 116 117
void ShaderSimpleFlat::getLocations()
{
	bind();
	*m_unif_ambiant   = glGetUniformLocation(this->program_handler(), "materialAmbient");
	*m_unif_diffuse   = glGetUniformLocation(this->program_handler(), "materialDiffuse");
	*m_unif_lightPos  = glGetUniformLocation(this->program_handler(), "lightPosition");
Sylvain Thery's avatar
Sylvain Thery committed
118 119
	*m_unif_backColor  = glGetUniformLocation(this->program_handler(), "backColor");
	*m_unif_planeClip = glGetUniformLocation(this->program_handler(), "planeClip");
120 121 122 123 124 125 126 127 128
	unbind();
}

void ShaderSimpleFlat::sendParams()
{
	bind();
	glUniform4fv(*m_unif_ambiant,  1, m_ambiant.data());
	glUniform4fv(*m_unif_diffuse,  1, m_diffuse.data());
	glUniform3fv(*m_unif_lightPos, 1, m_lightPos.data());
Sylvain Thery's avatar
Sylvain Thery committed
129 130 131
	glUniform4fv(*m_unif_backColor,  1, m_backColor.data());
	if (*m_unif_planeClip > 0)
		glUniform4fv(*m_unif_planeClip, 1, m_planeClip.data());
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
	unbind();
}

void ShaderSimpleFlat::setAmbiant(const Geom::Vec4f& ambiant)
{
	bind();
	glUniform4fv(*m_unif_ambiant,1, ambiant.data());
	m_ambiant = ambiant;
	unbind();
}

void ShaderSimpleFlat::setDiffuse(const Geom::Vec4f& diffuse)
{
	bind();
	glUniform4fv(*m_unif_diffuse,1, diffuse.data());
	m_diffuse = diffuse;
	unbind();
}


void ShaderSimpleFlat::setLightPosition(const Geom::Vec3f& lightPos)
{
	bind();
	glUniform3fv(*m_unif_lightPos,1,lightPos.data());
	m_lightPos = lightPos;
	unbind();
}


Sylvain Thery's avatar
Sylvain Thery committed
161 162 163 164 165 166 167 168 169
void ShaderSimpleFlat::setBackColor(const Geom::Vec4f& back)
{
	bind();
	glUniform4fv(*m_unif_backColor,1, back.data());
	m_backColor = back;
	unbind();
}


170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
void ShaderSimpleFlat::setParams(const Geom::Vec4f& ambiant, const Geom::Vec4f& diffuse, const Geom::Vec3f& lightPos)
{
	m_ambiant = ambiant;
	m_diffuse = diffuse;
	m_lightPos = lightPos;
	sendParams();
}

unsigned int ShaderSimpleFlat::setAttributeColor(VBO* vbo)
{
	m_vboColor = vbo;
	if (!m_with_color)
	{
		m_with_color=true;
		// set the define and recompile shader
185
		std::string gl3vert(GLSLShader::defines_gl());
186 187
		gl3vert.append("#define WITH_COLOR 1\n");
		gl3vert.append(vertexShaderText);
188
		std::string gl3frag(GLSLShader::defines_gl());
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
		gl3frag.append("#define WITH_COLOR 1\n");
		gl3frag.append(fragmentShaderText);
		loadShadersFromMemory(gl3vert.c_str(), gl3frag.c_str());

		// and treat uniforms
		getLocations();
		sendParams();
	}
	// bind th VA with WBO
	bind();
	unsigned int id = bindVA_VBO("VertexColor", vbo);
	unbind();
	return id;
}

void ShaderSimpleFlat::unsetAttributeColor()
{
	m_vboColor = NULL;
	if (m_with_color)
	{
		m_with_color = false;
		// unbind the VA
		bind();
		unbindVA("VertexColor");
		unbind();
		// recompile shader
215
		std::string gl3vert(GLSLShader::defines_gl());
216
		gl3vert.append(vertexShaderText);
217
		std::string gl3frag(GLSLShader::defines_gl());
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
		gl3frag.append(fragmentShaderText);
		loadShadersFromMemory(gl3vert.c_str(), gl3frag.c_str());
		// and treat uniforms
		getLocations();
		sendParams();
	}
}

void ShaderSimpleFlat::restoreUniformsAttribs()
{
	getLocations();
	sendParams();

	bind();
	bindVA_VBO("VertexPosition", m_vboPos);
	if (m_vboColor)
		bindVA_VBO("VertexColor", m_vboColor);

	unbind();
}

unsigned int ShaderSimpleFlat::setAttributePosition(VBO* vbo)
{
	m_vboPos = vbo;
	bind();
	unsigned int id = bindVA_VBO("VertexPosition", vbo);
	unbind();
	return id;
}

Sylvain Thery's avatar
Sylvain Thery committed
248 249 250 251 252 253 254 255 256 257 258 259
void ShaderSimpleFlat::setClippingPlane(const Geom::Vec4f& plane)
{
	if (*m_unif_planeClip > 0)
	{
		m_planeClip = plane;
		bind();
		glUniform4fv(*m_unif_planeClip, 1, plane.data());
		unbind();
	}
}


260 261 262 263

} // namespace Utils

} // namespace CGoGN