Added texture data format support

This commit is contained in:
2023-06-20 08:35:28 -07:00
parent 9aa6a0f529
commit 0b26b5a06a
20 changed files with 156 additions and 43 deletions

View File

@ -22,7 +22,7 @@ void TextureAsset::updateSync() {
this->state = 0x04;
this->texture.setSize(this->width, this->height, this->format);
this->texture.setSize(this->width, this->height, this->format, TEXTURE_DATA_FORMAT_UNSIGNED_BYTE);
this->texture.buffer(this->colors);
this->texture.wrapModeX = this->wrapModeX;
@ -57,7 +57,7 @@ void TextureAsset::updateAsync() {
switch(parseState) {
case TEXTURE_ASSET_HEADER_PARSE_STATE_VERSION: {
auto compared = strcmp(integer, "DT_1.00");
auto compared = strcmp(integer, "DT_2.00");
assertTrue(compared == 0);
j = 0;
parseState = TEXTURE_ASSET_HEADER_PARSE_STATE_WIDTH;
@ -120,7 +120,7 @@ void TextureAsset::updateAsync() {
}
}
this->colors = (struct Color *)((void *)(this->buffer + i));
this->colors = (uint8_t*)((void *)(this->buffer + i));
this->state = 0x03;
}

View File

@ -26,7 +26,7 @@ namespace Dawn {
AssetLoader loader;
uint8_t *buffer = nullptr;
int32_t width = -1, height = -1;
struct Color *colors;
uint8_t *colors;
enum TextureFormat format;
enum TextureWrapMode wrapModeX;
enum TextureWrapMode wrapModeY;

View File

@ -27,6 +27,11 @@ namespace Dawn {
TEXTURE_FILTER_MODE_LINEAR = 1
};
enum TextureDataFormat {
TEXTURE_DATA_FORMAT_UNSIGNED_BYTE = 0,
TEXTURE_DATA_FORMAT_FLOAT = 1
};
class ITexture : public StateOwner {
protected:
bool_t texturePropertiesNeedUpdating = true;
@ -69,11 +74,13 @@ namespace Dawn {
* @param width Width of the texture (in pixels).
* @param height Height of the texture (in pixels).
* @param format Data format of the texture to use.
* @param dataFormat Data format of the texture to use.
*/
virtual void setSize(
int32_t width,
int32_t height,
enum TextureFormat format
enum TextureFormat format,
enum TextureDataFormat dataFormat
) = 0;
/**

View File

@ -77,7 +77,7 @@ TrueTypeFaceTexture::TrueTypeFaceTexture(
y += face->glyph->bitmap.rows;
}
this->texture.setSize(w, h, TEXTURE_FORMAT_R);
this->texture.setSize(w, h, TEXTURE_FORMAT_R, TEXTURE_DATA_FORMAT_UNSIGNED_BYTE);
this->texture.buffer(buffer);
memoryFree(buffer);
}

View File

@ -13,10 +13,10 @@ Camera::Camera(SceneItem *item) :
renderTarget(item->scene->game->renderManager.getBackBuffer()),
fov(0.785398f),// 45 degrees,
type(CAMERA_TYPE_PERSPECTIVE),
orthoLeft(0.0f),
orthoRight(1.0f),
orthoBottom(0.0f),
orthoTop(1.0f),
orthoLeft(-0.5f),
orthoRight(0.5f),
orthoBottom(-0.5f),
orthoTop(0.5f),
clipNear(0.001f),
clipFar(1000.0f)
{

View File

@ -10,15 +10,34 @@ using namespace Dawn;
CameraTexture::CameraTexture(SceneItem *item) :
SceneItemComponent(item),
camera(nullptr),
renderTarget(32.0f, 32.0f)
renderTarget(32.0f, 32.0f),
updateOrtho(true)
{
}
void CameraTexture::onStart() {
if(this->camera == nullptr) this->camera = item->getComponent<Camera>();
useEffect([&]{
if(this->camera == nullptr) return;
auto effectCamera = [&]{
if(this->camera == nullptr) return teardown = [&]{};
this->camera->renderTarget = &this->renderTarget;
}, this->camera)();
if(!this->updateOrtho) return teardown = [&]{};
this->updateOrthoValues();
return teardown = useEvent([&](float_t w, float_t h){
this->updateOrthoValues();
}, this->camera->eventRenderTargetResized);
};
useEffectWithTeardown(effectCamera, this->camera);
useEffectWithTeardown(effectCamera, this->updateOrtho)();
}
void CameraTexture::updateOrthoValues() {
assertNotNull(this->camera);
float_t wr = this->camera->renderTarget->getWidth() / this->camera->renderTarget->getHeight();
wr *= 0.5f;//TODO: allow this to be editable
this->camera->orthoLeft = -wr;
this->camera->orthoRight = wr;
}

View File

@ -10,9 +10,17 @@
namespace Dawn {
class CameraTexture : public SceneItemComponent {
protected:
std::function<void()> teardown;
void updateOrthoValues();
public:
// @optional
StateProperty<Camera*> camera;
// @optional
StateProperty<bool_t> updateOrtho;
TextureRenderTarget renderTarget;
CameraTexture(SceneItem *item);

View File

@ -55,11 +55,11 @@ void TiledSprite::onStart() {
if(this->meshHost == nullptr || this->tileset == nullptr) return;
auto tile = this->tileset->getTile(this->tile);
glm::vec2 size;
glm::vec2 quadSize;
switch(this->sizeType) {
case TILED_SPRITE_SIZE_TYPE_SCALE: {
size = glm::vec2(
quadSize = glm::vec2(
this->tileset->getTileWidth(this->tile),
this->tileset->getTileHeight(this->tile)
) * (float_t)this->size;
@ -68,15 +68,15 @@ void TiledSprite::onStart() {
case TILED_SPRITE_SIZE_TYPE_WIDTH_RATIO: {
float_t rw = this->tileset->getTileHeight(this->tile) / this->tileset->getTileWidth(this->tile);
size.x = (float_t)this->size;
size.y = size.x * rw;
quadSize.x = (float_t)this->size;
quadSize.y = quadSize.x * rw;
break;
}
case TILED_SPRITE_SIZE_TYPE_HEIGHT_RATIO: {
float_t rh = this->tileset->getTileWidth(this->tile) / this->tileset->getTileHeight(this->tile);
size.y = (float_t)this->size;
size.x = size.y * rh;
quadSize.y = (float_t)this->size;
quadSize.x = quadSize.y * rh;
break;
}
@ -84,8 +84,8 @@ void TiledSprite::onStart() {
assertUnreachable();
}
this->meshHost->xy0 = -size;
this->meshHost->xy1 = size;
this->meshHost->xy0 = -quadSize;
this->meshHost->xy1 = quadSize;
}, {
&this->tile,
&this->meshHost,