0

I am trying to test the following simple example wherein I pass something to the vertex shader and read its output to print.

import moderngl
import numpy as np
import struct

vertex_shader_source = '''
    #version 330
    
    in vec3 attPosition;
    out vec2 varScreenCoord;
    
    void main ()
    {
        varScreenCoord = vec2(1.0, 1.5);
    }
'''

ctx = moderngl.create_standalone_context()
prog = ctx.program(vertex_shader=vertex_shader_source, varyings=['varScreenCoord'])

# input
verts = np.array([[0.0, 0.0, 0.0],
                  [1.0,  0.0, 0.0],
                  [1.0,  1.0, 0.0]], dtype='f4')
verts_buf = ctx.buffer(verts.tobytes())

# output
n, c = verts.shape
varScreenCoord = np.zeros((n, c-1)).astype(np.float32)
varScreenCoord_buf = ctx.buffer(varScreenCoord.tobytes())

vao = ctx.vertex_array(prog, content=[
    (verts_buf, '3f', 'attPosition')
])

vao.transform(varScreenCoord_buf, vertices=n)

data = struct.unpack("{}f".format(n*2), varScreenCoord_buf.read())
for i in range(0, n*2):
    print("value = {}".format(data[i]))

I get the following error when I run this

Traceback (most recent call last):
  File "/somepath/moderngl_sample.py", line 32, in <module>
    vao = ctx.vertex_array(prog, content=[
  File "/somepath/lib/python3.9/site-packages/moderngl/context.py", line 1140, in vertex_array
    return self._vertex_array(*args, **kwargs)
  File "/somepath/lib/python3.9/site-packages/moderngl/context.py", line 1169, in _vertex_array
    res.mglo, res._glo = self.mglo.vertex_array(program.mglo, content, index_buffer_mglo,
moderngl.error.Error: content[0][2] must be an attribute not NoneType

I have checked the buffer format here and it seems to be correct. This indicates that the name might not be referenced in the shader, but my shader clearly has that vertex attribute.

What could be causing this issue?

Thanks.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
WillyWonka
  • 91
  • 1
  • 7

1 Answers1

0

After using the attPosition value in the shader, this issue was fixed. For example, the shader must be changed as follows

    #version 330
    
    in vec3 attPosition;
    out vec2 varScreenCoord;
    
    void main ()
    {
        varScreenCoord = vec2(1.0, 1.5) * attPosition.xy;
    }
WillyWonka
  • 91
  • 1
  • 7