AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > VC编程

OpenGL编程轻松入门之使用颜色

51自学网 2015-08-30 http://www.51zixue.net
  通过上一节的例子我们已经知道一些简单的使用颜色的方法。这一节我们进一步讲讲颜色的使用。

  例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个不同颜色的球体

 

 

 
说明
:本教程来源互联网或网友上传或出版商,仅为学习研究或媒体推广,51zixue.net不保证资料的完整性。
上一篇:OpenGL编程轻松入门之坐标变换  下一篇:OpenGL编程轻松入门之特殊效果操作