plenty of additions to rendering. transformation now work almost as expected ...

This commit is contained in:
Hartmut Seichter 2019-02-12 21:28:20 +01:00
parent 841b0eeb46
commit 40b84fb78f
18 changed files with 242 additions and 251 deletions

View file

@ -30,20 +30,20 @@ public:
bool ready() const;
shader& bind(int location,float v);
shader& bind(int location,matrix4x4f const & v);
shader& bind(int location,vector4f const & v);
shader& set(int location,float v);
shader& set(int location,matrix4x4f const & v);
shader& set(int location,vector4f const & v);
int uniform_location(std::string const & name) const;
template<typename T>
shader & bind(std::string const & name, T&& value)
shader & set(std::string const & name, T&& value)
{
int location = uniform_location(name);
if (location >= 0)
return bind(location, std::forward<T>(value));
return set(location, std::forward<T>(value));
else
debug::e() << "missing uniform: "<< name;
debug::e() << "missing uniform: '" << name << "'";
return *this;
}

View file

@ -3,6 +3,8 @@
#include "pw/core/mesh.hpp"
#include "pw/core/timer.hpp"
#include "pw/core/axisangle.hpp"
#include "pw/core/serialize.hpp"
#include "pw/core/transform_tools.hpp"
#include "pw/core/debug.hpp"
#include "pw/visual/pipeline.hpp"
@ -17,6 +19,8 @@ class command {
};
class mesh_command : command {
// shader
@ -46,10 +50,13 @@ struct triangle_renderer
void setup()
{
const float z_val = -5.f;
mesh::vertex3array_t vertices = {
{ 0.0f, 0.5f, 0.0f} // 0
,{ 0.5f, -0.5f, 0.0f} // 1
,{-0.5f, -0.5f, 0.0f} // 2
{ 0.0f, 0.5f, z_val} // 0
,{ 0.5f, -0.5f, z_val} // 1
,{-0.5f, -0.5f, z_val} // 2
};
// actual indices
@ -59,71 +66,17 @@ struct triangle_renderer
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;
// sizeof(mesh::vertex3array_t::value_type)
// 9 * sizeof(mesh::vertex3array_t::value_type::value_type)
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vertex_size_bytes , amesh.vertices().data(), GL_STATIC_DRAW);
glGenVertexArrays(1,&vao);
glBindVertexArray(vao);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
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"(
#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;
})";
const char* vertex_shader_2 = R"(
#version 400
uniform mat4 model_mat;
uniform mat4 view_mat = mat4
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
in vec3 vertex_p;
void main() {
gl_Position = model_mat * vec4(vertex_p, 1.0);
gl_Position = projection * view * model * vec4(vertex_p, 1.0);
}
)";
@ -136,7 +89,7 @@ struct triangle_renderer
})";
shader_p.set_source(vertex_shader_2,shader::vertex);
shader_p.set_source(fragment_shader,shader::fragment);
shader_p.set_source(fragment_shader_2,shader::fragment);
if (!shader_p.build())
exit(-1);
@ -148,19 +101,34 @@ struct triangle_renderer
shader_p.use();
static float v = 0.0f;
v+= 0.01f;
if (v>1.0f) v = 0.0f;
vector4f test({0.5f,1-v,v,1.0f});
auto v_col = ping_pong(static_cast<float>(timer::now()),1.0f);
vector4f col({0.5f,1-v_col,v_col,1.0f});
matrix4x4f model_mat; model_mat.set_identity();
axisangle rot(vector3::right(),v);
auto v_angle = ping_pong(static_cast<float>(timer::now()),::pw::pi<float>());
axisangle rot(vector3::forward(),v_angle);
model_mat = rot.to_matrix();
matrix4x4f view_mat = transform_tools<float>::look_at(vector3({0,0,0}),
vector3::forward(),
vector3::up());
matrix4x4f proj_mat = transform_tools<float>::perspective_projection(deg_to_rad(45.f),
1.3f,
0.2f,1000.f);
// debug::d() << serialize::matrix(proj_mat);
// highly inefficient - should be cached -
shader_p.bind("input_color",test);
shader_p.bind("model_mat",model_mat);
shader_p.set("input_color",col);
shader_p.set("model",model_mat);
shader_p.set("view",view_mat);
shader_p.set("projection",proj_mat);
amesh_renderer.draw();

View file

@ -180,12 +180,12 @@ bool shader::ready() const
return _impl->is_valid();
}
shader &shader::bind(int location, const vector4f &v)
shader &shader::set(int location, const vector4f &v)
{
_impl->bind(location,v); return *this;
}
shader &shader::bind(int location, const matrix4x4f &v)
shader &shader::set(int location, const matrix4x4f &v)
{
_impl->bind(location,v); return *this;
}