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

@ -1,18 +1,2 @@
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,
uint8_t alpha) noexcept {
return {.pixel =
(value_type)(((((red << 8) | green) << 8) | blue) << 8) |
alpha};
(value_type)(((((alpha << 8) | blue) << 8) | green) << 8) |
red};
}
constexpr void to_float(float rgba_f[4]) const noexcept {
@ -58,7 +58,10 @@ struct RGBA final {
constexpr uint8_t blue() const noexcept {
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 {
pixel = (pixel & 0x00FFFFFF) | (v << 24);

View file

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

View file

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

View file

@ -1,117 +0,0 @@
/**
* paradiso - Paradigmen der Softwareentwicklung
*
* (c) Copyright 2023 Hartmut Seichter
*
*/
#include <paradiso/bitmap.hpp>
#include <paradiso/context.hpp>
#include <paradiso/geometry.hpp>
#include <paradiso/renderer.hpp>
#include <paradiso/shader.hpp>
#include <paradiso/sprite.hpp>
#include <paradiso/window.hpp>
#include <iomanip>
#include <iostream>
void setup_shaders(paradiso::Shader& shader) {
const auto unlit_v = R"(
#version 330 core
layout (location = 0) in vec3 vertices;
layout (location = 1) in vec3 normals;
layout (location = 2) in vec2 texture_coords;
// uniform mat4 model;
// uniform mat4 view;
// uniform mat4 projection;
out vec2 tex_c;
void main() {
tex_c = texture_coords;
// gl_Position = projection * view * model * vec4(vertices, 1.0);
gl_Position = vec4(vertices, 1.0);
}
)";
const auto unlit_f = R"(
#version 330 core
uniform vec4 color = vec4(1.0, 0.0, 0.0, 1.0);
uniform sampler2D tex_color;
in vec2 tex_c;
out vec4 frag_color;
void main() {
frag_color = texture(tex_color,tex_c); // * color;
})";
shader.set_source(paradiso::Shader::Type::Vertex, unlit_v);
shader.set_source(paradiso::Shader::Type::Fragment, unlit_f);
shader.build();
}
auto main() -> int {
auto checker_board = paradiso::Bitmap::empty(paradiso::Size{4, 4});
auto checker_board_2 = paradiso::Bitmap::from_data(
paradiso::Size{2, 2}, paradiso::RGBA::from_rgb(0x00, 0xFF, 0x00),
paradiso::RGBA::from_rgb(0x00, 0xFF, 0x00),
paradiso::RGBA::from_rgb(0x00, 0xFF, 0x00),
paradiso::RGBA::from_rgb(0x00, 0xFF, 0x00));
auto window = paradiso::Window();
window.set_size(paradiso::Size{.width = 1280, .height = 720})
.set_position(paradiso::Point{.x = 100, .y = 100})
.set_title("PardiSO")
.set_visible(true);
auto ctx = paradiso::Context{};
auto sprite = paradiso::Sprite{
.bitmap = checker_board_2
};
auto renderer = paradiso::Renderer{};
auto shader = paradiso::Shader{};
setup_shaders(shader);
uint8_t green_slider = 0x00;
bool want_close{false};
int frame_counter = 10;
window.set_keyboardcallback(
[&](auto& w, int key, int scancode, int action, int mods) {
if (key == 'Q' || key == 256)
want_close = true;
else if (key == 'A' ) {
green_slider += 10;
}
});
while (window.update([&](auto& w) -> bool {
ctx.set_clearcolor(paradiso::RGBA::from_rgb(0xFF, green_slider, 0x00));
// ctx.set_viewport(paradiso::Rectangle{
// .size = w.client_size()
// });
ctx.clear();
renderer.draw(sprite, shader);
return !want_close;
})) {
};
return 0;
}