Refactored mathematics module
Added AABBs Added code examples Added experimental support for texture arrays (1D/2D) Added initialisers (new way of initialising modules) Added global headers (Plus a global header generator script) Added pattern support for directory Added support for spinlocks critical section on Windows Added NzRenderWindow::SetFramerateLimit Core project now includes Mathematics files Fixed color implementation using double Fixed declaration needing renderer include Fixed MLT not clearing nextFree(File/Line) after Free Fixed move operators not being noexcept Fixed thread-safety (Now working correctly - If I'm lucky) Moved Resource to core New interface for modules New interface for the renderer Put some global functions to anonymous namespace Removed empty modules Renamed ThreadCondition to ConditionVariable Replaced redirect to cerr log option by duplicate to cout Setting mouse position relative to a window will make this window ignore the event Shaders sending methods no longer takes the uniform variable name (it's using ID instead) Using new OpenGL 4.3 header
This commit is contained in:
@@ -63,11 +63,22 @@ bool NzGLSLShader::Bind()
|
||||
|
||||
glUseProgram(m_program);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzGLSLShader::BindTextures()
|
||||
{
|
||||
for (auto it = m_textures.begin(); it != m_textures.end(); ++it)
|
||||
{
|
||||
glActiveTexture(GL_TEXTURE0 + it->second.unit);
|
||||
if (!it->second.texture->Bind())
|
||||
NazaraWarning("Failed to bind texture");
|
||||
TextureSlot& slot = it->second;
|
||||
if (!slot.updated)
|
||||
{
|
||||
glActiveTexture(GL_TEXTURE0 + slot.unit);
|
||||
if (!slot.texture->Bind())
|
||||
NazaraWarning("Failed to bind texture");
|
||||
|
||||
slot.updated = true;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -78,6 +89,7 @@ bool NzGLSLShader::Compile()
|
||||
NzContext::EnsureContext();
|
||||
|
||||
m_idCache.clear();
|
||||
m_textures.clear();
|
||||
|
||||
glLinkProgram(m_program);
|
||||
|
||||
@@ -86,8 +98,8 @@ bool NzGLSLShader::Compile()
|
||||
|
||||
if (success == GL_TRUE)
|
||||
{
|
||||
static NzString success("Linkage successful");
|
||||
m_log = success;
|
||||
static NzString successStr("Linkage successful");
|
||||
m_log = successStr;
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -131,7 +143,7 @@ bool NzGLSLShader::Create()
|
||||
glBindAttribLocation(m_program, attribIndex[nzElementUsage_Tangent], "Tangent");
|
||||
|
||||
NzString uniform = "TexCoord";
|
||||
unsigned int maxTexCoords = NazaraRenderer->GetMaxTextureUnits();
|
||||
unsigned int maxTexCoords = NzRenderer::GetMaxTextureUnits();
|
||||
for (unsigned int i = 0; i < maxTexCoords; ++i)
|
||||
{
|
||||
NzString uniformName = uniform + NzString::Number(i);
|
||||
@@ -148,6 +160,9 @@ void NzGLSLShader::Destroy()
|
||||
{
|
||||
NzContext::EnsureContext();
|
||||
|
||||
for (auto it = m_textures.begin(); it != m_textures.end(); ++it)
|
||||
it->second.texture->RemoveResourceReference();
|
||||
|
||||
for (GLuint shader : m_shaders)
|
||||
if (shader)
|
||||
glDeleteShader(shader);
|
||||
@@ -183,7 +198,7 @@ NzString NzGLSLShader::GetSourceCode(nzShaderType type) const
|
||||
return source;
|
||||
}
|
||||
|
||||
GLint NzGLSLShader::GetUniformLocation(const NzString& name) const
|
||||
int NzGLSLShader::GetUniformLocation(const NzString& name) const
|
||||
{
|
||||
std::map<NzString, GLint>::const_iterator it = m_idCache.find(name);
|
||||
GLint id;
|
||||
@@ -232,8 +247,8 @@ bool NzGLSLShader::Load(nzShaderType type, const NzString& source)
|
||||
glAttachShader(m_program, shader);
|
||||
m_shaders[type] = shader;
|
||||
|
||||
static NzString success("Compilation successful");
|
||||
m_log = success;
|
||||
static NzString successStr("Compilation successful");
|
||||
m_log = successStr;
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -280,226 +295,247 @@ bool NzGLSLShader::Lock()
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzGLSLShader::SendBoolean(const NzString& name, bool value)
|
||||
bool NzGLSLShader::SendBoolean(int location, bool value)
|
||||
{
|
||||
if (glProgramUniform1i)
|
||||
glProgramUniform1i(m_program, GetUniformLocation(name), value);
|
||||
glProgramUniform1i(m_program, location, value);
|
||||
else
|
||||
{
|
||||
Lock();
|
||||
glUniform1i(GetUniformLocation(name), value);
|
||||
glUniform1i(location, value);
|
||||
Unlock();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzGLSLShader::SendDouble(const NzString& name, double value)
|
||||
bool NzGLSLShader::SendDouble(int location, double value)
|
||||
{
|
||||
if (glProgramUniform1d)
|
||||
glProgramUniform1d(m_program, GetUniformLocation(name), value);
|
||||
glProgramUniform1d(m_program, location, value);
|
||||
else
|
||||
{
|
||||
Lock();
|
||||
glUniform1d(GetUniformLocation(name), value);
|
||||
glUniform1d(location, value);
|
||||
Unlock();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzGLSLShader::SendFloat(const NzString& name, float value)
|
||||
bool NzGLSLShader::SendFloat(int location, float value)
|
||||
{
|
||||
if (glProgramUniform1f)
|
||||
glProgramUniform1f(m_program, GetUniformLocation(name), value);
|
||||
glProgramUniform1f(m_program, location, value);
|
||||
else
|
||||
{
|
||||
Lock();
|
||||
glUniform1f(GetUniformLocation(name), value);
|
||||
glUniform1f(location, value);
|
||||
Unlock();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzGLSLShader::SendInteger(const NzString& name, int value)
|
||||
bool NzGLSLShader::SendInteger(int location, int value)
|
||||
{
|
||||
if (glProgramUniform1i)
|
||||
glProgramUniform1i(m_program, GetUniformLocation(name), value);
|
||||
glProgramUniform1i(m_program, location, value);
|
||||
else
|
||||
{
|
||||
Lock();
|
||||
glUniform1i(GetUniformLocation(name), value);
|
||||
glUniform1i(location, value);
|
||||
Unlock();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzGLSLShader::SendMatrix(const NzString& name, const NzMatrix4d& matrix)
|
||||
bool NzGLSLShader::SendMatrix(int location, const NzMatrix4d& matrix)
|
||||
{
|
||||
if (glProgramUniformMatrix4dv)
|
||||
glProgramUniformMatrix4dv(m_program, GetUniformLocation(name), 1, GL_FALSE, matrix);
|
||||
glProgramUniformMatrix4dv(m_program, location, 1, GL_FALSE, matrix);
|
||||
else
|
||||
{
|
||||
Lock();
|
||||
glUniformMatrix4dv(GetUniformLocation(name), 1, GL_FALSE, matrix);
|
||||
glUniformMatrix4dv(location, 1, GL_FALSE, matrix);
|
||||
Unlock();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzGLSLShader::SendMatrix(const NzString& name, const NzMatrix4f& matrix)
|
||||
bool NzGLSLShader::SendMatrix(int location, const NzMatrix4f& matrix)
|
||||
{
|
||||
if (glProgramUniformMatrix4fv)
|
||||
glProgramUniformMatrix4fv(m_program, GetUniformLocation(name), 1, GL_FALSE, matrix);
|
||||
glProgramUniformMatrix4fv(m_program, location, 1, GL_FALSE, matrix);
|
||||
else
|
||||
{
|
||||
Lock();
|
||||
glUniformMatrix4fv(GetUniformLocation(name), 1, GL_FALSE, matrix);
|
||||
glUniformMatrix4fv(location, 1, GL_FALSE, matrix);
|
||||
Unlock();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzGLSLShader::SendVector(const NzString& name, const NzVector2d& vector)
|
||||
bool NzGLSLShader::SendTexture(int location, const NzTexture* texture)
|
||||
{
|
||||
if (glProgramUniform2dv)
|
||||
glProgramUniform2dv(m_program, GetUniformLocation(name), 1, vector);
|
||||
else
|
||||
auto it = m_textures.find(location);
|
||||
if (it != m_textures.end())
|
||||
{
|
||||
Lock();
|
||||
glUniform2dv(GetUniformLocation(name), 1, vector);
|
||||
Unlock();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzGLSLShader::SendVector(const NzString& name, const NzVector2f& vector)
|
||||
{
|
||||
if (glProgramUniform2fv)
|
||||
glProgramUniform2fv(m_program, GetUniformLocation(name), 1, vector);
|
||||
else
|
||||
{
|
||||
Lock();
|
||||
glUniform2fv(GetUniformLocation(name), 1, vector);
|
||||
Unlock();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzGLSLShader::SendVector(const NzString& name, const NzVector3d& vector)
|
||||
{
|
||||
if (glProgramUniform3dv)
|
||||
glProgramUniform3dv(m_program, GetUniformLocation(name), 1, vector);
|
||||
else
|
||||
{
|
||||
Lock();
|
||||
glUniform3dv(GetUniformLocation(name), 1, vector);
|
||||
Unlock();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzGLSLShader::SendVector(const NzString& name, const NzVector3f& vector)
|
||||
{
|
||||
if (glProgramUniform3fv)
|
||||
glProgramUniform3fv(m_program, GetUniformLocation(name), 1, vector);
|
||||
else
|
||||
{
|
||||
Lock();
|
||||
glUniform3fv(GetUniformLocation(name), 1, vector);
|
||||
Unlock();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzGLSLShader::SendVector(const NzString& name, const NzVector4d& vector)
|
||||
{
|
||||
if (glProgramUniform4dv)
|
||||
glProgramUniform4dv(m_program, GetUniformLocation(name), 1, vector);
|
||||
else
|
||||
{
|
||||
Lock();
|
||||
glUniform4dv(GetUniformLocation(name), 1, vector);
|
||||
Unlock();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzGLSLShader::SendVector(const NzString& name, const NzVector4f& vector)
|
||||
{
|
||||
if (glProgramUniform4fv)
|
||||
glProgramUniform4fv(m_program, GetUniformLocation(name), 1, vector);
|
||||
else
|
||||
{
|
||||
Lock();
|
||||
glUniform4fv(GetUniformLocation(name), 1, vector);
|
||||
Unlock();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzGLSLShader::SendTexture(const NzString& name, NzTexture* texture)
|
||||
{
|
||||
static const unsigned int maxUnits = NazaraRenderer->GetMaxTextureUnits();
|
||||
|
||||
unsigned int unitUsed = m_textures.size();
|
||||
if (unitUsed >= maxUnits)
|
||||
{
|
||||
NazaraError("Unable to use texture \"" + name + "\" for shader: all available texture units are used");
|
||||
return false;
|
||||
}
|
||||
|
||||
// À partir d'ici nous savons qu'il y a au moins un identifiant de texture libre
|
||||
GLint location = GetUniformLocation(name);
|
||||
if (location == -1)
|
||||
{
|
||||
NazaraError("Parameter name \"" + name + "\" not found in shader");
|
||||
return false;
|
||||
}
|
||||
|
||||
nzUInt8 unit;
|
||||
if (unitUsed == 0)
|
||||
// Pas d'unité utilisée, la tâche est simple
|
||||
unit = 0;
|
||||
else
|
||||
{
|
||||
auto it = m_textures.rbegin(); // Itérateur vers la fin de la map
|
||||
unit = it->second.unit;
|
||||
if (unit == maxUnits-1)
|
||||
// Slot déjà utilisé
|
||||
TextureSlot& slot = it->second;
|
||||
if (slot.texture != texture)
|
||||
{
|
||||
// Il y a une place libre, mais pas à la fin
|
||||
for (; it != m_textures.rend(); ++it)
|
||||
slot.texture->RemoveResourceReference();
|
||||
|
||||
if (texture)
|
||||
{
|
||||
if (unit - it->second.unit > 1) // Si l'espace entre les indices est supérieur à 1, alors il y a une place libre
|
||||
slot.texture = texture;
|
||||
slot.texture->AddResourceReference();
|
||||
|
||||
slot.updated = false;
|
||||
}
|
||||
else
|
||||
m_textures.erase(it); // On supprime le slot
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
static const unsigned int maxUnits = NzRenderer::GetMaxTextureUnits();
|
||||
|
||||
unsigned int unitUsed = m_textures.size();
|
||||
if (unitUsed >= maxUnits)
|
||||
{
|
||||
NazaraError("Unable to use texture for shader: all available texture units are used");
|
||||
return false;
|
||||
}
|
||||
|
||||
// À partir d'ici nous savons qu'il y a au moins un identifiant de texture libre
|
||||
nzUInt8 unit;
|
||||
if (unitUsed == 0)
|
||||
// Pas d'unité utilisée, la tâche est simple
|
||||
unit = 0;
|
||||
else
|
||||
{
|
||||
auto it2 = m_textures.rbegin(); // Itérateur vers la fin de la map
|
||||
unit = it2->second.unit;
|
||||
if (unit == maxUnits-1)
|
||||
{
|
||||
// Il y a une place libre, mais pas à la fin
|
||||
for (; it2 != m_textures.rend(); ++it2)
|
||||
{
|
||||
unit--;
|
||||
break;
|
||||
if (unit - it2->second.unit > 1) // Si l'espace entre les indices est supérieur à 1, alors il y a une place libre
|
||||
{
|
||||
unit--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
// Il y a une place libre à la fin
|
||||
unit++;
|
||||
}
|
||||
|
||||
TextureSlot slot;
|
||||
slot.unit = unit;
|
||||
slot.texture = texture;
|
||||
texture->AddResourceReference();
|
||||
|
||||
m_textures[location] = slot;
|
||||
|
||||
if (glProgramUniform1i)
|
||||
glProgramUniform1i(m_program, location, unit);
|
||||
else
|
||||
// Il y a une place libre à la fin
|
||||
unit++;
|
||||
{
|
||||
Lock();
|
||||
glUniform1i(location, unit);
|
||||
Unlock();
|
||||
}
|
||||
}
|
||||
|
||||
m_textures[location] = TextureSlot{unit, texture};
|
||||
return true;
|
||||
}
|
||||
|
||||
if (glProgramUniform1i)
|
||||
glProgramUniform1i(m_program, location, unit);
|
||||
bool NzGLSLShader::SendVector(int location, const NzVector2d& vector)
|
||||
{
|
||||
if (glProgramUniform2dv)
|
||||
glProgramUniform2dv(m_program, location, 1, vector);
|
||||
else
|
||||
{
|
||||
Lock();
|
||||
glUniform1i(location, unit);
|
||||
glUniform2dv(location, 1, vector);
|
||||
Unlock();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzGLSLShader::SendVector(int location, const NzVector2f& vector)
|
||||
{
|
||||
if (glProgramUniform2fv)
|
||||
glProgramUniform2fv(m_program, location, 1, vector);
|
||||
else
|
||||
{
|
||||
Lock();
|
||||
glUniform2fv(location, 1, vector);
|
||||
Unlock();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzGLSLShader::SendVector(int location, const NzVector3d& vector)
|
||||
{
|
||||
if (glProgramUniform3dv)
|
||||
glProgramUniform3dv(m_program, location, 1, vector);
|
||||
else
|
||||
{
|
||||
Lock();
|
||||
glUniform3dv(location, 1, vector);
|
||||
Unlock();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzGLSLShader::SendVector(int location, const NzVector3f& vector)
|
||||
{
|
||||
if (glProgramUniform3fv)
|
||||
glProgramUniform3fv(m_program, location, 1, vector);
|
||||
else
|
||||
{
|
||||
Lock();
|
||||
glUniform3fv(location, 1, vector);
|
||||
Unlock();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzGLSLShader::SendVector(int location, const NzVector4d& vector)
|
||||
{
|
||||
if (glProgramUniform4dv)
|
||||
glProgramUniform4dv(m_program, location, 1, vector);
|
||||
else
|
||||
{
|
||||
Lock();
|
||||
glUniform4dv(location, 1, vector);
|
||||
Unlock();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzGLSLShader::SendVector(int location, const NzVector4f& vector)
|
||||
{
|
||||
if (glProgramUniform4fv)
|
||||
glProgramUniform4fv(m_program, location, 1, vector);
|
||||
else
|
||||
{
|
||||
Lock();
|
||||
glUniform4fv(location, 1, vector);
|
||||
Unlock();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user