4

I read a lot about the new concepts in OpenGL 4 in comparison to OpenGL 1. My problem is, that in old GLSL I could use the predefined gl_Vertex for calculations in my vertex shader out of the box. But in the current version 4.1 this has been removed.

There are many questions about this, but no one gives an example in how to actually make the old gl_Vertex available in the shader. I found some possibilities like layout (location = 0) in vec3 vertexpos; or glBindAttribLocation(shader, 0, "vertexpos");. But nowhere is explained where I got the knowledge in which index the current vertex position is stored. Actually I found this site with indexes but that doesn't seem to work with the above mentioned code snippets.

Maybe someone could bring light in the dark and show a very little example.

1 Answer 1

5

The reason why gl_Vertex no longer available is because pre-defined attributes were removed. What was glVertexPointer - now have to be manually set to user-defined named attribute with glVertexAttribPointer. There is no way for shader to know what data comes to what attribute - because it is just numbers; however, how this numbers are handled is up to programmer.

So, you have to define at least one attribute (say vertexpos, as in your example - but name could be almost anything), get attribute location (glGetAttribLocation, or use location specifier to assign default location) to convert symbolic name to attribute slot number, then assign attribute array with glVertexAttribPointer.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, so let's say in my shader I have the line layout (location = 0) in vec3 vertexpos;. I didn't found a function called glAttribPointer. I guess you meant glVertexAttribPointer. So I would call it with glVertexAttribPointer(0,3,GL_FLOAT,0,0). But where is the information which values will be stored in this attribute vertexpos? Where does the system know from, that I want the gl_Vertex in my vertexpos and not the gl_FragColor? Maybe I'm just misunderstanding the concept behind these attributes.
It does not know anything that isn't explicitly stated. You have to tell where data comes from. In your example, latest argument to attrib pointer is 0 - it starts directly from beginning of vertex buffer object. Of course VBO must be bound at the moment of pointer declaration, and filled with desired data. If you already had working program with gl_Vertex, all you need to do is replace glVertexPointer (this function is removed in GL 4) with glVertexAttribPointer (thanks for correction, my memory eludes me, I've initially used wrong name).
Thanks, I think I solved it but still have some problems. I may should open a new question for this.

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.