now lets get texturing working
This commit is contained in:
parent
d9bbef876e
commit
62281699ea
6 changed files with 190 additions and 23 deletions
BIN
share/assets/textures/checkerboard-512-32.png
Normal file
BIN
share/assets/textures/checkerboard-512-32.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1,016 B |
|
@ -56,6 +56,8 @@ public:
|
|||
|
||||
static uint32_t bytes_per_pixel(pixel_layout t);
|
||||
|
||||
typedef shared_ptr<image> ptr;
|
||||
|
||||
protected:
|
||||
|
||||
sizei _size;
|
||||
|
|
|
@ -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
|
||||
)
|
||||
|
||||
|
||||
|
|
64
src/visual/include/pw/visual/texture.hpp
Normal file
64
src/visual/include/pw/visual/texture.hpp
Normal file
|
@ -0,0 +1,64 @@
|
|||
#ifndef PW_VISUAL_TEXTURE_HPP
|
||||
#define PW_VISUAL_TEXTURE_HPP
|
||||
|
||||
#include <pw/core/globals.hpp>
|
||||
#include <pw/core/image.hpp>
|
||||
#include <pw/core/debug.hpp>
|
||||
|
||||
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<image> i,texture_shape s,texture_type = color);
|
||||
|
||||
|
||||
void set_image(shared_ptr<image> i);
|
||||
shared_ptr<image> 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> _image;
|
||||
|
||||
texture_type _type;
|
||||
texture_shape _shape;
|
||||
wrap_mode _wrap;
|
||||
|
||||
struct impl;
|
||||
unique_ptr<impl> _impl;
|
||||
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
|
@ -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<commands> ...
|
||||
};
|
||||
|
||||
|
||||
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);
|
||||
|
|
96
src/visual/src/texture.cpp
Normal file
96
src/visual/src/texture.cpp
Normal file
|
@ -0,0 +1,96 @@
|
|||
#include "pw/visual/texture.hpp"
|
||||
|
||||
#include <glad/glad.h>
|
||||
|
||||
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<impl>(*this);
|
||||
}
|
||||
|
||||
texture::texture(shared_ptr<image> 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<image> 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue