Textures are working now

This commit is contained in:
2023-11-19 11:39:48 -06:00
parent 830694ee0a
commit c2fb75df97
9 changed files with 93 additions and 69 deletions

View File

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

View File

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

View File

@ -11,7 +11,6 @@ namespace Dawn {
uint8_t r, g, b, a; uint8_t r, g, b, a;
}; };
#pragma pack(push, 4)
struct Color final { struct Color final {
/** /**
* Returns a color from a string. * 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_DEF(r,g,b,a) { r, g, b, a }
#define COLOR_WHITE COLOR_DEF(1.0f, 1.0f, 1.0f, 1.0f) #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_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_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_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 #define COLOR_TRANSPARENT COLOR_WHITE_TRANSPARENT
} }

View File

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

View File

@ -85,9 +85,6 @@ void RenderPipeline::renderSceneCamera(
renderPasses.insert(renderPasses.end(), rp.begin(), rp.end()); renderPasses.insert(renderPasses.end(), rp.begin(), rp.end());
} }
auto rp = renderPasses[0];
rp->bind();
// TODO: Make clearing the buffers editable! // TODO: Make clearing the buffers editable!
renderTarget->bind(); renderTarget->bind();
renderTarget->clear( renderTarget->clear(
@ -95,8 +92,6 @@ void RenderPipeline::renderSceneCamera(
RENDER_TARGET_CLEAR_DEPTH RENDER_TARGET_CLEAR_DEPTH
); );
rp->bind();
for(auto renderPass : renderPasses) { for(auto renderPass : renderPasses) {
renderPass->bind(); renderPass->bind();
renderPass->setData(); renderPass->setData();

View File

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

View File

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

View File

@ -23,15 +23,31 @@ void Dawn::helloWorldScene(Scene &s) {
cubeMesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT); cubeMesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT);
CubeMesh::buffer(cubeMesh, glm::vec3(-1, -1, -1), glm::vec3(1, 1, 1), 0, 0); 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 cubeItem = s.createSceneItem();
auto cubeMeshRenderer = cubeItem->addComponent<MeshRenderer>(); auto cubeMeshRenderer = cubeItem->addComponent<MeshRenderer>();
cubeMeshRenderer->mesh = cubeMesh; cubeMeshRenderer->mesh = cubeMesh;
auto cubeMaterial = cubeItem->addComponent<SimpleTexturedMaterial>(); auto cubeMaterial = cubeItem->addComponent<SimpleTexturedMaterial>();
// addSimpleComponent(cubeItem, [](auto &cmp, auto &events) { cubeMaterial->setTexture(texture);
// events.push_back(cmp.getScene()->onUnpausedUpdate.listen([&](float_t delta) {
// auto item = cmp.getItem(); addSimpleComponent(cubeItem, [](auto &cmp, auto &events) {
// item->setLocalRotation(item->getLocalRotation() * glm::quat(glm::vec3(1, 1, 0) * delta)); events.push_back(cmp.getScene()->onUnpausedUpdate.listen([&](float_t delta) {
// // item->setLocalScale(item->getLocalScale() + glm::vec3(1, 1, 1) * 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() { void Texture::updateTextureProperties() {
auto setWrapMode = [&](GLenum axis, enum TextureWrapMode wm) { auto setWrapMode = [](GLenum axis, enum TextureWrapMode wm) {
switch(wm) { switch(wm) {
case TEXTURE_WRAP_MODE_REPEAT: case TextureWrapMode::REPEAT:
glTexParameteri(GL_TEXTURE_2D, axis, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, axis, GL_REPEAT);
break; break;
case TEXTURE_WRAP_MODE_MIRRORED_REPEAT: case TextureWrapMode::MIRRORED_REPEAT:
glTexParameteri(GL_TEXTURE_2D, axis, GL_MIRRORED_REPEAT); glTexParameteri(GL_TEXTURE_2D, axis, GL_MIRRORED_REPEAT);
break; break;
case TEXTURE_WRAP_MODE_CLAMP_TO_EDGE: case TextureWrapMode::CLAMP_TO_EDGE:
glTexParameteri(GL_TEXTURE_2D, axis, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, axis, GL_CLAMP_TO_EDGE);
break; break;
case TEXTURE_WRAP_MODE_CLAMP_TO_BORDER: case TextureWrapMode::CLAMP_TO_BORDER:
glTexParameteri(GL_TEXTURE_2D, axis, GL_CLAMP_TO_BORDER); glTexParameteri(GL_TEXTURE_2D, axis, GL_CLAMP_TO_BORDER);
break; break;
@ -90,18 +90,18 @@ void Texture::updateTextureProperties() {
setWrapMode(GL_TEXTURE_WRAP_S, this->wrapModeX); setWrapMode(GL_TEXTURE_WRAP_S, this->wrapModeX);
setWrapMode(GL_TEXTURE_WRAP_T, this->wrapModeY); setWrapMode(GL_TEXTURE_WRAP_T, this->wrapModeY);
auto setFilterMode = [&]( auto setFilterMode = [](
GLenum minMag, GLenum minMag,
enum TextureFilterMode filter, enum TextureFilterMode filter,
enum TextureFilterMode mapFilterMode enum TextureFilterMode mapFilterMode
) { ) {
switch(filter) { switch(filter) {
case TEXTURE_FILTER_MODE_NEAREST: { case TextureFilterMode::NEAREST: {
glTexParameteri(GL_TEXTURE_2D, minMag, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, minMag, GL_NEAREST);
break; break;
} }
case TEXTURE_FILTER_MODE_LINEAR: { case TextureFilterMode::LINEAR: {
glTexParameteri(GL_TEXTURE_2D, minMag, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, minMag, GL_LINEAR);
break; break;
} }
@ -126,19 +126,19 @@ void Texture::bufferRaw(const void *data) {
GLenum format; GLenum format;
switch(this->format) { switch(this->format) {
case TEXTURE_FORMAT_R: case TextureFormat::R:
format = GL_RED; format = GL_RED;
break; break;
case TEXTURE_FORMAT_RG: case TextureFormat::RG:
format = GL_RG; format = GL_RG;
break; break;
case TEXTURE_FORMAT_RGB: case TextureFormat::RGB:
format = GL_RGB; format = GL_RGB;
break; break;
case TEXTURE_FORMAT_RGBA: case TextureFormat::RGBA:
format = GL_RGBA; format = GL_RGBA;
break; break;
@ -148,11 +148,11 @@ void Texture::bufferRaw(const void *data) {
GLenum dataFormat; GLenum dataFormat;
switch(this->dataFormat) { switch(this->dataFormat) {
case TEXTURE_DATA_FORMAT_UNSIGNED_BYTE: case TextureDataFormat::UNSIGNED_BYTE:
dataFormat = GL_UNSIGNED_BYTE; dataFormat = GL_UNSIGNED_BYTE;
break; break;
case TEXTURE_DATA_FORMAT_FLOAT: case TextureDataFormat::FLOAT:
dataFormat = GL_FLOAT; dataFormat = GL_FLOAT;
break; break;
@ -174,19 +174,20 @@ void Texture::bufferRaw(const void *data) {
void Texture::buffer(const struct ColorU8 pixels[]) { void Texture::buffer(const struct ColorU8 pixels[]) {
assertTrue( assertTrue(
this->dataFormat == TEXTURE_DATA_FORMAT_UNSIGNED_BYTE, this->dataFormat == TextureDataFormat::UNSIGNED_BYTE,
"Texture data format must be unsigned byte!" "Texture data format must be unsigned byte!"
); );
this->bufferRaw((void*)pixels); this->bufferRaw((void*)pixels);
} }
void Texture::buffer(const struct Color pixels[]) { void Texture::buffer(const struct Color pixels[]) {
std::cout << "Correct buffer" << std::endl;
assertTrue( assertTrue(
this->dataFormat == TEXTURE_DATA_FORMAT_FLOAT, this->dataFormat == TextureDataFormat::FLOAT,
"Texture data format must be float!" "Texture data format must be float!"
); );
assertTrue( assertTrue(
this->format == TEXTURE_FORMAT_RGBA, this->format == TextureFormat::RGBA,
"Texture format must be RGBA!" "Texture format must be RGBA!"
); );
this->bufferRaw((void*)pixels); this->bufferRaw((void*)pixels);
@ -194,7 +195,7 @@ void Texture::buffer(const struct Color pixels[]) {
void Texture::buffer(const uint8_t pixels[]) { void Texture::buffer(const uint8_t pixels[]) {
assertTrue( assertTrue(
this->dataFormat == TEXTURE_DATA_FORMAT_UNSIGNED_BYTE, this->dataFormat == TextureDataFormat::UNSIGNED_BYTE,
"Texture data format must be unsigned byte!" "Texture data format must be unsigned byte!"
); );
this->bufferRaw((void*)pixels); this->bufferRaw((void*)pixels);