OpenGL/C++ -- Color Scheme function

Posted on 2008-09-29
Last Modified: 2013-12-06
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?

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?  


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);

Open in new window

Question by:ExpExchHelp
  • 2
  • 2

Expert Comment

ID: 22603440

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)

then, your code are correct.

I think your compiler error are a bad conversion between float [] and float *, is it correct?
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

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"?

Any additional advise is appreciated.


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 ;)

so what about:

#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;

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

Author Closing Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question