OpenGLRenderer: Fix textures

This commit is contained in:
Jérôme Leclercq
2020-08-09 18:38:58 +02:00
parent 0da2ee6c99
commit ac7b523bc7
5 changed files with 60 additions and 51 deletions

View File

@@ -12,10 +12,24 @@
#include <Nazara/Renderer/Enums.hpp>
#include <Nazara/Utility/Enums.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Loader.hpp>
#include <optional>
#include <string>
namespace Nz
{
struct GLTextureFormat
{
GLint internalFormat;
GLenum format;
GLenum type;
GLenum swizzleR;
GLenum swizzleG;
GLenum swizzleB;
GLenum swizzleA;
};
inline std::optional<GLTextureFormat> DescribeTextureFormat(PixelFormat pixelFormat);
inline GLenum ToOpenGL(BlendFunc blendFunc);
inline GLenum ToOpenGL(FaceSide filter);
inline GLenum ToOpenGL(SamplerFilter filter);

View File

@@ -10,6 +10,19 @@
namespace Nz
{
inline std::optional<GLTextureFormat> DescribeTextureFormat(PixelFormat pixelFormat)
{
switch (pixelFormat)
{
case PixelFormat_A8: return GLTextureFormat { GL_R8, GL_RED, GL_UNSIGNED_BYTE, GL_ZERO, GL_ZERO, GL_ZERO, GL_RED };
case PixelFormat_RGB8: return GLTextureFormat { GL_SRGB8, GL_RGB, GL_UNSIGNED_BYTE, GL_RED, GL_GREEN, GL_BLUE, GL_ZERO };
case PixelFormat_RGBA8: return GLTextureFormat { GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE, GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA };
}
NazaraError("Unhandled PixelFormat 0x" + String::Number(UnderlyingCast(pixelFormat), 16));
return {};
}
inline GLenum ToOpenGL(BlendFunc blendFunc)
{
switch (blendFunc)

View File

@@ -29,7 +29,7 @@ namespace Nz::GL
inline void SetParameterfv(GLenum pname, const GLfloat* param);
inline void SetParameteriv(GLenum pname, const GLint* param);
inline void TexImage2D(GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border);
inline void TexImage2D(GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type);
inline void TexImage2D(GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void* data);
inline void TexSubImage2D(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void* data);
@@ -39,6 +39,8 @@ namespace Nz::GL
private:
static inline GLuint CreateHelper(OpenGLDevice& device, const Context& context);
static inline void DestroyHelper(OpenGLDevice& device, const Context& context, GLuint objectId);
TextureTarget m_target;
};
}

View File

@@ -13,7 +13,8 @@ namespace Nz::GL
assert(m_objectId);
const Context& context = EnsureDeviceContext();
context.glTexParameterf(m_objectId, pname, param);
context.BindTexture(m_target, m_objectId);
context.glTexParameterf(ToOpenGL(m_target), pname, param);
}
inline void Texture::SetParameteri(GLenum pname, GLint param)
@@ -21,7 +22,8 @@ namespace Nz::GL
assert(m_objectId);
const Context& context = EnsureDeviceContext();
context.glTexParameteri(m_objectId, pname, param);
context.BindTexture(m_target, m_objectId);
context.glTexParameteri(ToOpenGL(m_target), pname, param);
}
inline void Texture::SetParameterfv(GLenum pname, const GLfloat* param)
@@ -29,7 +31,8 @@ namespace Nz::GL
assert(m_objectId);
const Context& context = EnsureDeviceContext();
context.glTexParameterfv(m_objectId, pname, param);
context.BindTexture(m_target, m_objectId);
context.glTexParameterfv(ToOpenGL(m_target), pname, param);
}
inline void Texture::SetParameteriv(GLenum pname, const GLint* param)
@@ -37,38 +40,39 @@ namespace Nz::GL
assert(m_objectId);
const Context& context = EnsureDeviceContext();
context.glTexParameteriv(m_objectId, pname, param);
context.BindTexture(m_target, m_objectId);
context.glTexParameteriv(ToOpenGL(m_target), pname, param);
}
inline void Texture::TexImage2D(GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border)
inline void Texture::TexImage2D(GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type)
{
return TexImage2D(level, internalFormat, width, height, border, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
return TexImage2D(level, internalFormat, width, height, border, format, type, nullptr);
}
inline void Texture::TexImage2D(GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void* data)
{
const Context& context = EnsureDeviceContext();
context.BindTexture(TextureTarget::Target2D, m_objectId);
m_target = TextureTarget::Target2D;
context.glTexImage2D(GL_TEXTURE_2D, level, internalFormat, width, height, border, format, type, data);
const Context& context = EnsureDeviceContext();
context.BindTexture(m_target, m_objectId);
context.glTexImage2D(ToOpenGL(m_target), level, internalFormat, width, height, border, format, type, data);
//< TODO: Handle errors
}
inline void Texture::TexSubImage2D(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void* data)
{
const Context& context = EnsureDeviceContext();
context.BindTexture(TextureTarget::Target2D, m_objectId);
context.glTexSubImage2D(GL_TEXTURE_2D, level, xoffset, yoffset, width, height, format, type, data);
context.BindTexture(m_target, m_objectId);
context.glTexSubImage2D(ToOpenGL(m_target), level, xoffset, yoffset, width, height, format, type, data);
//< TODO: Handle errors
}
inline GLuint Texture::CreateHelper(OpenGLDevice& device, const Context& context)
{
GLuint sampler = 0;
context.glGenTextures(1U, &sampler);
GLuint texture = 0;
context.glGenTextures(1U, &texture);
return sampler;
return texture;
}
inline void Texture::DestroyHelper(OpenGLDevice& device, const Context& context, GLuint objectId)