| 网站首页 | 技术文章 | 下载频道 | 博客 | 编程论坛 |
 
| 技术教程首页 | 开发语言 | WEB开发 | .NET技术 | 数据库 | 操作系统 | 网页制作 |
 
 
您现在的位置: 编程中国 >> 技术教程 >> 开发语言 >> VC++ >> VC教程 >> 正文
  ►  OpenGL编程轻松入门之特殊效果操作
OpenGL编程轻松入门之特殊效果操作
作者:黄燕    阅读人次:……    文章来源:天极开发    发布时间:2006/9/17    网友评论()条
 
  每个物体在不同的环境在会有不同的视觉效果。为了使效果更加真实需要增加特殊效果。

  例8:绘制三个互相重叠的矩形,这三个矩形从左到右分别为绿、红、蓝。其中红色、蓝色矩形的透明度分别为50%的透明度,即alpha值为0.5,如图九所示。

#include <stdlib.h>
#include <GL/glut.h>
void myInit(void)
{
 glClearColor(0.2,0.8,0.8,0.0);//将背景设置为湖蓝色
 glEnable(GL_BLEND);//激活GL_BLEND
 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);//指定像素的数学算法
 glEnable(GL_FLAT);
}

void myDisplay(void)
{
 glClear(GL_COLOR_BUFFER_BIT);

 /*绘制一个绿色的矩形 alpha = 1.0*/
 glColor4f(0.0,1.0,0.0,1.0);

 glBegin(GL_POLYGON);
 glVertex3f(-0.75,0.5,0.0);
 glVertex3f(-0.75,-0.75,0.0);
 glVertex3f(0.5,-0.75,0.0);
 glVertex3f(0.5,0.5,0.0);
 glEnd();

 /*绘制一个红色的矩形 alpha = 0.5*/
 glColor4f(1.0,0.0,0.0,0.5);

 glBegin(GL_POLYGON);
 glVertex3f(-0.25,1.0,0.0);
 glVertex3f(-0.25,-0.25,0.0);
 glVertex3f(1.0,-0.25,0.0);
 glVertex3f(1.0,1.0,0.0);
 glEnd();

 /*绘制一个蓝色的矩形 alpha = 0.5*/
 glColor4f(0.0,0.0,1.0,0.5);

 glBegin(GL_POLYGON);
 glVertex3f(0.25,1.5,0.0);
 glVertex3f(0.25,0.25,0.0);
 glVertex3f(1.5,0.25,0.0);
 glVertex3f(1.5,1.5,0.0);
 glEnd();

 glFlush();
}

void myReshape(int w,int h)
{
 glViewport(0,0,w,h);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();

 if(w<h)
  glOrtho(-1.5,1.5,-1.5*(GLfloat)h/(GLfloat)w,1.5*(GLfloat)h/(GLfloat)w,-1.5,1.5);
 else
  glOrtho(-1.5*(GLfloat)w/(GLfloat)h,1.5*(GLfloat)w/(GLfloat)h,-1.5,1.5,-1.5,1.5);

 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
 glTranslatef(-0.4,0.0,0.0);
}

int main(int argc,char ** argv)
{
 /*初始化*/
 glutInit(&argc,argv);
 glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
 glutInitWindowSize(300,400);
 glutInitWindowPosition(200,200);

 /*创建窗口*/
 glutCreateWindow("BLEND POLYGON");

 /*绘制与显示*/
 myInit();
 glutReshapeFunc(myReshape);
 glutDisplayFunc(myDisplay);

 /*进入GLUT事件处理循环*/
 glutMainLoop();
 return(0);
}


图九:三个互相重叠的矩形


  ·glBlendFunc(GLenum sfactor,GLenum dfactor) 指定像素算法。sfactor指定红,绿,蓝及alpha源混合因素是如何计算的。dfactor指定红,绿,蓝及alpha目标混合因素是如何计算的。

[1] [2] [3] 下一页

 

 
文章录入:静夜思    责任编辑:静夜思 
  • 上一篇文章:

  • 下一篇文章:

  •  
    相关文章
    原创地带
    24小时热门帖子