Images are better organized but still need to be setup before rendering.

master
Zed A. Shaw 3 weeks ago
parent ccb06324c3
commit 6013283b00
  1. 2
      lua/builder_test.lua
  2. 3
      src/document.hpp
  3. 44
      src/image.cpp
  4. 21
      src/image.hpp
  5. 11
      src/render.cpp

@ -10,7 +10,7 @@ config{
slide{
h1 "Here's Mine",
image{
src="test.jpg"
src="assets/test.jpg"
}
}

@ -4,6 +4,8 @@
#include <vector>
#include <unordered_map>
#include <cstdint>
#include <memory>
#include "image.hpp"
enum class NodeType {
TEXT,
@ -28,6 +30,7 @@ struct Node {
NodeType type = NodeType::ERROR;
std::string content = "";
std::unordered_map<std::string, std::any> config{};
std::shared_ptr<Image> image = nullptr;
};
struct Slide {

@ -1,6 +1,18 @@
#include "image.hpp"
#include <stb_image.h>
// these never change so just put them here
float VERTICES[24] = {
// positions // texCoords
-1.0f, 1.0f, 0.0f, 1.0f, // top left
-1.0f, -1.0f, 0.0f, 0.0f, // bottom left
1.0f, -1.0f, 1.0f, 0.0f, // bottom right
-1.0f, 1.0f, 0.0f, 1.0f, // top left
1.0f, -1.0f, 1.0f, 0.0f, // bottom right
1.0f, 1.0f, 1.0f, 1.0f, // top right
};
unsigned int image_to_texture(unsigned char *data, int width, int height, int nrComponents) {
unsigned int textureID;
glGenTextures(1, &textureID);
@ -31,33 +43,17 @@ unsigned int image_to_texture(unsigned char *data, int width, int height, int nr
return textureID;
}
void Image::load(Node& node) {
dbc::check(node.type == NodeType::IMAGE, "That's not an image idiot.");
if(!node.config.contains("_image_data")) {
dbc::check(node.config.contains("src"), "Node doesn't have an image src.");
std::string filename = std::any_cast<std::string>(node.config.at("src"));
int width = 0;
int height = 0;
int nrComponents = 0;
stbi_set_flip_vertically_on_load(true);
unsigned char *data = stbi_load(filename.c_str(), &width, &height, &nrComponents, 0);
dbc::check(data != NULL, "Failed to stb_load image.");
unsigned int texture_id = image_to_texture(data, width, height, nrComponents);
void Image::load(const std::string& filename) {
stbi_set_flip_vertically_on_load(true);
unsigned char *data = stbi_load(filename.c_str(), &width, &height, &nrComponents, 0);
dbc::check(data != NULL, "Failed to stb_load image.");
node.config.try_emplace("_image_data", texture_id);
texture_id = image_to_texture(data, width, height, nrComponents);
stbi_image_free(data);
}
stbi_image_free(data);
}
void Image::render(Node& node, float xpos, float ypos, float w, float h) {
dbc::check(node.config.contains("_image_data"), "Node isn't configured for images, did you Image::load?");
unsigned int texture_id = std::any_cast<unsigned int>(node.config.at("_image_data"));
void Image::render() {
shader.use();
shader.setInt("texture1", texture_id);
glBindVertexArray(image_vao);
@ -71,7 +67,7 @@ Image::Image() {
glBindVertexArray(image_vao);
glBindBuffer(GL_ARRAY_BUFFER, image_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, sizeof(VERTICES), &VERTICES, GL_STATIC_DRAW);
// position attrib
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);

@ -1,25 +1,18 @@
#pragma once
#include "document.hpp"
#include "shader.hpp"
struct Image {
Shader shader{{"shaders/image.vert.glsl", "shaders/image.frag.glsl", ""}};
unsigned int image_vao = UINT_MAX;
unsigned int image_vbo = UINT_MAX;
float vertices[24] = {
// positions // texCoords
-0.5f, 0.5f, 0.0f, 1.0f, // top left
-0.5f, -0.5f, 0.0f, 0.0f, // bottom left
0.5f, -0.5f, 1.0f, 0.0f, // bottom right
-0.5f, 0.5f, 0.0f, 1.0f, // top left
0.5f, -0.5f, 1.0f, 0.0f, // bottom right
0.5f, 0.5f, 1.0f, 1.0f, // top right
};
unsigned int texture_id = UINT_MAX;
int width = 0;
int height = 0;
int nrComponents = 0;
// scale, rotate, position
Image();
void load(Node& node);
void render(Node& node, float xpos, float ypos, float w, float h);
void load(const std::string& filename);
void render();
};

@ -31,10 +31,13 @@ void Engine::render() {
if(node.type == NodeType::TEXT || node.type == NodeType::H1 || node.type == NodeType::UL) {
text.render(node.content, 25.0f, SCR_HEIGHT - y, 1.0f, glm::vec3(1.0f, 1.0f, 1.0f));
} else if(node.type == NodeType::IMAGE) {
Image img;
img.load(node);
img.render(node, -1.0f, -1.0f, 1.0f, 1.0f);
// it's an image, skip it
if(node.image == nullptr) {
node.image = std::make_shared<Image>();
std::string src = std::any_cast<std::string>(node.config.at("src"));
node.image->load(src);
}
node.image->render();
} else {
dbc::sentinel("Unknown NodeType");
}

Loading…
Cancel
Save