textureSticker.cpp 7.8 KB
Newer Older
Thery Sylvain's avatar
Thery Sylvain committed
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
Thery Sylvain's avatar
Thery Sylvain committed
25 26 27 28
#include "GL/glew.h"
#include "Utils/textureSticker.h"

#include "glm/glm.hpp"
Sylvain Thery's avatar
Sylvain Thery committed
29
#include "glm/gtc/matrix_transform.hpp"
Thery Sylvain's avatar
Thery Sylvain committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

namespace CGoGN
{

namespace Utils
{

// Initialize static variables and constants
bool TextureSticker::sm_isInitialized = false;
Utils::VBO* TextureSticker::sm_quadPositionsVbo = NULL;
Utils::VBO* TextureSticker::sm_quadTexCoordsVbo = NULL;
Utils::ShaderSimpleTexture* TextureSticker::sm_textureMappingShader = NULL;
Utils::ShaderTextureDepth* TextureSticker::sm_depthtextureMappingShader = NULL;

TextureSticker::TextureSticker()
{
}

TextureSticker::~TextureSticker()
{
	if (sm_textureMappingShader!= NULL)
		delete sm_textureMappingShader;

	if (sm_depthtextureMappingShader!= NULL)
		delete sm_depthtextureMappingShader;
}

void TextureSticker::fullScreenTexture(CGoGNGLuint texId)
{
	glClear(GL_COLOR_BUFFER_BIT);

	// Check if TextureSticker's elements have been initialized before
	if (!sm_isInitialized)
	{
		initializeElements();
		sm_isInitialized = true;
	}
	
	// Check if depth test is enabled
	GLboolean wasDepthTestEnabled = glIsEnabled(GL_DEPTH_TEST);
	
	// Disable depth test if it was enabled
	if (wasDepthTestEnabled == GL_TRUE)
		glDisable(GL_DEPTH_TEST);

	// Bind texture mapping shader
	sm_textureMappingShader->bind();
	
	// Set texture uniform
	sm_textureMappingShader->setTextureUnit(GL_TEXTURE0);
	sm_textureMappingShader->activeTexture(texId);
	
	// Set matrices uniforms
	glm::mat4 projMatrix = glm::ortho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
	glm::mat4 viewMatrix(1.0f);
	sm_textureMappingShader->updateMatrices(projMatrix, viewMatrix);

	// Draw quad
	sm_textureMappingShader->enableVertexAttribs();
89
	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Thery Sylvain's avatar
Thery Sylvain committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	sm_textureMappingShader->disableVertexAttribs();

	// Unbind texture mapping shader
	sm_textureMappingShader->unbind();

	// Re-enable depth test if it was enabled before
	if (wasDepthTestEnabled == GL_TRUE)
		glEnable(GL_DEPTH_TEST);
}


void TextureSticker::fullScreenTextureDepth(CGoGNGLuint texId, CGoGNGLuint dtexId)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// Check if TextureSticker's elements have been initialized before
	if (!sm_isInitialized)
	{
		initializeElements();
		sm_isInitialized = true;
	}

	// Check if depth test is enabled
113

114
//	GLboolean wasDepthTestEnabled = glIsEnabled(GL_DEPTH_TEST);
Thery Sylvain's avatar
Thery Sylvain committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

	// Disable depth test if it was enabled
//	if (wasDepthTestEnabled == GL_TRUE)
//		glDisable(GL_DEPTH_TEST);

	// Bind texture mapping shader
	sm_depthtextureMappingShader->bind();

	// Set texture uniform
	sm_depthtextureMappingShader->setTextureUnit(GL_TEXTURE0);
	sm_depthtextureMappingShader->activeTexture(texId);
	sm_depthtextureMappingShader->setDepthTextureUnit(GL_TEXTURE1);
	sm_depthtextureMappingShader->activeDepthTexture(dtexId);


	// Set matrices uniforms
	glm::mat4 projMatrix = glm::ortho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
	glm::mat4 viewMatrix(1.0f);
	sm_depthtextureMappingShader->updateMatrices(projMatrix, viewMatrix);

	// Draw quad
	sm_depthtextureMappingShader->enableVertexAttribs();
137
	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Thery Sylvain's avatar
Thery Sylvain committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
	sm_depthtextureMappingShader->disableVertexAttribs();

	// Unbind texture mapping shader
	sm_depthtextureMappingShader->unbind();

	// Re-enable depth test if it was enabled before
//	if (wasDepthTestEnabled == GL_TRUE)
//		glEnable(GL_DEPTH_TEST);
}





void TextureSticker::fullScreenShader(Utils::GLSLShader* shader)
{
	// Check if TextureSticker's elements have been initialized before
	if (!sm_isInitialized)
	{
		initializeElements();
		sm_isInitialized = true;
	}
	
	// Check if depth test is enabled
	GLboolean wasDepthTestEnabled = glIsEnabled(GL_DEPTH_TEST);
	
	// Disable depth test if it was enabled
	if (wasDepthTestEnabled == GL_TRUE)
		glDisable(GL_DEPTH_TEST);
	
	// Bind shader
	shader->bind();
	
	// Set matrices uniforms
	glm::mat4 projMatrix = glm::ortho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
	glm::mat4 viewMatrix(1.0f);
	shader->updateMatrices(projMatrix, viewMatrix);

	// Draw quad
	shader->enableVertexAttribs();
178
	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Thery Sylvain's avatar
Thery Sylvain committed
179 180 181 182 183 184 185 186 187 188 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 215 216 217 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 248 249 250 251 252 253 254 255 256 257 258 259 260
	shader->disableVertexAttribs();
	
	// Unbind shader
	shader->unbind();

	// Re-enable depth test if it was enabled before
	if (wasDepthTestEnabled == GL_TRUE)
		glEnable(GL_DEPTH_TEST);
}

VBO* TextureSticker::getPositionsVbo()
{
	// Check if TextureSticker's elements have been initialized before
	if (!sm_isInitialized)
	{
		initializeElements();
		sm_isInitialized = true;
//		return NULL;
	}
	
	return sm_quadPositionsVbo;
}

VBO* TextureSticker::getTexCoordsVbo()
{
	// Check if TextureSticker's elements have been initialized before
	if (!sm_isInitialized)
	{
		initializeElements();
		sm_isInitialized = true;
//		return NULL;
	}
	
	return sm_quadTexCoordsVbo;
}

void TextureSticker::initializeElements()
{
	// Initialize positions and texture coords Vbos
	
	sm_quadPositionsVbo = new Utils::VBO();
	sm_quadTexCoordsVbo = new Utils::VBO();
	sm_quadPositionsVbo->setDataSize(3);
	sm_quadTexCoordsVbo->setDataSize(2);
	sm_quadPositionsVbo->allocate(4);
	sm_quadTexCoordsVbo->allocate(4);

	GLfloat positions[] = {
		-1.0f, -1.0f, 0.0f,
		+1.0f, -1.0f, 0.0f,
		+1.0f, +1.0f, 0.0f,
		-1.0f, +1.0f, 0.0f
		};
	GLfloat texCoords[] = {
		0.0f, 0.0f,
		1.0f, 0.0f,
		1.0f, 1.0f,
		0.0f, 1.0f
		};
		
	GLfloat* positionsPtr = (GLfloat*) sm_quadPositionsVbo->lockPtr();
	memcpy(positionsPtr, positions, 3 * 4 * sizeof(GLfloat));
	sm_quadPositionsVbo->releasePtr();
	GLfloat* texCoordsPtr = (GLfloat*) sm_quadTexCoordsVbo->lockPtr();
	memcpy(texCoordsPtr, texCoords, 2 * 4 * sizeof(GLfloat));
	sm_quadTexCoordsVbo->releasePtr();
	
	// Initialize simple texture mapping shader
	sm_textureMappingShader = new Utils::ShaderSimpleTexture();
	sm_textureMappingShader->setAttributePosition(sm_quadPositionsVbo);
	sm_textureMappingShader->setAttributeTexCoord(sm_quadTexCoordsVbo);

	sm_depthtextureMappingShader = new Utils::ShaderTextureDepth();
	sm_depthtextureMappingShader->setAttributePosition(sm_quadPositionsVbo);
	sm_depthtextureMappingShader->setAttributeTexCoord(sm_quadTexCoordsVbo);

}

} // namespace Utils

} // namespace CGoGN