0

So let's say I have a variable called

current_color

and the string assigned to current_color is constantly changing.

Additionally I have a cube in PyOpenGl.py

To assign the cube's color I use

glColor3fv((0, 1, 0))

Would there be a way (referencing a color library) to have

glColor3fv((current_color))

and every time that variable updates, it changes the cubes color? To my understanding OpenGL is a state machine so I'd need to redraw the scene, or cube. Would you have any suggestions on how to do that?

Richard Wenzel
  • 65
  • 1
  • 2
  • 5
  • 1
    Every time you make a change to the data (like the color) you need to use `glColorxxx`again. To avoid such all-data-transfer-again you better move to modern OpenGL (version >= 3.2) and pass just an *uniform*. – Ripi2 Jun 21 '18 at 18:54
  • @Ripi2 PyOpenGL is only at 3.1 I believe – Richard Wenzel Jun 21 '18 at 19:10
  • glColorxxx is a command for OpenGL 1.1. It still works if you create an old gl-context or a *Forward compatibility* profile context. – Ripi2 Jun 21 '18 at 19:13
  • Pyopengl package version does not equal OpenGL version, the latter of which depends on your graphics card. For the approach you are showing with those "fixed-function" commands, you would just need to clear and redraw the scene. Otherwise, if you decide to use a shader based approach later on, you should do what @Ripi2 suggested. – CodeSurgeon Jun 21 '18 at 19:19
  • I just found ModernGL – Richard Wenzel Jun 21 '18 at 19:19

1 Answers1

0

You Can use A vertex shader and pass the color to it as a uniform variable. passing the current color to the frag shader as followed.

vertex shader

uniform vec3 current_color;
out vec3 out_color;

void main(){
    out_color = current_color;
}

fragment shader

in out_color;
out frag_color;

void main{
    frag_color = out_color;
}
  • Note the code above is only some sample code to get you started.

Every update you have to render your scene and pass the color to the shader.

Hope this helps.

theVortr3x
  • 70
  • 10