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

问题描述

现在,我已经形成一种模式超过25个顶点。我想线性插入第一个和最后顶点之间的颜色。问题是,当我写下面的code

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自动完成)在每个顶点,因为,我将有很多不同数量更多的色彩顶点。任何帮助将是非常美联社preciated。

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会插一个顶点和下之间的像素颜色,但我不知道有什么办法让它自动为插值顶点的中间值。通常情况下,这不是特别困难,但 - 你不想写code为每个顶点,无论如何,所以添加计算是pretty的琐碎的:

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的色彩插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 04:11