第四讲 使用颜色

通过上一节的例子我们已经知道一些简单的使用颜色的方法。这一节我们进一步讲讲颜色的使用。

  例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个不同颜色的球体
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值