Advertisement

OpenGL legacy to shader mapping

Started by August 17, 2017 09:12 AM
2 comments, last by Kjell Andersson 7 years, 5 months ago

I'm trying to get some legacy OpenGL code to run with a shader pipeline,

The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.

I've got a version 330 vertex shader to somewhat work:


#version 330

uniform mat4 osg_ModelViewProjectionMatrix;
uniform mat4 osg_ModelViewMatrix;

layout(location = 0) in vec4 Vertex;
layout(location = 2) in vec4 Normal; // Velocity
layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location?

out VertexData {
    vec4 color;
	vec3 velocity;
	float size;
} VertexOut;

void main(void)
{
	vec4 p0 = Vertex;
	vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f);
	vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz;

	VertexOut.velocity = velocity;
	VertexOut.size = TexCoord.y;
	gl_Position = osg_ModelViewMatrix * Vertex;
}

What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.

What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.

Question:

What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?

 

Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.

The only well-defined standard location is 0 for the vertex position attribute. The use of location 2 for normals and similarly for other attributes is a NVIDIA-specific thing, and will most probably break you hard on other vendors.

Please see this discussion on SO: https://stackoverflow.com/a/528824/87969

If you only care about those devices and are happy to rely on vendor-specific behaviour, the mapping should then be that gl_MultiTexCoord0 is at location 8, according to the docs.

To make it is hell. To fail is divine.

Advertisement

Thank you Zao, you saved my day! You gave the exact answer I was looking for.

I had my doubts about interoperability between legacy and modern OpenGL, and you confirmed it. It is however as you say, the texture coordinate was to be found at position 8 when I tested it, but since this may be NVIDIA specific I have to find a way around it.

This topic is closed to new replies.

Advertisement