<commit_before>/****************************************************************************

 This file is part of the GLC-lib library.
 Copyright (C) 2005-2008 <NAME> (<EMAIL>)
 Version 1.0.0, packaged on August, 2008.

 http://glc-lib.sourceforge.net

 GLC-lib is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 GLC-lib 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 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with GLC-lib; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*****************************************************************************/

//! \file glc_shader.cpp implementation of the GLC_Shader class.

#include "glc_shader.h"
#include <QTextStream>
#include <QMutexLocker>
#include "glc_exception.h"
#include "glc_state.h"

// Static member initialization
QStack<GLuint> GLC_Shader::m_ProgrammStack;

GLuint GLC_Shader::m_CurrentProgramm= 0;

QMutex GLC_Shader::m_Mutex;


GLC_Shader::GLC_Shader()
: m_VertexByteArray()
, m_VertexShader(0)
, m_FragmentByteArray()
, m_FragmentShader(0)
, m_ProgramShader(0)
, m_Name("Empty Shader")
{
	qDebug() << "Create Shader";
}

// Construct shader with specifie vertex and fragment
GLC_Shader::GLC_Shader(QFile& vertex, QFile& fragment)
: m_VertexByteArray()
, m_VertexShader(0)
, m_FragmentByteArray()
, m_FragmentShader(0)
, m_ProgramShader(0)
, m_Name()
{
	qDebug() << "Create Shader";
	setVertexAndFragmentShader(vertex, fragment);
}

// Copy constructor
GLC_Shader::GLC_Shader(const GLC_Shader& shader)
: m_VertexByteArray(shader.m_VertexByteArray)
, m_VertexShader(0)
, m_FragmentByteArray(shader.m_FragmentByteArray)
, m_FragmentShader(0)
, m_ProgramShader(0)
, m_Name(shader.m_Name)
{
	qDebug() << "Create Shader";
	if (0 != shader.m_ProgramShader)
	{
		createAndCompileProgrammShader();
	}
}

GLC_Shader::~GLC_Shader()
{
	qDebug() << "GLC_Shader::~GLC_Shader";
	deleteShader();
}

//////////////////////////////////////////////////////////////////////
// Get Functions
//////////////////////////////////////////////////////////////////////

// Return true if the shader can be deleted
bool GLC_Shader::canBeDeleted() const
{
	return m_CurrentProgramm != m_ProgramShader;
}

//////////////////////////////////////////////////////////////////////
// OpenGL Functions
//////////////////////////////////////////////////////////////////////

// Use this shader programm
void GLC_Shader::use()
{
	if (GLC_State::isInSelectionMode()) return;
	// Program shader must be valid
	Q_ASSERT(0 != m_ProgramShader);
	// Test if it is a valid program shader
	if (glIsProgram(m_ProgramShader) != GL_TRUE)
	{
		QString message("GLC_Shader::use() m_ProgramShader is not a valid program shader ");
		GLC_Exception exception(message);
		throw(exception);
	}

	QMutexLocker locker(&m_Mutex);
	// Test if the program shader is not already the current one
	if (m_CurrentProgramm != m_ProgramShader)
	{
		if (m_CurrentProgramm != 0)
		{
			m_ProgrammStack.push(m_CurrentProgramm);
		}
		m_CurrentProgramm= m_ProgramShader;
		glUseProgram(m_CurrentProgramm);
	}

}

// Use specified program shader
void GLC_Shader::use(GLuint shaderId)
{

	//qDebug() << "GLC_Shader::use(GLuint shaderId)";
	// Test if the program shader is not already the current one
	if (m_CurrentProgramm != shaderId)
	{
		if (m_CurrentProgramm != 0)
		{
			m_ProgrammStack.push(shaderId);
		}
		m_CurrentProgramm= shaderId;
		glUseProgram(m_CurrentProgramm);
	}
	else if (glIsProgram(shaderId) != GL_TRUE)	// Test if it is a valid program shader
	{
		QString message("GLC_Shader::use(GLuint id) id is not a valid program shader ");
		GLC_Exception exception(message);
		throw(exception);
	}

}


// Use previous program shader
void GLC_Shader::unuse()
{
	if (GLC_State::isInSelectionMode()) return;

	QMutexLocker locker(&m_Mutex);
	if (m_ProgrammStack.isEmpty())
	{
		m_CurrentProgramm= 0;
	}<commit_msg>Fix bug ing use method in selection mode.<commit_after>/****************************************************************************

 This file is part of the GLC-lib library.
 Copyright (C) 2005-2008 <NAME> (<EMAIL>)
 Version 1.0.0, packaged on August, 2008.

 http://glc-lib.sourceforge.net

 GLC-lib is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 GLC-lib 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 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with GLC-lib; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*****************************************************************************/

//! \file glc_shader.cpp implementation of the GLC_Shader class.

#include "glc_shader.h"
#include <QTextStream>
#include <QMutexLocker>
#include "glc_exception.h"
#include "glc_state.h"

// Static member initialization
QStack<GLuint> GLC_Shader::m_ProgrammStack;

GLuint GLC_Shader::m_CurrentProgramm= 0;

QMutex GLC_Shader::m_Mutex;


GLC_Shader::GLC_Shader()
: m_VertexByteArray()
, m_VertexShader(0)
, m_FragmentByteArray()
, m_FragmentShader(0)
, m_ProgramShader(0)
, m_Name("Empty Shader")
{
	qDebug() << "Create Shader";
}

// Construct shader with specifie vertex and fragment
GLC_Shader::GLC_Shader(QFile& vertex, QFile& fragment)
: m_VertexByteArray()
, m_VertexShader(0)
, m_FragmentByteArray()
, m_FragmentShader(0)
, m_ProgramShader(0)
, m_Name()
{
	qDebug() << "Create Shader";
	setVertexAndFragmentShader(vertex, fragment);
}

// Copy constructor
GLC_Shader::GLC_Shader(const GLC_Shader& shader)
: m_VertexByteArray(shader.m_VertexByteArray)
, m_VertexShader(0)
, m_FragmentByteArray(shader.m_FragmentByteArray)
, m_FragmentShader(0)
, m_ProgramShader(0)
, m_Name(shader.m_Name)
{
	qDebug() << "Create Shader";
	if (0 != shader.m_ProgramShader)
	{
		createAndCompileProgrammShader();
	}
}

GLC_Shader::~GLC_Shader()
{
	qDebug() << "GLC_Shader::~GLC_Shader";
	deleteShader();
}

//////////////////////////////////////////////////////////////////////
// Get Functions
//////////////////////////////////////////////////////////////////////

// Return true if the shader can be deleted
bool GLC_Shader::canBeDeleted() const
{
	return m_CurrentProgramm != m_ProgramShader;
}

//////////////////////////////////////////////////////////////////////
// OpenGL Functions
//////////////////////////////////////////////////////////////////////

// Use this shader programm
void GLC_Shader::use()
{
	if (GLC_State::isInSelectionMode()) return;
	// Program shader must be valid
	Q_ASSERT(0 != m_ProgramShader);
	// Test if it is a valid program shader
	if (glIsProgram(m_ProgramShader) != GL_TRUE)
	{
		QString message("GLC_Shader::use() m_ProgramShader is not a valid program shader ");
		GLC_Exception exception(message);
		throw(exception);
	}

	QMutexLocker locker(&m_Mutex);
	// Test if the program shader is not already the current one
	if (m_CurrentProgramm != m_ProgramShader)
	{
		if (m_CurrentProgramm != 0)
		{
			m_ProgrammStack.push(m_CurrentProgramm);
		}
		m_CurrentProgramm= m_ProgramShader;
		glUseProgram(m_CurrentProgramm);
	}

}

// Use specified program shader
void GLC_Shader::use(GLuint shaderId)
{
	if (GLC_State::isInSelectionMode()) return;
	//qDebug() << "GLC_Shader::use(GLuint shaderId)";
	// Test if the program shader is not already the current one
	if (m_CurrentProgramm != shaderId)
	{
		if (m_CurrentProgramm != 0)
		{
			m_ProgrammStack.push(shaderId);
		}
		m_CurrentProgramm= shaderId;
		glUseProgram(m_CurrentProgramm);
	}
	else if (glIsProgram(shaderId) != GL_TRUE)	// Test if it is a valid program shader
	{
		QString message("GLC_Shader::use(GLuint id) id is not a valid program shader ");
		GLC_Exception exception(message);
		throw(exception);
	}

}


// Use previous program shader
void GLC_Shader::unuse()
{
	if (GLC_State::isInSelectionMode()) return;

	QMutexLocker locker(&m_Mutex);
	if (m_ProgrammStack.isEmpty())
	{
		m_CurrentProgramm= 0;
	}
{'fraction_non_alphanumeric': 0.19717003015541637, 'fraction_numerical': 0.009742519137091163, 'mean_word_length': 3.6661255411255413, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3045458', 'n_tokens_mistral': 2799, 'n_tokens_neox': 2646, 'n_words': 738}