shaderSimpleFlat.cpp 6.87 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                                        *
*                                                                              *
*******************************************************************************/
24

Sylvain Thery's avatar
Sylvain Thery committed
25
#define CGoGN_UTILS_DLL_EXPORT 1
26 27 28 29 30 31 32 33 34 35 36
#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
37 38
#include "shaderSimpleFlatClip.vert"
#include "shaderSimpleFlatClip.frag"
39

Sylvain Thery's avatar
Sylvain Thery committed
40
ShaderSimpleFlat::ShaderSimpleFlat(bool withClipping, bool doubleSided):
41 42 43 44
	m_with_color(false),
	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
45
	m_backColor(0.0f,0.0f,0.0f,0.0f),
46
	m_vboPos(NULL),
Sylvain Thery's avatar
Sylvain Thery committed
47 48
	m_vboColor(NULL),
	m_planeClip(Geom::Vec4f(0.0f,0.0f,0.0f,0.0f))
49
{
50 51 52 53 54 55
	m_nameVS = "ShaderSimpleFlat_vs";
	m_nameFS = "ShaderSimpleFlat_fs";
//	m_nameGS = "ShaderSimpleFlat_gs";

	// get choose GL defines (2 or 3)
	// ans compile shaders
56
	std::string glxvert(GLSLShader::defines_gl());
57
	glxvert.append(vertexShaderText);
58
	std::string glxfrag(GLSLShader::defines_gl());
59 60 61
	if (doubleSided)
		glxfrag.append("#define DOUBLE_SIDED\n");
	glxfrag.append(fragmentShaderText);
62

Sylvain Thery's avatar
Sylvain Thery committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	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);
	}
83

Sylvain Thery's avatar
Sylvain Thery committed
84
	loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str());
85 86 87 88 89 90 91 92 93 94 95
	// and get and fill uniforms
	getLocations();
	sendParams();
}

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
96 97
	*m_unif_backColor  = glGetUniformLocation(this->program_handler(), "backColor");
	*m_unif_planeClip = glGetUniformLocation(this->program_handler(), "planeClip");
98 99 100 101 102 103 104 105 106
	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
107 108 109
	glUniform4fv(*m_unif_backColor,  1, m_backColor.data());
	if (*m_unif_planeClip > 0)
		glUniform4fv(*m_unif_planeClip, 1, m_planeClip.data());
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	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
137 138 139 140 141 142 143 144
void ShaderSimpleFlat::setBackColor(const Geom::Vec4f& back)
{
	bind();
	glUniform4fv(*m_unif_backColor,1, back.data());
	m_backColor = back;
	unbind();
}

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
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
160
		std::string gl3vert(GLSLShader::defines_gl());
161 162
		gl3vert.append("#define WITH_COLOR 1\n");
		gl3vert.append(vertexShaderText);
163
		std::string gl3frag(GLSLShader::defines_gl());
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
		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
190
		std::string gl3vert(GLSLShader::defines_gl());
191
		gl3vert.append(vertexShaderText);
192
		std::string gl3frag(GLSLShader::defines_gl());
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
		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
223 224 225 226 227 228 229 230 231 232 233
void ShaderSimpleFlat::setClippingPlane(const Geom::Vec4f& plane)
{
	if (*m_unif_planeClip > 0)
	{
		m_planeClip = plane;
		bind();
		glUniform4fv(*m_unif_planeClip, 1, plane.data());
		unbind();
	}
}

234 235 236
} // namespace Utils

} // namespace CGoGN