mirror of
https://github.com/NoelFB/blah.git
synced 2024-11-25 16:18:57 +08:00
simplified texture and framebuffer create methods
This commit is contained in:
parent
1570e9becf
commit
bd9736df03
|
@ -17,6 +17,7 @@ namespace Blah
|
|||
|
||||
// Up to 4 color attachments + 1 depth/stencil
|
||||
using Attachments = StackVector<TextureRef, 5>;
|
||||
using AttachmentFormats = StackVector<TextureFormat, 5>;
|
||||
|
||||
class FrameBuffer;
|
||||
using FrameBufferRef = std::shared_ptr<FrameBuffer>;
|
||||
|
@ -44,7 +45,7 @@ namespace Blah
|
|||
|
||||
// Creates a new FrameBuffer with the given Texture Attachments. You must provide at least one Attachment.
|
||||
// If the FrameBuffer creation fails, it will return an invalid FrameBufferRef.
|
||||
static FrameBufferRef create(int width, int height, const TextureFormat* attachments, int attachmentCount);
|
||||
static FrameBufferRef create(int width, int height, const AttachmentFormats& attachments);
|
||||
|
||||
// Gets the list of Attachments from the FrameBuffer
|
||||
virtual Attachments& attachments() = 0;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
#include <memory>
|
||||
#include <blah/core/filesystem.h>
|
||||
|
||||
namespace Blah
|
||||
{
|
||||
|
@ -50,12 +51,9 @@ namespace Blah
|
|||
static TextureRef create(const Image& image);
|
||||
|
||||
// Creates a new Texture.
|
||||
// If image data is provided, it should be the full size of the texture.
|
||||
// If the Texture creation fails, it will return an invalid TextureRef.
|
||||
static TextureRef create(int width, int height, unsigned char* rgba);
|
||||
|
||||
// Creates a new Texture.
|
||||
// If the Texture creation fails, it will return an invalid TextureRef.
|
||||
static TextureRef create(int width, int height, TextureFormat format);
|
||||
static TextureRef create(int width, int height, TextureFormat format, unsigned char* data = nullptr);
|
||||
|
||||
// Creates a new Texture from a Stream.
|
||||
// If the Texture creation fails, it will return an invalid TextureRef.
|
||||
|
@ -63,7 +61,7 @@ namespace Blah
|
|||
|
||||
// Creates a new Texture from a File.
|
||||
// If the Texture creation fails, it will return an invalid TextureRef.
|
||||
static TextureRef create(const char* file);
|
||||
static TextureRef create(const FilePath& file);
|
||||
|
||||
// gets the width of the texture
|
||||
virtual int width() const = 0;
|
||||
|
@ -75,12 +73,11 @@ namespace Blah
|
|||
virtual TextureFormat format() const = 0;
|
||||
|
||||
// Sets the data of the Texture.
|
||||
// Note that the pixel buffer should be in the same format as the Texture. There is no row padding.
|
||||
// If the pixel buffer isn't the same size as the texture, it will set the minimum available amount of data.
|
||||
// Note that the data should be the same format and size as the Texture. There is no row padding.
|
||||
virtual void set_data(unsigned char* data) = 0;
|
||||
|
||||
// Gets the data of the Texture.
|
||||
// Note that the pixel buffer will be written to in the same format as the Texture,
|
||||
// Note that the data will be written to in the same format as the Texture,
|
||||
// and you should allocate enough space for the full texture. There is no row padding.
|
||||
virtual void get_data(unsigned char* data) = 0;
|
||||
|
||||
|
|
|
@ -5,20 +5,18 @@ using namespace Blah;
|
|||
|
||||
FrameBufferRef FrameBuffer::create(int width, int height)
|
||||
{
|
||||
static const TextureFormat attachment = TextureFormat::RGBA;
|
||||
return create(width, height, &attachment, 1);
|
||||
return create(width, height, { TextureFormat::RGBA });
|
||||
}
|
||||
|
||||
FrameBufferRef FrameBuffer::create(int width, int height, const TextureFormat* attachments, int attachment_count)
|
||||
FrameBufferRef FrameBuffer::create(int width, int height, const AttachmentFormats& attachments)
|
||||
{
|
||||
BLAH_ASSERT(width > 0 && height > 0, "FrameBuffer width and height must be larger than 0");
|
||||
BLAH_ASSERT(attachment_count <= Attachments::MaxCapacity, "Exceeded maximum attachment count");
|
||||
BLAH_ASSERT(attachment_count > 0, "At least one attachment must be provided");
|
||||
BLAH_ASSERT(attachments.size() > 0, "At least one attachment must be provided");
|
||||
|
||||
int color_count = 0;
|
||||
int depth_count = 0;
|
||||
|
||||
for (int i = 0; i < attachment_count; i++)
|
||||
for (int i = 0; i < attachments.size(); i++)
|
||||
{
|
||||
BLAH_ASSERT((int)attachments[i] > (int)TextureFormat::None && (int)attachments[i] < (int)TextureFormat::Count, "Invalid texture format");
|
||||
|
||||
|
@ -31,5 +29,5 @@ FrameBufferRef FrameBuffer::create(int width, int height, const TextureFormat* a
|
|||
BLAH_ASSERT(depth_count <= 1, "FrameBuffer can only have 1 Depth/Stencil Texture");
|
||||
BLAH_ASSERT(color_count <= Attachments::MaxCapacity - 1, "Exceeded maximum Color attachment count");
|
||||
|
||||
return GraphicsBackend::create_framebuffer(width, height, attachments, attachment_count);
|
||||
return GraphicsBackend::create_framebuffer(width, height, attachments.data(), attachments.size());
|
||||
}
|
||||
|
|
|
@ -335,9 +335,9 @@ const float* Material::get_value(const char* name, i64* length) const
|
|||
offset += calc_uniform_size(uniform);
|
||||
}
|
||||
|
||||
Log::warn("No Uniform '%s' exists", name);
|
||||
*length = 0;
|
||||
return nullptr;
|
||||
Log::warn("No Uniform '%s' exists", name);
|
||||
}
|
||||
|
||||
const Vector<TextureRef>& Material::textures() const
|
||||
|
|
|
@ -8,54 +8,28 @@ using namespace Blah;
|
|||
|
||||
TextureRef Texture::create(const Image& image)
|
||||
{
|
||||
auto tex = create(image.width, image.height, TextureFormat::RGBA);
|
||||
if (tex)
|
||||
tex->set_data((unsigned char*)image.pixels);
|
||||
return tex;
|
||||
return create(image.width, image.height, TextureFormat::RGBA, (unsigned char*)image.pixels);
|
||||
}
|
||||
|
||||
TextureRef Texture::create(int width, int height, unsigned char* rgba)
|
||||
{
|
||||
auto tex = create(width, height, TextureFormat::RGBA);
|
||||
if (tex)
|
||||
tex->set_data(rgba);
|
||||
return tex;
|
||||
}
|
||||
|
||||
TextureRef Texture::create(int width, int height, TextureFormat format)
|
||||
TextureRef Texture::create(int width, int height, TextureFormat format, unsigned char* data)
|
||||
{
|
||||
BLAH_ASSERT(width > 0 && height > 0, "Texture width and height must be larger than 0");
|
||||
BLAH_ASSERT((int)format > (int)TextureFormat::None && (int)format < (int)TextureFormat::Count, "Invalid texture format");
|
||||
|
||||
return GraphicsBackend::create_texture(width, height, format);
|
||||
auto tex = GraphicsBackend::create_texture(width, height, format);
|
||||
|
||||
if (tex && data != nullptr)
|
||||
tex->set_data(data);
|
||||
|
||||
return tex;
|
||||
}
|
||||
|
||||
TextureRef Texture::create(Stream& stream)
|
||||
{
|
||||
Image img = Image(stream);
|
||||
|
||||
if (img.pixels && img.width > 0 && img.height > 0)
|
||||
{
|
||||
auto tex = create(img.width, img.height, TextureFormat::RGBA);
|
||||
if (tex)
|
||||
tex->set_data((unsigned char*)img.pixels);
|
||||
return tex;
|
||||
}
|
||||
|
||||
return TextureRef();
|
||||
return create(Image(stream));
|
||||
}
|
||||
|
||||
TextureRef Texture::create(const char* file)
|
||||
TextureRef Texture::create(const FilePath& file)
|
||||
{
|
||||
Image img = Image(file);
|
||||
|
||||
if (img.pixels)
|
||||
{
|
||||
auto tex = create(img.width, img.height, TextureFormat::RGBA);
|
||||
if (tex)
|
||||
tex->set_data((unsigned char*)img.pixels);
|
||||
return tex;
|
||||
}
|
||||
|
||||
return TextureRef();
|
||||
return create(Image(file));
|
||||
}
|
Loading…
Reference in New Issue
Block a user