顶点间的OpenGL颜色插值 [英] OpenGL Color Interpolation across vertices

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

问题描述

现在,我有超过 25 个顶点构成了一个模型.我想在第一个和最后一个顶点之间线性插入颜色.问题是当我编写以下代码时

Right now, I have more than 25 vertices that form a model. I want to interpolate color linearly between the first and last vertex. The Problem is when I write the following code

glColor3f(1.0,0.0,0.0);
vertex3f(1.0,1.0,1.0);
vertex3f(0.9,1.0,1.0);
.
.`<more vertices>;
glColor3f(0.0,0.0,1.0);
vertex3f(0.0,0.0,0.0);

除了最后一个顶点之外的所有顶点都是红色的.现在我想知道是否有一种方法可以在这些顶点之间插入颜色,而不必在每个顶点手动插入颜色(而不是本机,就像 opengl 自动执行的那样),因为我将在不同的地方有更多的颜色顶点.任何帮助将不胜感激.

All the vertices except that last one are red. Now I am wondering if there is a way to interpolate color across these vertices without me having to manually interpolate color (instead natively, like how opengl does it automatically) at each vertex since, I will be having a lot more number of colors at various vertices. Any help would be extremely appreciated.

谢谢!

推荐答案

OpenGL 将在一个顶点和下一个顶点之间插入像素的颜色,但我不知道有什么方法可以让它自动插入中间顶点的值.通常,这并不是特别困难——无论如何您都不想为每个单独的顶点编写代码,因此添加计算非常简单:

OpenGL will interpolate colors of pixels between one vertex and the next, but I don't know of any way to get it to automatically interpolate the values for intermediate vertexes. Normally, that's not particularly difficult though -- you don't want to write code for each individual vertex anyway, so adding the computation is pretty trivial:

class pointf { 
    GLfloat x, y, z;
};

std::vector<pointf> spots;

// ...
GLfloat start_blue = 1.0f;
GLfloat end_blue = 0.0f;
GLfloat start_green = 0.0f;
GLfloat end_green = 0.0f;
GLfloat start_red = 0.0f;
GLfloat end_red = 1.0f;

GLfloat size = spots.size();

glBegin(GL_POLYGON);
for (int i=0; i<spots.size(); i++) {
    GLfloat red = start_red + (end_red-start_red) * i/size;
    GLfloat green = start_green + (end_green-start_green) * i/size;
    GLfloat blue = start_blue + (end_blue-start_blue) * i/size;

    glColor3f(red, green, blue);
    glVertex3f(spots[i].x, spots[i].y, spots[i].z);
}
glEnd();

但有一件事:这对每个顶点进行纯线性插值.例如,它不会尝试考虑一个顶点和下一个顶点之间的距离.我猜想如何做这样的事情的问题是(至少部分)为什么 OpenGL 不自己尝试这样做.

One thing though: this does purely linear interpolation per vertex. It does not, for example, attempt to take into account the distance between one vertex and the next. I'd guess questions of how to do things like this are (at least part of) why OpenGL doesn't attempt this on its own.

对于跨半球的渐变,我会尝试这样的事情:

for a gradient across a hemisphere, I'd try something like this:

// Blue light on the left  
GLfloat blue[] = {0.0f, 0.0f, 1.0f, 1.0f};
GLfloat blue_pos[] = {-1.0f, 0.0f, -0.3f, 0.0f};

// red light on the right
GLfloat red[] = {1.0f, 0.0f, 0.0f, 1.0f};
GLfloat red_pos[] = {1.0f, 0.0f, -0.3f, 0.0f};

// turn on lighting:
glEnable(GL_LIGHTING);
glShadeModel(GL_SMOOTH);

// Set up the two lights:
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, blue);
glLightfv(GL_LIGHT0, GL_POSITION, blue_pos);

glEnable(GL_LIGHT1);
glLightfv(GL_LIGHT1, GL_DIFFUSE, red);
glLightfv(GL_LIGHT1, GL_POSITION, red_pos);

// set up the material for our sphere (light neutral gray):
GLfloat sph_mat[] = {0.8f, 0.8f, 0.8f, 1.0f};
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, sph_mat);

// Draw a sphere:
GLUquadric *q = gluNewQuadric();
gluQuadricOrientation(q, GLU_OUTSIDE);
gluQuadricDrawStyle(q, GLU_FILL);
gluQuadricNormals(q, GLU_SMOOTH);
gluSphere(q, 1.0, 64, 64);

目前,我已经完成了一个球体的外部,但做一个半球并没有太大的不同.

For the moment, I've done the outside of a sphere, but doing a hemisphere isn't drastically different.

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

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