added png serialization directly from memory, fixed trackballcamera movement

This commit is contained in:
Anselme FRANÇOIS 2016-07-14 22:34:07 +02:00
parent cfa3859cba
commit 1cca1287ee
4 changed files with 108 additions and 10 deletions

View File

@ -2,10 +2,31 @@
#include <glm/gtc/noise.hpp> #include <glm/gtc/noise.hpp>
#include "lodepng.h" #include "lodepng.h"
Image::Image(int myDepth, int myWidth, int myHeight, float frequency, float amplitude) : std::string getNameFromFilename(const std::string &filename)
{
int end;
for(end = filename.size()-1; end > 0; --end)
{
if(filename[end] == '.')
break;
}
int start;
for(start = end; start > 0; --start)
{
if(filename[start] == '/' || filename[start] == '\\')
{
++start;
break;
}
}
return filename.substr(start, end-start);
}
Image::Image(const std::string &name, int myDepth, int myWidth, int myHeight, float frequency, float amplitude) :
width(myWidth), width(myWidth),
height(myHeight), height(myHeight),
depth(myDepth) depth(myDepth),
m_name(name)
{ {
allocate(width * height * (depth/8)); allocate(width * height * (depth/8));
unsigned char* data = pixels.data(); unsigned char* data = pixels.data();
@ -52,6 +73,20 @@ Image::Image(const std::string &pngFile)
} }
} }
Image::Image(const unsigned char *pngPtr, size_t size)
{
unsigned int w, h;
unsigned error = lodepng::decode(pixels, w, h, pngPtr, size);
if(error)
fprintf(stderr, lodepng_error_text(error));
else
{
width = w;
height = h;
depth = pixels.size()*8/(width*height);
}
}
bool Image::save(const std::string &filename) bool Image::save(const std::string &filename)
{ {
LodePNGColorType colorType; LodePNGColorType colorType;
@ -62,8 +97,40 @@ bool Image::save(const std::string &filename)
case 8: colorType = LCT_GREY; break; case 8: colorType = LCT_GREY; break;
default: return false; break; default: return false; break;
} }
unsigned error = lodepng::encode(filename, pixels.data(), width, height, colorType, 8); unsigned error = lodepng::encode(filename, pixels, width, height, colorType, 8);
if(error) if(error)
fprintf(stderr, lodepng_error_text(error)); fprintf(stderr, lodepng_error_text(error));
return error != 0; return error != 0;
} }
bool Image::save(std::vector<unsigned char> &out)
{
LodePNGColorType colorType;
switch(depth)
{
case 32: colorType = LCT_RGBA; break;
case 24: colorType = LCT_RGB; break;
case 8: colorType = LCT_GREY; break;
default: return false; break;
}
unsigned error = lodepng::encode(out, pixels, width, height, colorType, 8);
if(error)
fprintf(stderr, lodepng_error_text(error));
return error != 0;
}
TexturePack::TexturePack(const std::string &filename)
{
m_name = getNameFromFilename(filename);
std::FILE *file = std::fopen(filename.c_str(), "r");
if(file == NULL)
return;
}
bool TexturePack::save(const std::string &path)
{
std::string filename = path + '/' + m_name + ".tpk";
std::FILE *file = std::fopen(filename.c_str(), "w");
if(file == NULL)
return false;
}

View File

@ -9,17 +9,35 @@ struct Image
int width; int width;
int height; int height;
int depth; int depth;
std::string m_name;
std::vector<unsigned char> pixels; std::vector<unsigned char> pixels;
Image() : width(0), height(0), depth(8) {} Image() : width(0), height(0), depth(8) {}
Image(int myDepth, int myWidth, int myHeight, float frequency, float amplitude); Image(const std::string &name, int myDepth, int myWidth, int myHeight, float frequency, float amplitude);
Image(const std::string &pngFile);
void allocate(int size) void allocate(int size)
{ pixels.resize(size); } { pixels.resize(size); }
/*
* PNG serialization
*/
Image(const std::string &pngFile);
Image(const unsigned char *pngPtr, size_t size);
bool save(const std::string &filename);
bool save(std::vector<unsigned char> &out);
};
struct TexturePack
{
std::string m_name;
std::vector<Image> m_images;
TexturePack(std::string name) : m_name(name) {}
TexturePack(const std::string &filename);
bool save(const std::string &filename); bool save(const std::string &filename);
}; };

View File

@ -24,10 +24,22 @@ void TrackBallCamera::rotateCamera(float dx, float dy)
void TrackBallCamera::moveCamera(float dx, float dy) void TrackBallCamera::moveCamera(float dx, float dy)
{ {
glm::mat4 inverseMVP = glm::inverse(m_projection * m_view); // this is absolutely not efficient, but it works
glm::vec4 diff = inverseMVP * glm::vec4(dx*m_dist*DEFAULT_MOVE_SPEED, dy*m_dist*DEFAULT_MOVE_SPEED, 0, 1); // you can try to optimize it, but please don't break it
m_center += glm::vec3(diff); glm::vec4 screenPos(m_center, 1);
computeView(); glm::mat4 MVP = m_projection * m_view;
glm::mat4 inverseMVP = glm::inverse(MVP);
screenPos = MVP * screenPos;
glm::vec4 pos(dx, -dy, (m_dist/m_far)*2 - 1, 1);
pos *= screenPos.w;
glm::vec4 diff = (inverseMVP * glm::vec4(0, 0, pos.z, pos.w)) - (inverseMVP * pos);
moveCamera(glm::vec3(diff));
}
void TrackBallCamera::moveCamera(const glm::vec3 &diff)
{
m_center += diff;
computeView();
} }
void TrackBallCamera::reset() void TrackBallCamera::reset()

View File

@ -20,6 +20,7 @@ public:
void rotateCamera(float dx, float dy); void rotateCamera(float dx, float dy);
void moveCamera(float dx, float dy); void moveCamera(float dx, float dy);
void moveCamera(const glm::vec3 &diff);
void zoom(int nbScrolls); void zoom(int nbScrolls);
void reset(); void reset();
glm::vec3 getDefaultPxInfo(); glm::vec3 getDefaultPxInfo();