OpenGL 颜色插值 [英] OpenGL Colour Interpolation

查看:22
本文介绍了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.

这是我使用的代码:

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天全站免登陆