简体   繁体   English

用负参数调用glClearColor时会发生什么?

[英]What happens when you call glClearColor with a negative argument?

I'm working on a simple OpenGL application that allows the user to adjust the background color of a simple window by clicking and dragging to either then left (make it less intense) or the right (more intense). 我正在开发一个简单的OpenGL应用程序,该应用程序允许用户通过单击并拖动到左侧(使其强度降低)或右侧(强度更高)来调整简单窗口的背景色。 When dragging to the right, for example, I have something like this: 例如,当向右拖动时,我会看到以下内容:

intensity += 0.0001*x;  //where x is the current mouse location 

...and to the left, the other way: ...向左,另一种方式:

intensity -= 0.0001*x;

intensity is global static GLfloat that gets passed to glClearColor in the display function, which is called multiple times. intensity是被传递给了全局静态GLfloat glClearColor在显示功能,这被称为多次。 This strategy works just fine when the user intensifies the color while dragging right, but it messes up after a certain point when dragging left: the color becomes less intense as I drag along and returns to black as it should, but then intensifies back into the original color. 当用户在向右拖动的同时增强颜色时,此策略效果很好,但在向左拖动的特定点后会变得混乱:当我拖动时,颜色变得不那么强烈,然后应恢复为黑色,但随后又变回黑色。原始颜色。

I think the problem is that the intensity, which should be between 0 and 1, is becoming negative when I drag too far and, thus, glClearColor is fixing the problem by just changing the sign back to positive (?). 我认为问题在于,当我拖动得太远时,应该在0到1之间的强度会变为负值,因此glClearColor只是通过将符号改回正值(?)来解决此问题。 If that is the case, how can I enforce that the values passed to glClearColor stay in the proper range? 在这种情况下,如何强制传递给glClearColor的值保持在适当的范围内? I tried to fix the problem by making sure that the current intensity would not become negative/adjustment would not happen if it is already too low, but it didn't seem to work: 我试图通过确保当前强度不会太低来解决该问题,如果它已经太低,则不会进行调整,但是它似乎没有用:

if(x <= xPrev && intensity >= (.0001*x))  //moving left to de-intensify color
  intensity -= 0.0001*x;

...Am I doing this incorrectly, or is glClearColor handling negative values differently than I expect? ...我做错了吗,还是glClearColor处理负值的方式与我预期的不同?

Another interesting issue that came up is that my "intensity" adjustments only seem to work on the r, g, and/or b values (ie, I pass "intensity" as one of those in glClearColor . Using the a value didn't work...but isn't it supposed to change color intensity? 出现的另一个有趣的问题是,我的“强度”调整似乎只对r,g和/或b值有效(即,我将glClearColor “强度”作为glClearColor中的值之一glClearColor 。使用a值没有工作...但是不应该改变颜色强度吗?

This sounds a bit like a hysteresis problem. 这听起来有点像磁滞问题。 It's not clear how events are received, or what coordinate system x is in. Perhaps drop an 'anchor' ( xStart ) when the button is pressed, and handle all subsequent values while dragging as an offset from this value. 目前尚不清楚事件的接收方式或x所在的坐标系。可能会在按下按钮时放下“ anchor”( xStart ),并处理所有后续值,同时从该值开始偏移以进行拖动。 What toolkit / library are you using for windows and events? 您使用什么工具包/库来处理Windows和事件? If you launch from a console, perhaps add a trace statements like: 如果从控制台启动,则可能添加跟踪语句,例如:

fprintf(stderr, "%.8e\\n", x);

to see if the values of x correspond to what you're expecting on each frame. 看看x的值是否与您期望的每一帧相对应。

glClearColor clamps values to [0, 1] , so it's probably not the issue. glClearColor将值限制为[0, 1] ,所以可能不是问题。 Perhaps a trace for intensity would be useful? 也许追踪intensity会有用吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我们用负参数调用Malloc时会发生什么? - what happens when we call Malloc with negative parameter? C函数指针-删除参数时会发生什么? - C Function Pointers - What happens when you drop an argument? 在现有变量上调用malloc时会发生什么? - What happens when you call malloc on an existing variable? 视觉上当您使用&符号调用相同的进程时会发生什么 - Visually what happens when you call same process with ampersand 当你调用 read() 的长度太大时会发生什么? - What happens when you call read() with length that is too large? 调用方法/函数时汇编语言会发生什么? - What happens in assembly language when you call a method/function? 当按位 AND 应用于负数时会发生什么? - What happens when bitwise AND is applied to negative numbers? 当您对与 dup2() 复制的管道文件描述符调用 close() 时会发生什么? - What happens when you call close() on a pipe file descriptor that was duplicated with dup2()? 当您调用带有返回值的函数而不将其分配给任何变量时会发生什么? - What happens when you call a function with return value without assigning it to any variable? 如果你不是浮动的话,会发生什么? - What happens when you logical not a float?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM