How can I pass UV set into vertex shader from C++ host program

Problems building or running the engine, queries about how to use features etc.
Post Reply
Posts: 1
Joined: Sat May 04, 2019 1:02 pm

How can I pass UV set into vertex shader from C++ host program

Post by borzykot »

Ogre Version: 1.6
Operating System: Windows/iOS/Android
Render System: GLES

My goal is to pass UV coords into GLSLES vertex shader dynamically from my C++ program.

Seems that OGRE can only pass 8 different UV sets into shader (like uv0, uv1 ... uv7 attributes). And it is supposed that we need to hardcode them inside the shader.

I tried to work-around this problem by modifiying VertexDeclarations in the SubMesh (uvX become uv0 so no need to modify shaders) but there is another issues with such a solution - I cannot use same Mesh for different entities.

My workaround:

Code: Select all

void cEntityActor::setUVSetAsDefault(Ogre::SubEntity& entity, UInt uvSet)
    auto* vertex_decl = entity.getSubMesh()->vertexData->vertexDeclaration;
    auto& elements = vertex_decl->getElements();

    int idx = 0;
    int uv0_elem_idx = <find uv0 element index>;
    int uvN_elem_idx = <find uvN element index>;
    const Ogre::VertexElement* uv0 = <find uv0 element>;
    const Ogre::VertexElement* uvN = <find uvN element>;

    auto uv0_idx = uv0->getIndex();
    auto uvN_idx = uvN->getIndex();

    vertex_decl->modifyElement(uv0_elem_idx, uv0->getSource(), uv0->getOffset(), uv0->getType(), uv0->getSemantic(), uvN_idx);
    vertex_decl->modifyElement(uvN_elem_idx, uvN->getSource(), uvN->getOffset(), uvN->getType(), uvN->getSemantic(), uv0_idx);

#version 100
attribute vec4 vertex;
attribute vec4 uv0; 
uniform mat4 worldviewproj_matrix;
varying vec2 oTexcoord2_0;

void main( void )
    gl_Position = worldviewproj_matrix * vertex;
    oTexcoord2_0 = uv0.xy;
Also there is another possible workaround - recompile shaders with required UV set, but it will take time and it is restricted by 8 possible UV sets.

Any advise? Maybe there is a way to somehow tune OGRE engine source code and 'redirect' for example uv1 into uv0 so uv0 from shader will take values from uv1 actually.

Thank you.

Posts: 204
Joined: Wed Mar 18, 2009 3:03 am
x 61

Re: How can I pass UV set into vertex shader from C++ host program

Post by rpgplayerrobin »

There is probably somewhere deep in the Ogre source where you could make a change to get this working, but except for that the only way I can think of is cloning the mesh for each type of UV you want to use.

There might be some other way to fix this if you can explain a bit more on what exactly you are trying to do though.

Post Reply