diff --git a/share/assets/textures/checkerboard-512-32.png b/share/assets/textures/checkerboard-512-32.png new file mode 100644 index 0000000..a0762fe Binary files /dev/null and b/share/assets/textures/checkerboard-512-32.png differ diff --git a/src/core/include/pw/core/image.hpp b/src/core/include/pw/core/image.hpp index 4c50ea6..cc4992b 100644 --- a/src/core/include/pw/core/image.hpp +++ b/src/core/include/pw/core/image.hpp @@ -56,6 +56,8 @@ public: static uint32_t bytes_per_pixel(pixel_layout t); + typedef shared_ptr ptr; + protected: sizei _size; diff --git a/src/visual/CMakeLists.txt b/src/visual/CMakeLists.txt index 8a5d623..bd8f4c5 100644 --- a/src/visual/CMakeLists.txt +++ b/src/visual/CMakeLists.txt @@ -3,6 +3,7 @@ set(hdrs include/pw/visual/mesh_renderer.hpp include/pw/visual/shader.hpp include/pw/visual/pipeline.hpp + include/pw/visual/texture.hpp ) set(srcs @@ -10,6 +11,7 @@ set(srcs src/shader.cpp src/context.cpp src/pipeline.cpp + src/texture.cpp ) diff --git a/src/visual/include/pw/visual/texture.hpp b/src/visual/include/pw/visual/texture.hpp new file mode 100644 index 0000000..9df412c --- /dev/null +++ b/src/visual/include/pw/visual/texture.hpp @@ -0,0 +1,64 @@ +#ifndef PW_VISUAL_TEXTURE_HPP +#define PW_VISUAL_TEXTURE_HPP + +#include +#include +#include + +namespace pw { + +class texture { + + enum texture_type { + color, + normal + }; + + enum texture_shape { + shape_2d, + shape_3d + }; + + enum wrap_mode { + wrap_repeat, + wrap_clamp, + wrap_clamp_to_edge, + wrap_clamp_to_repeat + }; + + + texture(); + + texture(shared_ptr i,texture_shape s,texture_type = color); + + + void set_image(shared_ptr i); + shared_ptr get() const { return _image; } + + void set_type(texture_type t); + texture_type type() const { return _type; } + + void set_shape(texture_shape s); + texture_shape shape() const { return _shape; } + + void set_wrap(wrap_mode w); + wrap_mode wrap() const { return _wrap; } + +protected: + + shared_ptr _image; + + texture_type _type; + texture_shape _shape; + wrap_mode _wrap; + + struct impl; + unique_ptr _impl; + + +}; + +} + + +#endif diff --git a/src/visual/src/pipeline.cpp b/src/visual/src/pipeline.cpp index aaa58f8..b91d92f 100644 --- a/src/visual/src/pipeline.cpp +++ b/src/visual/src/pipeline.cpp @@ -2,6 +2,7 @@ #include "pw/core/size.hpp" #include "pw/core/matrix.hpp" #include "pw/core/mesh.hpp" +#include "pw/core/timer.hpp" #include "pw/core/debug.hpp" @@ -13,6 +14,10 @@ namespace pw { +class queue { + +// vector ... +}; struct triangle_renderer @@ -24,6 +29,7 @@ struct triangle_renderer shader shader_p; mesh amesh; mesh_renderer amesh_renderer; + timer::tick_t tick; triangle_renderer() { @@ -32,11 +38,6 @@ struct triangle_renderer void setup() { -// float points[] = { -// 0.0f, 0.5f, 0.0f, -// 0.5f, -0.5f, 0.0f, -// -0.5f, -0.5f, 0.0f -// }; mesh::vertex3array_t vertices = { @@ -50,14 +51,13 @@ struct triangle_renderer amesh.set_indices(indices); amesh.set_vertices(vertices); - amesh_renderer.create(amesh); +#if 0 size_t vertex_size_bytes = amesh.vertices().size() * sizeof(mesh::vertex3array_t::value_type); size_t vertex_stride = amesh.vertices().front().size(); - debug::d() << 9 * sizeof(mesh::vertex3array_t::value_type::value_type) << " " << vertex_size_bytes; @@ -77,23 +77,8 @@ struct triangle_renderer glBindBuffer(GL_ARRAY_BUFFER, vbo); glVertexAttribPointer(0, vertex_stride, GL_FLOAT, GL_FALSE, 0, nullptr); - const char* vertex_shader = R"( - #version 400 - in vec3 vp; - void main() { - gl_Position = vec4(vp, 1.0); - } - )"; - const char *fragment_shader = R"( - #version 400 - uniform vec4 input_color = vec4(1.0, 0.0, 0.0, 1.0); - out vec4 frag_colour; - void main() { - frag_colour = input_color; - })"; -#if 0 GLuint vs = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vs, 1, &vertex_shader, NULL); glCompileShader(vs); @@ -107,6 +92,21 @@ struct triangle_renderer glLinkProgram(shader_programme); #endif + const char* vertex_shader = R"( + #version 400 + in vec3 vp; + void main() { + gl_Position = vec4(vp, 1.0); + } + )"; + + const char *fragment_shader = R"( + #version 400 + uniform vec4 input_color = vec4(1.0, 0.0, 0.0, 1.0); + out vec4 frag_colour; + void main() { + frag_colour = input_color; + })"; shader_p.set_source(fragment_shader,shader::fragment); shader_p.set_source(vertex_shader,shader::vertex); @@ -119,8 +119,9 @@ struct triangle_renderer void draw() { - shader_p.use(); + double t0 = timer::now(); + shader_p.use(); static float v = 0.0f; v+= 0.01f; @@ -131,6 +132,8 @@ struct triangle_renderer amesh_renderer.draw(); + debug::d() << 100 * (timer::now() - t0) << "ms"; + // glBindVertexArray(vao); // draw points 0-3 from the currently bound VAO with current in-use shader // glDrawArrays(GL_TRIANGLES, 0, 3); diff --git a/src/visual/src/texture.cpp b/src/visual/src/texture.cpp new file mode 100644 index 0000000..c562559 --- /dev/null +++ b/src/visual/src/texture.cpp @@ -0,0 +1,96 @@ +#include "pw/visual/texture.hpp" + +#include + +namespace pw { + + +struct texture::impl { + + texture &_host; + + GLuint _texture_id; + GLuint _texture_sampler; + + impl(texture& host) + : _host(host) + { + } + + GLuint gl_shape() { + switch (_host.shape()) { + case pw::texture::shape_2d: + return GL_TEXTURE_2D; + case pw::texture::shape_3d: + return GL_TEXTURE_3D; + } + } + + void create() + { + glGenTextures(1, &_texture_id); + glBindTexture(gl_shape(), _texture_id); + +// glTexImage1D(gl_shape(), 0, GL_R8, cosAngleResolution, 0, +// GL_RED, GL_UNSIGNED_BYTE, &textureData[0]); +// glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_BASE_LEVEL, 0); +// glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAX_LEVEL, 0); +// glBindTexture(GL_TEXTURE_1D, 0); + + glGenSamplers(1, &_texture_sampler); + glSamplerParameteri(_texture_sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glSamplerParameteri(_texture_sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + + + glSamplerParameteri(_texture_sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + + glBindTexture(gl_shape(),0); + } + + void destroy() + { + + } +}; + + + +// +// +// + +texture::texture() +{ + _impl = make_unique(*this); +} + +texture::texture(shared_ptr i, texture::texture_shape s, texture::texture_type t) +{ + texture(); + set_image(i); + set_shape(s); + set_type(t); +} + +void texture::set_image(shared_ptr i) +{ + _image = i; +} + +void texture::set_type(texture::texture_type t) +{ + _type = t; +} + +void texture::set_shape(texture_shape s) +{ + _shape = s; +} + +void texture::set_wrap(wrap_mode w) +{ + _wrap = w; +} + + +}