This commit is contained in:
Hartmut Seichter 2023-07-01 10:20:24 +02:00
parent 0eebe25ea9
commit b6691561e8
7 changed files with 58 additions and 51 deletions

View file

@ -21,3 +21,4 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_EXTENSIONS OFF)
add_subdirectory(src) add_subdirectory(src)
add_subdirectory(examples/simple)

View file

@ -0,0 +1,16 @@
set(paradiso_src
main.cpp
)
add_executable(
paradiso
${paradiso_src}
)
target_link_libraries(paradiso
paradiso_core
)
target_include_directories(paradiso
PRIVATE
lib)

View file

@ -62,10 +62,11 @@ auto main() -> int {
auto checker_board = paradiso::Bitmap::empty(paradiso::Size{4, 4}); auto checker_board = paradiso::Bitmap::empty(paradiso::Size{4, 4});
auto checker_board_2 = paradiso::Bitmap::from_data( auto checker_board_2 = paradiso::Bitmap::from_data(
paradiso::Size{2, 2}, paradiso::RGBA::from_rgb(0x00, 0xFF, 0x00), paradiso::Size{2, 2},
paradiso::RGBA::from_rgb(0x00, 0xFF, 0x00), paradiso::RGBA::from_rgb(0x00, 0xFF, 0x00), // G
paradiso::RGBA::from_rgb(0x00, 0xFF, 0x00), paradiso::RGBA::from_rgb(0xFF, 0x00, 0x00), // R
paradiso::RGBA::from_rgb(0x00, 0xFF, 0x00)); paradiso::RGBA::from_rgb(0x00, 0x00, 0xFF), // B
paradiso::RGBA::from_rgb(0xFF, 0x00, 0xFF));// C
auto window = paradiso::Window(); auto window = paradiso::Window();
@ -102,9 +103,9 @@ auto main() -> int {
while (window.update([&](auto& w) -> bool { while (window.update([&](auto& w) -> bool {
ctx.set_clearcolor(paradiso::RGBA::from_rgb(0xFF, green_slider, 0x00)); ctx.set_clearcolor(paradiso::RGBA::from_rgb(0xFF, green_slider, 0x00));
// ctx.set_viewport(paradiso::Rectangle{ ctx.set_viewport(paradiso::Rectangle{
// .size = w.client_size() .size = w.client_size()
// }); });
ctx.clear(); ctx.clear();
renderer.draw(sprite, shader); renderer.draw(sprite, shader);

View file

@ -1,18 +1,2 @@
add_subdirectory(lib) add_subdirectory(lib)
set(paradiso_src
main.cpp
)
add_executable(
paradiso
${paradiso_src}
)
target_link_libraries(paradiso
paradiso_core
)
target_include_directories(paradiso
PRIVATE
lib)

View file

@ -38,8 +38,8 @@ struct RGBA final {
static constexpr RGBA from_rgba(uint8_t red, uint8_t green, uint8_t blue, static constexpr RGBA from_rgba(uint8_t red, uint8_t green, uint8_t blue,
uint8_t alpha) noexcept { uint8_t alpha) noexcept {
return {.pixel = return {.pixel =
(value_type)(((((red << 8) | green) << 8) | blue) << 8) | (value_type)(((((alpha << 8) | blue) << 8) | green) << 8) |
alpha}; red};
} }
constexpr void to_float(float rgba_f[4]) const noexcept { constexpr void to_float(float rgba_f[4]) const noexcept {
@ -58,7 +58,10 @@ struct RGBA final {
constexpr uint8_t blue() const noexcept { constexpr uint8_t blue() const noexcept {
return uint8_t((pixel & 0xFF00) >> 8); return uint8_t((pixel & 0xFF00) >> 8);
} }
constexpr uint8_t alpha() const noexcept { return uint8_t(pixel & 0xFF); } constexpr uint8_t alpha() const noexcept {
return uint8_t(pixel & 0xFF);
}
constexpr RGBA& set_red(uint8_t v) noexcept { constexpr RGBA& set_red(uint8_t v) noexcept {
pixel = (pixel & 0x00FFFFFF) | (v << 24); pixel = (pixel & 0x00FFFFFF) | (v << 24);

View file

@ -30,6 +30,13 @@
namespace paradiso { namespace paradiso {
// 0 3 | y
// +------+ | ^
// | \ | | -- + - > x
// | \ | | / |
// +------+ | z
// 1 2 |
struct Sprite final { struct Sprite final {
using ChangeCountType = std::uint64_t; using ChangeCountType = std::uint64_t;
@ -39,7 +46,7 @@ struct Sprite final {
Vector2<float> pivot{Vector2<float>::zero()}; Vector2<float> pivot{Vector2<float>::zero()};
std::array<std::uint32_t, 6> indices{0, 3, 2, 2, 1, 0}; std::array<std::uint32_t, 6> indices{0, 2, 1, 0, 3, 2};
std::array<Vector3<float>, 4> vertices{ std::array<Vector3<float>, 4> vertices{
Vector3<float>::make(-1.0f, -1.0f, 0.0f), Vector3<float>::make(-1.0f, -1.0f, 0.0f),
@ -52,10 +59,10 @@ struct Sprite final {
Vector3<float>::z_axis(), Vector3<float>::z_axis()}; Vector3<float>::z_axis(), Vector3<float>::z_axis()};
std::array<Vector2<float>, 4> texture_coordinates{ std::array<Vector2<float>, 4> texture_coordinates{
Vector2<float>::make(0.0f, 0.0f), /// 0 Vector2<float>::make(0.0f, 0.0f),
Vector2<float>::make(0.0f, 1.0f), /// 1 Vector2<float>::make(1.0f, 0.0f),
Vector2<float>::make(1.0f, 1.0f), /// 2 Vector2<float>::make(1.0f, 1.0f),
Vector2<float>::make(1.0f, 0.0f) /// 3 Vector2<float>::make(0.0f, 1.0f)
}; };
ChangeCountType change_count{}; ChangeCountType change_count{};

View file

@ -22,8 +22,8 @@
*/ */
#include "paradiso/renderer.hpp" #include "paradiso/renderer.hpp"
#include "paradiso/bitmap.hpp" #include "paradiso/bitmap.hpp"
#include "paradiso/sprite.hpp"
#include "paradiso/shader.hpp" #include "paradiso/shader.hpp"
#include "paradiso/sprite.hpp"
#include "glad/glad.h" #include "glad/glad.h"
@ -53,7 +53,6 @@ struct Renderer::impl {
GL_TRUE == glIsVertexArray(vertex_array_obj); GL_TRUE == glIsVertexArray(vertex_array_obj);
} }
bool build(const Sprite& sprite) { bool build(const Sprite& sprite) {
// reset if the Renderer already in use // reset if the Renderer already in use
if (ready()) if (ready())
@ -114,7 +113,6 @@ struct Renderer::impl {
return ready(); return ready();
} }
void texture_bind() { void texture_bind() {
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture_id); glBindTexture(GL_TEXTURE_2D, texture_id);
@ -124,6 +122,9 @@ struct Renderer::impl {
void texture_update(const Bitmap& image) { void texture_update(const Bitmap& image) {
if (GL_FALSE == glIsTexture(texture_id)) { if (GL_FALSE == glIsTexture(texture_id)) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glGenTextures(1, &texture_id); glGenTextures(1, &texture_id);
glActiveTexture(GL_TEXTURE0 + 0); glActiveTexture(GL_TEXTURE0 + 0);
@ -139,21 +140,19 @@ struct Renderer::impl {
// setup new texture // setup new texture
glTexImage2D(GL_TEXTURE_2D, // target glTexImage2D(GL_TEXTURE_2D, // target
0, // level 0, // level
GL_RGB, // internal format GL_RGBA, // internal format
image.size.width, // width image.size.width, // width
image.size.height, // height image.size.height, // height
0, // border 0, // border
GL_RGBA, // format GL_BGRA, // format
GL_UNSIGNED_BYTE, // type GL_UNSIGNED_BYTE, // type
image.data.data() // pointer to data image.data.data() // pointer to data
); );
#if 0
// generate MipMaps // generate MipMaps
glGenerateMipmap(GL_TEXTURE_2D); glGenerateMipmap(GL_TEXTURE_2D);
glGenerateTextureMipmap(_textureId); glGenerateTextureMipmap(texture_id);
#endif
} else { } else {
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
@ -167,18 +166,15 @@ struct Renderer::impl {
0, // y-offset 0, // y-offset
image.size.width, // width image.size.width, // width
image.size.height, // height image.size.height, // height
GL_RGBA, // format GL_BGRA, // format
GL_UNSIGNED_BYTE, // type GL_UNSIGNED_BYTE, // type
image.data.data()); // pointer image.data.data()); // pointer
} }
// glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
} }
void texture_release() void texture_release() { glDeleteTextures(1, &texture_id); }
{
glDeleteTextures(1, &texture_id);
}
void release() { void release() {
@ -189,7 +185,6 @@ struct Renderer::impl {
glDeleteVertexArrays(1, &vertex_array_obj); glDeleteVertexArrays(1, &vertex_array_obj);
texture_release(); texture_release();
} }
void just_draw(const Sprite& sprite) { void just_draw(const Sprite& sprite) {
@ -239,7 +234,7 @@ bool Renderer::draw(const Sprite& sprite, const Shader& shader) {
// Renderer::impl::fetch_errors(__PRETTY_FUNCTION__); // Renderer::impl::fetch_errors(__PRETTY_FUNCTION__);
// impl_->texture_unbind(); impl_->texture_unbind(); // overkill but some cards are finicky
return true; return true;
} }