[osg-users] using modern shaders with osg - setting vertex attribute layout
Sebastian Messerschmidt
sebastian.messerschmidt at gmx.de
Fri Sep 1 02:46:47 PDT 2017
Hi Antiro,
> Hi,
>
> I am converting a project written in pure opengl to use OSG to improve compatibility and reduce maintenance. The original project used a deferred rendering pipeline with a pretty big set of custom shaders. I would like to reuse these shaders with minimum adjustments.
>
> In order to do this I want to use the same uniform names and the same layout for the VAO. I figured out that I can add callbacks to set the ModelView / Projection / etc matrices to uniforms with the same names as used in in the shaders, so that is taken care of.
>
> I do however not know how to specify the vertex attribute layout. My shaders all start with the following:
> #version 330 core //so can't use gl_Normal etc
> layout(location = 0) in vec3 position
> layout(location = 1) in vec3 normal
> layout(location = 2) in vec2 texCoords
> layout(location = ....
>
> I'm currently just using osgDB::readNodeFile() to read in the geometry and I'm assuming I need to do something more sophisticated to control how the vertex attributes are organized, but have been unable to figure out what.
>
> If anyone could point me in the right direction, that would be most helpful!
The easiest way is to convert the shaders to use the osg_ -prefix and to
turn on the vertex attribute aliasing (basically all OSG will provide
all gl_ inputs)
A list:
//OSG inputs
layout(location = 0) in vec4 osg_Vertex;
layout(location = 1) in vec3 osg_Normal;
layout(location = 2) in vec4 osg_Color;
layout(location = 3)in vec4 osg_MultiTexCoord0;
layout(location = 4)in vec4 osg_MultiTexCoord1;
layout(location = 5)in vec4 osg_MultiTexCoord2;
layout(location = 6)in vec4 osg_MultiTexCoord3;
layout(location = 7)in vec4 osg_MultiTexCoord4;
layout(location = 8)in vec4 osg_MultiTexCoord5;
layout(location = 9)in vec4 osg_MultiTexCoord6;
layout(location = 10)in vec4 osg_MultiTexCoord7;
In order to turn on aliasing:
viewer->getCamera()->getGraphicsContext()->getState()->setUseModelViewAndProjectionUniforms(true);
viewer->getCamera()->getGraphicsContext()->getState()->setUseVertexAttributeAliasing(true);
Note, that the first line will set up the gl_XYZMatrix aliases, so no
need for a custom callback for this (at least for the std-matrices)
So you get these:
uniform mat4 osg_ModelViewProjectionMatrix;
uniform mat4 osg_ModelViewMatrix;
uniform mat4 osg_ViewMatrixInverse;
uniform mat3 osg_NormalMatrix;
uniform mat4 osg_ViewMatrix;
etc.
Cheers
Sebastian
>
>
> Thank you!
>
> Cheers,
> antiro
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=71584#71584
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
More information about the osg-users
mailing list