Wie zeichne ich einen Regenbogen in Freeglut?
Ich versuche, eine regenbogenfarbene Handlungslegende in openGL zu zeichnen. Folgendes habe ich bisher:
glBegin(GL_QUADS);
for (int i = 0; i != legendElements; ++i)
{
GLfloat const cellColorIntensity = (GLfloat) i / (GLfloat) legendElements;
OpenGL::pSetHSV(cellColorIntensity*360.0f, 1.0f, 1.0f);
// draw the ith legend element
GLdouble const xLeft = xBeginRight - legendWidth;
GLdouble const xRight = xBeginRight;
GLdouble const yBottom = (GLdouble)i * legendHeight /
(GLdouble)legendElements + legendHeight;
GLdouble const yTop = yBottom + legendHeight;
glVertex2d(xLeft, yTop); // top-left
glVertex2d(xRight, yTop); // top-right
glVertex2d(xRight, yBottom); // bottom-right
glVertex2d(xLeft, yBottom); // bottom-left
}
glEnd();
legendElements
ist die Anzahl der diskreten Quadrate, aus denen der "Regenbogen" besteht.xLeft
,xRight
,yBottom
undyTop
sind die Eckpunkte, aus denen sich jedes Quadrat zusammensetzt.
wo die funktionOpenGL::pSetHSV
sieht aus wie das:
void pSetHSV(float h, float s, float v)
{
// H [0, 360] S and V [0.0, 1.0].
int i = (int)floor(h / 60.0f) % 6;
float f = h / 60.0f - floor(h / 60.0f);
float p = v * (float)(1 - s);
float q = v * (float)(1 - s * f);
float t = v * (float)(1 - (1 - f) * s);
switch (i)
{
case 0: glColor3f(v, t, p);
break;
case 1: glColor3f(q, v, p);
break;
case 2: glColor3f(p, v, t);
break;
case 3: glColor3f(p, q, v);
break;
case 4: glColor3f(t, p, v);
break;
case 5: glColor3f(v, p, q);
}
}
Ich habe diese Funktion vonhttp://forum.openframeworks.cc/t/hsv-color-setting/770
Wenn ich das zeichne, sieht es jedoch so aus:
Was ich möchte, ist ein Spektrum von Rot, Grün, Blau, Indigo, ViolettFarbton. Dies scheint jedoch nicht wirklich das zu sein, was passiert.
Ich verstehe nicht wirklich, wie die RGB / HSV-Konvertierung inpSetHSV()
funktioniert so ist es schwer für mich, das Problem zu identifizieren ..
BEARBEITEN: Hier ist die feste Version, inspiriert von Jongware (die Rechtecke wurden falsch gezeichnet):
// draw legend elements
glBegin(GL_QUADS);
for (int i = 0; i != legendElements; ++i)
{
GLfloat const cellColorIntensity = (GLfloat) i / (GLfloat) legendElements;
OpenGL::pSetHSV(cellColorIntensity * 360.0f, 1.0f, 1.0f);
// draw the ith legend element
GLdouble const xLeft = xBeginRight - legendWidth;
GLdouble const xRight = xBeginRight;
GLdouble const yBottom = (GLdouble)i * legendHeight /
(GLdouble)legendElements + legendHeight + yBeginBottom;
GLdouble const yTop = yBottom + legendHeight / legendElements;
glVertex2d(xLeft, yTop); // top-left
glVertex2d(xRight, yTop); // top-right
glVertex2d(xRight, yBottom); // bottom-right
glVertex2d(xLeft, yBottom); // bottom-left
}
glEnd();