Use alpha to achieve transparency. the source code of 3d objects can be penetrated as follows: # include # pragmacomment (lib, opengl32.lib) # pragmacomment (lib, glu32.lib) # pragmacomment (lib, glu32.lib) # pragma
Use alpha hybrid to achieve transparent effects and penetrate 3d objects
The source code is as follows:
# Include
# Include
# Include
# Pragma comment (lib, "opengl32.lib") # pragma comment (lib, "glu32.lib") # pragma comment (lib, "glu32.lib") # pragma comment (lib, "glut. lib ") # pragma comment (lib," glaux. lib ") UINT g_bmp [1]; // texture No. GLfloat LightAmbient [] = {0.5f, 0.5f, 0.5f, 1.0f}; // environment light parameter, semi-bright white GLfloat LightDiffuse [] = {1.0f, 1.0f, 1.0f, 1.0f}; // diffuse light parameter, GLfloat LightPosition [] = {1.5f, 0.0f,-2.0f, 1.0f }; // void Box (float x, float y, Float z) // cube {glPushMatrix (); glScalef (x, y, z); GL_TEXTURE_2D); // map valid glBegin (GL_QUADS); glTexCoord2f (0.0f, 0.0f); glVertex3f (-1.0f,-1.0f, 1.0f); // The former glTexCoord2f (1.0f, 0.0f); glVertex3f (1.0f,-1.0f, 1.0f); glTexCoord2f (1.0f, 1.0f); glVertex3f (1.0f, 1.0f, 1.0f); glTexCoord2f (0.0f, 1.0f); glVertex3f (-1.0f, 1.0f, 1.0f); glTexCoord2f (1.0f, 0.0f ); glVertex3f (-1.0f,-1.0f,-1.0f); // Post glTexCoord2f (1.0f, 1.0f); glVertex3f (-1.0f, 1.0f,-1.0f); glTexCoord2f (0.0f, 1.0f); glVertex3f (1.0f, 1.0f,-1.0f); glTexCoord2f (0.0f, 0.0f); glVertex3f (1.0f,-1.0f,-1.0f); glTexCoord2f (0.0f, 1.0f); glVertex3f (-1.0f, 1.0f,-1.0f); // glTexCoord2f (0.0f, 0.0f); glVertex3f (-1.0f, 1.0f, 1.0f); then (1.0f, 0.0f); glVertex3f (1.0f, 1.0f, 1.0f); then (1.0f, 1.0f); glVertex3f (1.0f, 1.0f,-1.0f); glTexCoord2f (1. 0f, 1.0f); glVertex3f (-1.0f,-1.0f,-1.0f); // glTexCoord2f (0.0f, 1.0f); glVertex3f (1.0f,-1.0f,-1.0f ); round (0.0f, 0.0f); glVertex3f (1.0f,-1.0f, 1.0f); round (1.0f, 0.0f); glVertex3f (-1.0f,-1.0f, 1.0f); glTexCoord2f (1.0f, 0.0f); glVertex3f (1.0f,-1.0f,-1.0f); // left glTexCoord2f (1.0f, 1.0f); glVertex3f (1.0f, 1.0f,-1.0f); glTexCoord2f (0.0f, 1.0f); glVertex3f (1.0f, 1.0f, 1.0f); glTexCoord 2f (0.0f, 0.0f); glVertex3f (1.0f,-1.0f, 1.0f); glTexCoord2f (0.0f, 0.0f); glVertex3f (-1.0f,-1.0f,-1.0f ); // right glTexCoord2f (1.0f, 0.0f); glVertex3f (-1.0f,-1.0f, 1.0f); glTexCoord2f (1.0f, 1.0f); glVertex3f (-1.0f, 1.0f, 1.0f ); glTexCoord2f (0.0f, 1.0f); glVertex3f (-1.0f, 1.0f,-1.0f); glEnd (); glDisable (GL_TEXTURE_2D); // cancel the glPopMatrix ();} bool LoadTexture (char * filename, GLuint & texture) {AUX_RGBImageRec * p Image = NULL; pImage = auxDIBImageLoadA (filename); // load the bitmap if (pImage = NULL) return false; glGenTextures (1, & texture ); // Generate the texture glBindTexture (GL_TEXTURE_2D, g_bmp [0]); // apply the texture // glTexImage2D (GL_TEXTURE_2D, 0, 3, pImage-> sizeX, pImage-> sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, pImage-> data); glTexParameteri (GL_TEXTURE_2D, cosine, GL_LINEAR); // linear filter glTexParameteri (GL_TEXTURE_2D, cosine LTER, iterator); // linear filtering of gluBuild2DMipmaps (GL_TEXTURE_2D, 4, pImage-> sizeX, pImage-> sizeY, GL_RGB, GL_UNSIGNED_BYTE, pImage-> data) during expansion ); // map data free (pImage-> data); // release the bitmap memory free (pImage); return true;} void init () {// g_text = gluNewQuadric (); // apply for the texture cache LoadTexture ("I: \ 4.bmp", g_bmp [0]); glLightfv (GL_LIGHT1, GL_AMBIENT, LightAmbient); // Set the environment light glLightfv (GL_LIGHT1, GL_DIFFUSE, lightDiffuse); // sets the diffuse light. GlLightfv (GL_LIGHT1, GL_POSITION, LightPosition); // set the position of the light source to GL_LIGHT1; // enable the GL_LIGHTING (GL_LIGHTING); // enable the glColor4f (1.0f, 1.0f, 1.0f, 1.0f, 0.5 F); // color alpha value glBlendFunc (GL_SRC_ALPHA, GL_ONE); // hybrid function GL_BLEND (GL_BLEND); // enable transparency. do not enable deep testing, that is, do not have glEnable (GL_DEPTH_TEST)} float s = 1, r = 0; void renderScene (void) {glClear (GL_COLOR_BUFFER_BIT | callback); glLoadIdentity (); glTranslatef (0.0f ,-5); glRotatef (r, 1.0f, 1.0f, 0.0f); glScalef (s, s, s); Box (1, 1, 1); r + = 0.01f; glMatrixMode (GL_PROJECTION);} void changeSize (int w, int h) {if (h = 0) h = 1; float ratio = 1.0 * w/h ); // unitized projection matrix. GlLoadIdentity (); glViewport (0, 0, w, h); // you can specify the window size as gluPerspective (45, ratio, 1, 1000 ); // set the correct projection matrix glMatrixMode (GL_MODELVIEW); // set the model View matrix glLoadIdentity (); gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0,-1.0, 0.0f, 1.0f, 0.0f); // Set the observation point} int light = 1; void keyfunc (unsigned char ch, int x, int y) {if (ch = 'w ') // zoom in and out s + = 0.1f; else if (ch = 's') s-= 0.1f; if (ch = 'd ') // d key switch light {if (light) {light = 0; glDisable (GL_LIGHTING); printf ("off \ n");} else {light = 1; glEnable (GL_LIGHTING); printf ("on \ n") ;}} int main (int argc, char * argv []) {gluinit (& argc, argv ); fig ); glutIdleFunc (renderScene); // call the function glureshapefunc (changeSize) when the specified program is idle; // specify the callback function glukeyboardfunc (keyfunc) when the window shape changes ); // keyboard callback function (); return 0 ;}
Image used:
Effect preview: