parent
cb3786b508
commit
ccb06324c3
|
After Width: | Height: | Size: 26 KiB |
@ -0,0 +1,12 @@ |
||||
#version 330 core |
||||
out vec4 FragColor; |
||||
|
||||
in vec2 TexCoord; |
||||
|
||||
// texture sampler |
||||
uniform sampler2D texture1; |
||||
|
||||
void main() |
||||
{ |
||||
FragColor = texture(texture1, TexCoord); |
||||
} |
||||
@ -0,0 +1,11 @@ |
||||
#version 330 core |
||||
layout (location = 0) in vec3 aPos; |
||||
layout (location = 1) in vec2 aTexCoord; |
||||
|
||||
out vec2 TexCoord; |
||||
|
||||
void main() |
||||
{ |
||||
gl_Position = vec4(aPos, 1.0); |
||||
TexCoord = aTexCoord; |
||||
} |
||||
@ -0,0 +1,83 @@ |
||||
#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); |
||||
} |
||||
@ -0,0 +1,25 @@ |
||||
#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
|
||||
}; |
||||
|
||||
Image(); |
||||
void load(Node& node); |
||||
void render(Node& node, float xpos, float ypos, float w, float h); |
||||
}; |
||||
Loading…
Reference in new issue