// 矩形顶部的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;
}
当前效果图