OPENGL学习笔记之源代码


#include <gl/glut.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

const int n = 40;//分割的精确度
const GLfloat R = 0.5f;
const GLfloat Pi = 3.1415926f;

const GLfloat factor = 0.1f;

void MyDisplay(void)
{
 int i;

 glClear(GL_COLOR_BUFFER_BIT);
 glPointSize(6.0f);
 glBegin(GL_POINTS);
  for (i=0;i<n;++i)
  {
   glVertex2f(R*cos(2*Pi/n*i),R*sin(2*Pi/n*i));
  }

 glEnd();

 //glRectf(-0.5f,-0.5f,0.5f,0.5f);
 glFlush();
}

void DisXing(void)
{
 GLfloat x;
 glClear(GL_COLOR_BUFFER_BIT);
 glBegin(GL_LINES);
  glVertex2f(-1.0f,0.0f);
  glVertex2f(1.0f,0.0f);
  glVertex2f(0.0f,-1.0f);
  glVertex2f(0.0f,1.0f);
 glEnd();
 glBegin(GL_LINE_STRIP);
  for (x=-1.0f/factor;x<1.0f/factor;x+=0.01f)
  {
   glVertex2f(x*factor,sin(x)*factor);
  }
 glEnd();

 glFlush();

}

void DisXian(void)
{
 glClear(GL_COLOR_BUFFER_BIT);
 glEnable(GL_LINE_STIPPLE);//启动虚线模式
 //glDisable(GL_LINE_STIPPLE);//关闭虚线模式
 glLineStipple(4,0xAAAA);
 glLineWidth(10.0f);

 glBegin(GL_LINES);
  glVertex2f(0.0f,0.0f);
  glVertex2f(0.5f,0.5f);
 glEnd();
 glFlush();

}

void DisPolygon(void)
{
 glClear(GL_COLOR_BUFFER_BIT);
 glPolygonMode(GL_FRONT,GL_FILL);//设置正面为填充方式
 glPolygonMode(GL_BACK,GL_LINE);//设置反面为线性方式
 //glPolygonMode(GL_FRONT_AND_BACK,GL_POINT);//设置两面均为顶点绘制方式
 glFrontFace(GL_CCW);//设置逆时针为正面

 glEnable(GL_CULL_FACE); //启动剔除模式
 glCullFace(GL_FRONT);  //剔除正面
 //glDisable(GL_CULL_FACE); //关闭剔除模式

 glBegin(GL_POLYGON);//逆时针绘制一个正边形,原点左下方
  glVertex2f(-0.5f,-0.5f);
  glVertex2f(0.0f,-0.5f);
  glVertex2f(0.0f,0.0f);
  glVertex2f(-0.5f,0.0f);
 glEnd();

 glBegin(GL_POLYGON);//顺时针绘制一个正边形,原点右上方
  glVertex2f(0.0f,0.0f);
  glVertex2f(0.0f,0.5f);
  glVertex2f(0.5f,0.5f);
  glVertex2f(0.5f,0.0f);
 glEnd();

 glFlush();
}

void LouPolygon(void)
{
 static GLubyte Mask[128];
 FILE*fp;
 fp = fopen("mask.bmp","rb");
 if(!fp)
  exit(0);
 if (fseek(fp,-(int)sizeof(Mask),SEEK_END))
  exit(0);
 if(! fread(Mask,sizeof(Mask),1,fp))
  exit(0);
 fclose(fp);
 glClear(GL_COLOR_BUFFER_BIT);
 glEnable(GL_POLYGON_STIPPLE);
 glPolygonStipple(Mask);
 glRectf(-0.5f,-0.5f,0.0f,0.0f);
 glDisable(GL_POLYGON_STIPPLE);
 glRectf(0.0f,0.0f,0.5f,0.5f);
 glFlush();
}

void DisColor(void)
{
 
 //glClearColor(1.0f,0.0f,0.0f,0.0f);//涮屏
 //glClear(GL_COLOR_BUFFER_BIT);
 
 //glColor3f(0.0f,1.0f,0.0f);//r,g,b,
 //glRectf(0.0f,0.0f,0.5f,0.5f);

 int i;
 glShadeModel(GL_FLAT);//单色方式
 glClear(GL_COLOR_BUFFER_BIT);
 glBegin(GL_TRIANGLE_FAN);
 glColor3f(1.0f,1.0f,1.0f);
 glVertex2f(0.0f,0.0f);
 for (i=0;i<=8;i++)
 {
  glColor3f(i&0x04,i&0x02,i&0x01);
  glVertex2f(cos(i*Pi/4),sin(i*Pi/4));
 }
 glEnd();
 glFlush();
}
#include <time.h>
double CalFrequency()
{
 static int count;
 static double save;
 static clock_t last,curent;
 double timegap;

 ++count;
 if (count<=50)
  return save;
 count=0;
 last = curent;
 curent = clock();

 timegap = (curent -  last)/(double)CLK_TCK;
 save = 50.0/timegap;

 return save;
}
static int day  = 0;
void DrawNorth(void)
{
 double FPS = CalFrequency();
 printf("FPS=%f\n",FPS);

 glEnable(GL_DEPTH_TEST);
 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
 glMatrixMode(GL_PROJECTION);//为了指定当前操作的是何种矩阵
 glLoadIdentity();
 gluPerspective(75,1,1,400000000);
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
 gluLookAt(0,-200000000,200000000,0,0,0,0,0,1);
 //红太阳
 glColor3f(1.0f,0.0f,0.0f);
 glutSolidSphere(69600000,20,20);
 //蓝地球
 glColor3f(0.0f,0.0f,1.0f);
 glRotatef(day/360.0*360,0.0f,0.0f,-1.0f);
 glTranslatef(150000000,0.0f,0.0f);
 glutSolidSphere(15945000,20,20);
 //黄月亮
 glColor3f(1.0f,1.0f,0.0f);
 glRotatef(day/30.0*360.0-day/360.0*360.0,0.0f,0.0f,-1.0f);
 glTranslatef(38000000,0.0f,0.0f);
 glutSolidSphere(4345000,20,20);

 //glOrtho();
 //gluOrtho2D(100,300,200,500); //正投影
 //glFrustum();
 //gluPerspective();//透视投影
 //glViewport(100,100,400,800);//绘制到窗口的范围
 //glPushMatrix();
 //glPopMatrix();//保存,恢复矩阵。

 glFlush();
 
  
 glutSwapBuffers();//双缓冲,交换画板
}

void MyIdle(void)
{
 ++day;
 if (day>=360)
 {
  day=0;
 }
 DrawNorth();
}

int main(int argc,char*argv[])
{
 glutInit(&argc,argv);
 glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);//双缓冲
 glutInitWindowPosition(100,100);
 glutInitWindowSize(400,400);
 glutCreateWindow("宇宙,go !");
 //glutDisplayFunc(&MyDisplay);
 //glutDisplayFunc(&DisXing);
 //glutDisplayFunc(&DisXian);
 //glutDisplayFunc(&DisPolygon);
 //glutDisplayFunc(&LouPolygon);
 //glutDisplayFunc(&DisColor);
 glutDisplayFunc(&DrawNorth);
 glutIdleFunc(*MyIdle);
 glutMainLoop();
 return  0;
}

你可能感兴趣的:(学习笔记,OpenGL)