Textures are working now

This commit is contained in:
2023-11-19 11:39:48 -06:00
parent 0515c48815
commit 688658c8e9
9 changed files with 93 additions and 69 deletions

View File

@ -39,7 +39,8 @@ namespace Dawn {
/**
* Cleans up the render pass.
*/
virtual ~IRenderPass() { }
virtual ~IRenderPass() {
}
};
template<class S, typename D>
@ -48,7 +49,7 @@ namespace Dawn {
std::shared_ptr<S> shader;
const std::unordered_map<
shadertexturebinding_t, std::shared_ptr<Texture>
> &textures;
> textures;
std::shared_ptr<Mesh> mesh;
const enum MeshDrawMode drawMode;
const int32_t indiceStart;
@ -98,7 +99,6 @@ namespace Dawn {
}
void bind() override {
std::cout << "textures: " << textures.size() << "\n";
shader->bind();
}
@ -108,7 +108,6 @@ namespace Dawn {
void upload() override {
for(auto &pair : textures) {
assertNotNull(pair.second, "Texture cannot be null!");
pair.second->bind(pair.first);
}
shader->upload();
@ -119,6 +118,9 @@ namespace Dawn {
mesh->draw(drawMode, indiceStart, indiceCount);
}
}
~RenderPass() override {
}
};
struct RenderPassContext {

View File

@ -19,7 +19,6 @@ void SimpleTexturedMaterial::setTexture(std::shared_ptr<Texture> texture) {
this->texture = texture;
}
void SimpleTexturedMaterial::setColor(const struct Color color) {
this->data.color = color;
}
@ -30,7 +29,6 @@ std::vector<std::shared_ptr<IRenderPass>> SimpleTexturedMaterial::getPasses(
this->data.model = this->getItem()->getWorldTransform();
this->data.projection = ctx.camera->getProjection();
this->data.view = ctx.camera->getItem()->getWorldTransform();
this->data.color = COLOR_RED;
auto textures = std::unordered_map<
shadertexturebinding_t, std::shared_ptr<Texture>
>();

View File

@ -11,7 +11,6 @@ namespace Dawn {
uint8_t r, g, b, a;
};
#pragma pack(push, 4)
struct Color final {
/**
* Returns a color from a string.
@ -71,7 +70,6 @@ namespace Dawn {
};
}
};
#pragma pack(pop)
#define COLOR_DEF(r,g,b,a) { r, g, b, a }
#define COLOR_WHITE COLOR_DEF(1.0f, 1.0f, 1.0f, 1.0f)
@ -85,5 +83,7 @@ namespace Dawn {
#define COLOR_CORNFLOWER_BLUE COLOR_DEF(0.4f, 0.6f, 0.9f, 1.0f)
#define COLOR_WHITE_TRANSPARENT COLOR_DEF(1.0f, 1.0f, 1.0f, 0.0f)
#define COLOR_BLACK_TRANSPARENT COLOR_DEF(0.0f, 0.0f, 0.0f, 0.0f)
#define COLOR_YELLOW COLOR_DEF(1.0f, 1.0f, 0.0f, 1.0f)
#define COLOR_CYAN COLOR_DEF(0.0f, 1.0f, 1.0f, 1.0f)
#define COLOR_TRANSPARENT COLOR_WHITE_TRANSPARENT
}

View File

@ -7,38 +7,38 @@
#include "display/Color.hpp"
namespace Dawn {
enum TextureFormat {
TEXTURE_FORMAT_R = 1,
TEXTURE_FORMAT_RG = 2,
TEXTURE_FORMAT_RGB = 3,
TEXTURE_FORMAT_RGBA = 4
enum class TextureFormat {
R = 1,
RG = 2,
RGB = 3,
RGBA = 4
};
enum TextureWrapMode {
TEXTURE_WRAP_MODE_REPEAT = 0,
TEXTURE_WRAP_MODE_MIRRORED_REPEAT = 1,
TEXTURE_WRAP_MODE_CLAMP_TO_EDGE = 2,
TEXTURE_WRAP_MODE_CLAMP_TO_BORDER = 3
enum class TextureWrapMode {
REPEAT = 0,
MIRRORED_REPEAT = 1,
CLAMP_TO_EDGE = 2,
CLAMP_TO_BORDER = 3
};
enum TextureFilterMode {
TEXTURE_FILTER_MODE_NEAREST = 0,
TEXTURE_FILTER_MODE_LINEAR = 1
enum class TextureFilterMode {
NEAREST = 0,
LINEAR = 1
};
enum TextureDataFormat {
TEXTURE_DATA_FORMAT_UNSIGNED_BYTE = 0,
TEXTURE_DATA_FORMAT_FLOAT = 1
enum class TextureDataFormat {
UNSIGNED_BYTE = 0,
FLOAT = 1
};
class ITexture {
public:
enum TextureWrapMode wrapModeX = TEXTURE_WRAP_MODE_REPEAT;
enum TextureWrapMode wrapModeY = TEXTURE_WRAP_MODE_REPEAT;
enum TextureFilterMode filterModeMin = TEXTURE_FILTER_MODE_NEAREST;
enum TextureFilterMode filterModeMag = TEXTURE_FILTER_MODE_NEAREST;
enum TextureFilterMode mipMapFilterModeMin = TEXTURE_FILTER_MODE_NEAREST;
enum TextureFilterMode mipMapFilterModeMag = TEXTURE_FILTER_MODE_NEAREST;
enum TextureWrapMode wrapModeX = TextureWrapMode::REPEAT;
enum TextureWrapMode wrapModeY = TextureWrapMode::REPEAT;
enum TextureFilterMode filterModeMin = TextureFilterMode::NEAREST;
enum TextureFilterMode filterModeMag = TextureFilterMode::NEAREST;
enum TextureFilterMode mipMapFilterModeMin = TextureFilterMode::NEAREST;
enum TextureFilterMode mipMapFilterModeMag = TextureFilterMode::NEAREST;
/**
* Returns the width of the texture.
@ -85,6 +85,14 @@ namespace Dawn {
*/
virtual void buffer(const struct ColorU8 pixels[]) = 0;
/**
* Buffer pixel data onto the GPU. Pixel buffering is rather costly so
* avoid doing this too often.
*
* @param pixels Array of pixels you're trying to buffer.
*/
virtual void buffer(const struct Color pixels[]) = 0;
/**
* Buffer pixel data onto the GPU. Pixel buffering is rather costly so
* avoid doing this too often.

View File

@ -84,9 +84,6 @@ void RenderPipeline::renderSceneCamera(
auto rp = renderable->getPasses(ctx);
renderPasses.insert(renderPasses.end(), rp.begin(), rp.end());
}
auto rp = renderPasses[0];
rp->bind();
// TODO: Make clearing the buffers editable!
renderTarget->bind();
@ -94,8 +91,6 @@ void RenderPipeline::renderSceneCamera(
RENDER_TARGET_CLEAR_COLOR |
RENDER_TARGET_CLEAR_DEPTH
);
rp->bind();
for(auto renderPass : renderPasses) {
renderPass->bind();

View File

@ -7,7 +7,7 @@
#include "dawnlibs.hpp"
namespace Dawn {
enum ShaderParameterType {
enum class ShaderParameterType {
VEC2,
VEC3,
VEC4,

View File

@ -71,14 +71,16 @@ void RenderHost::init(const std::shared_ptr<Game> game) {
backBufferRenderTarget->scale = (float_t)fbWidth / (float_t)windowWidth;
// Framebuffer callback
// glfwSetFramebufferSizeCallback(window, [&](
// GLFWwindow *window,
// int32_t width,
// int32_t height
// ) {
// if(this->window == nullptr || window != this->window) return;
// std::cout << "Resize" << std::endl;
// });
glfwSetFramebufferSizeCallback(window, [](
GLFWwindow *window,
int32_t width,
int32_t height
) {
// if(this->window == nullptr || window != this->window) return;
Game* game = (Game*)glfwGetWindowUserPointer(window);
assertNotNull(game, "Game cannot be null!");
game->renderHost.backBufferRenderTarget->setSize(width, height);
});
}
void RenderHost::update(const std::shared_ptr<Game> game) {
@ -96,6 +98,8 @@ void RenderHost::update(const std::shared_ptr<Game> game) {
assertNoGLError();
glDepthFunc(GL_LESS);
assertNoGLError();
glEnable(GL_DEPTH_TEST);
assertNoGLError();
// Pipeline
renderPipeline.render(game);

View File

@ -23,15 +23,31 @@ void Dawn::helloWorldScene(Scene &s) {
cubeMesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT);
CubeMesh::buffer(cubeMesh, glm::vec3(-1, -1, -1), glm::vec3(1, 1, 1), 0, 0);
auto texture = std::make_shared<Texture>();
texture->setSize(
3,
3,
TextureFormat::RGBA,
TextureDataFormat::FLOAT
);
struct Color colors[9] = {
COLOR_RED, COLOR_BLUE, COLOR_GREEN,
COLOR_MAGENTA, COLOR_WHITE, COLOR_BLACK,
COLOR_CORNFLOWER_BLUE, COLOR_YELLOW, COLOR_CYAN
};
texture->buffer(colors);
auto cubeItem = s.createSceneItem();
auto cubeMeshRenderer = cubeItem->addComponent<MeshRenderer>();
cubeMeshRenderer->mesh = cubeMesh;
auto cubeMaterial = cubeItem->addComponent<SimpleTexturedMaterial>();
// addSimpleComponent(cubeItem, [](auto &cmp, auto &events) {
// events.push_back(cmp.getScene()->onUnpausedUpdate.listen([&](float_t delta) {
// auto item = cmp.getItem();
// item->setLocalRotation(item->getLocalRotation() * glm::quat(glm::vec3(1, 1, 0) * delta));
// // item->setLocalScale(item->getLocalScale() + glm::vec3(1, 1, 1) * delta);
// }));
// });
cubeMaterial->setTexture(texture);
addSimpleComponent(cubeItem, [](auto &cmp, auto &events) {
events.push_back(cmp.getScene()->onUnpausedUpdate.listen([&](float_t delta) {
auto item = cmp.getItem();
item->setLocalRotation(item->getLocalRotation() * glm::quat(glm::vec3(1, 1, 0) * delta));
// item->setLocalScale(item->getLocalScale() + glm::vec3(1, 1, 1) * delta);
}));
});
}

View File

@ -63,21 +63,21 @@ void Texture::setSize(
// }
void Texture::updateTextureProperties() {
auto setWrapMode = [&](GLenum axis, enum TextureWrapMode wm) {
auto setWrapMode = [](GLenum axis, enum TextureWrapMode wm) {
switch(wm) {
case TEXTURE_WRAP_MODE_REPEAT:
case TextureWrapMode::REPEAT:
glTexParameteri(GL_TEXTURE_2D, axis, GL_REPEAT);
break;
case TEXTURE_WRAP_MODE_MIRRORED_REPEAT:
case TextureWrapMode::MIRRORED_REPEAT:
glTexParameteri(GL_TEXTURE_2D, axis, GL_MIRRORED_REPEAT);
break;
case TEXTURE_WRAP_MODE_CLAMP_TO_EDGE:
case TextureWrapMode::CLAMP_TO_EDGE:
glTexParameteri(GL_TEXTURE_2D, axis, GL_CLAMP_TO_EDGE);
break;
case TEXTURE_WRAP_MODE_CLAMP_TO_BORDER:
case TextureWrapMode::CLAMP_TO_BORDER:
glTexParameteri(GL_TEXTURE_2D, axis, GL_CLAMP_TO_BORDER);
break;
@ -90,18 +90,18 @@ void Texture::updateTextureProperties() {
setWrapMode(GL_TEXTURE_WRAP_S, this->wrapModeX);
setWrapMode(GL_TEXTURE_WRAP_T, this->wrapModeY);
auto setFilterMode = [&](
auto setFilterMode = [](
GLenum minMag,
enum TextureFilterMode filter,
enum TextureFilterMode mapFilterMode
) {
switch(filter) {
case TEXTURE_FILTER_MODE_NEAREST: {
case TextureFilterMode::NEAREST: {
glTexParameteri(GL_TEXTURE_2D, minMag, GL_NEAREST);
break;
}
case TEXTURE_FILTER_MODE_LINEAR: {
case TextureFilterMode::LINEAR: {
glTexParameteri(GL_TEXTURE_2D, minMag, GL_LINEAR);
break;
}
@ -126,19 +126,19 @@ void Texture::bufferRaw(const void *data) {
GLenum format;
switch(this->format) {
case TEXTURE_FORMAT_R:
case TextureFormat::R:
format = GL_RED;
break;
case TEXTURE_FORMAT_RG:
case TextureFormat::RG:
format = GL_RG;
break;
case TEXTURE_FORMAT_RGB:
case TextureFormat::RGB:
format = GL_RGB;
break;
case TEXTURE_FORMAT_RGBA:
case TextureFormat::RGBA:
format = GL_RGBA;
break;
@ -148,11 +148,11 @@ void Texture::bufferRaw(const void *data) {
GLenum dataFormat;
switch(this->dataFormat) {
case TEXTURE_DATA_FORMAT_UNSIGNED_BYTE:
case TextureDataFormat::UNSIGNED_BYTE:
dataFormat = GL_UNSIGNED_BYTE;
break;
case TEXTURE_DATA_FORMAT_FLOAT:
case TextureDataFormat::FLOAT:
dataFormat = GL_FLOAT;
break;
@ -174,19 +174,20 @@ void Texture::bufferRaw(const void *data) {
void Texture::buffer(const struct ColorU8 pixels[]) {
assertTrue(
this->dataFormat == TEXTURE_DATA_FORMAT_UNSIGNED_BYTE,
this->dataFormat == TextureDataFormat::UNSIGNED_BYTE,
"Texture data format must be unsigned byte!"
);
this->bufferRaw((void*)pixels);
}
void Texture::buffer(const struct Color pixels[]) {
std::cout << "Correct buffer" << std::endl;
assertTrue(
this->dataFormat == TEXTURE_DATA_FORMAT_FLOAT,
this->dataFormat == TextureDataFormat::FLOAT,
"Texture data format must be float!"
);
assertTrue(
this->format == TEXTURE_FORMAT_RGBA,
this->format == TextureFormat::RGBA,
"Texture format must be RGBA!"
);
this->bufferRaw((void*)pixels);
@ -194,7 +195,7 @@ void Texture::buffer(const struct Color pixels[]) {
void Texture::buffer(const uint8_t pixels[]) {
assertTrue(
this->dataFormat == TEXTURE_DATA_FORMAT_UNSIGNED_BYTE,
this->dataFormat == TextureDataFormat::UNSIGNED_BYTE,
"Texture data format must be unsigned byte!"
);
this->bufferRaw((void*)pixels);