pudding attributes moved into model class
This commit is contained in:
parent
67a7f9c3f2
commit
5374b7f76a
2
lib/sb
2
lib/sb
|
@ -1 +1 @@
|
||||||
Subproject commit 70bc054c7f763d1ec14c696d9c66bddefe0126bc
|
Subproject commit 950a95502bb178619376121ac9781ad1f2fe151e
|
|
@ -36,6 +36,7 @@ class Item : public Node
|
||||||
private:
|
private:
|
||||||
|
|
||||||
nlohmann::json json = {};
|
nlohmann::json json = {};
|
||||||
|
// Plane image;
|
||||||
std::vector<sb::Texture> image_textures;
|
std::vector<sb::Texture> image_textures;
|
||||||
std::string brand_name = "", product_name = "", upc = "";
|
std::string brand_name = "", product_name = "", upc = "";
|
||||||
int current_image_index = 0;
|
int current_image_index = 0;
|
||||||
|
|
|
@ -33,21 +33,45 @@ Model::Model(const std::map<std::string, sb::Attributes>& attributes_pack)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Construct a new model object by passing a list of names which will be used to initialize
|
||||||
|
* empty attributes objects with the given names */
|
||||||
|
Model::Model(const std::initializer_list<std::string>& names)
|
||||||
|
{
|
||||||
|
for (const std::string& name : names)
|
||||||
|
{
|
||||||
|
this->attributes(name, sb::Attributes());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Get the entire map of attributes, each wrapped in its shared pointer held by this object.
|
/* Get the entire map of attributes, each wrapped in its shared pointer held by this object.
|
||||||
* Can be used to iterate through the attributes. */
|
* Can be used to iterate through the attributes. */
|
||||||
std::map<std::string, std::shared_ptr<sb::Attributes>> Model::attributes()
|
std::map<std::string, std::shared_ptr<sb::Attributes>>& Model::attributes()
|
||||||
{
|
{
|
||||||
return model_attributes;
|
return model_attributes;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Get the attributes under name, wrapped in the shared pointer held by this object. Use
|
/* Get the attributes under name, wrapped in the shared pointer held by this object. This
|
||||||
* this to share ownership of the attributes or to gain access to the public interface
|
* function uses the at method of std::map, so name must refer to attributes already
|
||||||
* of the attributes. */
|
* stored in this model. Use this function to share ownership of the attributes or to gain
|
||||||
std::shared_ptr<sb::Attributes> Model::attributes(const std::string& name)
|
* access to the public interface of the attributes. */
|
||||||
|
std::shared_ptr<sb::Attributes>& Model::attributes(const std::string& name)
|
||||||
{
|
{
|
||||||
return model_attributes.at(name);
|
return model_attributes.at(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Get the attributes under name, wrapped in the shared pointer held by this object. This
|
||||||
|
* function uses operator[] or std::map, so this can be used to add new attributes to the
|
||||||
|
* object if they are wrapped in a shared pointer. */
|
||||||
|
std::shared_ptr<sb::Attributes>& Model::operator[](const std::string& name)
|
||||||
|
{
|
||||||
|
auto element = model_attributes.find(name);
|
||||||
|
if (element == model_attributes.end())
|
||||||
|
{
|
||||||
|
attributes(name, sb::Attributes{});
|
||||||
|
}
|
||||||
|
return model_attributes[name];
|
||||||
|
}
|
||||||
|
|
||||||
/* Assign name to attributes, copy and wrap in a shared pointer. The model can share
|
/* Assign name to attributes, copy and wrap in a shared pointer. The model can share
|
||||||
* ownership of the created attribute memory with callers that request it. */
|
* ownership of the created attribute memory with callers that request it. */
|
||||||
void Model::attributes(const std::string& name, const sb::Attributes& attributes)
|
void Model::attributes(const std::string& name, const sb::Attributes& attributes)
|
||||||
|
@ -81,7 +105,7 @@ void Model::disable()
|
||||||
|
|
||||||
/* Get the texture at name. This can be used to read the texture memory, share ownership of it, or
|
/* Get the texture at name. This can be used to read the texture memory, share ownership of it, or
|
||||||
* anything else a Texture object can be used for with direct calls to GL functions. */
|
* anything else a Texture object can be used for with direct calls to GL functions. */
|
||||||
sb::Texture Model::texture(const std::string& name)
|
sb::Texture& Model::texture(const std::string& name)
|
||||||
{
|
{
|
||||||
return model_texture.at(name);
|
return model_texture.at(name);
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,13 +39,15 @@ public:
|
||||||
Model();
|
Model();
|
||||||
Model(const std::map<std::string, std::shared_ptr<sb::Attributes>>&);
|
Model(const std::map<std::string, std::shared_ptr<sb::Attributes>>&);
|
||||||
Model(const std::map<std::string, sb::Attributes>&);
|
Model(const std::map<std::string, sb::Attributes>&);
|
||||||
std::map<std::string, std::shared_ptr<sb::Attributes>> attributes();
|
Model(const std::initializer_list<std::string>&);
|
||||||
std::shared_ptr<sb::Attributes> attributes(const std::string&);
|
std::map<std::string, std::shared_ptr<sb::Attributes>>& attributes();
|
||||||
|
std::shared_ptr<sb::Attributes>& attributes(const std::string&);
|
||||||
void attributes(const std::string&, const sb::Attributes&);
|
void attributes(const std::string&, const sb::Attributes&);
|
||||||
void attributes(const std::string&, const std::shared_ptr<sb::Attributes>&);
|
void attributes(const std::string&, const std::shared_ptr<sb::Attributes>&);
|
||||||
|
std::shared_ptr<sb::Attributes>& operator[](const std::string&);
|
||||||
void enable();
|
void enable();
|
||||||
void disable();
|
void disable();
|
||||||
sb::Texture texture(const std::string&);
|
sb::Texture& texture(const std::string&);
|
||||||
void texture(const std::string&, const sb::Texture&);
|
void texture(const std::string&, const sb::Texture&);
|
||||||
void transformation(const glm::mat4&);
|
void transformation(const glm::mat4&);
|
||||||
std::size_t size();
|
std::size_t size();
|
||||||
|
|
|
@ -38,6 +38,11 @@ Pudding::Pudding()
|
||||||
/* use gl context so we can draw 3D */
|
/* use gl context so we can draw 3D */
|
||||||
load_gl_context();
|
load_gl_context();
|
||||||
load_tiles();
|
load_tiles();
|
||||||
|
Model boom;
|
||||||
|
boom["hello world"] = std::make_shared<sb::Attributes>(sb::Attributes{1, 2, 3, 4, 5});
|
||||||
|
std::cout << *boom["hello world"] << std::endl;
|
||||||
|
boom.attributes("bam", {{4.2f, 42.0f, 420.0f}, {.69f, 6.9f, 69.0f}});
|
||||||
|
std::cout << *boom["bam"] << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Assign vertices, colors and texture UV coordinates to the pudding model */
|
/* Assign vertices, colors and texture UV coordinates to the pudding model */
|
||||||
|
@ -92,30 +97,30 @@ void Pudding::set_pudding_model(
|
||||||
/* triangle that includes top two vertices and first base vertex */
|
/* triangle that includes top two vertices and first base vertex */
|
||||||
start_vertex = &layer_top_ring[ii];
|
start_vertex = &layer_top_ring[ii];
|
||||||
end_vertex = &layer_top_ring[(ii + 1) % layer_top_ring.size()];
|
end_vertex = &layer_top_ring[(ii + 1) % layer_top_ring.size()];
|
||||||
pudding_attributes["position"].add(start_vertex->x, layer_top_y, start_vertex->y);
|
pudding_model["position"]->add(start_vertex->x, layer_top_y, start_vertex->y);
|
||||||
pudding_attributes["uv"].add(ring_start_vertex_u, layer_top_percent);
|
pudding_model["uv"]->add(ring_start_vertex_u, layer_top_percent);
|
||||||
pudding_attributes["position"].add(end_vertex->x, layer_top_y, end_vertex->y);
|
pudding_model["position"]->add(end_vertex->x, layer_top_y, end_vertex->y);
|
||||||
pudding_attributes["uv"].add(ring_start_vertex_u + u_step, layer_top_percent);
|
pudding_model["uv"]->add(ring_start_vertex_u + u_step, layer_top_percent);
|
||||||
pudding_attributes["color"].extend(*layer_top_color, 2);
|
pudding_model["color"]->extend(*layer_top_color, 2);
|
||||||
pudding_attributes["position"].add(layer_base_ring[ii].x, layer_base_y, layer_base_ring[ii].y);
|
pudding_model["position"]->add(layer_base_ring[ii].x, layer_base_y, layer_base_ring[ii].y);
|
||||||
pudding_attributes["uv"].add(ring_start_vertex_u, layer_base_percent);
|
pudding_model["uv"]->add(ring_start_vertex_u, layer_base_percent);
|
||||||
pudding_attributes["color"].add(*layer_bottom_color);
|
pudding_model["color"]->add(*layer_bottom_color);
|
||||||
/* triangle that includes bottom two vertices and second top vertex */
|
/* triangle that includes bottom two vertices and second top vertex */
|
||||||
start_vertex = &layer_base_ring[ii];
|
start_vertex = &layer_base_ring[ii];
|
||||||
pudding_attributes["position"].add(start_vertex->x, layer_base_y, start_vertex->y);
|
pudding_model["position"]->add(start_vertex->x, layer_base_y, start_vertex->y);
|
||||||
pudding_attributes["uv"].add(ring_start_vertex_u, layer_base_percent);
|
pudding_model["uv"]->add(ring_start_vertex_u, layer_base_percent);
|
||||||
pudding_attributes["color"].add(*layer_bottom_color);
|
pudding_model["color"]->add(*layer_bottom_color);
|
||||||
pudding_attributes["position"].add(end_vertex->x, layer_top_y, end_vertex->y);
|
pudding_model["position"]->add(end_vertex->x, layer_top_y, end_vertex->y);
|
||||||
pudding_attributes["uv"].add(ring_start_vertex_u + u_step, layer_top_percent);
|
pudding_model["uv"]->add(ring_start_vertex_u + u_step, layer_top_percent);
|
||||||
pudding_attributes["color"].add(*layer_top_color);
|
pudding_model["color"]->add(*layer_top_color);
|
||||||
end_vertex = &layer_base_ring[(ii + 1) % layer_base_ring.size()];
|
end_vertex = &layer_base_ring[(ii + 1) % layer_base_ring.size()];
|
||||||
pudding_attributes["position"].add(end_vertex->x, layer_base_y, end_vertex->y);
|
pudding_model["position"]->add(end_vertex->x, layer_base_y, end_vertex->y);
|
||||||
pudding_attributes["uv"].add(ring_start_vertex_u + u_step, layer_base_percent);
|
pudding_model["uv"]->add(ring_start_vertex_u + u_step, layer_base_percent);
|
||||||
pudding_attributes["color"].add(*layer_bottom_color);
|
pudding_model["color"]->add(*layer_bottom_color);
|
||||||
ring_start_vertex_u += u_step;
|
ring_start_vertex_u += u_step;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pudding_triangle_vertex_count = pudding_attributes["position"].count();
|
pudding_triangle_vertex_count = pudding_model["position"]->count();
|
||||||
/* process the top and bottom of pudding, filling each face with a triangle fan */
|
/* process the top and bottom of pudding, filling each face with a triangle fan */
|
||||||
float y = max_y;
|
float y = max_y;
|
||||||
const glm::vec3* face_color = &PUDDING_BROWN;
|
const glm::vec3* face_color = &PUDDING_BROWN;
|
||||||
|
@ -123,8 +128,8 @@ void Pudding::set_pudding_model(
|
||||||
for (float radius : {top_radius, base_radius})
|
for (float radius : {top_radius, base_radius})
|
||||||
{
|
{
|
||||||
/* first point in a GL_TRIANGLE_FAN is the center */
|
/* first point in a GL_TRIANGLE_FAN is the center */
|
||||||
pudding_attributes["position"].add(0.0f, y, 0.0f);
|
pudding_model["position"]->add(0.0f, y, 0.0f);
|
||||||
pudding_attributes["uv"].add(0.0f, 0.0f);
|
pudding_model["uv"]->add(0.0f, 0.0f);
|
||||||
layer_top_ring.clear();
|
layer_top_ring.clear();
|
||||||
sb::points_on_circle(layer_top_ring, ring_vertex_count, radius);
|
sb::points_on_circle(layer_top_ring, ring_vertex_count, radius);
|
||||||
/* loop through points on the face */
|
/* loop through points on the face */
|
||||||
|
@ -132,25 +137,22 @@ void Pudding::set_pudding_model(
|
||||||
{
|
{
|
||||||
start_vertex = &layer_top_ring[ii];
|
start_vertex = &layer_top_ring[ii];
|
||||||
/* for GL_TRIANGLE_FAN we just need to add an outer vertex */
|
/* for GL_TRIANGLE_FAN we just need to add an outer vertex */
|
||||||
pudding_attributes["position"].add(start_vertex->x, y, start_vertex->y);
|
pudding_model["position"]->add(start_vertex->x, y, start_vertex->y);
|
||||||
pudding_attributes["uv"].add(*start_vertex);
|
pudding_model["uv"]->add(*start_vertex);
|
||||||
/* connect the ring on the last vertex */
|
/* connect the ring on the last vertex */
|
||||||
if (ii == layer_top_ring.size() - 1)
|
if (ii == layer_top_ring.size() - 1)
|
||||||
{
|
{
|
||||||
end_vertex = &layer_top_ring[(ii + 1) % layer_top_ring.size()];
|
end_vertex = &layer_top_ring[(ii + 1) % layer_top_ring.size()];
|
||||||
pudding_attributes["position"].add(end_vertex->x, y, end_vertex->y);
|
pudding_model["position"]->add(end_vertex->x, y, end_vertex->y);
|
||||||
pudding_attributes["uv"].add(*end_vertex);
|
pudding_model["uv"]->add(*end_vertex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* single color for the entire layer_top_ring */
|
/* single color for the entire layer_top_ring */
|
||||||
pudding_attributes["color"].extend(*face_color, layer_top_ring.size() + 2);
|
pudding_model["color"]->extend(*face_color, layer_top_ring.size() + 2);
|
||||||
y = min_y;
|
y = min_y;
|
||||||
face_color = &PUDDING_YELLOW;
|
face_color = &PUDDING_YELLOW;
|
||||||
}
|
}
|
||||||
pudding_fan_vertex_count = (pudding_attributes["position"].count() - pudding_triangle_vertex_count) / 2;
|
pudding_fan_vertex_count = (pudding_model["position"]->count() - pudding_triangle_vertex_count) / 2;
|
||||||
pudding_attributes["position"].index(0);
|
|
||||||
pudding_attributes["color"].index(1);
|
|
||||||
pudding_attributes["uv"].index(2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Create GL context via super class and load vertices, UV data, and shaders */
|
/* Create GL context via super class and load vertices, UV data, and shaders */
|
||||||
|
@ -186,18 +188,17 @@ void Pudding::load_gl_context()
|
||||||
mvp_program = glCreateProgram();
|
mvp_program = glCreateProgram();
|
||||||
glAttachShader(mvp_program, vertex_shader);
|
glAttachShader(mvp_program, vertex_shader);
|
||||||
glAttachShader(mvp_program, fragment_shader);
|
glAttachShader(mvp_program, fragment_shader);
|
||||||
glBindAttribLocation(mvp_program, pudding_attributes["position"], "in_position");
|
pudding_model.attributes("position")->bind(0, mvp_program, "in_position");
|
||||||
glBindAttribLocation(mvp_program, pudding_attributes["color"], "in_color");
|
pudding_model.attributes("color")->bind(1, mvp_program, "in_color");
|
||||||
glBindAttribLocation(mvp_program, pudding_attributes["uv"], "vertex_uv");
|
pudding_model.attributes("uv")->bind(2, mvp_program, "vertex_uv");
|
||||||
sb::Log::gl_errors("after loading shaders");
|
sb::Log::gl_errors("after loading shaders");
|
||||||
/* Fill VBO with attribute data */
|
/* Fill VBO with attribute data */
|
||||||
vbo.allocate(plane.size() + pudding_attributes["uv"].size() + pudding_attributes["position"].size() +
|
vbo.allocate(plane.size() + pudding_model.size(), GL_STATIC_DRAW);
|
||||||
pudding_attributes["color"].size(), GL_STATIC_DRAW);
|
|
||||||
vbo.add(*plane.attributes("position"));
|
vbo.add(*plane.attributes("position"));
|
||||||
vbo.add(*plane.attributes("uv"));
|
vbo.add(*plane.attributes("uv"));
|
||||||
vbo.add(pudding_attributes["uv"]);
|
vbo.add(*pudding_model.attributes("uv"));
|
||||||
vbo.add(pudding_attributes["position"]);
|
vbo.add(*pudding_model.attributes("position"));
|
||||||
vbo.add(pudding_attributes["color"]);
|
vbo.add(*pudding_model.attributes("color"));
|
||||||
sb::Log::gl_errors("after filling VBO");
|
sb::Log::gl_errors("after filling VBO");
|
||||||
/* link shaders */
|
/* link shaders */
|
||||||
link_shader(flat_program);
|
link_shader(flat_program);
|
||||||
|
@ -746,9 +747,7 @@ void Pudding::update()
|
||||||
glUseProgram(flat_program);
|
glUseProgram(flat_program);
|
||||||
glUniform1f(uniform["flat"]["time"], time_seconds);
|
glUniform1f(uniform["flat"]["time"], time_seconds);
|
||||||
/* disable pudding attributes and enable rectangle attributes */
|
/* disable pudding attributes and enable rectangle attributes */
|
||||||
pudding_attributes["position"].disable();
|
pudding_model.disable();
|
||||||
pudding_attributes["color"].disable();
|
|
||||||
pudding_attributes["uv"].disable();
|
|
||||||
plane.enable();
|
plane.enable();
|
||||||
glUniform1i(uniform["flat"]["texture"], 0);
|
glUniform1i(uniform["flat"]["texture"], 0);
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
|
@ -771,17 +770,17 @@ void Pudding::update()
|
||||||
glUniformMatrix4fv(uniform["mvp"]["mvp"], 1, GL_FALSE, &mvp[0][0]);
|
glUniformMatrix4fv(uniform["mvp"]["mvp"], 1, GL_FALSE, &mvp[0][0]);
|
||||||
/* disable rectangle attributes and enable pudding attributes */
|
/* disable rectangle attributes and enable pudding attributes */
|
||||||
plane.disable();
|
plane.disable();
|
||||||
pudding_attributes["position"].enable();
|
pudding_model.attributes("position")->enable();
|
||||||
if (items.size() == 0)
|
if (items.size() == 0)
|
||||||
{
|
{
|
||||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||||
pudding_attributes["color"].enable();
|
pudding_model.attributes("color")->enable();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||||
pudding_attributes["color"].enable();
|
pudding_model.attributes("color")->enable();
|
||||||
pudding_attributes["uv"].enable();
|
pudding_model.attributes("uv")->enable();
|
||||||
glUniform1i(uniform["mvp"]["pudding texture"], 0);
|
glUniform1i(uniform["mvp"]["pudding texture"], 0);
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
current_item().get_active_image_texture().bind();
|
current_item().get_active_image_texture().bind();
|
||||||
|
@ -806,9 +805,7 @@ void Pudding::update()
|
||||||
/* switch to flat shader for item and camera */
|
/* switch to flat shader for item and camera */
|
||||||
glUseProgram(flat_program);
|
glUseProgram(flat_program);
|
||||||
/* disable pudding attributes and enable rectangle attributes */
|
/* disable pudding attributes and enable rectangle attributes */
|
||||||
glDisableVertexAttribArray(pudding_attributes["position"]);
|
pudding_model.disable();
|
||||||
glDisableVertexAttribArray(pudding_attributes["color"]);
|
|
||||||
glDisableVertexAttribArray(pudding_attributes["uv"]);
|
|
||||||
plane.enable();
|
plane.enable();
|
||||||
glDisable(GL_DEPTH_TEST);
|
glDisable(GL_DEPTH_TEST);
|
||||||
/* just need to set these once since we're drawing one texture per viewport */
|
/* just need to set these once since we're drawing one texture per viewport */
|
||||||
|
|
|
@ -85,8 +85,9 @@ private:
|
||||||
std::map<std::string, std::map<std::string, GLuint>> uniform;
|
std::map<std::string, std::map<std::string, GLuint>> uniform;
|
||||||
GLuint flat_program, mvp_program;
|
GLuint flat_program, mvp_program;
|
||||||
glm::mat4 projection, model = glm::mat4(1.0f), mvp;
|
glm::mat4 projection, model = glm::mat4(1.0f), mvp;
|
||||||
std::map<std::string, sb::Attributes> pudding_attributes;
|
Model pudding_model;
|
||||||
Plane plane;
|
Plane plane;
|
||||||
|
// Plane background, camera_frame;
|
||||||
bool show_item = false, reading_capture_frame = false;
|
bool show_item = false, reading_capture_frame = false;
|
||||||
SDL_GLContext capture_frame_thread_context = nullptr;
|
SDL_GLContext capture_frame_thread_context = nullptr;
|
||||||
std::vector<sb::Texture> tiles;
|
std::vector<sb::Texture> tiles;
|
||||||
|
|
Loading…
Reference in New Issue