[Turn] Introduction to OpenGL - Lesson 4

       

        OpenGL supports two color modes: one is RGBA and the other is color index mode.
       Regardless of the color mode, the computer must save some data for each pixel. The difference is that in the RGBA mode, the data directly represents the color; in the color index mode, the data represents an index. To get the real color, you must look up the index table.

1. RGBA color
       In RGBA mode, each pixel will save the following data: R value (red component), G value (green component), B value (blue component) and A value (alpha component). Among them, the combination of red, green, and blue can get the various colors we need, and alpha does not directly affect the color, it will be introduced later.
Choosing a color in RGBA mode is very simple, and only needs a function to do it.
The glColor* series of functions can be used to set colors. The three-parameter version can specify the values ​​of R, G, and B, and the A value is the default; the four-parameter version can specify the values ​​of R, G, B, and A, respectively. For example:
void glColor3f(GLfloat red, GLfloat green, GLfloat blue);
void glColor4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha);
(Remember? 3f means there are three floating point parameters ~ see in the second lesson A description of the glVertex* functions.)
takes a floating point number as a parameter, where 0.0 means not to use this color, and 1.0 means to use this color the most. For example:
glColor3f(1.0f, 0.0f, 0.0f); Indicates that green and blue are not used, and red is used the most, so the purest red is obtained.
glColor3f(0.0f, 1.0f, 1.0f); Indicates to use green, blue to the most, not red. The result of the blend is a light blue.
glColor3f(0.5f, 0.5f, 0.5f); Indicates that half of each color is used, and the effect is gray.
Note: Floating-point numbers can be accurate to a few decimal places, which does not mean that computers can display so many colors. In reality, the number of colors a computer can display will be determined by the hardware. If OpenGL can't find an exact color, it does something like "rounding up".

You can draw rectangles of different colors by changing the parameter value of glColor3f in the following code.

 

void myDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0f, 1.0f, 1.0f);
    glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
    glFlush();
}

 

Note: The glColor series functions, when the parameter types are different, the value representing the "largest" color is also different.
A function suffixed with f and d, with 1.0 for maximum usage.
A function that uses b as a suffix, with 127 representing the maximum use.
A function that uses ub as the suffix, with 255 representing the maximum use.
A function suffixed with s, with 32767 representing the maximum usage.
The function that uses us as the suffix is ​​65535 to indicate the maximum use.
These rules may seem cumbersome, but after being familiar with them, there will be no obstacles in actual use.

 

       Effect picture:

      

 

 

2. Indexed color
      In indexed color mode, OpenGL requires a color table. This table is equivalent to a painter's palette: although many colors can be called up, the number of colors that exist on the palette at the same time will not exceed the number of grids in the palette. Think of each entry in the color table as a grid on the palette: it holds a color.
When drawing in indexed color mode, when I say "I set the ith color to so-and-so", I actually set the ith style of the palette to so-and-so color. "I need the kth color to paint", then use the brush to dip the kth color palette.
The size of the color table is very limited, generally between 256 and 4096, and is always an integer power of 2. When plotting with indexed color, always set the color table first, then select the color.

2.1. Selecting a color
       Use the glIndex* series of functions to select a color in the color table. The most common of these is probably glIndexi, whose argument is an integer.
void glIndexi(GLint c);
Yes, this is really simple.

2.2. Setting the color table
       OpenGL does not directly provide a method for setting the color table, so setting the color table requires the support of the operating system. Windows and most other graphical operating systems we use have this feature, but use a different function. Just as I didn't describe how to write my own code to create a window under Windows, I also won't describe how to set the color table under Windows.
The GLUT toolkit provides the function glutSetColor to set the color table, but my tests always have problems. Now to give you a taste of indexed colors, I introduce you to another OpenGL toolkit: aux. This toolkit comes with VisualStudio and does not need to be installed separately, but it is out of date, here is just an experience, you don't need to go deep.

 

#include <windows.h>
#include <GL/gl.h>
#include <GL/glaux.h>

#pragma comment (lib, "opengl32.lib")
#pragma comment (lib, "glaux.lib")

#include <math.h>
const GLdouble Pi = 3.1415926536;
void myDisplay(void)
{
    int i;
    for(i=0; i<8; ++i)
        auxSetOneColor(i, (float)(i&0x04), (float)(i&0x02), (float)(i&0x01));
    glShadeModel(GL_FLAT);
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_TRIANGLE_FAN);
    glVertex2f(0.0f, 0.0f);
    for(i=0; i<=8; ++i)
    {
        glIndexi(i);
        glVertex2f(cos(i*Pi/4), sin(i*Pi/4));
    }
    glEnd();
    glFlush();
}

int main(void)
{
    auxInitDisplayMode(AUX_SINGLE|AUX_INDEX);
    auxInitPosition(0, 0, 400, 400);
    auxInitWindow(L"");
    myDisplay();
    Sleep(10 * 1000);
    return 0;
}

 

      其它部分大家都可以不管,只看myDisplay函数就可以了。首先,使用auxSetOneColor设置颜色表中的一格。循环八次就可以设置八格。
glShadeModel等下再讲,这里不提。
然后在循环中用glVertex设置顶点,同时用glIndexi改变顶点代表的颜色。
最终得到的效果是八个相同形状、不同颜色的三角形。

      索引颜色虽然讲得多了点。索引颜色的主要优势是占用空间小(每个像素不必单独保存自己的颜色,只用很少的二进制位就可以代表其颜色在颜色表中的位置),花 费系统资源少,图形运算速度快,但它编程稍稍显得不是那么方便,并且画面效果也会比RGB颜色差一些。“星际争霸”可能代表了256色的颜色表的画面效 果,虽然它在一台很烂的PC上也可以运行很流畅,但以目前的眼光来看,其画面效果就显得不足了。
目前的PC机性能已经足够在各种场合下使用RGB颜色,因此PC程序开发中,使用索引颜色已经不是主流。当然,一些小型设备例如GBA、手机等,索引颜色还是有它的用武之地。

 

3、指定清除屏幕用的颜色
     我们写:glClear(GL_COLOR_BUFFER_BIT);意思是把屏幕上的颜色清空。
但实际上什么才叫“空”呢?在宇宙中,黑色代表了“空”;在一张白纸上,白色代表了“空”;在信封上,信封的颜色才是“空”。
OpenGL用下面的函数来定义清楚屏幕后屏幕所拥有的颜色。
在RGB模式下,使用glClearColor来指定“空”的颜色,它需要四个参数,其参数的意义跟glColor4f相似。
      在索引颜色模式下,使用glClearIndex来指定“空”的颜色所在的索引,它需要一个参数,其意义跟glIndexi相似。

 

void myDisplay(void)
{
    glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    glFlush();
}

 
呵,这个还真简单~

 

 

 

4、指定着色模型
     OpenGL允许为同一多边形的不同顶点指定不同的颜色。例如:

 

#include <math.h>
const GLdouble Pi = 3.1415926536;
void myDisplay(void)
{
    int i;
    // glShadeModel(GL_FLAT);
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_TRIANGLE_FAN);
    glColor3f(1.0f, 1.0f, 1.0f);
    glVertex2f(0.0f, 0.0f);
    for(i=0; i<=8; ++i)
    {
        glColor3f(i&0x04, i&0x02, i&0x01);
        glVertex2f(cos(i*Pi/4), sin(i*Pi/4));
    }
    glEnd();
    glFlush();
}

 

       平滑效果图:



      单色效果图:

            

 


       在默认情况下,OpenGL会计算两点顶点之间的其它点,并为它们填上“合适”的颜色,使相邻的点的颜色值都比较接近。如果使用的是RGB模式,看起来就 具有渐变的效果。如果是使用颜色索引模式,则其相邻点的索引值是接近的,如果将颜色表中接近的项设置成接近的颜色,则看起来也是渐变的效果。但如果颜色表 中接近的项颜色却差距很大,则看起来可能是很奇怪的效果。
使用glShadeModel函数可以关闭这种计算,如果顶点的颜色不同,则将顶点之间的其它点全部设置为与某一个点相同。(直线以后指定的点的颜色为准,而多边形将以任意顶点的颜色为准,由实现决定。)为了避免这个不确定性,尽量在多边形中使用同一种颜色。
glShadeModel的使用方法:
glShadeModel(GL_SMOOTH);   // 平滑方式,这也是默认方式
glShadeModel(GL_FLAT);     // 单色方式

 

 

小结:
本课学习了如何设置颜色。其中RGB颜色方式是目前PC机上的常用方式。
可以设置glClear清除后屏幕所剩的颜色。
可以设置颜色填充方式:平滑方式或单色方式。

转自http://blog.csdn.net/andyhuabing/article/details/6957252

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326486810&siteId=291194637