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

Source: Internet
Author: User
Tags set background

Example 9: Draw an atomized cone, as shown in Figure 10, in order to observe the different atomization parameters, the program has added keyboard operation. "Shift+1", "shift+2" keys to increase and decrease the atomization concentration, "shift+3" set the start and end point of the atomization, "shift+4" and "shift+5" keys to change the atomization equation, "shift+6" the atomization color from white to green, as shown in Figure 11.

#include <stdlib.h>
#include <GL/glut.h>

Glfloat light_ambient[] = {0.1,0.1,0.1,0.0};
Glfloat light_diffuse[] = {1.0,1.0,1.0,0.0};
Glfloat light_specular[] = {1.0,1.0,1.0,0.0};
Glfloat light_position[] = { -10.0,0.0,5.0,0.0};

Glfloat material_ambient[] = {0.1745,0.01175,0.01175};
Glfloat material_diffuse[] = {0.61424,0.04136,0.04136};
Glfloat material_specular[] = {0.727811,0.626959,0.62659};

Glfloat fogcolorwhite[] = {1.0,1.0,1.0,1.0};
Glfloat fogcolorgreen[] = {0.0,1.0,0.0,1.0};
float fogdensity = 0.02;


void Myinit (void)
{
/* Set Background color * *
Glclearcolor (0.5,0.5,0.5,1.0);

/* Set the Light/*
GLLIGHTFV (gl_light0,gl_ambient,light_ambient);
GLLIGHTFV (Gl_light0,gl_diffuse,light_diffuse);
GLLIGHTFV (Gl_light0,gl_specular,light_specular);
GLLIGHTFV (gl_light0,gl_position,light_position);

/* Set Material * *
GLMATERIALFV (gl_front,gl_ambient,material_ambient);
GLMATERIALFV (Gl_front,gl_diffuse,material_diffuse);
GLMATERIALFV (Gl_front,gl_specular,material_specular);
Glmaterialf (gl_front,gl_shininess,0.6*128);

Glshademodel (Gl_smooth);

Glenable (gl_depth_test);
Gldepthfunc (gl_less);
Glenable (gl_lighting);
Glenable (GL_LIGHT0);
Glenable (Gl_auto_normal);
Glenable (gl_normalize);
Glfrontface (GL_CW);

/* Set atomization * *
Glenable (GL_FOG);
Glfogi (gl_fog_mode,gl_linear);
GLFOGFV (Gl_fog_color,fogcolorwhite);
GLFOGF (gl_fog_density,fogdensity);
GLFOGF (gl_fog_start,0.0);
GLFOGF (gl_fog_end,15.0);

Glhint (Gl_fog_hint,gl_dont_care);
}

/* Set different atomization effects according to different key values.
static void MyKey (unsigned char key,int x,int y)
{
Switch (key)
{
Case://shift+1
Fogdensity *= 1.10;
Glfogi (GL_FOG_MODE,GL_EXP);
GLFOGF (gl_fog_density,fogdensity);
Glutpostredisplay ();
Break

Case://shift+2
Fogdensity/= 1.10;
Glfogi (GL_FOG_MODE,GL_EXP);
GLFOGF (gl_fog_density,fogdensity);
Glutpostredisplay ();
Break

Case://shift+3
Glfogi (gl_fog_start,0.0);
Glfogi (gl_fog_end,8.0);
Glutpostredisplay ();
Break

Case://shift+4
Glfogi (GL_FOG_MODE,GL_EXP2);
Glutpostredisplay ();
Break

Case://shift+5
Glfogi (gl_fog_mode,gl_linear);
Glutpostredisplay ();
Break

Case://shift+6
GLFOGFV (Gl_fog_color,fogcolorgreen);
Glutpostredisplay ();
Break

Case://esc
Exit (0);

Default
Break
}
}

/* Draw a cone * *
void Mydisplay (void)
{
Glclear (Gl_color_buffer_bit|gl_depth_buffer_bit);

Glpushmatrix ();
Gltranslatef ( -4.0,4.0,0.0);
Glrotatef (30.0,1.0,1.0,0.0);
Glutsolidcone (1.0,50.0,20.0,20.0);
Glpopmatrix ();
Glutswapbuffers ();
}

void Myreshape (int w,int h)
{
Glviewport (0,0, (Glsizei) W, (Glsizei) h);
Glmatrixmode (gl_projection);
Glloadidentity ();
Gluperspective (100.0,1.0,1.0,20.0);
Glmatrixmode (Gl_modelview);
Glloadidentity ();
Gltranslatef (0.0,0.0,-15.5);
}

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

/* Create window * *
Glutcreatewindow ("FOG");

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

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

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.