How does OpenGL add 24-bit BMP images? An Alpha Channel and transparent implementation

Source: Internet
Author: User
# Include <windows. h>
# Include <Gl/glut. h>
# Include <Gl/Glaux. h>
# Include <stdio. h>

# Pragma comment (Lib, "opengl32.lib") // use opengl32.lib for Link
# Pragma comment (Lib, "glu32.lib") // use glu32.lib for Link
# Pragma comment (Lib, "Glaux. lib") // use Glaux. Lib for Link

Hwndhwnd;
Hdchdc;
Hglrchrc = NULL; // define the rendering environment
Hinstancehinstance; // sample program
Rect;

Int Sw = 600;
Int SH = 600;

Glfloat aspect;
Glfloat X5 = 0.0;
Glfloat y2 = 0.0;
Glfloatxrot; // X axis rotation
Glfloatyrot; // y axis rotation
Glfloatz = 8.0; // depth of the screen
Glfloatx = 0.0;
Glfloaty = 0.0;

Int lastxpos;
Int lastypos;

Bool islbdown;
Bool light; // light source-on/off
Bool LP; // do I have to press the L key?

Glfloat lightambient [] = {0.5f, 0.5f, 0.5f, 1.0}; // ambient light value
Glfloat lightdiffuse [] = {1.0, 1.0, 1.0, 1.0}; // The value of the scattered light.
Glfloat lightposition [] = {0.0, 0.0, 2.0, 1.0}; // illumination position

Glubyte pixel;

Gluinttexture [8]; // texture storage space

Aux_rgbimagerec * textureimage [8]; // creates a bucket for the texture

Int loadgltextures (int I, char * filename) // call bitmap and convert it to texture
{
Unsigned char * m_ucpdata;
Unsigned int X, Y, width, height;
Unsigned char temprgb;
Unsigned char * buffer;
File * FP;
Int status = false; // check the status.
// --------------------------------- Read the bitmap and check for errors. Exit if the bitmap does not exist.
If (textureimage [I] = auxdibimageload (filename ))
{
Width = textureimage [I]-> sizex;
Height = textureimage [I]-> sizey;
Status = true; // set status to true
Fp = fopen (filename, "A + B ");
If (! FP ){
Fclose (FP );
Return false;
}
// ---------------------------- Read the image RGB Information
M_ucpdata = (unsigned char *) malloc (width * height * 3 );
Fseek (FP, 54, seek_set); // the pointer skips the file header and moves it to the specified position.
Fread (m_ucpdata, sizeof (unsigned char), width * height * 3, FP); // read data from a specified position
For (INT imageidx = 0; imageidx <(width * height * 3); imageidx + = 3)
{
Temprgb = m_ucpdata [imageidx];
M_ucpdata [imageidx] = m_ucpdata [imageidx + 2];
M_ucpdata [imageidx + 2] = temprgb;
}
// ------------------------------- Change RGB to rgba
Buffer = (unsigned char *) malloc (width * height * 4 );
For (x = 0, y = 0; x <(width * height * 3); x + = 3, Y + = 4)
{
Buffer [y] = m_ucpdata [x];
Buffer [Y + 1] = m_ucpdata [x + 1];
Buffer [Y + 2] = m_ucpdata [x + 2];
If (m_ucpdata [x] == 255 & m_ucpdata [x + 1] == 255 & m_ucpdata [x + 2] = 255)
{
Buffer [Y + 3] = 0;
}
Else
Buffer [Y + 3] = 1;
}
// The modified rgba content is written back to textureimage [I]
Textureimage [I]-> DATA = (unsigned char *) realloc (textureimage [I]-> data, width * height * 4 );
For (int A = 0; A <width * height * 4; A ++)
{
Textureimage [I]-> data [a] = buffer [a];
If (a + 1) % 4 = 1)
{
Int u = textureimage [I]-> data [a];
}
}

Glgentextures (1, & texture [I]); // name a texture
Glbindtexture (gl_texture_2d, texture [I]); // create a texture object
Gltexparameteri (gl_texture_2d, gl_texture_mag_filter, gl_linear); // you can specify the color line filter for texture packaging.
Gltexparameteri (gl_texture_2d, gl_texture_min_filter, gl_linear_mipmap_nearest); // mipmapped Filtering
Glubuild2dmipmaps (gl_texture_2d, 3, width, height, gl_rgba, gl_unsigned_byte, textureimage [I]-> data); // scale the texture to a proper size
}
Else
{
If (textureimage [I]) // whether the texture exists
{
If (textureimage [I]-> data)
{
Free (textureimage [I]-> data); // release the memory occupied by the texture image
}
Else free (textureimage [I]); // release the image structure
}
Else return status;
}
Fclose (FP );
Delete buffer;
Return status;
}
Void sceneinit (int w, int H)
{
Loadgltextures (1, "obj_1.bmp ");

Glable (gl_texture_2d );

Glable (gl_alpha_test); // transparent partial sequence test
Glalphafunc (gl_greater, 0.5 );

Glable (gl_blend); // enable Hybrid
Glblendfunc (gl_src_alpha, gl_one_minus_src_alpha );

Glshademodel (gl_smooth); // agree to smooth coloring
Glclearcolor (0.0, 0.0, 0.0, 0.0 );
}
Void sceneresizeviewport (glsizei W, glsizei H)
{
If (H = 0)
{
H = 1;
}
Aspect = (glfloat) W/(glfloat) h;
Glviewport (0, 0, W, H );
Glmatrixmode (gl_projection); // select the projection matrix.
Glloadidentity (); // Reset
Gluperspective (45.0, aspect, 0.1f, 100.0); // sets the perspective to a 45-degree angle.
Glmatrixmode (gl_modelview );
Glloadidentity ();

}

Void sceneshow (glvoid) // all the drawing operations are performed here.
{
Glclear (gl_color_buffer_bit | gl_depth_buffer_bit); // clear the screen and clear the depth buffer
Glloadidentity (); // reset the current modelview Matrix
Glulookat (X, Y, Z, X1, Y1,-2.0, 0.0, 1.0, 0.0 );

Glbindtexture (gl_texture_2d, texture [1]);
Glbegin (gl_quads );
Glnormal3f (0.0, 0.0, 1.0 );
Gltexcoord2f (1.0, 0.0); glvertex3f (-0.5,-1.0, 2.0 );
Gltexcoord2f (1.0, 1.0); glvertex3f (-0.5, 0.0, 2.0 );
Gltexcoord2f (0.0, 1.0); glvertex3f (0.5, 0.0, 2.0 );
Gltexcoord2f (0.0, 0.0); glvertex3f (0.5,-1.0, 2.0 );
Glend ();

Glflush ();
}
// Create an OpenGL form
Void enableopengl ()
{
Pixelformatdescriptor PFD;
Int iformat;

HDC = getdc (hwnd );

Zeromemory (& PFD, sizeof (PFD ));
PFD. nsize = sizeof (PFD );
PFD. nversion = 1;
PFD. dwflags = pfd_draw_to_window |
Pfd_support_opengl |
Pfd_doublebuffer;
PFD. ipixeltype = pfd_type_rgba;
PFD. ccolorbits = 32;
PFD. cdepthbits = 32;
PFD. ilayertype = pfd_main_plane;

Iformat = choosepixelformat (HDC, & PFD );

Setpixelformat (HDC, iformat, & PFD );

HRC = wglcreatecontext (HDC );
Wglmakecurrent (HDC, HRC );
}
Void disableopengl ()
{
Wglmakecurrent (null, null );
Wgldeletecontext (HRC );
Releasedc (hwnd, HDC );
}
Lresult callback wndproc (hwnd, uint message, wparam, lparam)
{
Switch (Message)
{
Case wm_create:
Getwindowrect (hwnd, & rect );
Sw = rect. Right-rect. Left;
SH = rect. Bottom-rect. Top;
Sceneresizeviewport (SW, SH );
Return 0;
Case wm_size: // change the size of the form
Getwindowrect (hwnd, & rect );
Sw = rect. Right-rect. Left;
SH = rect. Bottom-rect. Top;
If (SW> 0 & SH> 0)
Sceneresizeviewport (SW, SH );
Return 0;
Default:
Return defwindowproc (hwnd, message, wparam, lparam );
}
}
Int apientry winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int ncmdshow)
{
Wndclass WC;
MSG;
Bool bquit = false;
WC. Style = cs_owndc;
WC. lpfnwndproc = wndproc;
WC. cbclsextra = 0;
WC. cbwndextra = 0;
WC. hinstance = hinstance;
WC. hicon = loadicon (null, idi_application );
WC. hcursor = loadcursor (null, idc_arrow );
WC. hbrbackground = (hbrush) getstockobject (black_brush );
WC. lpszmenuname = NULL;
WC. lpszclassname = "name ";
Registerclass (& WC );
Hwnd = createwindow ("name", "animation tech_base on 2D images ",
Ws_tiledwindow | ws_visible,
Getsystemmetrics (sm_cxscreen)/2-sw/2,
Getsystemmetrics (sm_cyscreen)/2-SH/2,
SW, sh, null, null, hinstance, null );
Changedisplaysettings (null, 0 );
Showwindow (hwnd, sw_show );
Updatewindow (hwnd );
Enableopengl ();
Fig );
Sceneinit (SW, SH );
Getwindowrect (hwnd, & rect );
Sw = rect. Right-rect. Left;
SH = rect. Bottom-rect. Top;
If (SW> 0 & SH> 0)
Sceneresizeviewport (SW, SH );
While (! Bquit)
{
If (peekmessage (& MSG, null, 0, 0, pm_remove ))
If (msg. Message = wm_quit)
Bquit = true;
Else
{
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}
Else
{
// OpenGL Animation
Sceneshow ();
Swapbuffers (HDC); // redraw Function
}
}
// Close and exit the program
Disableopengl ();
Showwindow (hwnd, sw_hide );
Destroywindow (hwnd );
Changedisplaysettings (null, 0 );
Return msg. wparam;
Return 0;

}


This is the program source code, in the loadgltextures () function.
I have read every pixel of the image. Add? Alpha Channel, and write it back to the cache, but it is not implemented when the output image is finally called.
When debugging, determine that the alpha channel of each pixel point is changed as required...
Excuse me, what is my problem?


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.