OpenGL Road (9) color mixing to achieve transparent effect, opengl road color mixing

Source: Internet
Author: User

OpenGL Road (9) color mixing to achieve transparent effect, opengl road color mixing

Use alpha hybrid to achieve transparent effects and penetrate 3d objects

The source code is as follows:

# Include <gl/glut. h> # include <gl/glaux. h> # include <stdio. h> # 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); glable (GL_TEXTURE_2D); // glBegin (GL_QUADS); glTexCoord2f (0.0f, 0.0f); glVertex3f (-1.0f,-1.0f, 1.0f ); // frontend vertex (1.0f, 0.0f); glVertex3f (1.0f,-1.0f, 1.0f); glTexCoord2f (1.0f, 1.0f); glVertex3f (1.0f, 1.0f, 1.0f); vertex (0.0f, 1.0f); glVertex3f (-1.0f, 1.0f, 1.0f); glTexCoord2f (1. 0f, 0.0f); glVertex3f (-1.0f,-1.0f,-1.0f); // glTexCoord2f (1.0f, 1.0f); glVertex3f (-1.0f, 1.0f,-1.0f ); random (0.0f, 1.0f); glVertex3f (1.0f, 1.0f,-1.0f); random (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); glTexCoord2f (1.0f, 1.0f, 0.0f); glVertex3f (1.0f, 1.0f, 1.0f); glTexCoord2 F (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); glTexCoord2f (0.0f, 0.0f); glVertex3f (1.0f,-1.0f, 1.0f ); glTexCoord2f (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); glTexC Oord2f (0.0f, 1.0f); glVertex3f (1.0f, 1.0f, 1.0f); then (0.0f, 0.0f); glVertex3f (1.0f,-1.0f, 1.0f); then (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 Load Texture (char * filename, GLuint & texture) {AUX_RGBImageRec * pImage = NULL; pImage = auxDIBImageLoadA (filename); // load the bitmap if (pImage = NULL) return false; glGenTextures (1, & texture); // generate glBindTexture (GL_TEXTURE_2D, g_bmp [0]); // texture effect // glTexImage2D (GL_TEXTURE_2D, 0, 3, pImage-> sizeX, pImage-> sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, pImage-> data); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Linear Filter glTexParameteri (GL_TEXTURE_2D, cosine, cosine) when downgrading; // Linear Filter gluBuild2DMipmaps (GL_TEXTURE_2D, 4, pImage-> sizeX, pImage-> sizeY, GL_RGB, GL_UNSIGNED_BYTE, pImage-> data); // 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 Set the ambient light glLightfv (GL_LIGHT1, GL_DIFFUSE, LightDiffuse); // set the diffuse light glLightfv (GL_LIGHT1, GL_POSITION, LightPosition); // set the Light Source Position glable (GL_LIGHT1 ); // enable the 1 light source glable (GL_LIGHTING); // enable the glColor4f (1.0f, 1.0f, 1.0f, 0.5f); // color 0.5 alpha value glBlendFunc (GL_SRC_ALPHA, GL_ONE ); // hybrid function glable (GL_BLEND); // enable transparency. Be sure not to enable the deep test, that is, do not have glEnable (GL_DEPTH_TEST)} float s = 1, r = 0; void renderScene (void) {glClear (GL_COLOR_BUFFER_BIT | GL _ DEPTH_BUFFER_BIT); glLoadIdentity (); glTranslatef (0.0f, 0.0f,-5); glRotatef (r, 1.0f, 1.0f, 0.0f); glScalef (s, s, s ); box (1, 1, 1); r + = 0.01f; fig ();} void changeSize (int w, int h) {if (h = 0) h = 1; float ratio = 1.0 * w/h; glMatrixMode (GL_PROJECTION); // 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:



How does OpenGL implement gradient transparency?

You can change the transparency based on the UV coordinates. can you achieve a gradient? The farther you go, the higher the transparency ..

How is the Web page transparency shown in the figure achieved?

1. It can be implemented using transparent PNG images.
2; it can also be implemented using code.
Example:. touming {
Opacity: 0.5;
-Moz-opacity: 0.5;
Filter: alpha (opacity = 50 );
Background: # fff;
}
Explanation: the background color is white. The transparency is fifty. You can modify the parameter 0.5/50 on your own.

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.