Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2708584
  • 博文数量: 416
  • 博客积分: 10220
  • 博客等级: 上将
  • 技术积分: 4193
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-15 09:47
文章分类

全部博文(416)

文章存档

2022年(1)

2021年(1)

2020年(1)

2019年(5)

2018年(7)

2017年(6)

2016年(7)

2015年(11)

2014年(1)

2012年(5)

2011年(7)

2010年(35)

2009年(64)

2008年(48)

2007年(177)

2006年(40)

我的朋友

分类: C/C++

2010-02-02 15:28:39

参考:http://blog.csdn.net/vagrxie/archive/2009/10/15/4676312.aspx

参考边做,把代码写在一块,比较乱

#include   
#include
#include   
// 矩形顶部的Green颜色
GLfloat gfTopGreen;
// 矩形底部的Red颜色
GLfloat gfBottomRed;
// 渐变方向
GLboolean gbShadeDir;
#define FRAME_PER_SECOND (60)
#define TIME_IN_FRAME (1000/FRAME_PER_SECOND)
#define WIDTH (400)
#define HEIGHT (400)
#define SHADE_SPEED (0.005)
#define PI (3.1415926535898)

void myinit(void);
void myReshape(GLsizei w, GLsizei h);
void display(void);
/* 初始化 alpha 融合的参数 */
void myinit(void)
{
 glEnable (GL_BLEND);
 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 glShadeModel (GL_FLAT);
 glClearColor (1.0, 1.0, 1.0, 1.0);
}
void display(void)
{
 glClear(GL_COLOR_BUFFER_BIT);
 
 glEnable(GL_BLEND);
 glBlendFunc(GL_ONE, GL_ZERO);
 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 glColor4f (1, 0.0, 0.0, 0.5);
 glRectf (-0.5, -0.5, 0.5, 0.5); 
 glColor4f (0.0, 1.0, 0.0, 0.5);
 glRectf (0.1, 0.2, 0.6, 0.6);
 glColor4f (0.0, 0.0, 1.0, 0.3);
 glRectf (0.4, 0.1, 1.9, 0.6);
 
 glColor4f (0, 0.0, 0.0, 0.1);
 glRectf (-0.01, -0.01, 0.01, 0.01); 
 glColor4f(1.0, 1.0, 1.0, 0.3);
 glBegin(GL_POLYGON);
 glVertex3f(-0.5, -0.5, 0.0);
 glVertex3f(0.5, -0.5, 0.0);
 glVertex3f(0.5, 0.5, 0.0);
 glVertex3f(-0.5, 0.5, 0.0);
 glEnd();

 glutSwapBuffers();
 
}
void myReshape(GLsizei w, GLsizei h)
{
 glViewport(0, 0, w, h);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 //if (w <= h)
 // gluOrtho2D (0.0, 1.0, 0.0, 1.0*(GLfloat)h/(GLfloat)w);
 //else
 // gluOrtho2D (0.0, 1.0*(GLfloat)w/(GLfloat)h, 0.0, 1.0);
 glMatrixMode(GL_MODELVIEW);
}
void TimerFunc(int value)  
{  
 glutPostRedisplay();  
 glutTimerFunc(100, TimerFunc, 1);  
}  
//OpenGL初始化开始
void SceneInit(int w,int h)
{
 glClearColor(0.0,0.0,0.0,0.0);
 glShadeModel(GL_SMOOTH);
 gfTopGreen=0.0;
 gfBottomRed=1.0;
 //默认方向,Top增长,Bottom减少
 gbShadeDir=true;
 //gluOrtho2D (0.0, (double)w, (double)h, 0.0);
}
void IncreaseToTop()
{
 gfTopGreen+=SHADE_SPEED;
 gfBottomRed-=SHADE_SPEED;
}
void DecreaseToTop()
{
 gfTopGreen-=SHADE_SPEED;
 gfBottomRed+=SHADE_SPEED;
}
void DrawRect()
{
 glBegin(GL_QUADS);
 glColor3f(0.0,gfTopGreen,0.0);
 glVertex3f(-0.5,-0.5,0.0);
 glVertex3f(0.5,-0.5,0.0);
 glColor3f(gfBottomRed,0.0,0.0);
 glVertex3f(0.5,0.5,0.0);
 glVertex3f(-0.5,0.5,0.0);
 glEnd();
 //glFlush();
 glutSwapBuffers();
}
void DrawCircle()
{
 GLint iCirclePoints = 50;
 glBegin(GL_POLYGON);
 static double dAngleColor = 0.0;
 for(int i = 0; i < 50; ++i)
 {
  double dAngle = 2 * PI * i / iCirclePoints + dAngleColor;
  glVertex3f(cos(dAngle)/2, sin(dAngle)/2, 0.0);
 }
 dAngleColor += 0.02;
 glEnd();
}
void DrawCircle(GLfloat R,GLfloat G,GLfloat B,GLfloat A)
{
 GLint iCirclePoints = 50;
 glBegin(GL_POLYGON);
 static double dAngleColor = 0.0;
 for(int i = 0; i < 50; ++i)
 {
  glColor4f(R/i, G, B, A);
  double dAngle = 2 * PI * i / iCirclePoints + dAngleColor;
  glVertex3f(cos(dAngle)/2, sin(dAngle)/2, 0.0);
 }
 dAngleColor += 0.02;
 glEnd();
}

//这里进行所有的绘图工作
void SceneShow2(GLvoid)
{
 glClear(GL_COLOR_BUFFER_BIT);//清空颜色缓冲区
 //默认方向时
 if(gbShadeDir)
 {
  if(gfTopGreen<=1.0)
  {
   IncreaseToTop();
  }
  else
  {
   gbShadeDir=false;
  }
 }
 else
 {
  if(gfTopGreen>=0.0)
  {
   DecreaseToTop();
  }
  else
  {
   gbShadeDir=true;
  }
 }
 DrawRect();
}
void SceneShow3(GLvoid)       
{
 glClear(GL_COLOR_BUFFER_BIT);
 glColor4f(1.0, 1.0, 1.0, 1.0);
 glBegin(GL_POLYGON);
 glVertex3f(200, 150, 0.0);
 glVertex3f(600, 150, 0.0);
 glVertex3f(600, 450, 0.0);
 glVertex3f(200, 450, 0.0);
 glEnd();
 glutSwapBuffers();
void SceneShow5(GLvoid)       
{
 glClear(GL_COLOR_BUFFER_BIT);   // 清空颜色缓冲区
 glColor4f(1.0, 1.0, 1.0, 1.0);
 glTranslatef(-0.5, 0.5, 0.0);
 DrawCircle();
 glTranslatef(0.5, -0.5, 0.0);
 glColor4f(1.0, 0.0, 0.0, 1.0);
 glTranslatef(0.5, 0.5, 0.0);
 DrawCircle();
 glTranslatef(-0.5, -0.5, 0.0);
 glColor4f(0.0, 1.0, 0.0, 1.0);
 glTranslatef(-0.5, -0.5, 0.0);
 DrawCircle();
 glTranslatef(0.5, 0.5, 0.0);
 glColor4f(0.0, 0.0, 1.0, 1.0);
 glTranslatef(0.5, -0.5, 0.0);
 DrawCircle();
 glTranslatef(-0.5, 0.5, 0.0);
 glutSwapBuffers();
}
void SceneShow6(GLvoid)       
{
 glClear(GL_COLOR_BUFFER_BIT);   // 清空颜色缓冲区
 glColor4f(1.0, 1.0, 1.0, 1.0);
 GLUquadricObj* qobj = gluNewQuadric();
 gluQuadricDrawStyle(qobj, GLU_FILL);
 gluDisk( qobj, 0.0, 0.5, 50, 1);
 glutSwapBuffers();

void SceneShow(GLvoid)       
{
 glClear(GL_COLOR_BUFFER_BIT);   // 清空颜色缓冲区
 glPushMatrix();
 glTranslatef(-0.5, 0.5, 0.0);
 DrawCircle(1.0, 1.0, 1.0, 1.0);
 glPopMatrix();
 glTranslatef(0.5, 0.5, 0.0);
 DrawCircle(1.0, 0.0, 0.0, 0.6);
 glTranslatef(-0.5, -0.5, 0.0);
 glTranslatef(-0.5, -0.5, 0.0);
 DrawCircle(0.0, 1.0, 0.0, 1.0);
 glTranslatef(0.5, 0.5, 0.0);
 
 glTranslatef(0.5, -0.5, 0.0);
 DrawCircle(0.0, 0.0, 1.0, 0.5);
 glTranslatef(-0.5, 0.5, 0.0);
 glutSwapBuffers();
}
int main(int argc, char* argv[]) 
{
 //glutInit(&argc, argv);  
 ////myinit();
 //glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);  
 //glutInitWindowSize(400, 300);  
 //glutCreateWindow("OpenGL Atom");  
 //glutReshapeFunc(myReshape);  
 //glutDisplayFunc(display);  
 ////glutTimerFunc(500, TimerFunc, 1);  
 ////SetupRC();  
 //glutMainLoop();  
 glutInit(&argc, argv);  
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);  
 glutInitWindowSize(WIDTH, HEIGHT);  
 glutCreateWindow("OpenGL Atom");  
 glutReshapeFunc(SceneInit);  
 glutDisplayFunc(SceneShow);  
 glutTimerFunc(TIME_IN_FRAME, TimerFunc, 1);  
 glutMainLoop();  
 return 1;  
}


当前效果图
阅读(1941) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-02-10 15:41:08

http://v.youku.com/v_show/id_XMTUxNDMzNTc2.html 仇仔演出视频,请大家欣赏,顺便帮顶~~