?
Solved

OpenGL/C++ -- Color Scheme function

Posted on 2008-09-29
5
Medium Priority
?
896 Views
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?
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);

Open in new window

0
Comment
Question by:ExpExchHelp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:jose_juan
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)

then, your code are correct.

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

Expert Comment

by:ikework
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

by:ExpExchHelp
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.

EEH


0
 
LVL 20

Accepted Solution

by:
ikework earned 2000 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;
glColor3fv(color[index]);


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

Author Closing Comment

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Suggested Courses

771 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