Select a color using the number in openGL -
you know glcolor3f(), contains red green , blue values. i'm developing simple program should allow select color i'm not sure how set set of rgb color values make callback. example,
int red = (1,0,0); glcolor3f(red);
obviously, has error i've tried other ways make work int[] red = {1,0,0}; still didn't work. can give me suggestions?
update #1: made program test. first code line should global state.
glfloat color[3] = {1,0,0};
then color should changed rand() in following:
int r1 = rand() % 100; if(r1 < 50) color = {0,1,0}; else color = {0,0,1}; glcolor3fv(color);
first: learn proper syntax of c, because this
int red = (1,0,0);
is not valid c. (c not know tuple type). types matter.
well use glfloat array , pass glcolor3fv
(if want use integers must use glcolor3i[v]
.
glfloat const red[3] = {1,0,0}; glcolor3fv(red);
but that's not useful, because not want artificially limit user's choices. present them color selection widget, returns individual color values, in struct
, pass opengl
struct color { enum {color_srgb, color_hsv, color_lab, /* other color spaces */} type; union { struct { glfloat r,g,b}; struct { glfloat h,s,v}; struct { glfloat l,a,b}; /* other color spaces */ }; }; /* defined somewhere else, converts color rgb color space */ void to_rgb(glfloat rgb[3], color const *c); void setcolor(color const *c) { glfloat rgb[3]; to_rgb(rgb, c); glcolor3fv(rgb); }
update due comment
to randomly select set of colors use this:
glfloat const green[] = {0.f, 1.f, 0.f}; glfloat const blue[] = {0.f, 0.f, 1.f};
in c arrays have interesting property, r-value pointer first element. hence practical means, wherever array goes argument, can use pointer.
glfloat const *color; /* pointer, not array */ int r1 = rand() % 100; if(r1 < 50) color = green; else color = blue; glcolor3fv(color);
Comments
Post a Comment