Some little modifications.

This commit is contained in:
Quentin BAZIN 2013-04-29 15:29:22 +02:00
parent e4dfdee93c
commit a5a9111661
17 changed files with 252 additions and 401 deletions

View File

@ -16,7 +16,7 @@ KubKraft=/home/quentin/Bureau/KubKraft CD=. filter=".gitignore *.c *.h *.diff *.
init.h
map.h
player.h
sdlglutils.h
texutils.h
types.h
}
source=source {
@ -28,6 +28,6 @@ KubKraft=/home/quentin/Bureau/KubKraft CD=. filter=".gitignore *.c *.h *.diff *.
main.cpp
map.cpp
player.cpp
sdlglutils.cpp
texutils.cpp
}
}

View File

@ -14,7 +14,7 @@ LDFLAGS := -g -Wl
#---------------------------------------------------------------------------------
# Any extra libraries you wish to link with your project
#---------------------------------------------------------------------------------
LIBS := -lGLEW -lGL -lGLU -lSDL -lXrandr
LIBS := -lGLEW -lGL -lGLU -lSDL -lXrandr -lpng
#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing

View File

@ -22,7 +22,7 @@
class Chunk {
public:
Chunk(int x, int y, int z, Textures textures);
Chunk(int x, int y, int z);
~Chunk();
void setSurroundingChunk(unsigned char face, Chunk* chunk);

View File

@ -20,15 +20,14 @@
#ifndef CONFIG_H
#define CONFIG_H
#define WIN_WIDTH 640
#define WIN_HEIGHT 480
#define WIN_WIDTH 800
#define WIN_HEIGHT 600
#define WIN_FOV 80
#define APP_LABEL "KubKraft"
#define NEAR 0.1
#define FAR 1000.0
#define VISION_ANGLE 70.0
#define DIST_NEAR 0.1
#define DIST_FAR 1000.
#define CHUNK_WIDTH 8
#define CHUNK_DEPTH 8

View File

@ -31,8 +31,6 @@ class Game {
void animate();
void draw();
void loadTextures();
void lockMouse();
void unlockMouse();
@ -46,8 +44,6 @@ class Game {
private:
bool m_cont;
bool m_paused;
Textures m_textures;
};
#endif // SCENE_H

View File

@ -33,10 +33,10 @@ typedef struct {
class Map {
public:
Map(u16 width, u16 depth, u16 height, Textures textures);
Map(u16 width, u16 depth, u16 height);
~Map();
void draw();
void render();
Chunk *findNearestChunk(float x, float y, float z);
@ -61,7 +61,7 @@ class Map {
u16 m_height;
u16 m_depth;
Textures m_textures;
GLuint m_texture;
u16 *m_map;

View File

@ -1,18 +0,0 @@
#ifndef SDLGLUTILS_H
#define SDLGLUTILS_H
#include <GL/gl.h>
#include <SDL/SDL.h>
#ifndef GL_CLAMP_TO_EDGE
#define GL_CLAMP_TO_EDGE 0x812F
#endif
GLuint loadTexture(const char * filename,bool useMipMap = true);
int takeScreenshot(const char * filename);
void drawAxis(double scale = 1);
int initFullScreen(unsigned int * width = NULL,unsigned int * height = NULL);
int XPMFromImage(const char * imagefile, const char * XPMfile);
SDL_Cursor * cursorFromXPM(const char * xpm[]);
#endif //SDLGLUTILS_H

7
include/texutils.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef TEXUTILS_H
#define TEXUTILS_H
GLuint loadTgaTexture(const char* filename);
GLuint loadPngTexture(const char* file_name, int* width = 0, int* height = 0);
#endif // TEXUTILS_H

View File

@ -36,7 +36,7 @@
using namespace std;
Chunk::Chunk(int x, int y, int z, Textures textures) {
Chunk::Chunk(int x, int y, int z) {
srand(time(NULL));
m_x = x;
@ -98,6 +98,11 @@ void Chunk::addCube(Cube *selectedCube, int type) {
int y = selectedCube->y();
int z = selectedCube->z();
/*if(x == 0) {
m_surroundingChunks[0]->addCube(m_surroundingChunks[0]->getCube(CHUNK_WIDTH - 1, y, z), type);
return;
}*/
if((selectedCube == NULL) || (selectedCube->selectedFace() == -1)) {
m_cubes[CUBE_POS(x, y, z)] = selectedCube;
return;

View File

@ -28,8 +28,6 @@
#include <GL/gl.h>
#include <GL/glu.h>
#include "sdlglutils.h"
#include "init.h"
#include "config.h"
#include "types.h"
@ -51,18 +49,10 @@ unsigned int Game::mapHeight = 8 << 3;
Game::Game() {
player = new Player(2, 2, 48, 90);
loadTextures();
map = new Map(mapWidth, mapDepth, mapHeight, m_textures);
map = new Map(mapWidth, mapDepth, mapHeight);
}
Game::~Game() {
// Deleting loaded textures
for(Textures::iterator element = m_textures.begin() ; element != m_textures.end() ; element++) {
glDeleteTextures(1, &element->second);
element->second = 0;
}
delete map;
delete player;
}
@ -206,8 +196,8 @@ void Game::draw() {
// Put camera
player->watch();
// Draw the map
map->draw();
// Display the map
map->render();
// HUD render
glPushMatrix();
@ -241,16 +231,6 @@ void Game::draw() {
SDL_GL_SwapBuffers();
}
void Game::loadTextures() {
// Load textures
m_textures["dirt"] = loadTexture("textures/dirt.bmp");
m_textures["grass"] = loadTexture("textures/grass.bmp");
m_textures["cobblestone"] = loadTexture("textures/cobblestone.bmp");
m_textures["stone"] = loadTexture("textures/stone.bmp");
m_textures["bedrock"] = loadTexture("textures/bedrock.bmp");
m_textures["textures"] = loadTexture("textures/textures.bmp");
}
void Game::lockMouse() {
SDL_WM_GrabInput(SDL_GRAB_ON);

View File

@ -65,7 +65,7 @@ void initOpenGL() {
glLoadIdentity();
// Visible area definition
gluPerspective(VISION_ANGLE, (GLdouble)WIN_WIDTH / (GLdouble)WIN_HEIGHT, NEAR, FAR);
gluPerspective(WIN_FOV, (GLdouble)WIN_WIDTH / (GLdouble)WIN_HEIGHT, DIST_NEAR, DIST_FAR);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

View File

@ -27,8 +27,6 @@
#include <SDL/SDL.h>
#include <GL/glew.h>
#include "sdlglutils.h"
#include "types.h"
#include "init.h"
#include "cube.h"

View File

@ -25,8 +25,9 @@
#include <cmath>
#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glew.h>
#include <png.h>
#include "types.h"
#include "config.h"
@ -37,6 +38,7 @@
#include "game.h"
#include "genutils.h"
#include "texutils.h"
using namespace std;
@ -44,12 +46,13 @@ Chunk *Map::currentChunk = NULL;
Chunk *Map::selectedChunk = NULL;
Cube *Map::selectedCube = NULL;
Map::Map(u16 width, u16 depth, u16 height, Textures textures) {
Map::Map(u16 width, u16 depth, u16 height) {
m_width = width;
m_depth = depth;
m_height = height;
m_textures = textures;
glGenTextures(1, &m_texture);
m_texture = loadPngTexture("textures/blocks.png");
m_map = (u16*)malloc(m_width * m_depth * m_height * sizeof(u16));
@ -92,7 +95,7 @@ Map::Map(u16 width, u16 depth, u16 height, Textures textures) {
for(u16 z = 0 ; z < m_height >> 3 ; z++) {
for(u16 y = 0 ; y < m_depth >> 3 ; y++) {
for(u16 x = 0 ; x < m_width >> 3 ; x++) {
m_chunks[CHUNK_POS(x, y, z)] = new Chunk(x << 3, y << 3, z << 3, m_textures);
m_chunks[CHUNK_POS(x, y, z)] = new Chunk(x << 3, y << 3, z << 3);
}
}
}
@ -146,9 +149,11 @@ Map::~Map() {
}
delete[] m_chunks;
glDeleteTextures(1, &m_texture);
}
void Map::draw() {
void Map::render() {
glColor3ub(0, 0, 0);
glBegin(GL_QUADS);
glVertex3f(0, 0, -0.01);
@ -158,7 +163,7 @@ void Map::draw() {
glEnd();
glColor3ub(255, 255, 255);
glBindTexture(GL_TEXTURE_2D, m_textures["textures"]);
glBindTexture(GL_TEXTURE_2D, m_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
@ -223,7 +228,7 @@ void Map::draw() {
}
Chunk *Map::findNearestChunk(float x, float y, float z) {
float distance = FAR;
float distance = DIST_FAR;
Chunk *chunk = NULL;
for(int i = 0 ; i < ((m_width >> 3) * (m_depth >> 3) * (m_height >> 3)); i++) {
@ -291,7 +296,7 @@ bool Map::intersectionLineCube(int cubeX, int cubeY, int cubeZ, vect3D lineOrigP
vect3D normal;
float k = 0;
float smallestK = FAR;
float smallestK = DIST_FAR;
s8 nearestFace = -1;
bool result = false;
@ -407,7 +412,7 @@ void Map::testCubes() {
directionVector.y = Game::player->pointTargetedy() - Game::player->y();
directionVector.z = Game::player->pointTargetedz() - Game::player->z();
float distance = FAR;
float distance = DIST_FAR;
Cube *cube = NULL;
int face = -1;
Chunk *chunk = NULL;

View File

@ -1,329 +0,0 @@
#include "sdlglutils.h"
#include <SDL/SDL.h>
#include <GL/glu.h>
SDL_Surface * flipSurface(SDL_Surface * surface);
GLuint loadTexture(const char * filename,bool useMipMap)
{
GLuint glID;
SDL_Surface * picture_surface = NULL;
SDL_Surface *gl_surface = NULL;
SDL_Surface * gl_fliped_surface = NULL;
Uint32 rmask, gmask, bmask, amask;
picture_surface = SDL_LoadBMP(filename);
if (picture_surface == NULL)
return 0;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
SDL_PixelFormat format = *(picture_surface->format);
format.BitsPerPixel = 32;
format.BytesPerPixel = 4;
format.Rmask = rmask;
format.Gmask = gmask;
format.Bmask = bmask;
format.Amask = amask;
gl_surface = SDL_ConvertSurface(picture_surface,&format,SDL_SWSURFACE);
gl_fliped_surface = flipSurface(gl_surface);
glGenTextures(1, &glID);
glBindTexture(GL_TEXTURE_2D, glID);
if (useMipMap)
{
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, gl_fliped_surface->w,
gl_fliped_surface->h, GL_RGBA,GL_UNSIGNED_BYTE,
gl_fliped_surface->pixels);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_LINEAR);
}
else
{
glTexImage2D(GL_TEXTURE_2D, 0, 4, gl_fliped_surface->w,
gl_fliped_surface->h, 0, GL_RGBA,GL_UNSIGNED_BYTE,
gl_fliped_surface->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
SDL_FreeSurface(gl_fliped_surface);
SDL_FreeSurface(gl_surface);
SDL_FreeSurface(picture_surface);
return glID;
}
int takeScreenshot(const char * filename)
{
GLint viewport[4];
Uint32 rmask, gmask, bmask, amask;
SDL_Surface * picture, * finalpicture;
glGetIntegerv(GL_VIEWPORT, viewport);
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
picture = SDL_CreateRGBSurface(SDL_SWSURFACE,viewport[2],viewport[3], 32,
rmask, gmask, bmask, amask);
SDL_LockSurface(picture);
glReadPixels(viewport[0],viewport[1],viewport[2],viewport[3],GL_RGBA,
GL_UNSIGNED_BYTE,picture->pixels);
SDL_UnlockSurface(picture);
finalpicture = flipSurface(picture);
if (SDL_SaveBMP(finalpicture, filename))
{
return -1;
}
SDL_FreeSurface(finalpicture);
SDL_FreeSurface(picture);
return 0;
}
SDL_Surface * flipSurface(SDL_Surface * surface)
{
int current_line,pitch;
SDL_Surface * fliped_surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
surface->w,surface->h,
surface->format->BitsPerPixel,
surface->format->Rmask,
surface->format->Gmask,
surface->format->Bmask,
surface->format->Amask);
SDL_LockSurface(surface);
SDL_LockSurface(fliped_surface);
pitch = surface->pitch;
for (current_line = 0; current_line < surface->h; current_line ++)
{
memcpy(&((unsigned char* )fliped_surface->pixels)[current_line*pitch],
&((unsigned char* )surface->pixels)[(surface->h - 1 -
current_line)*pitch],
pitch);
}
SDL_UnlockSurface(fliped_surface);
SDL_UnlockSurface(surface);
return fliped_surface;
}
void drawAxis(double scale)
{
glPushAttrib(GL_ALL_ATTRIB_BITS);
glPushMatrix();
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glEnable(GL_LINE_SMOOTH);
glLineWidth(2);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glScaled(scale,scale,scale);
glBegin(GL_LINES);
glColor3ub(255,0,0);
glVertex3i(0,0,0);
glVertex3i(1,0,0);
glColor3ub(0,255,0);
glVertex3i(0,0,0);
glVertex3i(0,1,0);
glColor3ub(0,0,255);
glVertex3i(0,0,0);
glVertex3i(0,0,1);
glEnd();
glPopMatrix();
glPopAttrib();
}
int initFullScreen(unsigned int * width,unsigned int * height)
{
SDL_Rect ** modes;
modes = SDL_ListModes(NULL,SDL_FULLSCREEN|SDL_OPENGL);
if ((modes == (SDL_Rect **)0)||(modes == (SDL_Rect **)-1))
return 0;
if (width != NULL)
*width = modes[0]->w;
if (height != NULL)
*height = modes[0]->h;
if (SDL_SetVideoMode(modes[0]->w,
modes[0]->h,
SDL_GetVideoInfo()->vfmt->BitsPerPixel,
SDL_FULLSCREEN|SDL_OPENGL) == NULL)
return -1;
else
{
return 0;
}
}
int XPMFromImage(const char * imagefile, const char * XPMfile)
{
SDL_Surface * image,*image32bits;
FILE * xpm;
Uint32 pixel;
Uint8 r,g,b,a;
int x,y;
unsigned int w;
char * xpm_name;
Uint32 rmask, gmask, bmask, amask;
image = SDL_LoadBMP(imagefile);
if (image == NULL)
return -1;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
image32bits = SDL_CreateRGBSurface(SDL_SWSURFACE,
image->w,image->h,
32,rmask, gmask, bmask, amask);
SDL_BlitSurface(image,NULL,image32bits,NULL);
SDL_FreeSurface(image);
xpm = fopen(XPMfile,"w");
xpm_name = (char*)malloc(strlen(imagefile)*sizeof(char));
strcpy(xpm_name,imagefile);
if (strchr(xpm_name,'.') != NULL)
*strchr(xpm_name,'.') = '\0';
fprintf(xpm,"const char *%s[] =\n",xpm_name);
free(xpm_name);
fprintf(xpm,"\t{\n");
fprintf(xpm,"\t\t/* width height num_colors chars_per_pixel */\n");
w = ((image->w%8) == 0)?image32bits->w:8*(image32bits->w/8+1);
fprintf(xpm,"\t\t\" %d %d 3 1 \",\n",w,image32bits->h);
fprintf(xpm,"\t\t/* colors */\n");
fprintf(xpm,"\t\t\"X c #000000\",\n");
fprintf(xpm,"\t\t\". c #ffffff\",\n");
fprintf(xpm,"\t\t\" c None\",\n");
fprintf(xpm,"\t\t/* pixels */\n");
SDL_LockSurface(image32bits);
for (y = 0; y < image32bits->h; y ++)
{
fprintf(xpm,"\t\t\"");
for (x = 0; x < image32bits->w ; x ++)
{
pixel = ((Uint32*)image32bits->pixels)[x+y*image32bits->pitch/4];
SDL_GetRGBA(pixel,image32bits->format,&r,&g,&b,&a);
if (a < 128)
fprintf(xpm," ");
else if ((r >= 128)||(g >= 128)||(b >= 128))
fprintf(xpm,".");
else
fprintf(xpm,"X");
}
for (x = image32bits->w ; (unsigned int)x < w ; x ++)
fprintf(xpm," ");
fprintf(xpm,"\",\n");
}
SDL_UnlockSurface(image32bits);
SDL_FreeSurface(image32bits);
fprintf(xpm,"\t\t\"0,0\"\n");
fprintf(xpm,"\t};\n");
return 0;
}
SDL_Cursor * cursorFromXPM(const char * xpm[])
{
int i, row, col;
int width, height;
Uint8 * data;
Uint8 * mask;
int hot_x, hot_y;
SDL_Cursor * cursor = NULL;
sscanf(xpm[0], "%d %d", &width, &height);
data = (Uint8*)calloc(width/8*height,sizeof(Uint8));
mask = (Uint8*)calloc(width/8*height,sizeof(Uint8));
i = -1;
for ( row=0; row<height; ++row )
{
for ( col=0; col<width; ++col )
{
if ( col % 8 )
{
data[i] <<= 1;
mask[i] <<= 1;
}
else
{
++i;
data[i] = mask[i] = 0;
}
switch (xpm[4+row][col])
{
case 'X':
data[i] |= 0x01;
mask[i] |= 0x01;
break;
case '.':
mask[i] |= 0x01;
break;
case ' ':
break;
}
}
}
sscanf(xpm[4+row], "%d,%d", &hot_x, &hot_y);
cursor = SDL_CreateCursor(data, mask, width, height, hot_x, hot_y);
free(data);
free(mask);
return cursor;
}

208
source/texutils.cpp Normal file
View File

@ -0,0 +1,208 @@
#include <GL/glew.h>
#include "texutils.h"
#include <cstdio>
#include <cstdlib>
#include <GL/gl.h>
#include <png.h>
struct struct_tgaHeader {
unsigned char identsize; // size of ID field that follows 18 byte header (0 usually)
unsigned char colourmaptype; // type of colour map 0=none, 1=has palette
unsigned char imagetype; // type of image 0=none,1=indexed,2=rgb,3=grey,+8=rle packed
unsigned short colourmapstart; // first colour map entry in palette
unsigned short colourmaplength; // number of colours in palette
unsigned char colourmapbits; // number of bits per palette entry 15,16,24,32
short xstart; // image x origin
short ystart; // image y origin
short width; // image width in pixels
short height; // image height in pixels
unsigned char bits; // image bits per pixel 8,16,24,32
unsigned char descriptor; // image descriptor bits (vh flip bits)
} __attribute__((packed));
typedef struct struct_tgaHeader t_tgaHeader;
GLuint loadTgaTexture(const char* filename)
{
FILE* tgaFile;
t_tgaHeader tgaHeader;
unsigned char* imageData = NULL;
tgaFile = fopen(filename, "rb");
if (tgaFile == NULL)
{
return 0;
}
fread(&tgaHeader, sizeof (t_tgaHeader), 1, tgaFile);
if ((tgaHeader.colourmaptype != 0) || (tgaHeader.imagetype != 2)
|| ((tgaHeader.bits != 24) && (tgaHeader.bits != 32)))
{
// 24-bit or 32-bit supported only
fclose(tgaFile);
return 0;
}
else
{
imageData = (unsigned char*)malloc(((tgaHeader.bits >> 3) * tgaHeader.width * tgaHeader.height) * sizeof (unsigned char));
fread(imageData, sizeof (unsigned char), (tgaHeader.bits >> 3) * tgaHeader.width * tgaHeader.height, tgaFile);
}
fclose(tgaFile);
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
GLint internalFormat = GL_RGBA;
GLint format = GL_BGRA;
if (tgaHeader.bits == 24)
{
internalFormat = GL_RGB;
format = GL_BGR;
}
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, tgaHeader.width, tgaHeader.height, 0, format, GL_UNSIGNED_BYTE, (void*)imageData);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
free(imageData);
return textureID;
}
GLuint loadPngTexture(const char* file_name, int* width, int* height)
{
png_byte header[8];
FILE *fp = fopen(file_name, "rb");
if (fp == 0)
{
perror(file_name);
return 0;
}
// read the header
fread(header, 1, 8, fp);
if (png_sig_cmp(header, 0, 8))
{
fprintf(stderr, "error: %s is not a PNG.\n", file_name);
fclose(fp);
return 0;
}
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
{
fprintf(stderr, "error: png_create_read_struct returned 0.\n");
fclose(fp);
return 0;
}
// create png info struct
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
fprintf(stderr, "error: png_create_info_struct returned 0.\n");
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
fclose(fp);
return 0;
}
// create png info struct
png_infop end_info = png_create_info_struct(png_ptr);
if (!end_info)
{
fprintf(stderr, "error: png_create_info_struct returned 0.\n");
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
fclose(fp);
return 0;
}
// the code in this if statement gets called if libpng encounters an error
if (setjmp(png_jmpbuf(png_ptr))) {
fprintf(stderr, "error from libpng\n");
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(fp);
return 0;
}
// init png reading
png_init_io(png_ptr, fp);
// let libpng know you already read the first 8 bytes
png_set_sig_bytes(png_ptr, 8);
// read all the info up to the image data
png_read_info(png_ptr, info_ptr);
// variables to pass to get info
int bit_depth, color_type;
png_uint_32 temp_width, temp_height;
// get info about png
png_get_IHDR(png_ptr, info_ptr, &temp_width, &temp_height, &bit_depth, &color_type,
NULL, NULL, NULL);
if (width){ *width = temp_width; }
if (height){ *height = temp_height; }
// Update the png info struct.
png_read_update_info(png_ptr, info_ptr);
// Row size in bytes.
int rowbytes = png_get_rowbytes(png_ptr, info_ptr);
// glTexImage2d requires rows to be 4-byte aligned
rowbytes += 3 - ((rowbytes-1) % 4);
// Allocate the image_data as a big block, to be given to opengl
png_byte * image_data;
image_data = (png_byte *) malloc(rowbytes * temp_height * sizeof(png_byte)+15);
if (image_data == NULL)
{
fprintf(stderr, "error: could not allocate memory for PNG image data\n");
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(fp);
return 0;
}
// row_pointers is for pointing to image_data for reading the png with libpng
png_bytep * row_pointers = (png_byte **) malloc(temp_height * sizeof(png_bytep));
if (row_pointers == NULL)
{
fprintf(stderr, "error: could not allocate memory for PNG row pointers\n");
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
free(image_data);
fclose(fp);
return 0;
}
// set the individual row_pointers to point at the correct offsets of image_data
unsigned int i;
for (i = 0; i < temp_height; i++)
{
row_pointers[temp_height - 1 - i] = image_data + i * rowbytes;
}
// read the png into image_data through row_pointers
png_read_image(png_ptr, row_pointers);
// Generate the OpenGL texture object
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, temp_width, temp_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// clean up
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
free(image_data);
free(row_pointers);
fclose(fp);
return texture;
}

BIN
textures/blocks.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 822 B