Use devil to load OpenGL texture

xiaoxiao2021-03-06  1

OpenGL itself does not have a read function with an image file. D3DX has, for the iPicture interface to load the image, but TGA, MNG PNG and other images do not support. Requires other code to load TGA, etc. It is very inconvenient, the most memorable, the code is much problem, often can't successfully load some images. I have long wanted to transplant the dead library to Devil, but people are lazy, I have been waiting today, huh, huh. Finally made a simple package. The code is very chaotic, only read the image to establish OpenGL texture, there should be a wider use. For example, SaveImage et al. When you return, you will be a better use. The code here is only one file, it is a DLL, you can compile it yourself. DEVIL is currently 1.6.7, please go to http://www.sourceforge.net/projects/openil/ to download, explain that Devil is originally called OpenIL, a library that learns OpenGL, because some reasons, renamed Devil . / *** Source code for gltexture.h *** / # iFNDef _gl_texture_h_ # define _gl_texture_h_

#include #include

Typedef struct imagesize {int width; int bits;} imageSize, * pimageSize

Typedef unsigned char * imagedata;

Typedef struct; unsigned char * pdata; int bits; gluint format; int imageid;} DibtexData, * pdibtexdata DIBTEXDATA, * PDIBTEXDATA DIBTEXDATA,

#ifdef _Debug # Pragma Comment (Lib, "GltexTured.Lib") # Else # Pragma Comment (Lib, "Gltexture.lib" # ENDIF

TypeDef gluint gltexture;

extern "C" int FlowToPower2 (int w); extern "C" IMAGEDATA LoadBlankImage (IMAGESIZE size); extern "C" int SetImagePixel (unsigned char * image, int x, int y, IMAGESIZE size, unsigned char cl [4]) ; extern "C" int GetImagePixel (unsigned char * image, int x, int y, IMAGESIZE size, unsigned char cl [4]); extern "C" void SetKeyColorAlpha (unsigned char * image, IMAGESIZE size, unsigned char cl [4 ], int R; int N); extern "c" int RoundFourbyte (int N); extern "c" DibtexData loadtexdata (char * filename); Extern "C" DibtexData createDibtexData (int W, INTH); Extern "C" void freedibtexdata (dibtexdata dib); Extern "C" void settexalpha (Glubyte Color [], Glubyte Alpha, DibtexData Dib, Int r); Extern "C" Void SetTexpixel (int X, int y, GLubyte color [], DIBTEXDATA dib); extern "C" void GetTexPixel (int x, int y, GLubyte color [], DIBTEXDATA dib); extern "C" GLuint BuildTexture (char * filename); extern "C" GLuint BuildTransparencyTexture ( Char * filename, Glubyt e color [], GLubyte alpha, int r); extern "C" GLuint BuildTextureFromImage (unsigned char * image, IMAGESIZE size); extern "C" GLuint BuildTextureFromRGBImage (unsigned char * image, IMAGESIZE size);

#endif / ** Here is the .def file, ** / EXPORTS LoadBlankImage SetImagePixel GetImagePixel SetKeyColorAlpha IsPowerOfTwo RoundFourByte LoadTexData CreateDibTexData FreeDibTexData SetTexAlpha SetTexPixel GetTexPixel BuildTexture BuildTransparencyTexture BuildTextureFromImage BuildTextureFromRGBImage following is true the code / ** Source Code ForglTexture.cpp ** // / GLTEXTURE.CPP: Defines the entry point for the dll Application.//include "stdafx.h" #include #include #include #include #include usingspace std;

#include #include

#pragma comment (lib, "devil.lib") # Pragma Comment (Lib, "ilu.lib")

/ **** The following content is written in the header file **** / typedef struct imageSize {int width, height; int bits;

Typedef unsigned char * imagedata;

Typedef struct; unsigned char * pdata; int bits; gluint format; int imageid;} DibtexData, * pdibtexdata DIBTEXDATA, * PDIBTEXDATA DIBTEXDATA,

extern "C" IMAGEDATA LoadBlankImage (IMAGESIZE size); extern "C" int SetImagePixel (unsigned char * image, int x, int y, IMAGESIZE size, unsigned char cl [4]); extern "C" int GetImagePixel (unsigned char * Image, int x, int y, imageSize size, unsigned char cl [4]); Extern "C" void setKeyColoralpha (unsigned char * image, imagesize size, unsigned char cl [4], int r); extern "C" int IsPowerOfTwo (int n); extern "C" int RoundFourByte (int n); extern "C" DIBTEXDATA LoadTexData (char * filename); extern "C" DIBTEXDATA CreateDibTexData (int w, int h); extern "C" void FreeDibTexData ( DibtexData Dib); Extern "C" Void Settexalpha (Glubyte Alpha, DibtexData Dib, Int R); Extern "C" Void SetTexpixel (int X, int y, glubyte color [], dibtexdata dib); extern " C "void GetTexPixel (int x, int y, GLubyte color [], DIBTEXDATA dib); extern" C "GLuint BuildTexture (char * filename); extern" C "GLuint BuildTransparencyTexture (char * filename, GLubyte color [], GLubyte alpha , INT R); extern "C" GL Uint BuildtextureFromImage (unsigned char * image, imagesize size); Extern "C" gluint buildtextureFromrgbimage (unsigned char * image);

/ **** More content to be written to the header file **** / # pragma comment (lib, "opengl32.lib") # Pragma Comment (lib, "glu32.lib") Void FreeImageData (unsigned char * pdata) ; BOOL APIENTRY DllMain (HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {static bool ilIsInit = false; if (ul_reason_for_call == DLL_PROCESS_ATTACH) {if (ilIsInit == false) {// Needed to initialize DevIL ilInit ();. ilIsInit = True;}}

IF (ul_reason_for_call == dll_process_detach) {i (ilisinit == true) {// Needed to Initialize Devil. Ilshutdown (); ilisinit = false;}} Return True;}} Return TRUE;

EXTERN "C" Int Flowtopower2 (int W) {return 1 << (int) FLOOR ((Double) W) / log (2.0F)) 0.5F);}

Extern "C" DibtexData Loadtexdata (Char * filename) {int width = 0; int bpp = 0; int format = 0; int DEPTH = 0;

INT neww = 0; int newh = 0;

Glubyte * prgba = null; iluint imageid;

IlgenImages; ilbindimage (imageid); ilboolean isloaded = IlloadImage (filename); if (! isloaded) {goto exit_entry;}

Width = ilGetInteger (IL_IMAGE_WIDTH); Height = ilGetInteger (IL_IMAGE_HEIGHT); Bpp = ilGetInteger (IL_IMAGE_BPP); Format = ilGetInteger (IL_IMAGE_FORMAT); Depth = ilGetInteger (IL_IMAGE_DEPTH);

Neww = flowtopower2 (width); newh = flowtopower2 (Height);

IF (neww! = width || new! = height) {ipuscale (neww, new, depth);} if (format! = IL_RGBA) {ilconvertimage (il_rgba, ilgetinteger (il_image_type);}

PRGBA = ILGETDATA (); DibTexData Dib; exit_entry: // Texture data has been created, now you can save the size and return Dib.Height = new; dib.width = neww; dib.pdata = prgba; dib.bits = 32; Dib.format = GL_RGBA; Dib.ImageId = ImageId; return dib;} extern "c" Dibtexdata createDibtexdata (int W, int h) {dibtexdata pt; int size = w * h * 4; pt.height = h; PT. Width = W; imageSize isize; isize.height = h; isize.width = W; isize.bits = 32; pt.pdata = loadingblankImage (isize); pt.imageId = -1;

Return Pt;}

extern "C" void FreeDibTexData (DIBTEXDATA dib) {if (ilIsImage (dib.ImageID)) {ilDeleteImages (1, (const ILuint *) & dib.ImageID); return;} if (dib.pdata) {FreeImageData (dib.pdata Return;}}

Extern "C" void settexalpha (Glubyte Alpha, DibtexData Dib, Int r) {int size = dib.height * dib.width * 4; for (int i = 0; i

Extern "C" Void SetTexpixel (int X, int y, glubyte color [], dibtexdata dib) {glubyte * pstart = dib.pdata (dib.height-1-y) * dib.width * 4; pstart = x * 4;

PStart [0] = color [2]; // r pstart [1] = color [1]; // g Pstart [2] = color [0]; // b Pstart [3] = color [3]; / / a

} extern "c" void gettexpixel (int X, int y, glubyte color [], dibtexdata dib) {glubyte * pstart = dib.pdata (dib.height-1-y) * dib.width * 4; pstart = x * 4;

Color [0] = pstart [2]; // r color [1] = pstart [1]; // g color [2] = pstart [0]; // b color [3] = pstart [3]; / / a

} EXTERN "C" Gluint BuildTexture (Char * filename) {

GLubyte * prgba; ILuint ImageID; ilGenImages (1, & ImageID); ilBindImage (ImageID); ILboolean IsLoaded = ilLoadImage (filename); if (! IsLoaded) return 0; int Width = ilGetInteger (IL_IMAGE_WIDTH); int Height = ilGetInteger (IL_IMAGE_HEIGHT) ; Int bpp = ilgetinteger (IL_IMAGE_BP); int format = ilgetinteger (IL_IMAGE_FORMAT); int Depth = ilgetinteger (IL_IMAGE_DEPTH);

INT neww = flowtopower2; int newh = flowtopower2 (Height);

IF (neww! = width || new! = height) {ivuscale (neww, newh, defth);

IF (Format! = IL_RGBA) {ilconvertimage (il_rgba, ilgetinteger (il_image_type);}

PRGBA = Ilgetdata (); imageSize size; size.bits = 32; size.width = neww; size.height = new;

IF (prgba == 0) {ildeleteimages; return 0;} Gluint T = BuildTextureFromImage (prgba, size); ildeleteimages; Return T;}

Extern "C" Gluint BuildTransparencyTexture (Char * FileName, Glubyte Color [], Glubyte Alpha, INT R) {

Gluint Texture;

Dibtexdata dib = loadingTexData (filename); if (dib.pdata == null) return 0;

GlGentexTures (1, & texture); GlbindTexture (GL_Texture_2D, Texture);

Settexalpha (Color, Alpha, DIB, R);

{GLTexImage2D (GL_Texture_2D, 0, 4, Dib.Width, Dib.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, DIB.PDATA);

glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glEnable (GL_TEXTURE_2D);

FREEDIBTEXDATA (DIB);

Return Texture;

} Extern "C" GLuint BuildTextureFromImage (unsigned char * image, IMAGESIZE size) {GLuint texture; glGenTextures (1, & texture); glBindTexture (GL_TEXTURE_2D, texture); if (size.bits == 32) {glTexImage2D (GL_TEXTURE_2D, 0, 4, size.width, size.height, 0, gl_rgba, gl_unsigned_byte, image);} if (size.bits == 24) {glteximage2d (GL_Texture_2D, 0, 4, size.width, size.height, 0, gl_rgb, GL_UNSIGNED_BYTE, image);} glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glEnable (GL_TEXTURE_2D);

Return Texture;

EXTERN "C" gluint buildtexturefromrgbimage (unsigned char * image) {gluint texture; glGENTEXTURES (1, & texture); glbindtexture (GL_Texture_2d, Texture);

if (size.bits == 32) {glTexImage2D (GL_TEXTURE_2D, 0,4, size.width, size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);} if (size.bits == 24) {glTexImage2D (GL_TEXTURE_2D, 0,4, size.width, size.height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);} glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); GLENABLE (GL_TEXTURE_2D);

Return Texture;

/ ************************************************** ************** Detects a number of squares of the number of 2: n For the digital output to be detected: 0 is the square of the number of 2. 1 is a 2 degree of square ************************************************** ********************* / EXTERN "C" Int ispoweroftwo (int N) {return! ((((N - 1) & n)! = 0); }

/ ************************************************** ************* Adjusts a number to be 4 **************************************** *********************************** * C "INT ROUNDFOURBYTE (INT N) {IF ((n%) 4) == 0) RETURN N; Else Return (N 4- (N% 4));} extern "c" unsigned char * loadblankImage (size.bits! = 24 && size.bits! = 32) Return null; int b_len = size.height * size.width * size.bits / 8;

Unsigned char * pdata = new unsigned char [b_len]; MEMSET ((void *) PDATA, 0, B_LEN); if (size.bits == 32) {for (int i = 0; i

Extern "C" Int setImagepixel (unsigned char * image, int x, int y, imageSize size, unsigned char cl [4]) {if (image == null) return 0; int bpp = size.bits / 8; unsigned char * Pixel = & image [bpp * size.width * y x * bpp];

IF (BPP == 3) {Pixel [0] = CL [0]; Pixel [1] = CL [1]; Pixel [2] = CL [2]; RETURN 1;} IF (bpp == 4) { Pixel [0] = CL [0]; Pixel [1] = CL [1]; Pixel [2] = CL [2]; Pixel [3] = CL [3]; Return 1;} return 0;}

Extern "C" int GetImagePixel (unsigned char * image, int x, int y, imageSize size, unsigned char cl [4]) {if (image == null) Return 0;

INT bpp = size.bits / 8; unsigned char * pixel = & image [bpp * size.width * y x * bpp];

IF (BPP == 3) {CL [0] = Pixel [0]; CL [1] = Pixel [1]; Cl [2] = Pixel [2]; Return 1;} IF (bpp == 4) { CL [0] = Pixel [0]; CL [1] = Pixel [1]; Cl [2] = Pixel [2]; Cl [3] = Pixel [3]; return 1;} return 0;} extern " C "Void SetKeyColoralpha (Unsigned Char * Image, ImageSize Size, Unsigned Char Cl [4], INT R) {

IF (image == null) return; for (int y = 0; y

Void FreeImageData (unsigned char * pdata) {if (pdata) delete [] PDATA;

转载请注明原文地址: http://www.9cbs.com/read-68194.html

New Post(0)