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.
 
 
 
 

132 lines
4.9 KiB

#include "ImageComponent.h"
#include <cmath>
#include <iostream>
#include <cstring>
#include "../../parsers/images/netpbm/pnm.h"
ImageComponent::ImageComponent(std::string filename, const float rawX, const float rawY, const float rawWidth, const float rawHeight, const int passedWindowWidth, const int passedWindowHeight) : BoxComponent(rawX, rawY, rawWidth, rawHeight, 0x00000000, passedWindowWidth, passedWindowHeight) {
//std::cout << "ImageComponent::ImageComponent" << std::endl;
windowWidth = passedWindowWidth;
windowHeight = passedWindowHeight;
x = rawX;
y = rawY;
width = rawWidth;
height = rawHeight;
RGBAPNMObject *anime = readPPM(filename.c_str());
unsigned int loadWidth = 1024, loadHeight = 1024;
if (!anime) {
std::cout << "Can't set up image component, couldn't read " << filename << std::endl;
} else {
loadWidth = anime->width;
loadHeight = anime->height;
}
if (loadWidth > 1024 || loadHeight > 1024) {
std::cout << "Can't set up image component, " << filename << " resolution too large " << std::endl;
loadWidth = 1024;
loadHeight = 1024;
tlsf_free(anime->m_Ptr);
delete anime;
anime = nullptr;
}
std::cout << "loading " << filename << " at " << loadWidth << "," << loadHeight << std::endl;
/*
response->textureWidth = pow(2, ceil(log(response->width) / log(2)));
response->textureHeight = pow(2, ceil(log(response->height) / log(2)));
*/
for (unsigned int py = 0; py < loadHeight; py++) {
for (unsigned int px = 0; px < loadWidth; px++) {
if (anime) {
for (unsigned int i = 0; i < 4; i++) {
//data[1023 - py][px][i] = anime.pixel_data[((px * 4) + (py * 4 * 1024)) + i];
data[loadWidth - 1 - py][px][i] = static_cast<unsigned char>(anime->m_Ptr[((px * 4) + (py * 4 * loadWidth)) + i]);
}
} else {
data[loadWidth - 1 - py][px][3] = 0x00; // just make it all transparent for now
}
}
}
float vx = rawX;
float vy = rawY;
float vWidth = rawWidth;
float vHeight = rawHeight;
pointToViewport(vx, vy);
distanceToViewport(vWidth, vHeight);
vertices[(0 * 5) + 0] = vx;
vertices[(0 * 5) + 1] = vy + vHeight;
vertices[(1 * 5) + 0] = vx + vWidth;
vertices[(1 * 5) + 1] = vy + vHeight;
vertices[(2 * 5) + 0] = vx + vWidth;
vertices[(2 * 5) + 1] = vy;
vertices[(3 * 5) + 0] = vx;
vertices[(3 * 5) + 1] = vy;
glGenVertexArrays(1, &vertexArrayObject);
glGenBuffers(1, &vertexBufferObject);
glGenBuffers(1, &elementBufferObject);
glBindVertexArray(vertexArrayObject);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferObject);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), nullptr);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), reinterpret_cast<void*>(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, static_cast<int>(loadWidth), static_cast<int>(loadHeight), 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
if (anime) {
tlsf_free(anime->m_Ptr);
delete anime;
}
}
// I guess we need this to change the image verticies
// FIXME: remove overrides for box and image componetns for resize
void ImageComponent::resize(const int passedWindowWidth, const int passedWindowHeight) {
//std::cout << "BoxComponent::resize" << std::endl;
windowWidth = passedWindowWidth;
windowHeight = passedWindowHeight;
//std::cout << "BoxComponent::resize - boundToPage " << boundToPage << std::endl;
// figure out new vertices
float vx = x;
float vy = y;
//std::cout << "placing box at " << (int)vx << "x" << (int)vy << " size: " << (int)rawWidth << "x" << (int)rawHeight << std::endl;
float vWidth = width;
float vHeight = height;
pointToViewport(vx, vy);
//std::cout << "vWidth before: " << (int)vWidth << std::endl;
distanceToViewport(vWidth, vHeight);
//std::cout << "vWidth after: " << (int)vWidth << std::endl;
//std::cout << "placing box at GL " << (int)vx << "x" << (int)vy << " size: " << (int)(vWidth*10000) << "x" << (int)(vHeight*10000) << std::endl;
vertices[(0 * 5) + 0] = vx;
vertices[(0 * 5) + 1] = vy + vHeight;
vertices[(1 * 5) + 0] = vx + vWidth;
vertices[(1 * 5) + 1] = vy + vHeight;
vertices[(2 * 5) + 0] = vx + vWidth;
vertices[(2 * 5) + 1] = vy;
vertices[(3 * 5) + 0] = vx;
vertices[(3 * 5) + 1] = vy;
verticesDirty = true;
}