shaderColorPerVertex.cpp 4.26 KB
Newer Older
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* 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.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                           *
21 22 23
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/
24

Sylvain Thery's avatar
Sylvain Thery committed
25
#define CGoGN_UTILS_DLL_EXPORT 1
26 27
#include "Utils/Shaders/shaderColorPerVertex.h"

28 29
namespace CGoGN
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
30

31 32 33
namespace Utils
{

34 35
#include "shaderColorPerVertex.vert"
#include "shaderColorPerVertex.frag"
36 37
#include "shaderColorPerVertexClip.vert"
#include "shaderColorPerVertexClip.frag"
38

39
ShaderColorPerVertex::ShaderColorPerVertex(bool withClipping, bool black_is_transparent)
40
{
41 42 43 44 45 46
	if (withClipping)
	{
		m_nameVS = "ShaderColorPerVertexClip_vs";
		m_nameFS = "ShaderColorPerVertexClip_fs";
		m_nameGS = "";

47
		std::string glxvert(GLSLShader::defines_gl());
48 49
		glxvert.append(vertexShaderClipText);

50
		std::string glxfrag(GLSLShader::defines_gl());
51 52 53 54 55
		if (black_is_transparent)
			glxfrag.append("#define BLACK_TRANSPARENCY 1\n");
		glxfrag.append(fragmentShaderClipText);

		loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str());
56

57 58
		*m_unif_planeClip = glGetUniformLocation(this->program_handler(),"planeClip");
		setClippingPlane(Geom::Vec4f (0.0f, 0.0f, 0.0f, 0.0f));
59

60 61 62 63 64 65
	}
	else
	{
		m_nameVS = "ShaderColorPerVertex_vs";
		m_nameFS = "ShaderColorPerVertex_fs";
		m_nameGS = "ShaderColorPerVertex_gs";
66

67
		std::string glxvert(GLSLShader::defines_gl());
68
		glxvert.append(vertexShaderText);
69

70
		std::string glxfrag(GLSLShader::defines_gl());
71 72 73 74 75 76 77
		if (black_is_transparent)
			glxfrag.append("#define BLACK_TRANSPARENCY 1\n");
		glxfrag.append(fragmentShaderText);

		loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str());

	}
78
	bind();
79 80 81
	*m_unif_alpha = glGetUniformLocation(this->program_handler(), "alpha");
	glUniform1f (*m_unif_alpha, 1.0f);
	m_opacity = 1.0f;
82
	unbind();
83

84 85 86 87 88
}

unsigned int ShaderColorPerVertex::setAttributePosition(VBO* vbo)
{
	m_vboPos = vbo;
89 90 91 92
	bind();
	unsigned int id = bindVA_VBO("VertexPosition", vbo);
	unbind();
	return id;
93 94 95 96 97
}

unsigned int ShaderColorPerVertex::setAttributeColor(VBO* vbo)
{
	m_vboCol = vbo;
98 99 100 101
	bind();
	unsigned int id = bindVA_VBO("VertexColor", vbo);
	unbind();
	return id;
102 103 104 105
}

void ShaderColorPerVertex::restoreUniformsAttribs()
{
106
	bind();
107 108
	bindVA_VBO("VertexPosition", m_vboPos);
	bindVA_VBO("VertexColor", m_vboCol);
109
	glUniform1f (*m_unif_alpha, m_opacity);
110
	glUniform4fv(*m_unif_planeClip, 1, m_planeClip.data());
111 112 113 114 115
	unbind();
}

void ShaderColorPerVertex::setOpacity(float op)
{
116
	m_opacity = op;
117
	bind();
118
	glUniform1f (*m_unif_alpha, m_opacity);
119
	unbind();
120 121
}

122 123 124 125 126 127 128 129
void ShaderColorPerVertex::setClippingPlane(const Geom::Vec4f& plane)
{
	m_planeClip = plane;
	bind();
	glUniform4fv(*m_unif_planeClip, 1, plane.data());
	unbind();
}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
130
} // namespace Utils
131

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
132
} // namespace CGoGN