vertex and index declarations

This commit is contained in:
2018-10-06 01:33:55 +07:00
parent 2c9a5fd327
commit 278e80f682
8 changed files with 1083 additions and 334 deletions

View File

@@ -10,69 +10,88 @@ 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"
"#version 120 \n"
" \n"
"attribute vec3 a_position; \n"
"attribute vec4 a_color; \n"
" \n"
"uniform float u_time = 0.0; \n"
" \n"
"varying vec4 color; \n"
" \n"
"void main(){ \n"
" color = a_color; \n"
" \n"
" float s = 0.7 + 0.3 * (cos(u_time * 0.003) + 1); \n"
" gl_Position = vec4(a_position * s, 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"
"#version 120 \n"
" \n"
"varying vec4 color; \n"
" \n"
"void main(){ \n"
" gl_FragColor = color; \n"
"}";
u16 indices[] = {
struct vertex {
v3f position;
color32 color;
};
u8 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}}};
{{-0.5f, 0.5f, 0.0f}, color32::red()},
{{-0.5f, -0.5f, 0.0f}, color32::green()},
{{ 0.5f, 0.5f, 0.0f}, color32::blue()},
{{ 0.5f, -0.5f, 0.0f}, color32::white()}};
}
int e2d_main() {
modules::initialize<debug>();
modules::initialize<debug>()
.register_sink<debug_console_sink>();
modules::initialize<input>();
modules::initialize<window>(v2u{640, 480}, "Enduro2D", false);
modules::initialize<window>(v2u{640, 480}, "Enduro2D", false)
.register_event_listener<window_input_source>(the<input>());
modules::initialize<render>(the<debug>(), the<window>());
the<debug>().register_sink<debug_console_sink>();
the<window>().register_event_listener<window_input_source>(the<input>());
auto index_decl = index_declaration(
index_declaration::index_type::unsigned_byte);
auto vertex_decl = vertex_declaration()
.add_attribute<v3f>("a_position")
.add_attribute<color32>("a_color").normalized();
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))));
make_memory_stream(buffer(fs_source_cstr, std::strlen(fs_source_cstr))),
vertex_decl);
const auto ib = the<render>().create_index_buffer(
indices, E2D_COUNTOF(indices), index_buffer::usage::static_draw);
buffer(indices, E2D_COUNTOF(indices) * sizeof(indices[0])),
index_decl,
index_buffer::usage::static_draw);
const auto vb = the<render>().create_vertex_buffer(
vertices, E2D_COUNTOF(vertices), vertex_buffer::usage::static_draw);
buffer(vertices, E2D_COUNTOF(vertices) * sizeof(vertices[0])),
vertex_decl,
vertex_buffer::usage::static_draw);
const auto begin_time = time::now_ms();
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>().set_clear_color({1.f, 0.4f, 0.f, 1.f});
the<render>().clear(true, true, true);
the<render>().draw(ps, ib, vb);
f32 t = (time::now_ms() - begin_time).cast_to<f32>().value;
ps->set_uniform("u_time", t);
{
the<render>().draw(render::topology::triangles, ps, ib, vb);
}
the<window>().swap_buffers(true);
the<input>().frame_tick();
window::poll_events();