using attributes class to store generic vertices

This commit is contained in:
frank 2021-10-08 23:43:51 -04:00
parent 72279ba628
commit 7ba46588ea
11 changed files with 136 additions and 94 deletions

View File

@ -1,12 +1,12 @@
# _______________
# //`````````````\\
# //~~~~~~~~~~~~~~~\\ + a game by @ohsqueezy (ohsqueezy.itch.io) & @sleepin (instagram.com/sleepin)
# //=================\\ + code is licensed for copy, modification and redistribution (git.nugget.fun/pudding)
# // \\
# //-G--U--N--K--I--S--S-\\ 😀 Thank you for choosing Puddendo for your business 😀
# //_______________________\\
# ```````````````````````````
#
# _______________ ,----------------------------------------------------------------.
# //`````````````\\ \ \
# //~~~~~~~~~~~~~~~\\ \ by @ohsqueezy & @sleepin \
# //=================\\ \ [ohsqueezy.itch.io] [sleepin.itch.io] \
# // \\ \ \
# // \\ \ code released under the zlib license [git.nugget.fun/pudding] \
# // ☆ GUNKISS ☆ \\ \ \
# //_________________________\\ `----------------------------------------------------------------'
#
# This Makefile should build a Linux binary from `make linux`. Other platform targets have not been tested yet.
#######################
@ -71,6 +71,7 @@ $(SB_SRC_DIR)Pixels.o : $(addprefix $(SB_SRC_DIR),Box.hpp extension.hpp Log.hpp)
$(SB_SRC_DIR)Audio.o : $(addprefix $(SB_SRC_DIR),Node.hpp Display.hpp Configuration.hpp Box.hpp filesystem.hpp extension.hpp)
$(SB_SRC_DIR)Texture.o : $(addprefix $(SB_SRC_DIR),GLObject.hpp filesystem.hpp Log.hpp)
$(SB_SRC_DIR)GLObject.o : $(addprefix $(SB_SRC_DIR),Log.hpp)
$(SB_SRC_DIR)Attributes.o : $(addprefix $(SB_SRC_DIR),Log.hpp extension.hpp)
$(SRC_DIR)Item.o : $(addprefix $(SB_SRC_DIR),extension.hpp Node.hpp Texture.hpp Log.hpp)
$(SRC_DIR)Pudding.o : $(SRC_H_FILES) $(SB_H_FILES)
%.o : %.cpp %.hpp

View File

@ -47,7 +47,7 @@
"enabled": true,
"json-save": true,
"json-save-directory": "local/scans",
"barcode": "613008719548",
"barcode": "",
"capture-device": "/dev/video0"
},
"api":

2
lib/sb

@ -1 +1 @@
Subproject commit f70ea1c21595376ea8bc02d80140721eaf627520
Subproject commit 1690bb5f1996c4f6e342cc59ffa05fa15f9d77ed

View File

@ -1,3 +1,11 @@
/* _______________ +---------------------------------------------------------------------------------------+
//~~~~~~~~~~~~~\\ | a game by @ohsqueezy & @sleepin |
//```````````````\\ | [ohsqueezy.itch.io] [instagram.com/sleepin] |
//_0_0_0_0_0_0_0_0_\\ | |
//_/_/_/_/___\_\_\_\_\\ | with code licensed for copy, modification and redistribution [git.nugget.fun/pudding] |
//GGGUUUNNNKKKIIISSSSSS\\ | |
//_/__/__/__/_\__\__\__\_\\ | 😀 Thank you for choosing Puddendo for your business 😀 |
+---------------------------------------------------------------------------------------+ */
#include "Item.hpp"
Item::Item(Node* parent) : Node(parent) {};

View File

@ -1,3 +1,11 @@
/* _______________ +---------------------------------------------------------------------------------------+
//~~~~~~~~~~~~~\\ | a game by @ohsqueezy & @sleepin |
//```````````````\\ | [ohsqueezy.itch.io] [instagram.com/sleepin] |
//_0_0_0_0_0_0_0_0_\\ | |
//_/_/_/_/___\_\_\_\_\\ | with code licensed for copy, modification and redistribution [git.nugget.fun/pudding] |
//GGGUUUNNNKKKIIISSSSSS\\ | |
//_/__/__/__/_\__\__\__\_\\ | 😀 Thank you for choosing Puddendo for your business 😀 |
+---------------------------------------------------------------------------------------+ */
#ifndef Item_h_
#define Item_h_

View File

@ -1,12 +1,11 @@
/*
_______________
//`````````````\\ + a game by @ohsqueezy (ohsqueezy.itch.io) & @sleepin (instagram.com/sleepin)
//~~~~~~~~~~~~~~~\\ + code is licensed for copy, modification and redistribution (git.nugget.fun/pudding)
//=================\\
/// \\\
///G-*U-*N-*K-*I-*S-*S\\\ 😀 Thank you for choosing Puddendo for your business 😀
//_______________________\\
```````````````````````````
/* _______________ ,----------------------------------------------------------------.
//`````````````\\ \ \
//~~~~~~~~~~~~~~~\\ \ by @ohsqueezy & @sleepin \
//=================\\ \ [ohsqueezy.itch.io] [sleepin.itch.io] \
// \\ \ \
// \\ \ code released under the zlib license [git.nugget.fun/pudding] \
// GUNKISS \\ \ \
//_________________________\\ `----------------------------------------------------------------'
Generate a custom pudding from food product UPC codes and help a pair of rats take over the video game industry, using
their extraterrestrial ability to turn trash into performance enhancing drug puddings that enable business professionals
@ -93,30 +92,30 @@ void Pudding::set_pudding_model(
/* triangle that includes top two vertices and first base vertex */
start_vertex = &layer_top_ring[ii];
end_vertex = &layer_top_ring[(ii + 1) % layer_top_ring.size()];
pudding_vertices.push_back({start_vertex->x, layer_top_y, start_vertex->y});
pudding_uv.push_back({ring_start_vertex_u, layer_top_percent});
pudding_vertices.push_back({end_vertex->x, layer_top_y, end_vertex->y});
pudding_uv.push_back({ring_start_vertex_u + u_step, layer_top_percent});
pudding_colors.insert(pudding_colors.end(), 2, *layer_top_color);
pudding_vertices.push_back({layer_base_ring[ii].x, layer_base_y, layer_base_ring[ii].y});
pudding_uv.push_back({ring_start_vertex_u, layer_base_percent});
pudding_colors.push_back(*layer_bottom_color);
pudding_attributes["vertices"].add(start_vertex->x, layer_top_y, start_vertex->y);
pudding_attributes["uv"].add(ring_start_vertex_u, layer_top_percent);
pudding_attributes["vertices"].add(end_vertex->x, layer_top_y, end_vertex->y);
pudding_attributes["uv"].add(ring_start_vertex_u + u_step, layer_top_percent);
pudding_attributes["color"].extend(*layer_top_color, 2);
pudding_attributes["vertices"].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_attributes["color"].add(*layer_bottom_color);
/* triangle that includes bottom two vertices and second top vertex */
start_vertex = &layer_base_ring[ii];
pudding_vertices.push_back({start_vertex->x, layer_base_y, start_vertex->y});
pudding_uv.push_back({ring_start_vertex_u, layer_base_percent});
pudding_colors.push_back(*layer_bottom_color);
pudding_vertices.push_back({end_vertex->x, layer_top_y, end_vertex->y});
pudding_uv.push_back({ring_start_vertex_u + u_step, layer_top_percent});
pudding_colors.push_back(*layer_top_color);
pudding_attributes["vertices"].add(start_vertex->x, layer_base_y, start_vertex->y);
pudding_attributes["uv"].add(ring_start_vertex_u, layer_base_percent);
pudding_attributes["colors"].add(*layer_bottom_color);
pudding_attributes["vertices"].add(end_vertex->x, layer_top_y, end_vertex->y);
pudding_attributes["uv"].add(ring_start_vertex_u + u_step, layer_top_percent);
pudding_attributes["color"].add(*layer_top_color);
end_vertex = &layer_base_ring[(ii + 1) % layer_base_ring.size()];
pudding_vertices.push_back({end_vertex->x, layer_base_y, end_vertex->y});
pudding_uv.push_back({ring_start_vertex_u + u_step, layer_base_percent});
pudding_colors.push_back(*layer_bottom_color);
pudding_attributes["vertices"].add(end_vertex->x, layer_base_y, end_vertex->y);
pudding_attributes["uv"].add(ring_start_vertex_u + u_step, layer_base_percent);
pudding_attributes["color"].add(*layer_bottom_color);
ring_start_vertex_u += u_step;
}
}
pudding_triangle_vertex_count = pudding_vertices.size();
pudding_triangle_vertex_count = pudding_attributes["vertices"].count();
/* process the top and bottom of pudding, filling each face with a triangle fan */
float y = max_y;
const glm::vec3* face_color = &PUDDING_BROWN;
@ -124,8 +123,8 @@ void Pudding::set_pudding_model(
for (float radius : {top_radius, base_radius})
{
/* first point in a GL_TRIANGLE_FAN is the center */
pudding_vertices.push_back({0, y, 0});
pudding_uv.push_back({0, 0});
pudding_attributes["vertices"].add(0.0f, y, 0.0f);
pudding_attributes["uv"].add(0.0f, 0.0f);
layer_top_ring.clear();
sb::points_on_circle(layer_top_ring, ring_vertex_count, radius);
/* loop through points on the face */
@ -133,22 +132,22 @@ void Pudding::set_pudding_model(
{
start_vertex = &layer_top_ring[ii];
/* for GL_TRIANGLE_FAN we just need to add an outer vertex */
pudding_vertices.push_back({start_vertex->x, y, start_vertex->y});
pudding_uv.push_back(*start_vertex);
pudding_attributes["vertices"].add(start_vertex->x, y, start_vertex->y);
pudding_attributes["uv"].add(*start_vertex);
/* connect the ring on the last vertex */
if (ii == layer_top_ring.size() - 1)
{
end_vertex = &layer_top_ring[(ii + 1) % layer_top_ring.size()];
pudding_vertices.push_back({end_vertex->x, y, end_vertex->y});
pudding_uv.push_back(*end_vertex);
pudding_attributes["vertices"].add(end_vertex->x, y, end_vertex->y);
pudding_attributes["uv"].add(*end_vertex);
}
}
/* single color for the entire layer_top_ring */
pudding_colors.insert(pudding_colors.end(), layer_top_ring.size() + 2, *face_color);
pudding_attributes["colors"].extend(*face_color, layer_top_ring.size() + 2);
y = min_y;
face_color = &PUDDING_YELLOW;
}
pudding_fan_vertex_count = (pudding_vertices.size() - pudding_triangle_vertex_count) / 2;
pudding_fan_vertex_count = (pudding_attributes["vertices"].count() - pudding_triangle_vertex_count) / 2;
}
/* Create GL context via super class and load vertices, UV data, and shaders */
@ -164,16 +163,19 @@ void Pudding::load_gl_context()
/* Generate a vertex array object ID, bind it as current */
vao.generate();
vao.bind();
/* 2D vertices for any texture that is a plane spanning the screen */
std::array<glm::vec2, 6> rectangle_vertices = {{
{-1.0f, 1.0f}, {1.0f, 1.0f}, {-1.0f, -1.0f},
{1.0f, 1.0f}, {1.0f, -1.0f}, {-1.0f, -1.0f}
}};
/* UV map for mapping a texture onto a plane */
std::array<glm::vec2, 6> rectangle_uv = {{
{0.0f, 1.0f}, {1.0f, 1.0f}, {0.0f, 0.0f},
{1.0f, 1.0f}, {1.0f, 0.0f}, {0.0f, 0.0f}
}};
/* 2D attributes (location and UV) for any texture that is a plane spanning the screen */
std::vector<sb::Attributes> rectangle_attributes = {
/* rectangle vertices will be added later */
sb::Attributes({
{-1.0f, 1.0f}, {1.0f, 1.0f}, {-1.0f, -1.0f},
{1.0f, 1.0f}, {1.0f, -1.0f}, {-1.0f, -1.0f}
}),
/* first three rectangle UV coordinates, three more will be added later */
sb::Attributes({
{0.0f, 1.0f}, {1.0f, 1.0f}, {0.0f, 0.0f},
{1.0f, 1.0f}, {1.0f, 0.0f}, {0.0f, 0.0f}
})
};
/* Generate one vertex buffer object to hold all vertices and rectangle UV map. Since we're using one buffer, data
* will be copied in one after the other, offset to after the previous data location. The same buffer offset will
* be passed to the vertex attributes for each data. */
@ -181,26 +183,26 @@ void Pudding::load_gl_context()
vbo.target(GL_ARRAY_BUFFER);
vbo.bind();
/* allocate space for vertices, UV and colors, and copy rectangle vertices in at initialization */
GLsizeiptr vbo_size = (rectangle_vertices.size() + rectangle_uv.size() + pudding_uv.size()) * sizeof(glm::vec2) +
(pudding_vertices.size() + pudding_colors.size()) * sizeof(glm::vec3);
glBufferData(GL_ARRAY_BUFFER, vbo_size, rectangle_vertices.data(), GL_STATIC_DRAW);
GLsizeiptr vbo_size = rectangle_attributes[0].size() + rectangle_attributes[1].size() + pudding_attributes["uv"].size() +
pudding_attributes["vertices"].size() + pudding_attributes["colors"].size();
glBufferData(GL_ARRAY_BUFFER, vbo_size, rectangle_attributes[0], GL_STATIC_DRAW);
/* specify the rectangle vertex attributes as consecutive 2D float coords */
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
/* copy rectangle UV data into the VBO, offset to after the vertex data, set up attributes */
GLintptr offset = rectangle_vertices.size() * sizeof(glm::vec2);
glBufferSubData(GL_ARRAY_BUFFER, offset, rectangle_uv.size() * sizeof(glm::vec2), rectangle_uv.data());
GLintptr offset = rectangle_attributes[0].size();
glBufferSubData(GL_ARRAY_BUFFER, offset, rectangle_attributes[1].size(), rectangle_attributes[1]);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, reinterpret_cast<GLvoid*>(offset));
/* copy pudding vertices into VBO, offset to after the rectangle UV, and set up vertex attributes for 3D */
offset += rectangle_uv.size() * sizeof(glm::vec2);
glBufferSubData(GL_ARRAY_BUFFER, offset, pudding_vertices.size() * sizeof(glm::vec3), pudding_vertices.data());
offset += rectangle_attributes[1].size();
glBufferSubData(GL_ARRAY_BUFFER, offset, pudding_attributes["vertices"].size(), pudding_attributes["vertices"]);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, reinterpret_cast<GLvoid*>(offset));
/* copy pudding color values into VBO, offset to after pudding vertices and set as 3D */
offset += pudding_vertices.size() * sizeof(glm::vec3);
glBufferSubData(GL_ARRAY_BUFFER, offset, pudding_colors.size() * sizeof(glm::vec3), pudding_colors.data());
offset += pudding_attributes["vertices"].size();
glBufferSubData(GL_ARRAY_BUFFER, offset, pudding_attributes["colors"].size(), pudding_attributes["colors"]);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, reinterpret_cast<GLvoid*>(offset));
/* copy pudding UV values into VBO, offset to after pudding color vertices and set as 2D */
offset += pudding_colors.size() * sizeof(glm::vec3);
glBufferSubData(GL_ARRAY_BUFFER, offset, pudding_uv.size() * sizeof(glm::vec2), pudding_uv.data());
offset += pudding_attributes["colors"].size();
glBufferSubData(GL_ARRAY_BUFFER, offset, pudding_attributes["uv"].size(), pudding_attributes["uv"]);
glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, 0, reinterpret_cast<GLvoid*>(offset));
/* Load two shader programs, one for rendering the flat objects, and one for rendering the 3D model. Load, configure,
* and link the flat shader program first. */

View File

@ -1,3 +1,12 @@
/* _______________ ,----------------------------------------------------------------.
//`````````````\\ \ \
//~~~~~~~~~~~~~~~\\ \ by @ohsqueezy & @sleepin \
//=================\\ \ [ohsqueezy.itch.io] [sleepin.itch.io] \
// \\ \ \
// \\ \ code released under the zlib license [git.nugget.fun/pudding] \
// GUNKISS \\ \ \
//_________________________\\ `---------------------------------------------------------------*/
#ifndef Pudding_h_
#define Pudding_h_
@ -7,10 +16,7 @@
#include <algorithm>
#include <thread>
#include <memory>
#include <type_traits>
#include <filesystem>
#include <curl/curl.h>
#include <variant>
#include "SDL.h"
#include "SDL_image.h"
#include "sdl2-gfx/SDL2_gfxPrimitives.h"
@ -24,11 +30,13 @@
#include "Game.hpp"
#include "Color.hpp"
#include "extension.hpp"
#include "filesystem.hpp"
#include "Item.hpp"
#include "Animation.hpp"
#include "Texture.hpp"
#include "GLObject.hpp"
#include "Log.hpp"
#include "Attributes.hpp"
class VAO : public GLObject
{
@ -49,29 +57,6 @@ public:
void vao_deleter(GLuint*);
class Attributes
{
private:
using Vertices1D = std::vector<std::variant<unsigned int, int, bool, float, double>>;
using Vertices2D = std::vector<glm::vec<2, std::variant<unsigned int, int, bool, float, double>, glm::defaultp>>;
using Vertices3D = std::vector<glm::vec<3, std::variant<unsigned int, int, bool, float, double>, glm::defaultp>>;
using Vertices4D = std::vector<glm::vec<4, std::variant<unsigned int, int, bool, float, double>, glm::defaultp>>;
using Vertices = std::vector<std::variant<Vertices1D, Vertices2D, Vertices3D, Vertices4D>>;
std::vector<Vertices> vertices;
public:
Attributes();
Attributes(Vertices);
Attributes(std::vector<Vertices>);
void index(int);
int index() const;
void enable() const;
};
class Buffer : public GLObject
{
@ -84,6 +69,7 @@ public:
Buffer();
Buffer(GLenum, GLenum);
void generate();
void target(GLenum);
GLenum target() const;
@ -92,7 +78,7 @@ public:
void allocate(GLenum, GLenum, GLsizeiptr);
template<typename T>
Attributes add(VAO& vao, std::vector<T> vertex_attributes)
sb::Attributes add(VAO& vao, std::vector<T> vertex_attributes)
{
vao.increment();
// glVertexAttribPointer(vao.counted(),
@ -147,6 +133,11 @@ private:
uv_transformation_uniform_location, flat_texture_uniform_location, coordinate_bound_uniform_location,
flat_time_uniform_location, scroll_uniform_location;
glm::mat4 projection, model = glm::mat4(1.0f), mvp;
std::map<std::string, sb::Attributes> pudding_attributes = {
{"vertices", sb::Attributes()},
{"uv", sb::Attributes()},
{"color", sb::Attributes()}
};
std::vector<glm::vec3> pudding_vertices, pudding_colors;
std::vector<glm::vec2> pudding_uv;
bool show_item = false, reading_capture_frame = false;

View File

@ -1,3 +1,11 @@
/* _______________ +---------------------------------------------------------------------------------------+
//~~~~~~~~~~~~~\\ | a game by @ohsqueezy & @sleepin |
//```````````````\\ | [ohsqueezy.itch.io] [instagram.com/sleepin] |
//_0_0_0_0_0_0_0_0_\\ | |
//_/_/_/_/___\_\_\_\_\\ | with code licensed for copy, modification and redistribution [git.nugget.fun/pudding] |
//GGGUUUNNNKKKIIISSSSSS\\ | |
//_/__/__/__/_\__\__\__\_\\ | 😀 Thank you for choosing Puddendo for your business 😀 |
+---------------------------------------------------------------------------------------+ */
#version 130
in vec2 uv;

View File

@ -1,3 +1,11 @@
/* _______________ +---------------------------------------------------------------------------------------+
//~~~~~~~~~~~~~\\ | a game by @ohsqueezy & @sleepin |
//```````````````\\ | [ohsqueezy.itch.io] [instagram.com/sleepin] |
//_0_0_0_0_0_0_0_0_\\ | |
//_/_/_/_/___\_\_\_\_\\ | with code licensed for copy, modification and redistribution [git.nugget.fun/pudding] |
//GGGUUUNNNKKKIIISSSSSS\\ | |
//_/__/__/__/_\__\__\__\_\\ | 😀 Thank you for choosing Puddendo for your business 😀 |
+---------------------------------------------------------------------------------------+ */
#version 130
in vec2 in_position;

View File

@ -1,3 +1,11 @@
/* _______________ +---------------------------------------------------------------------------------------+
//~~~~~~~~~~~~~\\ | a game by @ohsqueezy & @sleepin |
//```````````````\\ | [ohsqueezy.itch.io] [instagram.com/sleepin] |
//_0_0_0_0_0_0_0_0_\\ | |
//_/_/_/_/___\_\_\_\_\\ | with code licensed for copy, modification and redistribution [git.nugget.fun/pudding] |
//GGGUUUNNNKKKIIISSSSSS\\ | |
//_/__/__/__/_\__\__\__\_\\ | 😀 Thank you for choosing Puddendo for your business 😀 |
+---------------------------------------------------------------------------------------+ */
#version 130
#define TRANSFORMATION_NONE 0

View File

@ -1,3 +1,11 @@
/* _______________ +---------------------------------------------------------------------------------------+
//~~~~~~~~~~~~~\\ | a game by @ohsqueezy & @sleepin |
//```````````````\\ | [ohsqueezy.itch.io] [instagram.com/sleepin] |
//_0_0_0_0_0_0_0_0_\\ | |
//_/_/_/_/___\_\_\_\_\\ | with code licensed for copy, modification and redistribution [git.nugget.fun/pudding] |
//GGGUUUNNNKKKIIISSSSSS\\ | |
//_/__/__/__/_\__\__\__\_\\ | 😀 Thank you for choosing Puddendo for your business 😀 |
+---------------------------------------------------------------------------------------+ */
#version 130
#define PI 3.1415926535897932384626433832795