Commit 28241811 authored by Robert Menzel's avatar Robert Menzel

added function to flip texture data for OpenGL coordinate system

parent 36bf0a8f
......@@ -34,7 +34,7 @@ public:
height(0),
depth(0),
format(GL_RGBA),
type(GL_UNSIGNED_BYTE),
mType(GL_UNSIGNED_BYTE),
paddingBytesPerRow(0)
{}
virtual ~TextureData(void)
......@@ -47,12 +47,26 @@ public:
// ========================================================================================================= \/
public:
GLubyte* getData (void) const { return pData; }
//! width in pixel
GLsizei getWidth (void) const { return width; }
//! height in pixel
GLsizei getHeight (void) const { return height; }
//! depth in pixel
GLsizei getDepth (void) const { return depth; }
//! channels etc. e.g. GL_RGB
GLenum getFormat (void) const { return format; }
GLenum getType (void) const { return type; }
//! data type (e.g. GL_BYTE)
GLenum getType (void) const { return mType; }
//! each line can have a few bytes of padding, number of bytes is returned
GLsizei getPadding (void) const { return paddingBytesPerRow; }
//! in bytes
glm::uvec3 getSize (void) const { return glm::uvec3( width, height, depth ); }
//! the byte alignment of each pixel row, e.g. 1,2,4,8 (bytes)
......@@ -60,6 +74,9 @@ public:
//! 1,2,3 or 4 based on format
GLsizei getNumberOfChannels() const;
//! flips the image horizontally as some image formats have a different coordinate system as OpenGL has.
void flipHorizontally();
// ========================================================================================================= \/
// ================================================================================================= SETTERS \/
// ========================================================================================================= \/
......@@ -69,7 +86,7 @@ public:
void setHeight(GLsizei _height) { height = _height; }
void setDepth (GLsizei _depth) { depth = _depth; }
void setFormat(GLenum _format) { format = _format; }
void setType (GLenum _type) { type = _type; }
void setType (GLenum _type) { mType = _type; }
void setPadding(GLsizei _padding){ paddingBytesPerRow = _padding; }
void setSize (const glm::uvec3 &_size) { width = _size.x; height = _size.y; depth = _size.z; }
......@@ -77,12 +94,13 @@ public:
// ================================================================================================== FIELDS \/
// ========================================================================================================= \/
private:
size_t getBytesPerScanline() const;
GLubyte* pData;
GLsizei width;
GLsizei height;
GLsizei depth;
GLenum format; // channel types and count
GLenum type; // data type
GLenum mType; // data type
GLsizei paddingBytesPerRow; // number of padding bytes added per row: glReadPixel can read with padding and
// some image writers support/need this as well (e.g. QT)
};
......
......@@ -10,9 +10,10 @@
using namespace ACGL;
using namespace ACGL::OpenGL;
GLsizei TextureData::getPackAlignment()const {
GLsizei TextureData::getPackAlignment() const
{
size_t dataAlignment = Utils::pointerAlignment( pData );
size_t rowAlignment = Utils::pointerAlignment( pData+(width*getNumberOfChannels() + paddingBytesPerRow) );
size_t rowAlignment = Utils::pointerAlignment( pData + getBytesPerScanline() );
return std::min( dataAlignment, rowAlignment ); //minimum of the data and the begining of the second row
}
......@@ -22,3 +23,29 @@ GLsizei TextureData::getNumberOfChannels() const
{
return ACGL::OpenGL::getNumberOfChannels( format );
}
void TextureData::flipHorizontally()
{
size_t scanlineInBytes = getBytesPerScanline();
GLubyte *tmpScanLine = new GLubyte[ scanlineInBytes ];
for (GLsizei line = 0; line < height/2; ++line) {
size_t topLine = line;
size_t bottomLine = height - line - 1;
void *topLinePtr = pData + topLine*scanlineInBytes;
void *bottomLinePtr = pData + bottomLine*scanlineInBytes;
memcpy( tmpScanLine, topLinePtr, scanlineInBytes ); // top -> tmp
memcpy( topLinePtr, bottomLinePtr, scanlineInBytes ); // bottom -> top
memcpy( bottomLinePtr, tmpScanLine, scanlineInBytes ); // tmp -> bottom
}
delete[] tmpScanLine;
}
size_t TextureData::getBytesPerScanline() const
{
return width*getNumberOfChannels()*getGLTypeSize(mType) + paddingBytesPerRow;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment