You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.6 KiB
84 lines
2.6 KiB
|
3 weeks ago
|
#include "image.hpp"
|
||
|
|
#include <stb_image.h>
|
||
|
|
|
||
|
|
unsigned int image_to_texture(unsigned char *data, int width, int height, int nrComponents) {
|
||
|
|
unsigned int textureID;
|
||
|
|
glGenTextures(1, &textureID);
|
||
|
|
|
||
|
|
GLenum format = GL_RGB;
|
||
|
|
|
||
|
|
switch(nrComponents) {
|
||
|
|
case 1:
|
||
|
|
format = GL_RED;
|
||
|
|
break;
|
||
|
|
case 3:
|
||
|
|
format = GL_RGB;
|
||
|
|
break;
|
||
|
|
case 4:
|
||
|
|
format = GL_RGBA;
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
dbc::sentinel($F("Impossible nrComponents={} when loading image", nrComponents));
|
||
|
|
}
|
||
|
|
|
||
|
|
glBindTexture(GL_TEXTURE_2D, textureID);
|
||
|
|
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
|
||
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||
|
|
|
||
|
|
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);
|
||
|
|
|
||
|
|
node.config.try_emplace("_image_data", texture_id);
|
||
|
|
|
||
|
|
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"));
|
||
|
|
|
||
|
|
shader.use();
|
||
|
|
shader.setInt("texture1", texture_id);
|
||
|
|
glBindVertexArray(image_vao);
|
||
|
|
glBindTexture(GL_TEXTURE_2D, texture_id);
|
||
|
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||
|
|
}
|
||
|
|
|
||
|
|
Image::Image() {
|
||
|
|
glGenVertexArrays(1, &image_vao);
|
||
|
|
glGenBuffers(1, &image_vbo);
|
||
|
|
|
||
|
|
glBindVertexArray(image_vao);
|
||
|
|
glBindBuffer(GL_ARRAY_BUFFER, image_vbo);
|
||
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices, GL_STATIC_DRAW);
|
||
|
|
|
||
|
|
// position attrib
|
||
|
|
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
|
||
|
|
glEnableVertexAttribArray(0);
|
||
|
|
|
||
|
|
// color attribute
|
||
|
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
|
||
|
|
glEnableVertexAttribArray(1);
|
||
|
|
}
|