Added ShaderManager (Experimental)
Former-commit-id: 327e373f2b932e31184e88c5f29bd5bd8fa3ba46
This commit is contained in:
@@ -2,99 +2,22 @@
|
||||
// This file is part of the "Nazara Engine - Graphics module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#ifndef NAZARA_RENDERER_OPENGL
|
||||
#define NAZARA_RENDERER_OPENGL // Nécessaire pour inclure les headers OpenGL
|
||||
#endif
|
||||
|
||||
#include <Nazara/Renderer/OpenGL.hpp>
|
||||
#include <Nazara/Graphics/TextureBackground.hpp>
|
||||
#include <Nazara/Renderer/Renderer.hpp>
|
||||
#include <Nazara/Renderer/ShaderBuilder.hpp>
|
||||
#include <Nazara/Renderer/ShaderManager.hpp>
|
||||
#include <memory>
|
||||
#include <Nazara/Graphics/Debug.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
static NzShader* s_shader = nullptr;
|
||||
static int s_textureLocation;
|
||||
|
||||
NzShader* BuildShader()
|
||||
{
|
||||
const char* fragmentSource110 =
|
||||
"#version 110\n"
|
||||
"varying vec2 vTexCoord;\n"
|
||||
"uniform sampler2D Texture;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" gl_FragColor = texture(Texture, vTexCoord);\n"
|
||||
"}\n";
|
||||
|
||||
const char* fragmentSource140 =
|
||||
"#version 140\n"
|
||||
"in vec2 vTexCoord;\n"
|
||||
"out vec4 RenderTarget0;\n"
|
||||
"uniform sampler2D Texture;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" RenderTarget0 = texture(Texture, vTexCoord);\n"
|
||||
"}\n";
|
||||
|
||||
const char* vertexSource110 =
|
||||
"#version 110\n"
|
||||
"attribute vec2 VertexPosition;\n"
|
||||
"varying vec2 vTexCoord;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" gl_Position = vec4(VertexPosition, 0.0, 1.0);\n"
|
||||
" vTexCoord = vec2((VertexPosition.x + 1.0)*0.5, (VertexPosition.y + 1.0)*0.5);\n"
|
||||
"}\n";
|
||||
|
||||
const char* vertexSource140 =
|
||||
"#version 140\n"
|
||||
"in vec2 VertexPosition;\n"
|
||||
"out vec2 vTexCoord;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" gl_Position = vec4(VertexPosition, 0.0, 1.0);\n"
|
||||
" vTexCoord = vec2((VertexPosition.x + 1.0)*0.5, (VertexPosition.y + 1.0)*0.5);\n"
|
||||
"}\n";
|
||||
|
||||
///TODO: Remplacer ça par des ShaderNode
|
||||
std::unique_ptr<NzShader> shader(new NzShader(nzShaderLanguage_GLSL));
|
||||
shader->SetPersistent(false);
|
||||
|
||||
bool useGLSL140 = (NzOpenGL::GetVersion() >= 310);
|
||||
|
||||
if (!shader->Load(nzShaderType_Fragment, (useGLSL140) ? fragmentSource140 : fragmentSource110))
|
||||
{
|
||||
NazaraError("Failed to load fragment shader");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!shader->Load(nzShaderType_Vertex, (useGLSL140) ? vertexSource140 : vertexSource110))
|
||||
{
|
||||
NazaraError("Failed to load vertex shader");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!shader->Compile())
|
||||
{
|
||||
NazaraError("Failed to compile shader");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
s_textureLocation = shader->GetUniformLocation("Texture");
|
||||
|
||||
return shader.release();
|
||||
}
|
||||
}
|
||||
|
||||
NzTextureBackground::NzTextureBackground()
|
||||
{
|
||||
if (!s_shader)
|
||||
s_shader = BuildShader();
|
||||
NzShaderManagerParams params;
|
||||
params.target = nzShaderTarget_FullscreenQuad;
|
||||
params.flags = 0;
|
||||
params.fullscreenQuad.alphaMapping = false;
|
||||
params.fullscreenQuad.alphaTest = false;
|
||||
params.fullscreenQuad.diffuseMapping = true;
|
||||
|
||||
m_shader = s_shader;
|
||||
m_shader = NzShaderManager::Get(params);
|
||||
}
|
||||
|
||||
NzTextureBackground::NzTextureBackground(NzTexture* texture) :
|
||||
@@ -103,19 +26,14 @@ NzTextureBackground()
|
||||
m_texture = texture;
|
||||
}
|
||||
|
||||
NzTextureBackground::~NzTextureBackground()
|
||||
{
|
||||
if (m_shader.Reset())
|
||||
s_shader = nullptr;
|
||||
}
|
||||
|
||||
void NzTextureBackground::Draw(const NzScene* scene) const
|
||||
{
|
||||
NazaraUnused(scene);
|
||||
|
||||
static NzRenderStates states;
|
||||
|
||||
m_shader->SendInteger(s_textureLocation, 0);
|
||||
m_shader->SendColor(m_shader->GetUniformLocation(nzShaderUniform_MaterialDiffuse), NzColor::White);
|
||||
m_shader->SendInteger(m_shader->GetUniformLocation(nzShaderUniform_MaterialDiffuseMap), 0);
|
||||
|
||||
NzRenderer::SetRenderStates(states);
|
||||
NzRenderer::SetShader(m_shader);
|
||||
|
||||
Reference in New Issue
Block a user