opengl画五角星

vc6.0++编译,需要加opengl32.lib  glu32.lib glaux.lib;

代码功能: 固定窗口画任意个五角星,角度任意,颜色任意,数量任意;

opengl画五角星_第1张图片

#include 

#include 
#include 
#include 

#include 
#include 
#include 
#include 
#define PI 3.1415926
float r2 = 100;
float r1 = 50;
float a = 400+4;
float b = 400;
float x_axis = 100;
float y_axis = 100;
float xl = 800;
float yl = 800; 


float  color1 ;
	   float color2 ;
	   float color3 ;
//绘制图形函数
float r=1,g=0;
float x0=30.0f;
int count ;


void init()
{
      glClearColor(230.0/255,180.0/255,80.0/255,0.0);//背景
	  
	  glClear(GL_COLOR_BUFFER_BIT);
}

void axis()//坐标
{

  glEnable(GL_LINE_STIPPLE);
  glDisable(GL_LINE_SMOOTH);
  glLineStipple(1,0x0F0F);
    
  glBegin(GL_LINES);
  glColor3f(0.5,0,0);

  glVertex2f(xl/2.0+4.0f, 0.0f);

  glVertex2f(xl/2.0+4.0f, yl);

 
  glVertex2f(0.0f, xl/2.0+4.0f);

  glVertex2f(xl, yl/2.0+4.0f);
  glEnd();
}
void CALLBACK draw(void)

{ 

  /**/
  axis();
  /**/
    
    glEnable(GL_LINE_SMOOTH);

    glLineStipple(1,0x0f0f);
    
   float ax[5];
   float ay[5];
   float bx[5];
   float by[5];
   int i = 0;
   int j  = 0;
   float b2x[5],b2y[5],a1x[5],a1y[5];
   srand(time(NULL)); 
   int h = rand()%6+3;
   
   for(j=  0 ; j  < h ; j++)

   {  

	   
       	i = rand()%400+200;/* i是[1,200]区间内的一个整数 */
        a = float(i);
     	i = rand()% 400+200;

	    b = float(i);
		float rate = float(rand()% 100+50)/100.0;
  
   int angle = rand()%360+1;
 
    for (i = 0; i < 5 ; i++)
   {
	   b2x[i] =  a + rate*r2 * cos((i*72+18+angle)* PI / 180.0);
	   b2y[i] =  b + rate*r2 * sin((i*72+18+angle)* PI / 180.0);

       a1x[i] =  a + rate*r1 * cos((i*72+54+angle)* PI / 180.0);
	   a1y[i] =  b + rate* r1 * sin((i*72+54+angle)* PI / 180.0);
	 
   }
   
  
 /**/


       glBegin(GL_LINE_LOOP);
      
	   glVertex2f(b2x[0],b2y[0]);
   
       for (i = 0; i < 4 ; i++)
	   { 
		  float color1 = rand()%255/100.0;
	      float color2 = rand()%255/100.0;
     	  float color3 = rand()%255/100.0;
		   glColor3f(color1,color2,color3);
	       glVertex2f(a1x[i],a1y[i]);
		   glVertex2f(b2x[i+1],b2y[i+1]);
	 
   }
         glVertex2f(a1x[4],a1y[4]);

         glEnd();

		 }
 glFinish(); 
    

}



void CALLBACK change()
{
      
	draw();
	glClear(GL_COLOR_BUFFER_BIT);
	
	
}



//主函数


void main()
{
    auxInitDisplayMode(AUX_SINGLE|AUX_RGBA);
    auxInitPosition(x_axis,y_axis,xl,yl);
    auxInitWindow("CGOpenGL");

    init();
    auxIdleFunc(change);//一定要在auxMainLoop前面
    auxMainLoop(draw);   
}

你可能感兴趣的:(opengl)