Solved

# OpenGL/C++ -- Color Scheme function

Posted on 2008-09-29
874 Views
I'm a beginner in OpenGL / C programming.

For a program that we're modifying, we need to apply a 10-color schema that will do the following:
a) determine what range a point/area is within a bell curve
b) depending on the value of the point/area, apply a color

Currently, I'm using the IF/ELSE IF approach (as shown in the snippet below).    The function works and it draws the bell curve in the designated colors.   However, instead of the IF / ELSE IF, we are supposed to use an array.

The next line of code defines my array... and it compiles fine.
float color [10][3]={{1.00, 0.00, 0.00},{1.00, 0.60, 0.01},{1.00, 1.00, 0.00},{0.60, 0.80, 0.00},{0.00, 0.50, 0.00},{0.60, 0.80, 1.00},{0.20, 0.40, 1.00},{0.80, 0.60, 1.00},{0.50, 0.00, 0.50},{0.50, 0.50, 0.50}};

However, the next line of code throws a compile error.  I'm not sure as to how I should modify the line of code.   Any hints?
glColor3FV(color[(int)(z*10.0/1.0)]);

I believe the "z" may (or may not) be replaced with the >> normal(x, y) <<.   Again, I'm not sure though.

Can anyone provide me some help on this?

Thanks,
EEH

``````if (0.000 <= normal(x, y) <= 0.010)

glColor3f(1.00, 0.00, 0.00);

else if (0.011 < normal(x, y) <= 0.020)

glColor3f(1.00, 0.60, 0.01);

else if (0.021 < normal(x, y) <= 0.030)

glColor3f(1.00, 1.00, 0.00);

else if (0.031 < normal(x, y) <= 0.040)

glColor3f(0.60, 0.80, 0.00);

else if (0.041 < normal(x, y) <= 0.050)

glColor3f(0.00, 0.50, 0.00);

else if (0.051 < normal(x, y) <= 0.060)

glColor3f(0.60, 0.80, 1.00);

else if (0.061 < normal(x, y) <= 0.070)

glColor3f(0.20, 0.40, 1.00);

else if (0.071 < normal(x, y) <= 0.080)

glColor3f(0.80, 0.60, 1.00);

else if (0.081 < normal(x, y) <= 0.090)

glColor3f(0.50, 0.00, 0.50);

else if (0.091 < normal(x, y) <= 1.000)

glColor3f(0.50, 0.50, 0.50);
``````
0
Question by:ExpExchHelp
• 2
• 2

LVL 5

Expert Comment

ID: 22603440
Hi,

yes, you can make all you think (array and not use intermediate z).

you don't explain the compiler error... but

I think, normal between 0 and 1 (you must be sure! not negative values!) then

0 <= z <= 1
0 <= z * 10 <= 10
0 <= [ z * 10 ] <= 10 (round operator)

I think your compiler error are a bad conversion between float [] and float *, is it correct?
0

LVL 20

Expert Comment

ID: 22603608
glColor3FV should be glColor3fv

but as jose said already, please show us the compiler error and the definition of all variables you use and how they are computed, i.e. "z"

cheers ike
0

Author Comment

ID: 22604636
ikework -- yes, you're right, changing glColor3FV to glColor3fv fixed the error.  It compiles and builds fine now.

Current Status:
The program works now; however, the color distribution is very coarse.    When I first used the IF / ELSE IF statements, I used the following:
if (0.00 <= normal(x, y) <= 0.10)     .... given that 80% seemed to be "orange" (2nd color element), I changed the values to:
if (0.000 <= normal(x, y) <= 0.010)   ... that seemed to work fine but I never saw any "red" (1st color element).

Now, using >> glColor3fv(color[(int)(z*10.0/1.0)]); << I still get a coarse color distribution (red and orange or 1st & 2nd color element only.

Given that I need to stay between 0 and 1 AND need to use 10 colors, I presume the "*10" is based on those 10 colors, right?    I don't know as to why I'd divide by "1"?   Is this necessary?   How could I create a "finer" distribution"?

EEH

0

LVL 20

Accepted Solution

ikework earned 500 total points
ID: 22605509
>> I don't know as to why I'd divide by "1"?   Is this necessary?

no, 10/1 is 10 ;)

#include "assert.h"

// make sure "z" is not out of range
assert( z >= 0.0f );
assert( z <= 1.0f );
int index = (int)(z * 10.0f);
// clamp it to a range between 0 .. 9, index might be 10 for z == 1.0f
if( 10 == index ) index = 9;
glColor3fv(color[index]);

well, again i'm assuming "z" is declared as float or double, is it?
0

Author Closing Comment

ID: 31501415
Thanks -- I think it works fine now.   I appreciate your support.
0

## Featured Post

### Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were smallâ€¦
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I wasâ€¦
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.