move camera textures into class

This commit is contained in:
frank 2021-10-29 22:10:28 -04:00
parent a41dbfd3b1
commit c461475ea6
7 changed files with 80 additions and 54 deletions

View File

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

2
lib/sb

@ -1 +1 @@
Subproject commit 950a95502bb178619376121ac9781ad1f2fe151e
Subproject commit 2c0517b9e28ab2be46c1402bab2dafffd24c6637

View File

@ -147,9 +147,38 @@ void Background::next()
}
/* Return the currently active tile texture. */
const sb::Texture& Background::tile() const
const sb::Texture& Background::current() const
{
auto position = model_texture.begin();
std::advance(position, offset);
return position->second;
}
CameraView::CameraView() : Plane()
{
texture("front", sb::Texture());
texture("back", sb::Texture());
}
void CameraView::generate(const glm::vec2& size)
{
for (sb::Texture* buffer : {&texture("front"), &texture("back")})
{
buffer->generate(size);
}
}
sb::Texture& CameraView::current()
{
return swapped ? texture("back") : texture("front");
}
sb::Texture& CameraView::free()
{
return swapped ? texture("front") : texture("back");
}
void CameraView::swap()
{
swapped = !swapped;
}

View File

@ -86,7 +86,24 @@ private:
public:
void next();
const sb::Texture& tile() const;
const sb::Texture& current() const;
};
class CameraView : public Plane
{
private:
bool swapped = false;
public:
CameraView();
void generate(const glm::vec2&);
sb::Texture& current();
sb::Texture& free();
void swap();
};

View File

@ -38,21 +38,6 @@ Pudding::Pudding()
/* use gl context so we can draw 3D */
load_gl_context();
load_tiles();
Model boom, kaboom;
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;
kaboom = boom;
std::cout << *kaboom.attributes("hello world") << std::endl;
background_copy = background;
background_copy.next();
background = background_copy;
background_copy_2 = background_copy;
background_copy_2.next();
background = background_copy_2;
background_copy = background_copy_2;
background = background_copy;
}
/* Assign vertices, colors and texture UV coordinates to the pudding model */
@ -190,17 +175,17 @@ void Pudding::load_gl_context()
flat_program = glCreateProgram();
glAttachShader(flat_program, vertex_shader);
glAttachShader(flat_program, fragment_shader);
plane.attributes("position")->bind(0, flat_program, "in_position");
plane.attributes("uv")->bind(1, flat_program, "vertex_uv");
/* load, configure and link the 3D world program */
vertex_shader = load_shader("src/mvp.vert", GL_VERTEX_SHADER);
fragment_shader = load_shader("src/mvp.frag", GL_FRAGMENT_SHADER);
plane.attributes("position")->bind(12, flat_program, "in_position");
plane.attributes("uv")->bind(13, flat_program, "vertex_uv");
mvp_program = glCreateProgram();
glAttachShader(mvp_program, vertex_shader);
glAttachShader(mvp_program, fragment_shader);
pudding_model.attributes("position")->bind(0, mvp_program, "in_position");
pudding_model.attributes("color")->bind(1, mvp_program, "in_color");
pudding_model.attributes("uv")->bind(2, mvp_program, "vertex_uv");
pudding_model.attributes("position")->bind(2, mvp_program, "vertex_position");
pudding_model.attributes("uv")->bind(3, mvp_program, "vertex_uv");
pudding_model.attributes("color")->bind(4, mvp_program, "vertex_color");
sb::Log::gl_errors("after loading shaders");
/* Fill VBO with attribute data */
vbo.allocate(plane.size() + pudding_model.size(), GL_STATIC_DRAW);
@ -256,10 +241,7 @@ void Pudding::initialize_camera()
"fps video capture device ID #" << device_id << " using " << capture.getBackendName();
/* generate two textures that will store the video frames with the intention of double buffering them
* for threaded texture loading */
for (sb::Texture* texture : {&capture_texture_front_buffer, &capture_texture_back_buffer})
{
texture->generate({capture.get(cv::CAP_PROP_FRAME_WIDTH), capture.get(cv::CAP_PROP_FRAME_HEIGHT)});
}
camera_view.generate({capture.get(cv::CAP_PROP_FRAME_WIDTH), capture.get(cv::CAP_PROP_FRAME_HEIGHT)});
}
else
{
@ -317,7 +299,7 @@ void Pudding::respond(SDL_Event& event)
}
else if (get_delegate().compare(event, "tile"))
{
background_copy.next();
background.next();
}
}
@ -680,13 +662,12 @@ int Pudding::capture_frame(void* game)
/* rotate the opencv matrix 180 to work with opengl coords */
cv::flip(frame, frame, -1);
/* use whichever texture ID is not being used by the main rendering thread */
sb::Texture& texture = pudding->capture_texture == pudding->capture_texture_front_buffer ?
pudding->capture_texture_back_buffer : pudding->capture_texture_front_buffer;
sb::Texture& texture = pudding->camera_view.free();
/* bind texture for accepting pixel data */
texture.bind();
/* fill texture memory with last frame's pixels */
texture.load(frame.ptr(), {frame.cols, frame.rows}, GL_BGR, GL_UNSIGNED_BYTE);
pudding->capture_texture = texture;
pudding->camera_view.swap();
if (pudding->get_configuration()["scan"]["enabled"])
{
/* convert to gray and scan with zbar */
@ -761,7 +742,7 @@ void Pudding::update()
plane.enable();
glUniform1i(uniform["flat"]["texture"], 0);
glActiveTexture(GL_TEXTURE0);
background_copy.tile().bind();
background.current().bind();
/* set blend to modify white part of background, color passed is in HSV format */
glUniform3f(uniform["flat"]["blend"], 0.0f, 0.0f, 1.0f);
glUniform1i(uniform["flat"]["scroll"], true);
@ -845,7 +826,7 @@ void Pudding::update()
viewport_box.left(window_box(true).left());
glViewport(viewport_box.left(), viewport_box.bottom(), viewport_box.width(), viewport_box.height());
/* bind texture for drawing */
capture_texture.bind();
camera_view.current().bind();
/* draws rectangle vertices and rectangle texture using UV coords */
glDrawArrays(GL_TRIANGLES, 0, plane.attributes("position")->count());
}

View File

@ -86,12 +86,10 @@ private:
glm::mat4 projection, model = glm::mat4(1.0f), mvp;
Model pudding_model;
Plane plane;
Background background, background_copy, background_copy_2;
// Plane background, camera_frame;
Background background;
CameraView camera_view;
bool show_item = false, reading_capture_frame = false;
SDL_GLContext capture_frame_thread_context = nullptr;
sb::Texture capture_texture_front_buffer, capture_texture_back_buffer;
sb::Texture& capture_texture = capture_texture_front_buffer;
sb::VAO vao;
sb::VBO vbo;

View File

@ -1,11 +1,12 @@
/* _______________ +---------------------------------------------------------------------------------------+
//~~~~~~~~~~~~~\\ | 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 😀 |
+---------------------------------------------------------------------------------------+ */
/* _______________ ,----------------------------------------------------------------.
//`````````````\\ \ \
//~~~~~~~~~~~~~~~\\ \ by @ohsqueezy & @sleepin \
//=================\\ \ [ohsqueezy.itch.io] [sleepin.itch.io] \
// \\ \ \
// \\ \ code released under the zlib license [git.nugget.fun/pudding] \
// ☆ GUNKISS ☆ \\ \ \
//_________________________\\ `---------------------------------------------------------------*/
#version 130
#define PI 3.1415926535897932384626433832795
@ -16,8 +17,8 @@
#define EFFECT_SNAKE 1
#define EFFECT_WOBBLE 2
in vec3 in_position;
in vec3 in_color;
in vec3 vertex_position;
in vec3 vertex_color;
in vec2 vertex_uv;
uniform mat4 mvp;
uniform float time;
@ -32,7 +33,7 @@ out vec3 clip_coordinates;
* flattened coordinates (after projection matrix has been applied) */
void wobble()
{
gl_Position.x += sin(time * 10.0) * in_position.y / 2.0;
gl_Position.x += sin(time * 10.0) * vertex_position.y / 2.0;
}
/* Contort the X-coordinate according the time step and Y-coordinate using the sine function. This contorts
@ -43,7 +44,7 @@ void wobble()
void snake()
{
gl_Position = vec4(
in_position.x + AMPLITUDE * sin(2.0 * PI / PERIOD * (time + in_position.y * PERIOD / WAVELENGTH)), in_position.yz, 1);
vertex_position.x + AMPLITUDE * sin(2.0 * PI / PERIOD * (time + vertex_position.y * PERIOD / WAVELENGTH)), vertex_position.yz, 1);
}
void main()
@ -54,7 +55,7 @@ void main()
}
else
{
gl_Position = vec4(in_position, 1);
gl_Position = vec4(vertex_position, 1);
}
gl_Position = mvp * gl_Position;
if (effect == EFFECT_WOBBLE)
@ -62,9 +63,9 @@ void main()
wobble();
}
/* passing to fragment program */
ex_color = in_color;
x_center_proximity = 1.8 - abs(in_position[0]);
ex_color = vertex_color;
x_center_proximity = 1.8 - abs(vertex_position[0]);
fragment_uv = vertex_uv;
original_coordinates = in_position;
original_coordinates = vertex_position;
clip_coordinates = gl_Position.xyz;
}