任何人都可以解释以下opengl代码?

时间:2015-03-21 06:42:14

标签: opengl visual-c++ graphics

任何人都可以解释当函数调用

时glvertex3fv是如何工作的
GLfloat v[][8][3]={{{-1.5,-1.5,-1.0}, {-1.3,-1.5,-1.0}, {-1.3,-1.3,-1.0}, {-1.5,-1.3,-1.0}, {-1.6,-1.6,1.0}, {-1.4,-1.6,1.0}, {-1.4,-1.4,1.0}, {-1.6,-1.4,1.0}} ,

{{-1.3,-1.5,-1.0}, {-1.1,-1.5,-1.0}, {-1.1,-1.3,-1.0}, {-1.3,-1.3,-1.0}, {-1.4,-1.6,1.0}, {-1.2,-1.6,1.0}, {-1.2,-1.4,1.0}, {-1.4,-1.4,1.0}} ,

{{-1.1,-1.5,-1.0}, {-0.9,-1.5,-1.0}, {-0.9,-1.3,-1.0}, {-1.1,-1.3,-1.0}, {-1.2,-1.6,1.0}, {-1.0,-1.6,1.0}, {-1.0,-1.4,1.0}, {-1.2,-1.4,1.0}} ,

{{-1.5,-1.3,-1.0}, {-1.3,-1.3,-1.0}, {-1.3,-1.1,-1.0}, {-1.5,-1.1,-1.0}, {-1.6,-1.4,1.0}, {-1.4,-1.4,1.0}, {-1.4,-1.2,1.0}, {-1.6,-1.2,1.0}} ,

{{-1.3,-1.3,-1.0}, {-1.1,-1.3,-1.0}, {-1.1,-1.1,-1.0}, {-1.3,-1.1,-1.0}, {-1.4,-1.4,1.0}, {-1.2,-1.4,1.0}, {-1.2,-1.2,1.0}, {-1.4,-1.2,1.0}} ,

{{-1.1,-1.3,-1.0}, {-0.9,-1.3,-1.0}, {-0.9,-1.1,-1.0}, {-1.1,-1.1,-1.0}, {-1.2,-1.4,1.0}, {-1.0,-1.4,1.0}, {-1.0,-1.2,1.0}, {-1.2,-1.2,1.0}} ,

{{-1.5,-1.1,-1.0}, {-1.3,-1.1,-1.0}, {-1.3,-0.9,-1.0}, {-1.5,-0.9,-1.0}, {-1.6,-1.2,1.0}, {-1.4,-1.2,1.0}, {-1.4,-1.0,1.0}, {-1.6,-1.0,1.0}} ,

{{-1.3,-1.1,-1.0}, {-1.1,-1.1,-1.0}, {-1.1,-0.9,-1.0}, {-1.3,-0.9,-1.0}, {-1.4,-1.2,1.0}, {-1.2,-1.2,1.0}, {-1.2,-1.0,1.0}, {-1.4,-1.0,1.0}} ,

{{-1.1,-1.1,-1.0}, {-0.9,-1.1,-1.0}, {-0.9,-0.9,-1.0}, {-1.1,-0.9,-1.0}, {-1.2,-1.2,1.0}, {-1.0,-1.2,1.0}, {-1.0,-1.0,1.0}, {-1.2,-1.0,1.0}}};

GLfloat colors[][3]={{0.0,0.0,0.0}, //black

{1.0,0.0,0.0}, //red

{1.0,1.0,0.0}, //yellow

{0.0,1.0,0.0}, //green

{1.0,0.0,1.0}, //magenta

{0.0,1.0,1.0}}; //cyan ....

void polygon(int a,int b,int c,int d,int i)

{

glBegin(GL_POLYGON);

glColor3fv(colors[a]);

glVertex3fv(v[i][a]); //wt is the meaning of this line

glColor3fv(colors[b]);

glVertex3fv(v[i][b]);

glColor3fv(colors[c]);

glVertex3fv(v[i][c]);

glColor3fv(colors[d]);

glVertex3fv(v[i][d]);

glFlush();

glEnd(); }

void colorcube() {

polygon(0,3,2,1,i); //wt is the meaning of i

polygon(2,3,7,6,i);

polygon(0,4,7,3,i);

polygon(1,2,6,5,i);

polygon(4,5,6,7,i);

polygon(0,1,5,4,i);

}

1 个答案:

答案 0 :(得分:0)

注意:这是OpenGL立即绘图模式。它被弃用并且不鼓励使用近20(!)年。 1996年,OpenGL-1.1引入了顶点阵列,从那时起它就优于立即模式。

glVertex是旧的,已弃用的OpenGL立即绘图模式的一部分。在glBegin ... glEnd块之间,使用任意数量的glVertex调用将基元绘制到屏幕上;控制绘制内容的属性在glVertex调用之前调用任何属性设置调用“选择”然后glVertex将发出什么。

顶点是为其指定的任何属性及其位置的整个组合。

首先让我们看看单点:

glBegin(GL_POINTS); /* for every call of glVertex draw a point */

/* tip the "pen" into a bucket of bright red */
glColor3f(1,0,0);
/* draw a point, with whatever color currently is set at 1,1,1 */
glVertex3f(1,1,1);

/* let's make the pen blue */
glColor3f(0,0,1);

/* wait, we want it to be green */
glColor3f(0,1,0);
/* only the last value set for an attribute matters.
 * so the point drawn at 0,1,0 will be green */
glVertex3f(0,1,0);

/* and so on */
/* ... */

glEnd();

绘制GL_POINTS到GL_LINES或GL_TRIANGLES的区别在于,您必须发出顶点的对或三元组才能显示屏幕上显示的内容(所有内容都带有glBegin,glEnd块)。

现在,您经常拥有某些较大内存缓冲区中包含的属性的数据。在这种情况下,将属性setter函数指向该内存而不是传递参数更有意义。这由OpenGL调用名称中的字母“v”表示。所以

GLfloat red_green_blue[3][3] = {
   {1,0,0},
   {0,1,0},
   {0,0,1}
};

glColor3fv(red_green_blue[0]) /* points to the 'red' part */

这相当于

glColor3f(
    red_green_blue[0][0],
    red_green_blue[0][1],
    red_green_blue[0][2] );

显然'v'形式更短,更容易阅读。

现在在你的代码中,它归结为从多维数组中取消引用位置。你想知道'我'是什么意思。我建议你在一张纸上用铅笔跟踪代码,弄清楚OpenGL中有哪些值。