比顶点少的颜色 [英] Fewer Colors Than Vertices

查看:182
本文介绍了比顶点少的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在旧的已弃用的OpenGL中,我们可以这样做:

In the old deprecated OpenGL, we could do something like this:

glBegin(...);
   glColor3f(r_1,g_1,b_1);
   glVertex3f(x_1, y_1, z_1);
   glVertex3f(x_2, y_2, z_2);
   ...
   glVertex3f(x_n, y_n, z_n);

   glColor3f(r_2, g_2, b_2);
   glVertex3f(x_(n+1), y_(n+1), z_(n+1));
   glVertex3f(x_(n+2), y_(n+2), z_(n+2));
   ...
   glVertex3f(x_2n, y_2n, z_2n);

   ...
glEnd();

也就是说,我认为每n个连续的顶点共享相同的颜色。使用新的和非过时的OpenGL也可以做到这一点吗?

That is, I am saying that each n consecutive vertices share the same color. Is the same possible to be done with the new and non-deprecated OpenGL?

例如,如果我有一个立方体,这意味着我有36个顶点。如果我想让每个面具有1种颜色,则每个连续的6个顶点必须共享该颜色。目前,我已人为地将每种颜色的颜色数据复制6次,使得顶点数组和颜色数组的大小相同。有什么其他方法吗?希望我的问题很清楚。

For example, if I have a cube, it means that I have 36 vertices. If I want each face to have 1 color, then each consecutive 6 vertices must share that color. Currently I have artificially copied the color data 6 times for each color so that the sizes of vertex array and color array are the same. Is there any other way around this? Hope my question was clear.

推荐答案

也许这种伪代码可以为你清除:

Maybe this kind of pseudocode clears things up for you:

GLfloat color_state{r,g,b};
GLfloat normal_state{x,y,z};
...

glColor4f(r,g,b,a):
    color_state.r = r
    color_state.g = g
    color_state.b = b
    color_state.a = a

glNormalf(x,y,z):
    normal_state.x = x
    normal_state.y = y
    normal_state.z = z

glVertex3f(x,y,z):
    __glinternal_submit_vertex(
        position = {x,y,z},
        normal = normal_state,
        color = color_state,
        ... );

这是OpenGL的内部工作原理。颜色不在顶点之间共享。从当前状态为每个提交的顶点创建一个副本。现在,使用顶点数组和顶点缓冲区对象,负担取决于你,做正确的数据复制。

This is how OpenGL immediate works internally. Colors were not shared among vertices. A copy was created from the current state for each vertex submitted. Now, with Vertex Arrays and Vertex Buffer Object the burden lies upon you, to do the right data duplication.

这篇关于比顶点少的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆