Added graphics/ImageData class
This commit is contained in:
parent
a91fbe7166
commit
ffd3e3f852
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
|
|
||||||
|
#include "../graphics/ImageData.h"
|
||||||
#include "../graphics/Texture.h"
|
#include "../graphics/Texture.h"
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
@ -11,7 +13,7 @@
|
|||||||
#ifdef LIBPNG
|
#ifdef LIBPNG
|
||||||
#include <png.h>
|
#include <png.h>
|
||||||
|
|
||||||
int _png_load(const char* file, int* width, int* height){
|
ImageData* _png_load(const char* file, int* width, int* height){
|
||||||
FILE *f;
|
FILE *f;
|
||||||
int is_png, bit_depth, color_type, row_bytes;
|
int is_png, bit_depth, color_type, row_bytes;
|
||||||
png_infop info_ptr, end_info;
|
png_infop info_ptr, end_info;
|
||||||
@ -19,42 +21,40 @@ int _png_load(const char* file, int* width, int* height){
|
|||||||
png_byte header[8], *image_data;
|
png_byte header[8], *image_data;
|
||||||
png_bytepp row_pointers;
|
png_bytepp row_pointers;
|
||||||
png_structp png_ptr;
|
png_structp png_ptr;
|
||||||
GLuint texture;
|
|
||||||
int alpha;
|
|
||||||
|
|
||||||
if ( !( f = fopen(file, "r" ) ) ) {
|
if (!( f = fopen(file, "r" ) ) ) {
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
fread( header, 1, 8, f );
|
fread( header, 1, 8, f );
|
||||||
is_png = !png_sig_cmp( header, 0, 8 );
|
is_png = !png_sig_cmp( header, 0, 8 );
|
||||||
if ( !is_png ) {
|
if ( !is_png ) {
|
||||||
fclose( f );
|
fclose( f );
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL,
|
png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL,
|
||||||
NULL, NULL );
|
NULL, NULL );
|
||||||
if ( !png_ptr ) {
|
if ( !png_ptr ) {
|
||||||
fclose( f );
|
fclose( f );
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
info_ptr = png_create_info_struct( png_ptr );
|
info_ptr = png_create_info_struct( png_ptr );
|
||||||
if ( !info_ptr ) {
|
if ( !info_ptr ) {
|
||||||
png_destroy_read_struct( &png_ptr, (png_infopp) NULL,
|
png_destroy_read_struct( &png_ptr, (png_infopp) NULL,
|
||||||
(png_infopp) NULL );
|
(png_infopp) NULL );
|
||||||
fclose( f );
|
fclose( f );
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
end_info = png_create_info_struct( png_ptr );
|
end_info = png_create_info_struct( png_ptr );
|
||||||
if ( !end_info ) {
|
if ( !end_info ) {
|
||||||
png_destroy_read_struct( &png_ptr, (png_infopp) NULL,
|
png_destroy_read_struct( &png_ptr, (png_infopp) NULL,
|
||||||
(png_infopp) NULL );
|
(png_infopp) NULL );
|
||||||
fclose( f );
|
fclose( f );
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
if ( setjmp( png_jmpbuf( png_ptr ) ) ) {
|
if ( setjmp( png_jmpbuf( png_ptr ) ) ) {
|
||||||
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
|
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
|
||||||
fclose( f );
|
fclose( f );
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
png_init_io( png_ptr, f );
|
png_init_io( png_ptr, f );
|
||||||
png_set_sig_bytes( png_ptr, 8 );
|
png_set_sig_bytes( png_ptr, 8 );
|
||||||
@ -65,56 +65,43 @@ int _png_load(const char* file, int* width, int* height){
|
|||||||
*height = t_height;
|
*height = t_height;
|
||||||
png_read_update_info( png_ptr, info_ptr );
|
png_read_update_info( png_ptr, info_ptr );
|
||||||
row_bytes = png_get_rowbytes( png_ptr, info_ptr );
|
row_bytes = png_get_rowbytes( png_ptr, info_ptr );
|
||||||
image_data = (png_bytep) malloc( row_bytes * t_height * sizeof(png_byte) );
|
image_data = new png_byte[row_bytes * t_height];
|
||||||
if ( !image_data ) {
|
if (!image_data) {
|
||||||
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
|
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
|
||||||
fclose( f );
|
fclose(f);
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
row_pointers = (png_bytepp) malloc( t_height * sizeof(png_bytep) );
|
row_pointers = (png_bytepp) malloc( t_height * sizeof(png_bytep) );
|
||||||
if ( !row_pointers ) {
|
if ( !row_pointers ) {
|
||||||
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
|
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
|
||||||
free( image_data );
|
delete[] image_data;
|
||||||
fclose( f );
|
fclose(f);
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
for (unsigned int i = 0; i < t_height; ++i ) {
|
for (unsigned int i = 0; i < t_height; ++i ) {
|
||||||
row_pointers[t_height - 1 - i] = image_data + i * row_bytes;
|
row_pointers[t_height - 1 - i] = image_data + i * row_bytes;
|
||||||
}
|
}
|
||||||
png_read_image( png_ptr, row_pointers );
|
png_read_image(png_ptr, row_pointers);
|
||||||
|
|
||||||
|
ImageFormat format;
|
||||||
switch ( png_get_color_type( png_ptr, info_ptr ) ) {
|
switch ( png_get_color_type( png_ptr, info_ptr ) ) {
|
||||||
case PNG_COLOR_TYPE_RGBA:
|
case PNG_COLOR_TYPE_RGBA:
|
||||||
alpha = GL_RGBA;
|
format = ImageFormat::rgba8888;
|
||||||
break;
|
break;
|
||||||
case PNG_COLOR_TYPE_RGB:
|
case PNG_COLOR_TYPE_RGB:
|
||||||
alpha = GL_RGB;
|
format = ImageFormat::rgb888;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
printf( "Color type %d not supported!\n",
|
printf( "Color type %d not supported!\n",
|
||||||
png_get_color_type( png_ptr, info_ptr ) );
|
png_get_color_type( png_ptr, info_ptr ) );
|
||||||
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
|
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
// configure second post-processing framebuffer
|
ImageData* image = new ImageData(format, *width, *height, (void*)image_data);
|
||||||
|
|
||||||
glGenTextures(1, &texture);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, texture);
|
|
||||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, t_width, t_height, 0,
|
|
||||||
alpha, GL_UNSIGNED_BYTE, (GLvoid *) image_data);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
||||||
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_MAX_LEVEL, 1);
|
|
||||||
glGenerateMipmap(GL_TEXTURE_2D);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
|
||||||
|
|
||||||
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
|
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
|
||||||
free( image_data );
|
|
||||||
free( row_pointers );
|
free( row_pointers );
|
||||||
fclose( f );
|
fclose( f );
|
||||||
return texture;
|
return image;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
#include <spng.h>
|
#include <spng.h>
|
||||||
@ -205,18 +192,8 @@ int _png_load(const char* file, int* pwidth, int* pheight){
|
|||||||
delete[] out;
|
delete[] out;
|
||||||
|
|
||||||
unsigned int texture;
|
unsigned int texture;
|
||||||
int alpha = GL_RGBA;
|
|
||||||
|
|
||||||
glGenTextures(1, &texture);
|
ImageData* image = new ImageData(ImageFormat::rgba8888, ihdr.width, ihdr.height, (void*)flipped);
|
||||||
glBindTexture(GL_TEXTURE_2D, texture);
|
|
||||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ihdr.width, ihdr.height, 0,
|
|
||||||
alpha, GL_UNSIGNED_BYTE, (GLvoid *) flipped);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
|
||||||
|
|
||||||
pwidth[0] = ihdr.width;
|
pwidth[0] = ihdr.width;
|
||||||
pheight[0] = ihdr.height;
|
pheight[0] = ihdr.height;
|
||||||
@ -224,17 +201,17 @@ int _png_load(const char* file, int* pwidth, int* pheight){
|
|||||||
spng_ctx_free(ctx);
|
spng_ctx_free(ctx);
|
||||||
delete[] pngbuf;
|
delete[] pngbuf;
|
||||||
|
|
||||||
return texture;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Texture* png::load_texture(std::string filename){
|
Texture* png::load_texture(std::string filename){
|
||||||
int width, height;
|
int width, height;
|
||||||
GLuint texture = _png_load(filename.c_str(), &width, &height);
|
ImageData* image = _png_load(filename.c_str(), &width, &height);
|
||||||
if (texture == 0){
|
if (image == nullptr){
|
||||||
std::cerr << "Could not load texture " << filename << std::endl;
|
std::cerr << "Could not load image " << filename << std::endl;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
return new Texture(texture, width, height);
|
return Texture::from(image);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,8 +4,10 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class Texture;
|
class Texture;
|
||||||
|
class ImageData;
|
||||||
|
|
||||||
namespace png {
|
namespace png {
|
||||||
|
extern ImageData load_image(std::string filename);
|
||||||
extern Texture* load_texture(std::string filename);
|
extern Texture* load_texture(std::string filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -133,4 +133,5 @@ Engine::~Engine() {
|
|||||||
std::cout << "-- shutting down" << std::endl;
|
std::cout << "-- shutting down" << std::endl;
|
||||||
delete assets;
|
delete assets;
|
||||||
Window::terminate();
|
Window::terminate();
|
||||||
|
std::cout << "-- engine finished" << std::endl;
|
||||||
}
|
}
|
||||||
@ -19,7 +19,7 @@ Batch2D::Batch2D(size_t capacity) : capacity(capacity), offset(0), color(1.0f, 1
|
|||||||
unsigned char pixels[] = {
|
unsigned char pixels[] = {
|
||||||
255, 255, 255, 255,
|
255, 255, 255, 255,
|
||||||
};
|
};
|
||||||
blank = new Texture(pixels, 1, 1);
|
blank = new Texture(pixels, 1, 1, GL_RGBA);
|
||||||
_texture = nullptr;
|
_texture = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -19,7 +19,7 @@ Batch3D::Batch3D(size_t capacity) : capacity(capacity), offset(0), color(1.0f, 1
|
|||||||
unsigned char pixels[] = {
|
unsigned char pixels[] = {
|
||||||
255, 255, 255, 255,
|
255, 255, 255, 255,
|
||||||
};
|
};
|
||||||
blank = new Texture(pixels, 1, 1);
|
blank = new Texture(pixels, 1, 1, GL_RGBA);
|
||||||
_texture = nullptr;
|
_texture = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
14
src/graphics/ImageData.cpp
Normal file
14
src/graphics/ImageData.cpp
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#include "ImageData.h"
|
||||||
|
|
||||||
|
ImageData::ImageData(ImageFormat format, uint width, uint height, void* data)
|
||||||
|
: format(format), width(width), height(height), data(data) {
|
||||||
|
}
|
||||||
|
|
||||||
|
ImageData::~ImageData() {
|
||||||
|
switch (format) {
|
||||||
|
case ImageFormat::rgb888:
|
||||||
|
case ImageFormat::rgba8888:
|
||||||
|
delete[] data;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
37
src/graphics/ImageData.h
Normal file
37
src/graphics/ImageData.h
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#ifndef GRAPHICS_IMAGE_DATA_H_
|
||||||
|
#define GRAPHICS_IMAGE_DATA_H_
|
||||||
|
|
||||||
|
#include "../typedefs.h"
|
||||||
|
|
||||||
|
enum class ImageFormat {
|
||||||
|
rgb888,
|
||||||
|
rgba8888
|
||||||
|
};
|
||||||
|
|
||||||
|
class ImageData {
|
||||||
|
ImageFormat format;
|
||||||
|
uint width;
|
||||||
|
uint height;
|
||||||
|
void* data;
|
||||||
|
public:
|
||||||
|
ImageData(ImageFormat format, uint width, uint height, void* data);
|
||||||
|
~ImageData();
|
||||||
|
|
||||||
|
void* getData() const {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
ImageFormat getFormat() const {
|
||||||
|
return format;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint getWidth() const {
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint getHeight() const {
|
||||||
|
return height;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // GRAPHICS_IMAGE_DATA_H_
|
||||||
@ -1,15 +1,18 @@
|
|||||||
#include "Texture.h"
|
#include "Texture.h"
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
#include "ImageData.h"
|
||||||
|
|
||||||
Texture::Texture(unsigned int id, int width, int height) : id(id), width(width), height(height) {
|
Texture::Texture(unsigned int id, int width, int height) : id(id), width(width), height(height) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Texture::Texture(unsigned char* data, int width, int height) : width(width), height(height) {
|
Texture::Texture(unsigned char* data, int width, int height, uint format) : width(width), height(height) {
|
||||||
glGenTextures(1, &id);
|
glGenTextures(1, &id);
|
||||||
glBindTexture(GL_TEXTURE_2D, id);
|
glBindTexture(GL_TEXTURE_2D, id);
|
||||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
|
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0,
|
||||||
GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *) data);
|
format, GL_UNSIGNED_BYTE, (GLvoid *) data);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
@ -21,7 +24,6 @@ Texture::~Texture() {
|
|||||||
|
|
||||||
void Texture::bind(){
|
void Texture::bind(){
|
||||||
glBindTexture(GL_TEXTURE_2D, id);
|
glBindTexture(GL_TEXTURE_2D, id);
|
||||||
// glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Texture::reload(unsigned char* data){
|
void Texture::reload(unsigned char* data){
|
||||||
@ -30,3 +32,15 @@ void Texture::reload(unsigned char* data){
|
|||||||
GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *) data);
|
GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *) data);
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Texture* Texture::from(const ImageData* image) {
|
||||||
|
uint format;
|
||||||
|
const void* data = image->getData();
|
||||||
|
switch (image->getFormat()) {
|
||||||
|
case ImageFormat::rgb888: format = GL_RGB; break;
|
||||||
|
case ImageFormat::rgba8888: format = GL_RGBA; break;
|
||||||
|
default:
|
||||||
|
throw std::runtime_error("unsupported image data format");
|
||||||
|
}
|
||||||
|
return new Texture((unsigned char*)data, image->getWidth(), image->getHeight(), format);
|
||||||
|
}
|
||||||
@ -3,17 +3,21 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
class ImageData;
|
||||||
|
|
||||||
class Texture {
|
class Texture {
|
||||||
public:
|
public:
|
||||||
unsigned int id;
|
unsigned int id;
|
||||||
int width;
|
int width;
|
||||||
int height;
|
int height;
|
||||||
Texture(unsigned int id, int width, int height);
|
Texture(unsigned int id, int width, int height);
|
||||||
Texture(unsigned char* data, int width, int height);
|
Texture(unsigned char* data, int width, int height, uint format);
|
||||||
~Texture();
|
~Texture();
|
||||||
|
|
||||||
void bind();
|
void bind();
|
||||||
void reload(unsigned char* data);
|
void reload(unsigned char* data);
|
||||||
|
|
||||||
|
static Texture* from(const ImageData* image);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* GRAPHICS_TEXTURE_H_ */
|
#endif /* GRAPHICS_TEXTURE_H_ */
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user