In the two-dimensional game, we almost do not open the wizard to draw this process, in addition to directly in OpenGL read the image and draw, we often use the texture to complete the process, the texture to the XY plane on the patch, to make two-dimensional game effect.
In this way, we can easily use OpenGL to provide us with some of the methods to perform the wizard's transformation, rather than using a large number of stickers to manually complete the transformation process, but also by adjusting the depth of information to determine the occlusion of the object, without having to think about the order of the drawing, because we know that In the two-dimensional world, who obscures who is related to the drawing order.
However, we also found that our elves are not always boxy, so the texture will be loaded into the background color, we want to remove the background color, a very good idea is to use a transparent (alpha) channel image, but unfortunately, OpenGL cannot directly support this format, So this method is not feasible.
Another common idea is this: we use a color to represent the background color, open the alpha test in OpenGL, set the texture image to RGBA format, and after reading this color, we manually set the alpha to 0, which is fully transparent, otherwise set to 1, that is, opaque. We can realize that this background color selection is required, it can not be the same as the image opaque part of RGB, otherwise they will be incorrectly recognized as transparent.
Here is an OpenGL-based mini-game I'm writing recently, using a transparent texture, where the rockets are temporarily looking for images to test the effects, and the pictures below are made by themselves. Because the rockets did not have special treatment, so we found that the middle part of it is also pierced, this is the code exists some defects, it requires us to special processing of the image to use.
Code will be released after completion, in order to demonstrate the transparent texture effect, I gave a separate test code:
The original image (Figure 1 from the network):
Use transparent texture after effect:
In this code, the background color is specified by the parameter.
Note that at the time of drawing, we draw the normal texture of the patch, and then uniformly draw the transparent texture of the patch, in the drawing of transparent texture, we need to set the depth test to read-only mode, and then change back to the original state, to prevent the process of destroying the depth of information.
Test.h
#pragma once#define glut_disable_atexit_hack #include "gl/glut. H " void Loadtex (int i, char *filename, gluint* texture);//General texture void Loadtex (int i, char *filename, gluint* Texture,unsi gned char* backgroundcolor);//Transparent texture
Texture.cpp
#define _crt_secure_no_warnings #include <stdio.h> #include <windows.h> #include " Test.h " #define BITMAP_ID 0x4d42 //read texture picture static unsigned char *loadbitmapfile (char *filename, Bitmapinfoheader *bitmapinfoheader) { file *fileptr; //file pointer bitmapfileheader bitmapfileheader; //bitmap file header unsigned char *bitmapimage; // Bitmap image Data int imageidx = 0; //Image location index unsigned char temprgb; //Exchange variables //Open file in "binary + read" mode Filename fileptr = fopen (filename, "RB"); if (fileptr = = NULL) {& nbsp; printf ("File not open\n"); return NULL; } //read into bitmap file diagram fread (& Bitmapfileheader, sizeof (Bitmapfileheader), 1, fileptr); //verify bitmap file &NBSP;IF (Bitmapfileheader.bftype! = bitmap_id) { fprintf (stderr, "Error in Loadbitmapfile:the file is not a bitmap file\n "); return null; }& nbsp; //Read bitmap information header fread (bitmapinfoheader, sizeof ( Bitmapinfoheader), 1, fileptr); //Move the file pointer to bitmap data fseek ( Fileptr, Bitmapfileheader.bfoffbits, Seek_set); //create enough memory for loading image data bitmapimage = new unsigned char[bitmapinfoheader->bisizeimage]; // Verify that the memory is created successfully if (!bitmapimage) { fprintf ( stderr, "Error in Loadbitmapfile:memory error\n"); return null; } //read into bitmap image data fread (bitmapImage, 1, Bitmapinfoheader->bisizeimage, Fileptr); //confirm read-in Success if ( BitmapImage = = NULL) { fprintf (stderr, "Error in Loadbitmapfile:memory error\n"); nbsp; return null; } //because the format saved in bitmap is BGR, The following exchanges R and B values, get RGB format for (imageidx = 0; Imageidx < bitmapinfoheader-> biSizeImage; Imageidx + = 3) { temprgb = bitmapimage[imageidx]; &nBsp bitmapimage[imageidx] = bitmapimage[imageidx + 2]; Bitmapimage[imageidx + 2] = temprgb; } //Close bitmap image file & Nbsp;fclose (fileptr); return bitmapImage;} Read texture pictures static unsigned char *loadbitmapfile (char *filename, Bitmapinfoheader *bitmapinfoheader, unsigned char* backgroundcolor) { file *fileptr; //file pointer & nbsp bitmapfileheader bitmapfileheader; //bitmap file header unsigned char *bitmapimage; //bitmap image Data int imageidx = 0; //Image location Index //open files in "binary + read" mode filename fileptr = fopen (filename, "RB"); if (fileptr = = NULL) { printf ("File not O pen\n "); return null; } //read in bitmap file diagram fread (&bitmapfileheader, sizeof (Bitmapfileheader), 1, fileptr); //Verify if BITMAP file if (bitmapfileheader.bftype! = bitmap_id) { & nbsp fprintf (stderr, "Error in Loadbitmapfile:the file was not a bitmap file\n"); &NBSP;&N bsp; return null; } //read bitmap information header fread (Bitmapinfoheader, sizeof (Bitmapinfoheader), 1, fileptr); //Move the file pointer to bitmap data fseek (fileptr, BITMAPFILEHEADER.BFOFFBITS, Seek_set); //create enough memory for loading image data bitmapimage = new unsigned char [bitmapinfoheader->bisizeimage]; //Verify that the memory is created successfully if (! BitmapImage) { fprintf (stderr, "Error in Loadbitmapfile:memory error\n"); & nbsp return null; } //read in bitmap image data fread (bitmapImage, 1, Bitmapinfoheader->bisizeimage, fileptr); //confirm read-in Success if (bitmapImage = = NULL) { fprintf (stderr, "Error In Loadbitmapfile:memory error\n "); return null; } unsigned char* bitmapdata; //Texture data bitmapdata = new unsigned char [BITMAPINFOHEADER->BISIZEIMAGE/3 * 4]; int COunt = 0; //add alpha channel for (imageidx = 0; Imageidx < bitmapinfoheader-> biSizeImage; Imageidx + = 3) { bitmapdata[count] = bitmapimage[imageidx + 2]; & nbsp; bitmapdata[count + 1] = Bitmapimage[imageidx + 1]; bitmapdata[ Count + 2] = bitmapimage[imageidx]; if (bitmapdata[count] = = backgroundcolor[0]& nbsp; && bitmapdata[count + 1] = = Backgroundcolor[1] & nbsp && Bitmapdata[count + 2] = = backgroundcolor[2]) { bitmapdata[count + 3] = 0; } else Bitmapdata[count + 3] = 255; count + = 4; & nbsp } //Close bitmap image file   fclose (fileptr); return BitmapData;} Functions loaded with Textures void Loadtex (int i, char *filename, gluint* texture) { bitmapinfoheader bitmapinfoheader; // Bitmap Information Header unsigned char* bitmapdata; //Texture data bitmapdata = loadbitmapfile (filename, &bitmapinfoheader); Glbindtexture (gl_texture_2d, texture[i]); //Specify the zoom/Shrink filter For current textures gltexparameteri (gl_texture_2d, Gl_texture_mag_filter, Gl_nearest); gltexparameteri (gl_texture_2d, GL_TEXTURE_MIN_FILTER, GL_ NEAREST); glteximage2d (gl_texture_2d, 0, //mipmap hierarchy (typically, top level) GL_RGB, //We want the texture to have red, green, and blue data Bitmapinfoheader.biwidth,//texture broadband, must be n, if there is a border +2 Bitmapinfoheader.biheight,//texture height, must be n, if there is a border +2 0,//Border (0 = no border, 1 = border) GL_RGB, // Bitmap data format gl_unsigned_byte,//type of each color data bitmapdata); //bitmap data pointer }// Functions for loading textures void Loadtex (int i, char *filename, gluint* texture, unsigned char* backgroundcolor) { bitm Apinfoheader bitmapinfoheader; //Bitmap information header unsigned char* bitmapdata; //Texture data bitmapdata = loadbitmapfile (filename, &bitmapinfoheader,backgroundcolor); glbindtexture (gl_texture_2d, texture[i]); //Specifies how the current texture is enlarged/reduced by filtering gltexparameteri (gl_texture_2d, Gl_texture_mag_filter, gl_nearest); &NBsp;gltexparameteri (gl_texture_2d, Gl_texture_min_filter, gl_nearest); glTexImage2D (GL_TEXTURE_ 2d, 0, //mipmap level (usually, top) GL_RGBA, //We want the texture to have red, green, blue, alpha data bitmapinfoheader.biwidth,//texture broadband, must be n, if there is a border +2 bitmapinfoheader.biheight,//texture height, must be n, if there is a border +2 0,//border (0 = no border, 1 = with border) GL_RGBA, //bitmap data format gl_ Unsigned_byte,//type of data per color bitmapdata); / /bitmap data pointers }
Main.cpp
#define _crt_secure_no_warnings #include <stdio.h> #include <string.h> #include <time.h> #include <stdlib.h> #include "test.h" #include <math.h>/* for cos (), sin (), and sqrt () */Gluint texture[2]; Viewport float Whratio;int wheight = 0;int Wwidth = 0;//viewpoint float center[] = {0, 0, 0};float eye[] = {0, 0, 5};void drawr ECT (Gluint texture) {glenable (gl_texture_2d); Glbindtexture (gl_texture_2d, texture); Select texture Texture[status] Const glfloat x1 = -0.5, x2 = 0.5;const glfloat y1 = -0.5, y2 = 0.5;const glfloat point[4][2] = { {x1,y1},{x2,y1},{x2,y2},{x1,y2}};int dir[4][2] = {{0,0},{1,0},{},{0,1}};glbegin (gl_quads); for (int i = 0; I < 4; i++) {Gltexcoord2iv (dir[i]); GLVERTEX2FV (Point[i]);} Glend (); gldisable (gl_texture_2d);} void Drawscene () {Glpushmatrix (); Glscalef (8, 6, 1);d Rawrect (texture[0]); Glpopmatrix (); Gldepthmask (Gl_false); Glpushmatrix (); Gltranslatef (0.0f,-1.0f, 1.0f); Glscalef (1.8, 2, 0);d Rawrect (texture[1]); Glpopmatrix (); GLDepthmask (gl_true);} void Updateview (int height, int width) {glviewport (0, 0, width, height); Glmatrixmode (gl_projection);//set matrix mode for projection Glloa Didentity (); The initialization matrix is the unit matrix Whratio = (glfloat) width/(glfloat) height; Set the display scale Glortho (-3, 3,-3, 3,-100, 100); Positive projection Glmatrixmode (Gl_modelview); Set the matrix mode to model}void reshape (int width, int height) {if (height = = 0)//If height is 0 {height = 1; Let the height be 1 (avoid the occurrence of denominator 0)}wheight = Height;wwidth = Width;updateview (Wheight, wwidth); Update View}void idle () {Glutpostredisplay ();} void Init () {Srand (Unsigned (Time (NULL))), glenable (gl_depth_test);//Open depth Test glenable (gl_alpha_test); Glalphafunc (Gl_ GREATER, 0.5); glenable (gl_lighting); Turn on light mode Glclearcolor (1.0f, 1.0f, 1.0f, 1.0f), unsigned char color[] = {255,255,255};glgentextures (2, texture); Loadtex ( 0, "1.bmp", texture); Loadtex (1, "2.bmp", Texture,color);} void Redraw () {Glclear (Gl_color_buffer_bit | Gl_depth_buffer_bit);//clear color and depth cache Glmatrixmode (Gl_modelview); glloadidentity ();The initialization matrix is the unit matrix Glulookat (Eye[0], eye[1], eye[2], center[0], center[1], center[2], 0, 1, 0); 0,0,0 (0,5,50), y-axis upward glpolygonmode (Gl_front, Gl_fill), Glfrontface (GL_CCW); glenable (gl_cull_face);// Enable lighting calculation glenable (gl_lighting);//Specify Ambient light intensity (RGBA) glfloat ambientlight[] = {2.0f, 2.0f, 2.0f, 1.0f};//set the lighting model, Apply the RGBA intensity value specified by Ambientlight to ambient light GLLIGHTMODELFV (gl_light_model_ambient, ambientlight);//Enable color tracking glenable (gl_color_ MATERIAL);//Set the ambient light and scattering light material properties of the polygon front, Trace glcolorglcolormaterial (Gl_front, Gl_ambient_and_diffuse);d rawscene ();//Plot the scene Glutswapbuffers ();//Swap buffer}int main (int argc, char *argv[]) {glutinit (&ARGC, argv);//initialization of GLUT Glutinitdisplay Mode (Glut_rgba | glut_depth | glut_double);//Initialize display mode: RGB color model, depth test, double buffered glutinitwindowsize (800, 600);//Set window size int windowhandle = Glutcre Atewindow ("Simple GLUT App");//Set Window caption Glutdisplayfunc (redraw); Register Draw callback function Glutreshapefunc (reshape); Register Redraw callback function Glutidlefunc (idle);//Register Global callback function: call when idle Init (); Glutmainloop (); Glut Event processing loop return 0;}
[OpenGL] A transparent sprite based on texture rendering