GL_CHECK_CODE. index and vertex buffers. hardcoded vertex wip.

This commit is contained in:
2018-10-03 20:27:46 +07:00
parent c3c71e06f0
commit 3038c6bb0e
5 changed files with 774 additions and 162 deletions

View File

@@ -16,8 +16,11 @@ namespace e2d
class mouse;
class keyboard;
class input;
class vertex;
class shader;
class texture;
class index_buffer;
class vertex_buffer;
class render;
class vfs;
class window;

View File

@@ -10,6 +10,17 @@
namespace e2d
{
//
// vertex
//
class vertex {
public:
v3f pos;
v2f uv[2];
color32 color;
};
//
// shader
//
@@ -82,14 +93,61 @@ namespace e2d
void set_wrap(wrap u, wrap v) noexcept;
void set_filter(filter min, filter mag) noexcept;
const v2u& native_size() const noexcept;
const v2u& original_size() const noexcept;
private:
internal_state_uptr state_;
};
using texture_ptr = std::shared_ptr<texture>;
//
// index buffer
//
class index_buffer final : private noncopyable {
private:
friend class render;
class internal_state;
using internal_state_uptr = std::unique_ptr<internal_state>;
public:
enum class usage {
static_draw,
stream_draw,
dynamic_draw
};
public:
index_buffer(internal_state_uptr);
~index_buffer() noexcept;
void update(const u16* indices, std::size_t count, std::size_t offset) noexcept;
std::size_t count() const noexcept;
private:
internal_state_uptr state_;
};
using index_buffer_ptr = std::shared_ptr<index_buffer>;
//
// vertex buffer
//
class vertex_buffer final : private noncopyable {
private:
friend class render;
class internal_state;
using internal_state_uptr = std::unique_ptr<internal_state>;
public:
enum class usage {
static_draw,
stream_draw,
dynamic_draw
};
public:
vertex_buffer(internal_state_uptr);
~vertex_buffer() noexcept;
void update(const vertex* vertices, std::size_t count, std::size_t offset) noexcept;
std::size_t count() const noexcept;
private:
internal_state_uptr state_;
};
using vertex_buffer_ptr = std::shared_ptr<vertex_buffer>;
//
// render
//
@@ -160,18 +218,37 @@ namespace e2d
invert
};
public:
render(debug& debug);
render(debug& d, window& w);
~render() noexcept;
shader_ptr create_shader(
input_stream_uptr vertex,
input_stream_uptr fragment);
texture_ptr create_texture(const image& image);
texture_ptr create_texture(const v2u& size, image_data_format format);
texture_ptr create_texture(
const image& image);
texture_ptr create_texture(
const v2u& size,
image_data_format format);
index_buffer_ptr create_index_buffer(
const u16* indices,
std::size_t count,
index_buffer::usage usage);
vertex_buffer_ptr create_vertex_buffer(
const vertex* vertices,
std::size_t count,
vertex_buffer::usage usage);
void clear(bool color, bool depth, bool stencil) noexcept;
void draw(
const shader_ptr& ps,
const index_buffer_ptr& ib,
const vertex_buffer_ptr& vb) noexcept;
void set_view(const m4f& view) noexcept;
void set_projection(const m4f& projection) noexcept;
void set_viewport(u32 x, u32 y, u32 w, u32 h) noexcept;
@@ -197,7 +274,6 @@ namespace e2d
void set_clear_color(const color& color) noexcept;
void set_color_mask(bool r, bool g, bool b, bool a);
private:
debug& debug_;
class internal_state;
std::unique_ptr<internal_state> state_;
};

View File

@@ -7,19 +7,72 @@
#include "../common.hpp"
using namespace e2d;
namespace
{
const char* vs_source_cstr =
"#version 120 \n"
" \n"
"attribute vec3 in_pos; \n"
"attribute vec2 in_uv0; \n"
"attribute vec2 in_uv1; \n"
"attribute vec4 in_color; \n"
" \n"
"varying vec2 uv0; \n"
"varying vec2 uv1; \n"
"varying vec4 color; \n"
" \n"
"void main(){ \n"
" uv0 = in_uv0; \n"
" uv1 = in_uv1; \n"
" color = in_color; \n"
" gl_Position = vec4(in_pos, 1.0); \n"
"}";
const char* fs_source_cstr =
"#version 120 \n"
" \n"
"varying vec2 uv0; \n"
"varying vec2 uv1; \n"
"varying vec4 color; \n"
" \n"
"void main(){ \n"
" gl_FragColor = color; \n"
"}";
u16 indices[] = {
0, 1, 2, 2, 1, 3};
const vertex vertices[] = {
{{-0.5f, 0.5f, 0.0f}, {{0.f, 0.f}, {0.f, 0.f}}, {0xFF, 0x00, 0x00, 0xFF}},
{{-0.5f, -0.5f, 0.0f}, {{0.f, 0.f}, {0.f, 0.f}}, {0x00, 0xFF, 0x00, 0xFF}},
{{ 0.5f, 0.5f, 0.0f}, {{0.f, 0.f}, {0.f, 0.f}}, {0x00, 0x00, 0xFF, 0xFF}},
{{ 0.5f, -0.5f, 0.0f}, {{0.f, 0.f}, {0.f, 0.f}}, {0xFF, 0xFF, 0xFF, 0xFF}}};
}
int e2d_main() {
modules::initialize<debug>();
modules::initialize<input>();
modules::initialize<render>(the<debug>());
modules::initialize<window>(v2u{640, 480}, "Enduro2D", false);
modules::initialize<render>(the<debug>(), the<window>());
the<debug>().register_sink<debug_console_sink>();
the<window>().register_event_listener<window_input_source>(the<input>());
const auto ps = the<render>().create_shader(
make_memory_stream(buffer(vs_source_cstr, std::strlen(vs_source_cstr))),
make_memory_stream(buffer(fs_source_cstr, std::strlen(fs_source_cstr))));
const auto ib = the<render>().create_index_buffer(
indices, E2D_COUNTOF(indices), index_buffer::usage::static_draw);
const auto vb = the<render>().create_vertex_buffer(
vertices, E2D_COUNTOF(vertices), vertex_buffer::usage::static_draw);
const keyboard& k = the<input>().keyboard();
while ( !the<window>().should_close() && !k.is_key_just_released(keyboard_key::escape) ) {
the<render>().set_clear_color({1.f, 0.4f, 0.f});
the<render>().clear(true, true, true);
the<render>().draw(ps, ib, vb);
the<window>().swap_buffers(true);
the<input>().frame_tick();
window::poll_events();

View File

@@ -7,6 +7,7 @@
#pragma once
#include <enduro2d/core/debug.hpp>
#include <enduro2d/core/window.hpp>
#include <enduro2d/core/render.hpp>
#define E2D_RENDER_MODE_NONE 1

File diff suppressed because it is too large Load Diff