1

I am trying to port some javascript code with opengl to python. But cannot figure out what I am doing wrong in translating prog.uniform[u] = gl.getUniformLocation(prog, u);

Javascript:

let v = buildShader(vert, gl.VERTEX_SHADER);
let f = buildShader(frag, gl.FRAGMENT_SHADER);
let prog = gl.createProgram();
gl.attachShader(prog, v);
gl.attachShader(prog, f);
gl.linkProgram(prog);
prog.uniform = {};
u = ['model','bounds','frac','aspect'];
_.each(u, function(u){ prog.uniform[u] = gl.getUniformLocation(prog, u); });

Python3/PyOpenGl:

v = self.buildShader(vert, GL_VERTEX_SHADER)
f = self.buildShader(frag, GL_FRAGMENT_SHADER)
prog = glCreateProgram()
glAttachShader(prog, v)
glAttachShader(prog, f)
glLinkProgram(prog)
for u in ['model','bounds','frac','aspect']:
  loc = glGetUniformLocations(prog,u)
  glProgramUniform(prog,loc,u)
0

1 Answer 1

3

glProgramUniform assigns a value to a uniform, where the 3rd paramter is the value.

glProgramUniform(prog,loc,u) makes not any sense, when u is string which is the name name of the uniform.

You have to create a dictionary which contains the locations of a uniform for each name:

uniform = {}
for u in ['model','bounds','frac','aspect']:
    uniform[u] = glGetUniformLocation(prog, u)

or simply

uniform = { u : glGetUniformLocation(prog, u) for u in ['model','bounds','frac','aspect'] }
Sign up to request clarification or add additional context in comments.

4 Comments

Thx, how to port the prog.uniform[u]= part to python3?
@Nard No way, because in python prog is t name of the shader program object, which is just a number. You have to create a tuple (prog, uniform).
Stupid question probably, but then how do I pass the uniform-variable to opengl or is this not what is happening in the js-code prog.uniform[u] = ?
@Nard No prog.uniform[u] doesn't assign anything to uniform variable on the GPU in WebGL (not OpenGL). This is done by gl.Uniform*

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.