OpenGL色彩插值 [英] OpenGL Colour Interpolation

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

问题描述

我目前正在使用C ++和OpenGL进行一个小项目,并且正在尝试实现一种类似于photoshop中的颜色选择工具,如下所示.

I'm currently working on an little project in C++ and OpenGL and am trying to implement a colour selection tool similar to that in photoshop, as below.

但是,我在插入大正方形时遇到麻烦. 在使用8800 GTS的台式计算机上工作时,结果是相似的,但融合并不那么顺利.

However I am having trouble with interpolation of the large square. Working on my desktop computer with a 8800 GTS the result was similar but the blending wasn't as smooth.

这是我正在使用的代码:

This is the code I am using:

GLfloat swatch[] = { 0,0,0, 1,1,1, mR,mG,mB, 0,0,0 };
GLint swatchVert[] = { 400,700, 400,500, 600,500, 600,700 };

glVertexPointer(2, GL_INT, 0, swatchVert);
glColorPointer(3, GL_FLOAT, 0, swatch);
glDrawArrays(GL_QUADS, 0, 4);

移动到装有Intel Graphics HD 3000的笔记本电脑上时,即使没有代码更改,此结果甚至更糟.

Moving onto my laptop with Intel Graphics HD 3000, this result was even worse with no change in code.

我以为是OpenGL将四边形分成两个三角形,所以我尝试使用三角形进行渲染,然后自己在正方形的中间插值颜色,但这仍然与我期望的结果不完全相同.

I thought it was OpenGL splitting the quad into two triangles, so I tried rendering using triangles and interpolating the colour in the middle of the square myself but it still doesnt quite match the result I was hoping for.

推荐答案

OpenGL对从顶点着色器发出的值到片段着色器的每个片段输入值使用重心插值.正如您已经猜对的那样,您看到的是将四边形分割为三角形的效果.

OpenGL uses barycentric interpolation on the values emitted from a vertex shader to the per-fragment input values of a fragment shader. What you see there is the effect of splitting the quad into triangles as you already did guess right.

现在看一下:其中有一个右上角的三角形,上面有红色,因此向下插值很好,其中带有红色的数量.然后是左下角的三角形,其中只有灰色.当然,右上角三角形的红色不会导致左下角三角形的灰色.

Now look at it: There's the top right triangle which has red in it, and hence interpolates nicely down with a red amount in it. And then there's the bottom left triangle with only gray in it. Of course the red of the top right triangle will not contribute to the gray of the lower left one.

问题是,插值发生在RGB空间中,但是要获得所需的结果,必须将其放置在HSV或HSL空间中,其中H(色相)应保持恒定.

The problem is, that the interpolation happens in RGB space, but for your desired outcome would have to be placed in HSV or HSL space, where H (Hue) would be kept constant.

但是,这不仅仅是插值问题.您遇到的问题还在于,颜色不是线性插值的;而是线性插值.大多数显示器都应用了非线性函数,也称为伽马"(实际上,伽马是施加到输入值的幂的指数).

However it's not just a matter of interpolation. What gets in your way as well is, that colours don't interpolate linearly; most displays have a nonlinear function applied, also known as the "gamma" (actually gamma is the exponent of the power applied to the input values).

您的OpenGL上下文可能在颜色校正的颜色空间中,也可能不在颜色校正的颜色空间中.您需要对此进行测试.然后,知道帧缓冲区所在的颜色空间后,必须使用片段着色器将重心坐标的每个片段变换(使用顶点着色器评估)应用于每个片段值.

Your OpenGL context may be in a color corrected color space or not. You need to test for this. Then knowing in which color space the framebuffer resides, you must apply a per fragment transform of the barycentric coordinates (evaluated using a vertex shader) into per fragment values using a fragment shader.

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

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