Added texture data format support
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -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;
|
||||
}
|
@ -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);
|
||||
|
@ -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,
|
||||
|
@ -19,8 +19,20 @@ add_subdirectory(save)
|
||||
# Assets
|
||||
set(LIMINAL_ASSETS_DIR ${DAWN_ASSETS_DIR}/games/liminal)
|
||||
|
||||
tool_texture(texture_eth FILE=${LIMINAL_ASSETS_DIR}/textures/eth.png)
|
||||
tool_texture(texture_border FILE=${LIMINAL_ASSETS_DIR}/textures/texture_test.png)
|
||||
tool_texture(texture_eth_faces_day
|
||||
FILE="${LIMINAL_ASSETS_DIR}/textures/eth/faces_day.png"
|
||||
)
|
||||
tool_texture(texture_eth_faces_night
|
||||
FILE="${LIMINAL_ASSETS_DIR}/textures/eth/faces_night.png"
|
||||
)
|
||||
tool_texture(texture_eth_poses_day
|
||||
FILE="${LIMINAL_ASSETS_DIR}/textures/eth/poses_day.png"
|
||||
)
|
||||
|
||||
|
||||
tool_texture(texture_border
|
||||
FILE=${LIMINAL_ASSETS_DIR}/textures/texture_test.png
|
||||
)
|
||||
tool_truetype(font_main
|
||||
REGULAR=${LIMINAL_ASSETS_DIR}/fonts/Ysabeau-Medium.ttf
|
||||
BOLD=${LIMINAL_ASSETS_DIR}/fonts/Ysabeau-SemiBold.ttf
|
||||
|
@ -36,7 +36,12 @@ int32_t Texture::getHeight() {
|
||||
return this->height;
|
||||
}
|
||||
|
||||
void Texture::setSize(int32_t width, int32_t height, enum TextureFormat format) {
|
||||
void Texture::setSize(
|
||||
int32_t width,
|
||||
int32_t height,
|
||||
enum TextureFormat format,
|
||||
enum TextureDataFormat dataFormat
|
||||
) {
|
||||
if(this->id != -1) {
|
||||
glDeleteTextures(1, &this->id);
|
||||
this->id = -1;
|
||||
@ -45,6 +50,7 @@ void Texture::setSize(int32_t width, int32_t height, enum TextureFormat format)
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
this->format = format;
|
||||
this->dataFormat = dataFormat;
|
||||
|
||||
glGenTextures(1, &this->id);
|
||||
if(this->id <= 0) throw "Texture generation failed!";
|
||||
@ -151,7 +157,6 @@ void Texture::updateTextureProperties() {
|
||||
|
||||
void Texture::bufferRaw(void *data) {
|
||||
assertTrue(this->isReady());
|
||||
glBindTexture(GL_TEXTURE_2D, this->id);
|
||||
|
||||
GLenum format;
|
||||
switch(this->format) {
|
||||
@ -175,25 +180,43 @@ void Texture::bufferRaw(void *data) {
|
||||
assertUnreachable();
|
||||
}
|
||||
|
||||
GLenum dataFormat;
|
||||
switch(this->dataFormat) {
|
||||
case TEXTURE_DATA_FORMAT_UNSIGNED_BYTE:
|
||||
dataFormat = GL_UNSIGNED_BYTE;
|
||||
break;
|
||||
|
||||
case TEXTURE_DATA_FORMAT_FLOAT:
|
||||
dataFormat = GL_FLOAT;
|
||||
break;
|
||||
|
||||
default:
|
||||
assertUnreachable();
|
||||
}
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, this->id);
|
||||
glTexImage2D(
|
||||
GL_TEXTURE_2D, 0, format,
|
||||
this->width, this->height,
|
||||
0, format, GL_UNSIGNED_BYTE, data
|
||||
0, format, dataFormat, data
|
||||
);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
this->texturePropertiesNeedUpdating = true;
|
||||
}
|
||||
|
||||
void Texture::buffer(struct ColorU8 pixels[]) {
|
||||
assertTrue(this->dataFormat == TEXTURE_DATA_FORMAT_UNSIGNED_BYTE);
|
||||
this->bufferRaw((void*)pixels);
|
||||
}
|
||||
|
||||
void Texture::buffer(struct Color pixels[]) {
|
||||
assertTrue(this->dataFormat == TEXTURE_DATA_FORMAT_FLOAT);
|
||||
assertTrue(this->format == TEXTURE_FORMAT_RGBA);
|
||||
this->bufferRaw((void*)pixels);
|
||||
// assertUnreachable();
|
||||
}
|
||||
|
||||
void Texture::buffer(uint8_t pixels[]) {
|
||||
assertTrue(this->dataFormat == TEXTURE_DATA_FORMAT_UNSIGNED_BYTE);
|
||||
this->bufferRaw((void*)pixels);
|
||||
}
|
||||
|
||||
|
@ -20,6 +20,7 @@ namespace Dawn {
|
||||
int32_t height = -1;
|
||||
GLuint id = -1;
|
||||
enum TextureFormat format;
|
||||
enum TextureDataFormat dataFormat;
|
||||
|
||||
void updateTextureProperties();
|
||||
void bufferRaw(void *data);
|
||||
@ -28,7 +29,12 @@ namespace Dawn {
|
||||
Texture();
|
||||
int32_t getWidth() override;
|
||||
int32_t getHeight() override;
|
||||
void setSize(int32_t width, int32_t height, enum TextureFormat format) override;
|
||||
void setSize(
|
||||
int32_t width,
|
||||
int32_t height,
|
||||
enum TextureFormat format,
|
||||
enum TextureDataFormat dataFormat
|
||||
) override;
|
||||
void fill(struct Color) override;
|
||||
void fill(uint8_t) override;
|
||||
bool_t isReady() override;
|
||||
|
@ -26,7 +26,7 @@ void TextureRenderTarget::setSize(float_t width, float_t height) {
|
||||
if(this->fboId != -1) glDeleteFramebuffers(1, &this->fboId);
|
||||
|
||||
// Resize texture
|
||||
this->texture.setSize((int32_t)width, (int32_t)height, TEXTURE_FORMAT_RGBA);
|
||||
this->texture.setSize((int32_t)width, (int32_t)height, TEXTURE_FORMAT_RGBA, TEXTURE_DATA_FORMAT_FLOAT);
|
||||
this->eventRenderTargetResized.invoke(this, width, height);
|
||||
|
||||
// Create Frame Buffer
|
||||
|
@ -17,10 +17,10 @@ void SimpleBillboardedShader::compile() {
|
||||
// Vertex Shader
|
||||
"#version 330 core\n"
|
||||
"layout (location = 0) in vec3 aPos;\n"
|
||||
"layout (location = 1) in vec2 aTexCoord;\n"
|
||||
"layout (location = 1) in vec2 aTexCoord;\n" +
|
||||
|
||||
RenderPipelineShaderBuffer::getShaderUniform() + ""
|
||||
|
||||
"uniform mat4 u_Proj;\n"
|
||||
"uniform mat4 u_View;\n"
|
||||
"uniform mat4 u_Model;\n"
|
||||
|
||||
"out vec2 o_TextCoord;\n"
|
||||
@ -31,7 +31,7 @@ void SimpleBillboardedShader::compile() {
|
||||
"vec3 right = normalize(cross(up, viewDirection));\n"
|
||||
"up = normalize(cross(viewDirection, right));\n"
|
||||
"vec3 billboardPosCam = vec3(u_View * vec4(billboardPos, 1.0));\n"
|
||||
"gl_Position = u_Proj * vec4(billboardPosCam + (right * aPos.x + up * aPos.y), 1.0);\n"
|
||||
"gl_Position = u_Projection * vec4(billboardPosCam + (right * aPos.x + up * aPos.y), 1.0);\n"
|
||||
"o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);\n"
|
||||
"}",
|
||||
|
||||
@ -53,10 +53,10 @@ void SimpleBillboardedShader::compile() {
|
||||
);
|
||||
#endif
|
||||
|
||||
this->paramProjection = this->getParameterByName("u_Proj");
|
||||
this->paramView = this->getParameterByName("u_View");
|
||||
this->paramModel = this->getParameterByName("u_Model");
|
||||
this->paramColor = this->getParameterByName("u_Color");
|
||||
this->paramTexture = this->getParameterByName("u_Text");
|
||||
this->paramHasTexture = this->getParameterByName("u_HasTexture");
|
||||
|
||||
this->bufferRenderPipeline = this->getBufferLocationByName(RenderPipelineShaderBuffer::getShaderUniformName());
|
||||
}
|
@ -5,12 +5,12 @@
|
||||
|
||||
#pragma once
|
||||
#include "display/shader/Shader.hpp"
|
||||
#include "display/shader/buffers/RenderPipelineShaderBuffer.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class SimpleBillboardedShader : public Shader {
|
||||
public:
|
||||
shaderparameter_t paramProjection;
|
||||
shaderparameter_t paramView;
|
||||
shaderbufferlocation_t bufferRenderPipeline;
|
||||
shaderparameter_t paramModel;
|
||||
shaderparameter_t paramColor;
|
||||
shaderparameter_t paramTexture;
|
||||
|
@ -23,7 +23,32 @@ void SimpleBillboardedMaterial::onDispose() {
|
||||
}
|
||||
|
||||
std::vector<struct ShaderPassItem> SimpleBillboardedMaterial::getRenderPasses(IRenderableContext &context) {
|
||||
return {
|
||||
auto mesh = this->item->getComponent<MeshRenderer>();
|
||||
auto shader = this->getGame()->renderManager.getShaderManager()->getShader<SimpleBillboardedShader>(this->shaderLock);
|
||||
|
||||
};
|
||||
assertNotNull(mesh);
|
||||
assertNotNull(mesh->mesh);
|
||||
assertNotNull(shader);
|
||||
|
||||
struct ShaderPassItem onlyPass;
|
||||
onlyPass.mesh = mesh->mesh;
|
||||
onlyPass.shader = shader;
|
||||
onlyPass.colorValues[shader->paramColor] = this->color;
|
||||
onlyPass.matrixValues[shader->paramModel] = this->transform->getWorldTransform();
|
||||
onlyPass.parameterBuffers[shader->bufferRenderPipeline] = &context.renderPipeline->shaderBuffer;
|
||||
|
||||
onlyPass.renderFlags = (
|
||||
RENDER_MANAGER_RENDER_FLAG_BLEND |
|
||||
RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST
|
||||
);
|
||||
|
||||
if(this->texture != nullptr) {
|
||||
onlyPass.boolValues[shader->paramHasTexture] = true;
|
||||
onlyPass.textureSlots[0] = this->texture;
|
||||
onlyPass.textureValues[shader->paramTexture] = 0;
|
||||
} else {
|
||||
onlyPass.boolValues[shader->paramHasTexture] = false;
|
||||
}
|
||||
|
||||
return { onlyPass };
|
||||
}
|
@ -5,6 +5,7 @@
|
||||
|
||||
#pragma once
|
||||
#include "scene/components/display/Material.hpp"
|
||||
#include "scene/components/display/mesh/MeshRenderer.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class SimpleBillboardedMaterial : public Material {
|
||||
|
@ -71,7 +71,7 @@ int32_t TextureTool::start() {
|
||||
|
||||
// Write info
|
||||
char headerBuffer[256];
|
||||
size_t headerBufferLength = sprintf((char *)headerBuffer, "DT_1.00|%i|%i|%i|%i|%i|%i|%i|",
|
||||
size_t headerBufferLength = sprintf((char *)headerBuffer, "DT_2.00|%i|%i|%i|%i|%i|%i|%i|",
|
||||
w,
|
||||
h,
|
||||
4, // RGBA,
|
||||
|
@ -16,17 +16,24 @@ void SceneAssetGenerator::generate(
|
||||
std::string tabs
|
||||
) {
|
||||
std::string assetType = "";
|
||||
asset->usageName = "asset" + std::to_string(assetNumber++);
|
||||
|
||||
if(asset->ref.empty()) {
|
||||
asset->usageName = "asset" + std::to_string(assetNumber++);
|
||||
} else {
|
||||
asset->usageName = asset->ref;
|
||||
}
|
||||
|
||||
switch(asset->type) {
|
||||
case SCENE_ASSET_TYPE_TEXTURE:
|
||||
assetType = "TextureAsset";
|
||||
assetMap[asset->fileName] = "&" + asset->usageName + "->texture";
|
||||
assetMap[asset->usageName] = "&" + asset->usageName + "->texture";
|
||||
break;
|
||||
|
||||
case SCENE_ASSET_TYPE_TRUETYPE_FONT:
|
||||
assetType = "TrueTypeAsset";
|
||||
assetMap[asset->fileName] = "&" + asset->usageName + "->font";
|
||||
assetMap[asset->usageName] = "&" + asset->usageName + "->font";
|
||||
break;
|
||||
|
||||
default:
|
||||
|
@ -12,7 +12,9 @@ std::vector<std::string> SceneAssetParser::getRequiredAttributes() {
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> SceneAssetParser::getOptionalAttributes() {
|
||||
return { };
|
||||
return {
|
||||
{ "ref", "" }
|
||||
};
|
||||
}
|
||||
|
||||
int32_t SceneAssetParser::onParse(
|
||||
@ -31,6 +33,8 @@ int32_t SceneAssetParser::onParse(
|
||||
*error = "Unknown asset type '" + values["type"] + "'";
|
||||
return 1;
|
||||
}
|
||||
|
||||
out->ref = values["ref"];
|
||||
|
||||
return 0;
|
||||
}
|
@ -16,6 +16,7 @@ namespace Dawn {
|
||||
SceneAssetType type;
|
||||
std::string fileName;
|
||||
std::string usageName;
|
||||
std::string ref;
|
||||
};
|
||||
|
||||
class SceneAssetParser : public XmlParser<SceneAsset> {
|
||||
|
Reference in New Issue
Block a user