-
Notifications
You must be signed in to change notification settings - Fork 2
/
util_opengl.cpp
126 lines (113 loc) · 3.49 KB
/
util_opengl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*----------------------------------------------------------------------------
chugl
Chuck OpenGL library module
Copyright (c) 2014 Spencer Salazar. All rights reserved.
https://ccrma.stanford.edu/~spencer/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
U.S.A.
-----------------------------------------------------------------------------*/
#include "util_opengl.h"
#include <stdlib.h>
#include <math.h>
template<typename V>
inline V &v2d_x(V *v, size_t i) { return v[i*2]; }
template<typename V>
inline V &v2d_y(V *v, size_t i) { return v[i*2+1]; }
void gen2d_ellipse_fan(GLfloat *buf, GLint nVertex, float x, float y, float xRadius, float yRadius)
{
v2d_x(buf,0) = x; v2d_y(buf,0) = y;
float two_pi = 2.0f*M_PI;
for(int i = 1; i < nVertex; i++)
{
float fraction = ((float)(i-1))/(nVertex-2);
v2d_x(buf, i) = x+xRadius*cos(fraction*two_pi);
v2d_y(buf, i) = y+yRadius*sin(fraction*two_pi);
}
}
// courtesy of http://www.cs.rit.edu/~ncs/color/t_convert.html
// r,g,b values are from 0 to 1
// h = [0,360], s = [0,1], v = [0,1]
// if s == 0, then h = -1 (undefined)
// void RGBtoHSV( float r, float g, float b, float *h, float *s, float *v )
// {
// float min, max, delta;
// min = MIN( r, g, b );
// max = MAX( r, g, b );
// *v = max; // v
// delta = max - min;
// if( max != 0 )
// *s = delta / max; // s
// else {
// // r = g = b = 0 // s = 0, v is undefined
// *s = 0;
// *h = -1;
// return;
// }
// if( r == max )
// *h = ( g - b ) / delta; // between yellow & magenta
// else if( g == max )
// *h = 2 + ( b - r ) / delta; // between cyan & yellow
// else
// *h = 4 + ( r - g ) / delta; // between magenta & cyan
// *h *= 60; // degrees
// if( *h < 0 )
// *h += 360;
// }
void HSVtoRGB( t_CKFLOAT *r, t_CKFLOAT *g, t_CKFLOAT *b, t_CKFLOAT h, t_CKFLOAT s, t_CKFLOAT v )
{
int i;
float f, p, q, t;
if( s == 0 ) {
// achromatic (grey)
*r = *g = *b = v;
return;
}
h /= 60; // sector 0 to 5
i = floor( h );
f = h - i; // factorial part of h
p = v * ( 1 - s );
q = v * ( 1 - s * f );
t = v * ( 1 - s * ( 1 - f ) );
switch( i ) {
case 0:
*r = v;
*g = t;
*b = p;
break;
case 1:
*r = q;
*g = v;
*b = p;
break;
case 2:
*r = p;
*g = v;
*b = t;
break;
case 3:
*r = p;
*g = q;
*b = v;
break;
case 4:
*r = t;
*g = p;
*b = v;
break;
default: // case 5:
*r = v;
*g = p;
*b = q;
break;
}
}