OpenGL programming easy to get started special effect operation (1)

Source: Internet
Author: User

Each object has a different visual effect in different environments. Special effects need to be added to make the effect more real.

Example 8: Draws three overlapping rectangles, which are green, red, and blue from left to right respectively. The transparency of the red and blue rectangles is 50% transparency, that is, the alpha value is 0.5, as shown in figure nine.

#include <stdlib.h>
#include <GL/glut.h>
void Myinit (void)
{
Glclearcolor (0.2,0.8,0.8,0.0)//Set Background to Lake Blue
Glenable (gl_blend);//Activate Gl_blend
Glblendfunc (Gl_src_alpha,gl_one_minus_src_alpha);//The mathematical algorithm for specifying pixels
Glenable (Gl_flat);
}

void Mydisplay (void)
{
Glclear (Gl_color_buffer_bit);

/* Draw a green rectangle alpha = 1.0*/
glcolor4f (0.0,1.0,0.0,1.0);

Glbegin (Gl_polygon);
glvertex3f ( -0.75,0.5,0.0);
glvertex3f ( -0.75,-0.75,0.0);
glvertex3f (0.5,-0.75,0.0);
glvertex3f (0.5,0.5,0.0);
Glend ();

/* Draw a red rectangle alpha = 0.5*/
glcolor4f (1.0,0.0,0.0,0.5);

Glbegin (Gl_polygon);
glvertex3f ( -0.25,1.0,0.0);
glvertex3f ( -0.25,-0.25,0.0);
glvertex3f (1.0,-0.25,0.0);
glvertex3f (1.0,1.0,0.0);
Glend ();

/* Draw a blue rectangle alpha = 0.5*/
glcolor4f (0.0,0.0,1.0,0.5);

Glbegin (Gl_polygon);
glvertex3f (0.25,1.5,0.0);
glvertex3f (0.25,0.25,0.0);
glvertex3f (1.5,0.25,0.0);
glvertex3f (1.5,1.5,0.0);
Glend ();

Glflush ();
}

void Myreshape (int w,int h)
{
Glviewport (0,0,W,H);
Glmatrixmode (gl_projection);
Glloadidentity ();

if (wGlortho ( -1.5,1.5,-1.5* (glfloat) h/(glfloat) w,1.5* glfloat (h/) glfloat);
Else
Glortho ( -1.5* (glfloat) w/(glfloat) h,1.5* glfloat (w/) glfloat);

Glmatrixmode (Gl_modelview);
Glloadidentity ();
Gltranslatef ( -0.4,0.0,0.0);
}

int main (int argc,char * * argv)
{
/* Initialize * *
Glutinit (&AMP;ARGC,ARGV);
Glutinitdisplaymode (glut_single| GLUT_RGB);
Glutinitwindowsize (300,400);
Glutinitwindowposition (200,200);

/* Create window * *
Glutcreatewindow ("BLEND POLYGON");

/* Draw and display * *
Myinit ();
Glutreshapefunc (Myreshape);
Glutdisplayfunc (Mydisplay);

/* Enter GLUT event handling cycle * *
Glutmainloop ();
return (0);
}



Figure 9:3 A rectangle that overlaps each other


Glblendfunc (glenum sfactor,glenum dfactor) specifies the pixel algorithm. Sfactor specifies how the red, green, blue, and alpha source blending factors are calculated. Dfactor specifies how the red, green, blue, and alpha target blend factors are calculated.

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.