Faery
Faery

Reputation: 4650

OpenGL - easy way to change the value of a variable interactively

What I want is to be able to change the value of a variable through the window with the graphics, not from the code. It's ok if there are several fixed values and there is a choice only between them. Can you please give me some ideas?

I was thinking of something like this: |--|--|--|--|--|, where each vertical line repesents a value and when you click the line, the value is changed, but it's a bit difficult for me to implement this. Any simpler ideas

Upvotes: 2

Views: 1259

Answers (3)

fen
fen

Reputation: 10125

I suggest using http://www.antisphere.com/Wiki/tools:anttweakbar - AntTweakBar that is a great and simple to use GUI library.

Basically it renders a window where you can easily place your variables.

Initialize this and simply add your variable:

float myFloat = 0.0f;
TwAddVarRW(myBar, "myFloat", TW_TYPE_FLOAT, &myFloat, "min=-10.0 max=10.0 step=0.1");

Upvotes: 0

Schnigges
Schnigges

Reputation: 1326

Different keyboard inputs are probably the easiest way to achieve this. Just use a switch-statement to distinguish between the different possibilites and store the value inside a variable. If you need to access it from your shaders, use the value as a uniform.

Upvotes: 2

Foggzie
Foggzie

Reputation: 9821

Here's a quick keyboard-based solution (I'm assuming C++):

Let's assume your values are all in an array called values:

const int NUM_VALUES = 5;
int values[NUM_VALUES];

Keep track of the selected value:

int selectionIndex = 0;

Make a function to handle key presses:

void keyPressed (unsigned char key, int x, int y) { 
    if (key == GLUT_KEY_LEFT) {
        if(--selectionIndex < 0)
            selectionIndex = NUM_VALUES - 1;
    } 
    else if (key == GLUT_KEY_RIGHT) {
        if(++selectionIndex >= NUM_VALUES)
            selectionIndex = 0;
    }  
    else if (key == GLUT_KEY_UP) {
        values[selectionIndex]++;
    }  
    else if (key == GLUT_KEY_DOWN) {
        values[selectionIndex]--;
    }   
}

Finally, in your initialization, link keyPressed to glutKeyboardFunc:

glutKeyboardFunc(keyPressed); 

Upvotes: 2

Related Questions