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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Video by: Grant
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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now