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. 36
      src/image.cpp
  4. 21
      src/image.hpp
  5. 11
      src/render.cpp

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

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

@ -1,6 +1,18 @@
#include "image.hpp" #include "image.hpp"
#include <stb_image.h> #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 image_to_texture(unsigned char *data, int width, int height, int nrComponents) {
unsigned int textureID; unsigned int textureID;
glGenTextures(1, &textureID); glGenTextures(1, &textureID);
@ -31,33 +43,17 @@ unsigned int image_to_texture(unsigned char *data, int width, int height, int nr
return textureID; return textureID;
} }
void Image::load(Node& node) { void Image::load(const std::string& filename) {
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); stbi_set_flip_vertically_on_load(true);
unsigned char *data = stbi_load(filename.c_str(), &width, &height, &nrComponents, 0); unsigned char *data = stbi_load(filename.c_str(), &width, &height, &nrComponents, 0);
dbc::check(data != NULL, "Failed to stb_load image."); dbc::check(data != NULL, "Failed to stb_load image.");
unsigned int texture_id = image_to_texture(data, width, height, nrComponents); texture_id = image_to_texture(data, width, height, nrComponents);
node.config.try_emplace("_image_data", texture_id);
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.use();
shader.setInt("texture1", texture_id); shader.setInt("texture1", texture_id);
glBindVertexArray(image_vao); glBindVertexArray(image_vao);
@ -71,7 +67,7 @@ Image::Image() {
glBindVertexArray(image_vao); glBindVertexArray(image_vao);
glBindBuffer(GL_ARRAY_BUFFER, image_vbo); 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 // position attrib
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);

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

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

Loading…
Cancel
Save