标题 | OpenGL编程轻松入门之使用颜色 |
内容 | 通过上一节的例子我们已经知道一些简单的使用颜色的方法。这一节我们进一步讲讲颜色的使用。 例2:本例子使用颜色引索模式绘制8个不同颜色的球体,如图二所示。阅读此例时,请主要关注函数palette和DrawColotFans。 glIndex设置当前颜色索引。参数为当前颜色索引。本例中glIndexd 函数的参数j+1对应palette中auxSetOneColor函数中的i+1,auxSetOneColor函数的后三个函数制定对应的颜色,颜色值由变量rgb[8][3]定义。 #include <GL/glut.h> #include <GL/glaux.h> void init(void) { glClearColor(1.0,1.0,1.0,1.0); glClear(GL_COLOR_BUFFER_BIT); glShadeModel(GL_SMOOTH); } void palette(void) { GLint i; static GLfloat rgb[8][3]={{1,0,0},{1,0,0.5},{1,0,1}, {0,0,1},{0,1,1},{0,1,0},{1,1,0},{1,0.5,0}}; for(i = 0;i<8;i++) { auxSetOneColor(i+1,rgb[i][0],rgb[i][1],rgb[i][2]);//设置颜色 } } void DrawColorFans(void) { GLint j; glTranslatef(-15,-15,0); for(j = 0;j<8;j++) { glIndexd(j+1);//设置当前颜色索引 /*在不同位置绘制球体*/ glTranslatef(j,j-1,0); glutSolidSphere(1,20,20); } } void CALLBACK display(void) { palette(); DrawColorFans(); glFlush(); } void CALLBACK reshape(GLsizei w,GLsizei h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(100,1,1,20); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0,0,-15); } void main() { auxInitDisplayMode(AUX_SINGLE|AUX_INDEX); auxInitPosition(100,100,500,500); auxInitWindow("draw the color sphere"); init(); auxReshapeFunc(reshape); auxMainLoop(display); } ![]() 图二:8个不同颜色的球体 |
随便看 |
|
在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。