0
votes

I'm trying to pass a single float to my vertex shader so I can set gl_PointSize to be that float. However, it's not working and I think it's due to me using glVertexAttribPointer:

glVertexAttribPointer(1, 1, GLenum(GL_FLOAT), GLboolean(GL_FALSE), GLsizei(MemoryLayout<Vertex>.size), BUFFER_OFFSET(2 * MemoryLayout<Float>.size))

I need the buffer offset since I'm also setting the position using glVertexAttribPointer before that using this:

glVertexAttribPointer(0, 2, GLenum(GL_FLOAT), GLboolean(GL_FALSE), GLsizei(MemoryLayout<Vertex>.size), BUFFER_OFFSET(0))

This is my vertex structure:

struct Vertex {
  var x, y, size: Float
}

This is the vertex shader:

attribute vec4 Position;
attribute float Size; // I tried vec4, no luck

void main(void) {
  gl_PointSize = Size;
  gl_Position = Position;
}

Any help would be appreciated!

1
What makes you think the glVertexAttribPointer call is the culprit?derhass
It has vertex in the name when I'm trying to transfer a float, and I can't think of what else could be causing the problem since another part afterward works fine using itMysteryPancake
Well, Vertex doesn't mean Vector. In the GL, a Vertex is just a set of attributes, where each attribute can be a scalar or a two to four dimensional vector, so your usage of a vertex attribute here is fine. Did you actually enable vertex attrib array 1?derhass
Yep, at the very start. Do I need to define it as a vec4 in the shader even though it isn't or something?MysteryPancake
No, it sets up the attribute at index 1. But you don't know that Size is at index 1, unless you set it that way, for example by calling glBindAttribLocation() before linking the shader program.Reto Koradi

1 Answers

0
votes

Reto Koradi had the answer! You need to call glBindAttribLocation beforehand when compiling the shader to bind it correctly. It's all working now!

Also, before a moderator asks me to, I can't accept his answer as the correct answer as it was a comment on my original question.