Graphics/Backgrounds: Update backgrounds to new coding-style
Former-commit-id: 9f96b93706fd8417d6262392f0ce9ab9ca1985c3
This commit is contained in:
parent
0ec0e02a5f
commit
6092b0692b
|
|
@ -8,12 +8,19 @@
|
||||||
#define NAZARA_ABSTRACTBACKGROUND_HPP
|
#define NAZARA_ABSTRACTBACKGROUND_HPP
|
||||||
|
|
||||||
#include <Nazara/Prerequesites.hpp>
|
#include <Nazara/Prerequesites.hpp>
|
||||||
|
#include <Nazara/Core/ObjectLibrary.hpp>
|
||||||
|
#include <Nazara/Core/ObjectRef.hpp>
|
||||||
#include <Nazara/Graphics/Config.hpp>
|
#include <Nazara/Graphics/Config.hpp>
|
||||||
#include <Nazara/Graphics/Enums.hpp>
|
#include <Nazara/Graphics/Enums.hpp>
|
||||||
|
|
||||||
|
class NzAbstractBackground;
|
||||||
class NzAbstractViewer;
|
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:
|
public:
|
||||||
NzAbstractBackground() = default;
|
NzAbstractBackground() = default;
|
||||||
|
|
@ -22,6 +29,9 @@ class NAZARA_GRAPHICS_API NzAbstractBackground
|
||||||
virtual void Draw(const NzAbstractViewer* viewer) const = 0;
|
virtual void Draw(const NzAbstractViewer* viewer) const = 0;
|
||||||
|
|
||||||
virtual nzBackgroundType GetBackgroundType() const = 0;
|
virtual nzBackgroundType GetBackgroundType() const = 0;
|
||||||
|
|
||||||
|
private:
|
||||||
|
static NzBackgroundLibrary::LibraryMap s_library;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // NAZARA_ABSTRACTBACKGROUND_HPP
|
#endif // NAZARA_ABSTRACTBACKGROUND_HPP
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,11 @@
|
||||||
#include <Nazara/Graphics/AbstractBackground.hpp>
|
#include <Nazara/Graphics/AbstractBackground.hpp>
|
||||||
#include <Nazara/Renderer/UberShader.hpp>
|
#include <Nazara/Renderer/UberShader.hpp>
|
||||||
|
|
||||||
|
class NzColorBackground;
|
||||||
|
|
||||||
|
using NzColorBackgroundConstRef = NzObjectRef<const NzColorBackground>;
|
||||||
|
using NzColorBackgroundRef = NzObjectRef<NzColorBackground>;
|
||||||
|
|
||||||
class NAZARA_GRAPHICS_API NzColorBackground : public NzAbstractBackground
|
class NAZARA_GRAPHICS_API NzColorBackground : public NzAbstractBackground
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
@ -24,6 +29,8 @@ class NAZARA_GRAPHICS_API NzColorBackground : public NzAbstractBackground
|
||||||
|
|
||||||
void SetColor(const NzColor& color);
|
void SetColor(const NzColor& color);
|
||||||
|
|
||||||
|
template<typename... Args> static NzColorBackgroundRef New(Args&&... args);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
NzColor m_color;
|
NzColor m_color;
|
||||||
NzUberShaderConstRef m_uberShader;
|
NzUberShaderConstRef m_uberShader;
|
||||||
|
|
@ -32,4 +39,6 @@ class NAZARA_GRAPHICS_API NzColorBackground : public NzAbstractBackground
|
||||||
int m_vertexDepthUniform;
|
int m_vertexDepthUniform;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#include <Nazara/Graphics/ColorBackground.inl>
|
||||||
|
|
||||||
#endif // NAZARA_COLORBACKGROUND_HPP
|
#endif // NAZARA_COLORBACKGROUND_HPP
|
||||||
|
|
|
||||||
|
|
@ -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>
|
||||||
|
|
@ -15,23 +15,33 @@
|
||||||
#include <Nazara/Utility/IndexBuffer.hpp>
|
#include <Nazara/Utility/IndexBuffer.hpp>
|
||||||
#include <Nazara/Utility/VertexBuffer.hpp>
|
#include <Nazara/Utility/VertexBuffer.hpp>
|
||||||
|
|
||||||
|
class NzSkyboxBackground;
|
||||||
|
|
||||||
|
using NzSkyboxBackgroundConstRef = NzObjectRef<const NzSkyboxBackground>;
|
||||||
|
using NzSkyboxBackgroundRef = NzObjectRef<NzSkyboxBackground>;
|
||||||
|
|
||||||
class NAZARA_GRAPHICS_API NzSkyboxBackground : public NzAbstractBackground
|
class NAZARA_GRAPHICS_API NzSkyboxBackground : public NzAbstractBackground
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
NzSkyboxBackground();
|
NzSkyboxBackground(NzTextureRef cubemapTexture = NzTextureRef());
|
||||||
NzSkyboxBackground(NzTexture* cubemapTexture);
|
~NzSkyboxBackground() = default;
|
||||||
~NzSkyboxBackground();
|
|
||||||
|
|
||||||
void Draw(const NzAbstractViewer* viewer) const;
|
void Draw(const NzAbstractViewer* viewer) const;
|
||||||
|
|
||||||
nzBackgroundType GetBackgroundType() const;
|
nzBackgroundType GetBackgroundType() const;
|
||||||
NzTexture* GetTexture() const;
|
inline const NzTextureRef& GetTexture() const;
|
||||||
const NzTextureSampler& GetTextureSampler();
|
inline NzTextureSampler& GetTextureSampler();
|
||||||
|
inline const NzTextureSampler& GetTextureSampler() const;
|
||||||
|
|
||||||
void SetTexture(NzTexture* cubemapTexture);
|
inline void SetTexture(NzTextureRef cubemapTexture);
|
||||||
void SetTextureSampler(const NzTextureSampler& sampler);
|
inline void SetTextureSampler(const NzTextureSampler& sampler);
|
||||||
|
|
||||||
|
template<typename... Args> static NzSkyboxBackgroundRef New(Args&&... args);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
static bool Initialize();
|
||||||
|
static void Uninitialize();
|
||||||
|
|
||||||
NzTextureRef m_texture;
|
NzTextureRef m_texture;
|
||||||
NzTextureSampler m_sampler;
|
NzTextureSampler m_sampler;
|
||||||
NzIndexBufferRef m_indexBuffer;
|
NzIndexBufferRef m_indexBuffer;
|
||||||
|
|
@ -39,4 +49,6 @@ class NAZARA_GRAPHICS_API NzSkyboxBackground : public NzAbstractBackground
|
||||||
NzVertexBufferRef m_vertexBuffer;
|
NzVertexBufferRef m_vertexBuffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#include <Nazara/Graphics/SkyboxBackground.inl>
|
||||||
|
|
||||||
#endif // NAZARA_SKYBOXBACKGROUND_HPP
|
#endif // NAZARA_SKYBOXBACKGROUND_HPP
|
||||||
|
|
|
||||||
|
|
@ -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>
|
||||||
|
|
@ -12,18 +12,24 @@
|
||||||
#include <Nazara/Renderer/UberShader.hpp>
|
#include <Nazara/Renderer/UberShader.hpp>
|
||||||
#include <Nazara/Renderer/Texture.hpp>
|
#include <Nazara/Renderer/Texture.hpp>
|
||||||
|
|
||||||
|
class NzTextureBackground;
|
||||||
|
|
||||||
|
using NzTextureBackgroundConstRef = NzObjectRef<const NzTextureBackground>;
|
||||||
|
using NzTextureBackgroundRef = NzObjectRef<NzTextureBackground>;
|
||||||
|
|
||||||
class NAZARA_GRAPHICS_API NzTextureBackground : public NzAbstractBackground
|
class NAZARA_GRAPHICS_API NzTextureBackground : public NzAbstractBackground
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
NzTextureBackground();
|
NzTextureBackground(NzTextureRef texture = NzTextureRef());
|
||||||
NzTextureBackground(NzTexture* texture);
|
|
||||||
|
|
||||||
void Draw(const NzAbstractViewer* viewer) const;
|
void Draw(const NzAbstractViewer* viewer) const;
|
||||||
|
|
||||||
nzBackgroundType GetBackgroundType() 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:
|
private:
|
||||||
NzTextureRef m_texture;
|
NzTextureRef m_texture;
|
||||||
|
|
@ -34,4 +40,6 @@ class NAZARA_GRAPHICS_API NzTextureBackground : public NzAbstractBackground
|
||||||
int m_vertexDepthUniform;
|
int m_vertexDepthUniform;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#include <Nazara/Graphics/TextureBackground.inl>
|
||||||
|
|
||||||
#endif // NAZARA_TEXTUREBACKGROUND_HPP
|
#endif // NAZARA_TEXTUREBACKGROUND_HPP
|
||||||
|
|
|
||||||
|
|
@ -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>
|
||||||
|
|
@ -6,3 +6,5 @@
|
||||||
#include <Nazara/Graphics/Debug.hpp>
|
#include <Nazara/Graphics/Debug.hpp>
|
||||||
|
|
||||||
NzAbstractBackground::~NzAbstractBackground() = default;
|
NzAbstractBackground::~NzAbstractBackground() = default;
|
||||||
|
|
||||||
|
NzBackgroundLibrary::LibraryMap NzAbstractBackground::s_library;
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,9 @@
|
||||||
// This file is part of the "Nazara Engine - Graphics module"
|
// This file is part of the "Nazara Engine - Graphics module"
|
||||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
// 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/SkyboxBackground.hpp>
|
||||||
#include <Nazara/Graphics/Camera.hpp>
|
#include <Nazara/Core/ErrorFlags.hpp>
|
||||||
#include <Nazara/Graphics/Scene.hpp>
|
#include <Nazara/Graphics/AbstractViewer.hpp>
|
||||||
#include <Nazara/Renderer/OpenGL.hpp>
|
|
||||||
#include <Nazara/Renderer/Renderer.hpp>
|
#include <Nazara/Renderer/Renderer.hpp>
|
||||||
#include <Nazara/Utility/IndexBuffer.hpp>
|
#include <Nazara/Utility/IndexBuffer.hpp>
|
||||||
#include <Nazara/Utility/VertexBuffer.hpp>
|
#include <Nazara/Utility/VertexBuffer.hpp>
|
||||||
|
|
@ -19,217 +14,31 @@
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
NzIndexBuffer* BuildIndexBuffer()
|
static NzIndexBufferRef s_indexBuffer;
|
||||||
{
|
static NzRenderStates s_renderStates;
|
||||||
std::unique_ptr<NzIndexBuffer> indexBuffer(new NzIndexBuffer(false, 36, nzDataStorage_Hardware, nzBufferUsage_Static));
|
static NzShaderRef s_shader;
|
||||||
indexBuffer->SetPersistent(false);
|
static NzVertexBufferRef s_vertexBuffer;
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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_indexBuffer = s_indexBuffer;
|
||||||
m_sampler.SetWrapMode(nzSamplerWrap_Clamp); // Nécessaire pour ne pas voir les côtés
|
m_sampler.SetWrapMode(nzSamplerWrap_Clamp); // Nécessaire pour ne pas voir les côtés
|
||||||
m_shader = s_shader;
|
m_shader = s_shader;
|
||||||
m_vertexBuffer = s_vertexBuffer;
|
m_vertexBuffer = s_vertexBuffer;
|
||||||
}
|
|
||||||
|
|
||||||
NzSkyboxBackground::NzSkyboxBackground(NzTexture* cubemapTexture) :
|
SetTexture(std::move(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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NzSkyboxBackground::Draw(const NzAbstractViewer* viewer) const
|
void NzSkyboxBackground::Draw(const NzAbstractViewer* viewer) const
|
||||||
{
|
{
|
||||||
static NzRenderStates states(BuildRenderStates());
|
|
||||||
|
|
||||||
NzMatrix4f skyboxMatrix(viewer->GetViewMatrix());
|
NzMatrix4f skyboxMatrix(viewer->GetViewMatrix());
|
||||||
skyboxMatrix.SetTranslation(NzVector3f::Zero());
|
skyboxMatrix.SetTranslation(NzVector3f::Zero());
|
||||||
|
|
||||||
NzRenderer::SetIndexBuffer(m_indexBuffer);
|
NzRenderer::SetIndexBuffer(m_indexBuffer);
|
||||||
NzRenderer::SetMatrix(nzMatrixType_View, skyboxMatrix);
|
NzRenderer::SetMatrix(nzMatrixType_View, skyboxMatrix);
|
||||||
NzRenderer::SetMatrix(nzMatrixType_World, NzMatrix4f::Scale(NzVector3f(viewer->GetZNear())));
|
NzRenderer::SetMatrix(nzMatrixType_World, NzMatrix4f::Scale(NzVector3f(viewer->GetZNear())));
|
||||||
NzRenderer::SetRenderStates(states);
|
NzRenderer::SetRenderStates(s_renderStates);
|
||||||
NzRenderer::SetShader(m_shader);
|
NzRenderer::SetShader(m_shader);
|
||||||
NzRenderer::SetTexture(0, m_texture);
|
NzRenderer::SetTexture(0, m_texture);
|
||||||
NzRenderer::SetTextureSampler(0, m_sampler);
|
NzRenderer::SetTextureSampler(0, m_sampler);
|
||||||
|
|
@ -245,29 +54,107 @@ nzBackgroundType NzSkyboxBackground::GetBackgroundType() const
|
||||||
return nzBackgroundType_Skybox;
|
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
|
s_indexBuffer.Reset();
|
||||||
if (cubemapTexture)
|
s_shader.Reset();
|
||||||
{
|
s_vertexBuffer.Reset();
|
||||||
if (!cubemapTexture->IsValid())
|
|
||||||
{
|
|
||||||
NazaraError("Texture must be valid");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!cubemapTexture->IsCubemap())
|
|
||||||
{
|
|
||||||
NazaraError("Texture must be a cubemap");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
m_texture = cubemapTexture;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ namespace
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
NzTextureBackground::NzTextureBackground()
|
NzTextureBackground::NzTextureBackground(NzTextureRef texture)
|
||||||
{
|
{
|
||||||
m_uberShader = NzUberShaderLibrary::Get("Basic");
|
m_uberShader = NzUberShaderLibrary::Get("Basic");
|
||||||
|
|
||||||
|
|
@ -37,12 +37,8 @@ NzTextureBackground::NzTextureBackground()
|
||||||
m_materialDiffuseUniform = shader->GetUniformLocation("MaterialDiffuse");
|
m_materialDiffuseUniform = shader->GetUniformLocation("MaterialDiffuse");
|
||||||
m_materialDiffuseMapUniform = shader->GetUniformLocation("MaterialDiffuseMap");
|
m_materialDiffuseMapUniform = shader->GetUniformLocation("MaterialDiffuseMap");
|
||||||
m_vertexDepthUniform = shader->GetUniformLocation("VertexDepth");
|
m_vertexDepthUniform = shader->GetUniformLocation("VertexDepth");
|
||||||
}
|
|
||||||
|
|
||||||
NzTextureBackground::NzTextureBackground(NzTexture* texture) :
|
SetTexture(std::move(texture));
|
||||||
NzTextureBackground()
|
|
||||||
{
|
|
||||||
m_texture = texture;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NzTextureBackground::Draw(const NzAbstractViewer* viewer) const
|
void NzTextureBackground::Draw(const NzAbstractViewer* viewer) const
|
||||||
|
|
@ -68,13 +64,3 @@ nzBackgroundType NzTextureBackground::GetBackgroundType() const
|
||||||
{
|
{
|
||||||
return nzBackgroundType_Texture;
|
return nzBackgroundType_Texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
NzTexture* NzTextureBackground::GetTexture() const
|
|
||||||
{
|
|
||||||
return m_texture;
|
|
||||||
}
|
|
||||||
|
|
||||||
void NzTextureBackground::SetTexture(NzTexture* texture)
|
|
||||||
{
|
|
||||||
m_texture = texture;
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue