扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
#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); } |
图九:三个互相重叠的矩形 |
#include <stdlib.h> #include <GL/glut.h> GLfloat light_ambient[] = {0.1,0.1,0.1,0.0}; GLfloat light_diffuse[] = {1.0,1.0,1.0,0.0}; GLfloat light_specular[] = {1.0,1.0,1.0,0.0}; GLfloat light_position[] = {-10.0,0.0,5.0,0.0}; GLfloat material_ambient[] = {0.1745,0.01175,0.01175}; GLfloat material_diffuse[] = {0.61424,0.04136,0.04136}; GLfloat material_specular[] = {0.727811,0.626959,0.62659}; GLfloat fogColorWhite[] = {1.0,1.0,1.0,1.0}; GLfloat fogColorGreen[] = {0.0,1.0,0.0,1.0}; float fogDensity = 0.02; void myInit(void) { /*设置背景色*/ glClearColor(0.5,0.5,0.5,1.0); /*设置光照*/ glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient); glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse); glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular); glLightfv(GL_LIGHT0,GL_POSITION,light_position); /*设置材质*/ glMaterialfv(GL_FRONT,GL_AMBIENT,material_ambient); glMaterialfv(GL_FRONT,GL_DIFFUSE,material_diffuse); glMaterialfv(GL_FRONT,GL_SPECULAR,material_specular); glMaterialf(GL_FRONT,GL_SHININESS,0.6*128); glShadeModel(GL_SMOOTH); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE); glFrontFace(GL_CW); /*设置雾化*/ glEnable(GL_FOG); glFogi(GL_FOG_MODE,GL_LINEAR); glFogfv(GL_FOG_COLOR,fogColorWhite); glFogf(GL_FOG_DENSITY,fogDensity); glFogf(GL_FOG_START,0.0); glFogf(GL_FOG_END,15.0); glHint(GL_FOG_HINT,GL_DONT_CARE); } /*根据不同的键值设置不同的雾化效果*/ static void myKey(unsigned char key,int x,int y) { switch(key) { case 33: //shift+1 fogDensity *= 1.10; glFogi(GL_FOG_MODE,GL_EXP); glFogf(GL_FOG_DENSITY,fogDensity); glutPostRedisplay(); break; case 64: //shift+2 fogDensity /= 1.10; glFogi(GL_FOG_MODE,GL_EXP); glFogf(GL_FOG_DENSITY,fogDensity); glutPostRedisplay(); break; case 35: //shift+3 glFogi(GL_FOG_START,0.0); glFogi(GL_FOG_END,8.0); glutPostRedisplay(); break; case 36: //shift+4 glFogi(GL_FOG_MODE,GL_EXP2); glutPostRedisplay(); break; case 37: //shift+5 glFogi(GL_FOG_MODE,GL_LINEAR); glutPostRedisplay(); break; case 94: //shift+6 glFogfv(GL_FOG_COLOR,fogColorGreen); glutPostRedisplay(); break; case 27: //Esc exit(0); default: break; } } /*绘制圆锥体*/ void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); glTranslatef(-4.0,4.0,0.0); glRotatef(30.0,1.0,1.0,0.0); glutSolidCone(1.0,50.0,20.0,20.0); glPopMatrix(); glutSwapBuffers(); } void myReshape(int w,int h) { glViewport(0,0,(GLsizei)w,(GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(100.0,1.0,1.0,20.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0,0.0,-15.5); } int main(int argc,char ** argv) { /*初始化*/ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(300,300); glutInitWindowPosition(100,100); /*创建窗口*/ glutCreateWindow(" FOG "); /*绘制与显示*/ myInit(); glutKeyboardFunc(myKey); glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay); /*进入GLUT事件处理循环*/ glutMainLoop(); return 0; } |
图十:被雾化的圆锥体 |
图十一:雾化为绿色的圆锥体 |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者