Graphics/Backgrounds: Update backgrounds to new coding-style

Former-commit-id: 9f96b93706fd8417d6262392f0ce9ab9ca1985c3
This commit is contained in:
Lynix 2015-06-23 23:35:11 +02:00
parent 0ec0e02a5f
commit 6092b0692b
10 changed files with 253 additions and 248 deletions

View File

@ -8,12 +8,19 @@
#define NAZARA_ABSTRACTBACKGROUND_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/ObjectLibrary.hpp>
#include <Nazara/Core/ObjectRef.hpp>
#include <Nazara/Graphics/Config.hpp>
#include <Nazara/Graphics/Enums.hpp>
class NzAbstractBackground;
class NzAbstractViewer;
class NAZARA_GRAPHICS_API NzAbstractBackground
using NzBackgroundConstRef = NzObjectRef<const NzAbstractBackground>;
using NzBackgroundLibrary = NzObjectLibrary<NzAbstractBackground>;
using NzBackgroundRef = NzObjectRef<NzAbstractBackground>;
class NAZARA_GRAPHICS_API NzAbstractBackground : public NzRefCounted
{
public:
NzAbstractBackground() = default;
@ -22,6 +29,9 @@ class NAZARA_GRAPHICS_API NzAbstractBackground
virtual void Draw(const NzAbstractViewer* viewer) const = 0;
virtual nzBackgroundType GetBackgroundType() const = 0;
private:
static NzBackgroundLibrary::LibraryMap s_library;
};
#endif // NAZARA_ABSTRACTBACKGROUND_HPP

View File

@ -12,6 +12,11 @@
#include <Nazara/Graphics/AbstractBackground.hpp>
#include <Nazara/Renderer/UberShader.hpp>
class NzColorBackground;
using NzColorBackgroundConstRef = NzObjectRef<const NzColorBackground>;
using NzColorBackgroundRef = NzObjectRef<NzColorBackground>;
class NAZARA_GRAPHICS_API NzColorBackground : public NzAbstractBackground
{
public:
@ -24,6 +29,8 @@ class NAZARA_GRAPHICS_API NzColorBackground : public NzAbstractBackground
void SetColor(const NzColor& color);
template<typename... Args> static NzColorBackgroundRef New(Args&&... args);
private:
NzColor m_color;
NzUberShaderConstRef m_uberShader;
@ -32,4 +39,6 @@ class NAZARA_GRAPHICS_API NzColorBackground : public NzAbstractBackground
int m_vertexDepthUniform;
};
#include <Nazara/Graphics/ColorBackground.inl>
#endif // NAZARA_COLORBACKGROUND_HPP

View File

@ -0,0 +1,17 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Graphics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <memory>
#include <Nazara/Graphics/Debug.hpp>
template<typename... Args>
NzColorBackgroundRef NzColorBackground::New(Args&&... args)
{
std::unique_ptr<NzColorBackground> object(new NzColorBackground(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
#include <Nazara/Graphics/DebugOff.hpp>

View File

@ -15,23 +15,33 @@
#include <Nazara/Utility/IndexBuffer.hpp>
#include <Nazara/Utility/VertexBuffer.hpp>
class NzSkyboxBackground;
using NzSkyboxBackgroundConstRef = NzObjectRef<const NzSkyboxBackground>;
using NzSkyboxBackgroundRef = NzObjectRef<NzSkyboxBackground>;
class NAZARA_GRAPHICS_API NzSkyboxBackground : public NzAbstractBackground
{
public:
NzSkyboxBackground();
NzSkyboxBackground(NzTexture* cubemapTexture);
~NzSkyboxBackground();
NzSkyboxBackground(NzTextureRef cubemapTexture = NzTextureRef());
~NzSkyboxBackground() = default;
void Draw(const NzAbstractViewer* viewer) const;
nzBackgroundType GetBackgroundType() const;
NzTexture* GetTexture() const;
const NzTextureSampler& GetTextureSampler();
inline const NzTextureRef& GetTexture() const;
inline NzTextureSampler& GetTextureSampler();
inline const NzTextureSampler& GetTextureSampler() const;
void SetTexture(NzTexture* cubemapTexture);
void SetTextureSampler(const NzTextureSampler& sampler);
inline void SetTexture(NzTextureRef cubemapTexture);
inline void SetTextureSampler(const NzTextureSampler& sampler);
template<typename... Args> static NzSkyboxBackgroundRef New(Args&&... args);
private:
static bool Initialize();
static void Uninitialize();
NzTextureRef m_texture;
NzTextureSampler m_sampler;
NzIndexBufferRef m_indexBuffer;
@ -39,4 +49,6 @@ class NAZARA_GRAPHICS_API NzSkyboxBackground : public NzAbstractBackground
NzVertexBufferRef m_vertexBuffer;
};
#include <Nazara/Graphics/SkyboxBackground.inl>
#endif // NAZARA_SKYBOXBACKGROUND_HPP

View File

@ -0,0 +1,45 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Graphics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <memory>
#include <Nazara/Graphics/Debug.hpp>
inline const NzTextureRef& NzSkyboxBackground::GetTexture() const
{
return m_texture;
}
inline NzTextureSampler& NzSkyboxBackground::GetTextureSampler()
{
return m_sampler;
}
inline const NzTextureSampler& NzSkyboxBackground::GetTextureSampler() const
{
return m_sampler;
}
inline void NzSkyboxBackground::SetTexture(NzTextureRef cubemapTexture)
{
NazaraAssert(!cubemapTexture || cubemapTexture->IsValid(), "Invalid texture");
NazaraAssert(!cubemapTexture || cubemapTexture->IsCubemap(), "Texture must be a cubemap");
m_texture = std::move(cubemapTexture);
}
void NzSkyboxBackground::SetTextureSampler(const NzTextureSampler& sampler)
{
m_sampler = sampler;
}
template<typename... Args>
NzSkyboxBackgroundRef NzSkyboxBackground::New(Args&&... args)
{
std::unique_ptr<NzSkyboxBackground> object(new NzSkyboxBackground(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
#include <Nazara/Graphics/DebugOff.hpp>

View File

@ -12,18 +12,24 @@
#include <Nazara/Renderer/UberShader.hpp>
#include <Nazara/Renderer/Texture.hpp>
class NzTextureBackground;
using NzTextureBackgroundConstRef = NzObjectRef<const NzTextureBackground>;
using NzTextureBackgroundRef = NzObjectRef<NzTextureBackground>;
class NAZARA_GRAPHICS_API NzTextureBackground : public NzAbstractBackground
{
public:
NzTextureBackground();
NzTextureBackground(NzTexture* texture);
NzTextureBackground(NzTextureRef texture = NzTextureRef());
void Draw(const NzAbstractViewer* viewer) const;
nzBackgroundType GetBackgroundType() const;
NzTexture* GetTexture() const;
inline const NzTextureRef& GetTexture() const;
void SetTexture(NzTexture* texture);
inline void SetTexture(NzTextureRef texture);
template<typename... Args> static NzTextureBackgroundRef New(Args&&... args);
private:
NzTextureRef m_texture;
@ -34,4 +40,6 @@ class NAZARA_GRAPHICS_API NzTextureBackground : public NzAbstractBackground
int m_vertexDepthUniform;
};
#include <Nazara/Graphics/TextureBackground.inl>
#endif // NAZARA_TEXTUREBACKGROUND_HPP

View File

@ -0,0 +1,29 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Graphics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <memory>
#include <Nazara/Graphics/Debug.hpp>
inline const NzTextureRef& NzTextureBackground::GetTexture() const
{
return m_texture;
}
inline void NzTextureBackground::SetTexture(NzTextureRef texture)
{
NazaraAssert(!texture || texture->IsValid(), "Invalid texture");
m_texture = std::move(texture);
}
template<typename... Args>
NzTextureBackgroundRef NzTextureBackground::New(Args&&... args)
{
std::unique_ptr<NzTextureBackground> object(new NzTextureBackground(std::forward<Args>(args)...));
object->SetPersistent(false);
return object.release();
}
#include <Nazara/Graphics/DebugOff.hpp>

View File

@ -6,3 +6,5 @@
#include <Nazara/Graphics/Debug.hpp>
NzAbstractBackground::~NzAbstractBackground() = default;
NzBackgroundLibrary::LibraryMap NzAbstractBackground::s_library;

View File

@ -2,14 +2,9 @@
// 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/Graphics/SkyboxBackground.hpp>
#include <Nazara/Graphics/Camera.hpp>
#include <Nazara/Graphics/Scene.hpp>
#include <Nazara/Renderer/OpenGL.hpp>
#include <Nazara/Core/ErrorFlags.hpp>
#include <Nazara/Graphics/AbstractViewer.hpp>
#include <Nazara/Renderer/Renderer.hpp>
#include <Nazara/Utility/IndexBuffer.hpp>
#include <Nazara/Utility/VertexBuffer.hpp>
@ -19,217 +14,31 @@
namespace
{
NzIndexBuffer* BuildIndexBuffer()
{
std::unique_ptr<NzIndexBuffer> indexBuffer(new NzIndexBuffer(false, 36, nzDataStorage_Hardware, nzBufferUsage_Static));
indexBuffer->SetPersistent(false);
nzUInt16 indices[6*6] =
{
0, 1, 2, 0, 2, 3,
3, 2, 6, 3, 6, 7,
7, 6, 5, 7, 5, 4,
4, 5, 1, 4, 1, 0,
0, 3, 7, 0, 7, 4,
1, 6, 2, 1, 5, 6
};
if (!indexBuffer->Fill(indices, 0, 36))
{
NazaraError("Failed to create index buffer");
return nullptr;
}
return indexBuffer.release();
}
NzShader* BuildShader()
{
const char* fragmentSource110 =
"#version 110\n"
"varying vec3 vTexCoord;\n"
"uniform samplerCube Skybox;\n"
"uniform float VertexDepth;\n"
"void main()\n"
"{\n"
" gl_FragColor = textureCube(Skybox, vTexCoord);\n"
" gl_FragDepth = VertexDepth;\n"
"}\n";
const char* fragmentSource140 =
"#version 140\n"
"in vec3 vTexCoord;\n"
"out vec4 RenderTarget0;\n"
"uniform samplerCube Skybox;\n"
"uniform float VertexDepth;\n"
"void main()\n"
"{\n"
" RenderTarget0 = texture(Skybox, vTexCoord);\n"
" gl_FragDepth = VertexDepth;\n"
"}\n";
const char* vertexSource110 =
"#version 110\n"
"attribute vec3 VertexPosition;\n"
"varying vec3 vTexCoord;\n"
"uniform mat4 WorldViewProjMatrix;\n"
"void main()\n"
"{\n"
" gl_Position = WorldViewProjMatrix * vec4(VertexPosition, 1.0);\n"
" vTexCoord = vec3(VertexPosition.x, VertexPosition.y, -VertexPosition.z);\n"
"}\n";
const char* vertexSource140 =
"#version 140\n"
"in vec3 VertexPosition;\n"
"out vec3 vTexCoord;\n"
"uniform mat4 WorldViewProjMatrix;\n"
"void main()\n"
"{\n"
" gl_Position = WorldViewProjMatrix * vec4(VertexPosition, 1.0);\n"
" vTexCoord = vec3(VertexPosition.x, VertexPosition.y, -VertexPosition.z);\n"
"}\n";
///TODO: Remplacer ça par des ShaderNode
std::unique_ptr<NzShader> shader(new NzShader);
shader->SetPersistent(false);
if (!shader->Create())
{
NazaraError("Failed to create shader");
return nullptr;
}
bool useGLSL140 = (NzOpenGL::GetVersion() >= 310);
if (!shader->AttachStageFromSource(nzShaderStage_Fragment, (useGLSL140) ? fragmentSource140 : fragmentSource110))
{
NazaraError("Failed to load fragment shader");
return nullptr;
}
if (!shader->AttachStageFromSource(nzShaderStage_Vertex, (useGLSL140) ? vertexSource140 : vertexSource110))
{
NazaraError("Failed to load vertex shader");
return nullptr;
}
if (!shader->Link())
{
NazaraError("Failed to link shader");
return nullptr;
}
shader->SendInteger(shader->GetUniformLocation("Skybox"), 0);
shader->SendFloat(shader->GetUniformLocation("VertexDepth"), 1.f);
return shader.release();
}
NzRenderStates BuildRenderStates()
{
NzRenderStates states;
states.depthFunc = nzRendererComparison_Equal;
states.faceCulling = nzFaceSide_Front;
states.parameters[nzRendererParameter_DepthBuffer] = true;
states.parameters[nzRendererParameter_DepthWrite] = false;
states.parameters[nzRendererParameter_FaceCulling] = true;
return states;
}
NzVertexBuffer* BuildVertexBuffer()
{
std::unique_ptr<NzVertexBuffer> vertexBuffer(new NzVertexBuffer(NzVertexDeclaration::Get(nzVertexLayout_XYZ), 8, nzDataStorage_Hardware, nzBufferUsage_Static));
vertexBuffer->SetPersistent(false);
float vertices[8*(sizeof(float)*3)] =
{
-1.0, 1.0, 1.0,
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, -1.0,
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
};
if (!vertexBuffer->Fill(vertices, 0, 8))
{
NazaraError("Failed to create vertex buffer");
return nullptr;
}
return vertexBuffer.release();
}
static NzIndexBuffer* s_indexBuffer = nullptr;
static NzShader* s_shader = nullptr;
static NzVertexBuffer* s_vertexBuffer = nullptr;
static NzIndexBufferRef s_indexBuffer;
static NzRenderStates s_renderStates;
static NzShaderRef s_shader;
static NzVertexBufferRef s_vertexBuffer;
}
NzSkyboxBackground::NzSkyboxBackground()
NzSkyboxBackground::NzSkyboxBackground(NzTextureRef cubemapTexture)
{
if (!s_indexBuffer)
s_indexBuffer = BuildIndexBuffer();
if (!s_shader)
s_shader = BuildShader();
if (!s_vertexBuffer)
s_vertexBuffer = BuildVertexBuffer();
m_indexBuffer = s_indexBuffer;
m_sampler.SetWrapMode(nzSamplerWrap_Clamp); // Nécessaire pour ne pas voir les côtés
m_shader = s_shader;
m_vertexBuffer = s_vertexBuffer;
}
NzSkyboxBackground::NzSkyboxBackground(NzTexture* cubemapTexture) :
NzSkyboxBackground()
{
SetTexture(cubemapTexture);
}
NzSkyboxBackground::~NzSkyboxBackground()
{
if (m_indexBuffer.Reset())
s_indexBuffer = nullptr;
if (m_shader.Reset())
s_shader = nullptr;
if (m_vertexBuffer.Reset())
s_vertexBuffer = nullptr;
SetTexture(std::move(cubemapTexture));
}
void NzSkyboxBackground::Draw(const NzAbstractViewer* viewer) const
{
static NzRenderStates states(BuildRenderStates());
NzMatrix4f skyboxMatrix(viewer->GetViewMatrix());
skyboxMatrix.SetTranslation(NzVector3f::Zero());
NzRenderer::SetIndexBuffer(m_indexBuffer);
NzRenderer::SetMatrix(nzMatrixType_View, skyboxMatrix);
NzRenderer::SetMatrix(nzMatrixType_World, NzMatrix4f::Scale(NzVector3f(viewer->GetZNear())));
NzRenderer::SetRenderStates(states);
NzRenderer::SetRenderStates(s_renderStates);
NzRenderer::SetShader(m_shader);
NzRenderer::SetTexture(0, m_texture);
NzRenderer::SetTextureSampler(0, m_sampler);
@ -245,29 +54,107 @@ nzBackgroundType NzSkyboxBackground::GetBackgroundType() const
return nzBackgroundType_Skybox;
}
NzTexture* NzSkyboxBackground::GetTexture() const
bool NzSkyboxBackground::Initialize()
{
return m_texture;
const nzUInt16 indices[6*6] =
{
0, 1, 2, 0, 2, 3,
3, 2, 6, 3, 6, 7,
7, 6, 5, 7, 5, 4,
4, 5, 1, 4, 1, 0,
0, 3, 7, 0, 7, 4,
1, 6, 2, 1, 5, 6
};
const float vertices[8 * 3 * sizeof(float)] =
{
-1.0, 1.0, 1.0,
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, -1.0,
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
};
///TODO: Replace by ShaderNode (probably after Vulkan)
const char* fragmentShaderSource =
"#version 140\n"
"in vec3 vTexCoord;\n"
"out vec4 RenderTarget0;\n"
"uniform samplerCube Skybox;\n"
"uniform float VertexDepth;\n"
"void main()\n"
"{\n"
" RenderTarget0 = texture(Skybox, vTexCoord);\n"
" gl_FragDepth = VertexDepth;\n"
"}\n";
const char* vertexShaderSource =
"#version 140\n"
"in vec3 VertexPosition;\n"
"out vec3 vTexCoord;\n"
"uniform mat4 WorldViewProjMatrix;\n"
"void main()\n"
"{\n"
" gl_Position = WorldViewProjMatrix * vec4(VertexPosition, 1.0);\n"
" vTexCoord = vec3(VertexPosition.x, VertexPosition.y, -VertexPosition.z);\n"
"}\n";
try
{
NzErrorFlags flags(nzErrorFlag_ThrowException, true);
// Index buffer
NzIndexBufferRef indexBuffer = NzIndexBuffer::New(false, 36, nzDataStorage_Hardware, nzBufferUsage_Static);
indexBuffer->Fill(indices, 0, 36);
// Vertex buffer
NzVertexBufferRef vertexBuffer = NzVertexBuffer::New(NzVertexDeclaration::Get(nzVertexLayout_XYZ), 8, nzDataStorage_Hardware, nzBufferUsage_Static);
vertexBuffer->Fill(vertices, 0, 8);
// Shader
NzShaderRef shader = NzShader::New();
shader->AttachStageFromSource(nzShaderStage_Fragment, fragmentShaderSource);
shader->AttachStageFromSource(nzShaderStage_Vertex, vertexShaderSource);
shader->Link();
shader->SendInteger(shader->GetUniformLocation("Skybox"), 0);
shader->SendFloat(shader->GetUniformLocation("VertexDepth"), 1.f);
// Renderstates
s_renderStates.depthFunc = nzRendererComparison_Equal;
s_renderStates.faceCulling = nzFaceSide_Front;
s_renderStates.parameters[nzRendererParameter_DepthBuffer] = true;
s_renderStates.parameters[nzRendererParameter_DepthWrite] = false;
s_renderStates.parameters[nzRendererParameter_FaceCulling] = true;
// Exception-free zone
s_indexBuffer = std::move(indexBuffer);
s_shader = std::move(shader);
s_vertexBuffer = std::move(vertexBuffer);
}
catch (const std::exception& e)
{
NazaraError("Failed to initialise: " + NzString(e.what()));
return false;
}
return true;
}
void NzSkyboxBackground::SetTexture(NzTexture* cubemapTexture)
void NzSkyboxBackground::Uninitialize()
{
#if NAZARA_GRAPHICS_SAFE
if (cubemapTexture)
{
if (!cubemapTexture->IsValid())
{
NazaraError("Texture must be valid");
return;
}
if (!cubemapTexture->IsCubemap())
{
NazaraError("Texture must be a cubemap");
return;
}
}
#endif
m_texture = cubemapTexture;
s_indexBuffer.Reset();
s_shader.Reset();
s_vertexBuffer.Reset();
}

View File

@ -22,7 +22,7 @@ namespace
}
}
NzTextureBackground::NzTextureBackground()
NzTextureBackground::NzTextureBackground(NzTextureRef texture)
{
m_uberShader = NzUberShaderLibrary::Get("Basic");
@ -37,12 +37,8 @@ NzTextureBackground::NzTextureBackground()
m_materialDiffuseUniform = shader->GetUniformLocation("MaterialDiffuse");
m_materialDiffuseMapUniform = shader->GetUniformLocation("MaterialDiffuseMap");
m_vertexDepthUniform = shader->GetUniformLocation("VertexDepth");
}
NzTextureBackground::NzTextureBackground(NzTexture* texture) :
NzTextureBackground()
{
m_texture = texture;
SetTexture(std::move(texture));
}
void NzTextureBackground::Draw(const NzAbstractViewer* viewer) const
@ -68,13 +64,3 @@ nzBackgroundType NzTextureBackground::GetBackgroundType() const
{
return nzBackgroundType_Texture;
}
NzTexture* NzTextureBackground::GetTexture() const
{
return m_texture;
}
void NzTextureBackground::SetTexture(NzTexture* texture)
{
m_texture = texture;
}