#ifndef GLAD #define GLAD #include #endif #include #include #include #include #include #include #include #include "block.hpp" #include "chunk.hpp" unsigned char* loadImage(const char* fileName, int* width, int* height, int* numberOfChannels){ png_image image; //Standard practice when working with libpng image.version= PNG_IMAGE_VERSION; image.opaque=NULL; png_image_begin_read_from_file(&image,fileName); //Error handling switch(image.warning_or_error){ case 0: break; case 1: printf("Warning while reading %s: %s\n",fileName,image.message); break; case 2: case 3: printf("Error while reading %s: %s\n",fileName, image.message); return(NULL); } #define FORMAT PNG_FORMAT_RGBA image.format = FORMAT; //The amount of memory one row of the image takes up size_t row_stride = PNG_IMAGE_SAMPLE_CHANNELS(FORMAT)*image.width; //create the buffer unsigned char* buffer = (unsigned char*)malloc(row_stride*image.height); //Reads the image and stores it in buffer png_image_finish_read(&image, NULL, buffer, row_stride, NULL); *width = image.width; *height = image.height; *numberOfChannels = PNG_IMAGE_SAMPLE_CHANNELS(FORMAT); png_image_free(&image); return(buffer); } unsigned int createTexture(const char* pngFile){ int width,height,nrChannels; unsigned char* data = loadImage(pngFile, &width,&height,&nrChannels); unsigned int texture; glGenTextures(1,&texture); glBindTexture(GL_TEXTURE_2D,texture); //This makes sure the texture is pixelated and not blurry glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); //Sends the bitmap to the GPU glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,data); free(data); return(texture); }