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);
|
static uint32_t bytes_per_pixel(pixel_layout t);
|
||||||
|
|
||||||
|
typedef shared_ptr<image> ptr;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
sizei _size;
|
sizei _size;
|
||||||
|
|
|
@ -3,6 +3,7 @@ set(hdrs
|
||||||
include/pw/visual/mesh_renderer.hpp
|
include/pw/visual/mesh_renderer.hpp
|
||||||
include/pw/visual/shader.hpp
|
include/pw/visual/shader.hpp
|
||||||
include/pw/visual/pipeline.hpp
|
include/pw/visual/pipeline.hpp
|
||||||
|
include/pw/visual/texture.hpp
|
||||||
)
|
)
|
||||||
|
|
||||||
set(srcs
|
set(srcs
|
||||||
|
@ -10,6 +11,7 @@ set(srcs
|
||||||
src/shader.cpp
|
src/shader.cpp
|
||||||
src/context.cpp
|
src/context.cpp
|
||||||
src/pipeline.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/size.hpp"
|
||||||
#include "pw/core/matrix.hpp"
|
#include "pw/core/matrix.hpp"
|
||||||
#include "pw/core/mesh.hpp"
|
#include "pw/core/mesh.hpp"
|
||||||
|
#include "pw/core/timer.hpp"
|
||||||
|
|
||||||
|
|
||||||
#include "pw/core/debug.hpp"
|
#include "pw/core/debug.hpp"
|
||||||
|
@ -13,6 +14,10 @@
|
||||||
|
|
||||||
namespace pw {
|
namespace pw {
|
||||||
|
|
||||||
|
class queue {
|
||||||
|
|
||||||
|
// vector<commands> ...
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
struct triangle_renderer
|
struct triangle_renderer
|
||||||
|
@ -24,6 +29,7 @@ struct triangle_renderer
|
||||||
shader shader_p;
|
shader shader_p;
|
||||||
mesh amesh;
|
mesh amesh;
|
||||||
mesh_renderer amesh_renderer;
|
mesh_renderer amesh_renderer;
|
||||||
|
timer::tick_t tick;
|
||||||
|
|
||||||
triangle_renderer()
|
triangle_renderer()
|
||||||
{
|
{
|
||||||
|
@ -32,11 +38,6 @@ struct triangle_renderer
|
||||||
|
|
||||||
void setup()
|
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 = {
|
mesh::vertex3array_t vertices = {
|
||||||
|
@ -50,14 +51,13 @@ struct triangle_renderer
|
||||||
amesh.set_indices(indices);
|
amesh.set_indices(indices);
|
||||||
amesh.set_vertices(vertices);
|
amesh.set_vertices(vertices);
|
||||||
|
|
||||||
|
|
||||||
amesh_renderer.create(amesh);
|
amesh_renderer.create(amesh);
|
||||||
|
#if 0
|
||||||
|
|
||||||
|
|
||||||
size_t vertex_size_bytes = amesh.vertices().size() * sizeof(mesh::vertex3array_t::value_type);
|
size_t vertex_size_bytes = amesh.vertices().size() * sizeof(mesh::vertex3array_t::value_type);
|
||||||
size_t vertex_stride = amesh.vertices().front().size();
|
size_t vertex_stride = amesh.vertices().front().size();
|
||||||
|
|
||||||
|
|
||||||
debug::d() << 9 * sizeof(mesh::vertex3array_t::value_type::value_type) << " "
|
debug::d() << 9 * sizeof(mesh::vertex3array_t::value_type::value_type) << " "
|
||||||
<< vertex_size_bytes;
|
<< vertex_size_bytes;
|
||||||
|
|
||||||
|
@ -77,6 +77,21 @@ struct triangle_renderer
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||||
glVertexAttribPointer(0, vertex_stride, GL_FLOAT, GL_FALSE, 0, nullptr);
|
glVertexAttribPointer(0, vertex_stride, GL_FLOAT, GL_FALSE, 0, nullptr);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
|
||||||
|
glShaderSource(vs, 1, &vertex_shader, NULL);
|
||||||
|
glCompileShader(vs);
|
||||||
|
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
|
||||||
|
glShaderSource(fs, 1, &fragment_shader, NULL);
|
||||||
|
glCompileShader(fs);
|
||||||
|
|
||||||
|
shader_programme = glCreateProgram();
|
||||||
|
glAttachShader(shader_programme, fs);
|
||||||
|
glAttachShader(shader_programme, vs);
|
||||||
|
glLinkProgram(shader_programme);
|
||||||
|
#endif
|
||||||
|
|
||||||
const char* vertex_shader = R"(
|
const char* vertex_shader = R"(
|
||||||
#version 400
|
#version 400
|
||||||
in vec3 vp;
|
in vec3 vp;
|
||||||
|
@ -93,21 +108,6 @@ struct triangle_renderer
|
||||||
frag_colour = input_color;
|
frag_colour = input_color;
|
||||||
})";
|
})";
|
||||||
|
|
||||||
#if 0
|
|
||||||
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
|
|
||||||
glShaderSource(vs, 1, &vertex_shader, NULL);
|
|
||||||
glCompileShader(vs);
|
|
||||||
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
|
|
||||||
glShaderSource(fs, 1, &fragment_shader, NULL);
|
|
||||||
glCompileShader(fs);
|
|
||||||
|
|
||||||
shader_programme = glCreateProgram();
|
|
||||||
glAttachShader(shader_programme, fs);
|
|
||||||
glAttachShader(shader_programme, vs);
|
|
||||||
glLinkProgram(shader_programme);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
shader_p.set_source(fragment_shader,shader::fragment);
|
shader_p.set_source(fragment_shader,shader::fragment);
|
||||||
shader_p.set_source(vertex_shader,shader::vertex);
|
shader_p.set_source(vertex_shader,shader::vertex);
|
||||||
|
|
||||||
|
@ -119,8 +119,9 @@ struct triangle_renderer
|
||||||
|
|
||||||
void draw()
|
void draw()
|
||||||
{
|
{
|
||||||
shader_p.use();
|
double t0 = timer::now();
|
||||||
|
|
||||||
|
shader_p.use();
|
||||||
|
|
||||||
static float v = 0.0f;
|
static float v = 0.0f;
|
||||||
v+= 0.01f;
|
v+= 0.01f;
|
||||||
|
@ -131,6 +132,8 @@ struct triangle_renderer
|
||||||
|
|
||||||
amesh_renderer.draw();
|
amesh_renderer.draw();
|
||||||
|
|
||||||
|
debug::d() << 100 * (timer::now() - t0) << "ms";
|
||||||
|
|
||||||
// glBindVertexArray(vao);
|
// glBindVertexArray(vao);
|
||||||
// draw points 0-3 from the currently bound VAO with current in-use shader
|
// draw points 0-3 from the currently bound VAO with current in-use shader
|
||||||
// glDrawArrays(GL_TRIANGLES, 0, 3);
|
// 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