技术开发 频道

OpenGL编程入门之颜色与坐标

【IT168技术文档】

  通过上一节的例子我们已经知道一些简单的使用颜色的方法。这一节我们进一步讲讲颜色的使用。
 
  本例子使用颜色引索模式绘制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); }

0
相关文章