简体   繁体   中英

Names of OpenGL attributes for the vertex shader

I currently load some attributes for my vertex shader in the following way:

glBindBuffer(GL_ARRAY_BUFFER, MeshVBs[eyeNum]->GLBuffer);
UINT stride = sizeof(ovrDistortionVertex);
const VertexAttribDesc VertexDesc[] =
//Name, Size, Type, Normalized, Offset
{   {"Position",    2, GL_FLOAT, false, offsetof(ovrDistortionVertex, ScreenPosNDC)},
    {"inV", 1, GL_FLOAT, false, offsetof(ovrDistortionVertex, VignetteFactor)},
    {"inTexCoord0", 2, GL_FLOAT, false, offsetof(ovrDistortionVertex, TanEyeAnglesR)},
    {"inTexCoord1", 2, GL_FLOAT, false, offsetof(ovrDistortionVertex, TanEyeAnglesG)},
    {"inTexCoord2", 2, GL_FLOAT, false, offsetof(ovrDistortionVertex, TanEyeAnglesB)}    };
for (size_t i = 0; i < 5; i++)
{
    VertexAttribDesc vad = VertexDesc[i];
    glEnableVertexAttribArray((GLuint)i);   
    glVertexAttribPointer((GLuint)i, vad.Size, vad.Type, vad.Normalized, stride, reinterpret_cast<char*>(vad.Offset));
}

That is the used vertex shader

#version 330 core
uniform vec2 EyeToSourceUVScale;
uniform vec2 EyeToSourceUVOffset;
attribute vec2 Position;        
attribute float inT;                
attribute vec2 inTexCoord0; 
attribute vec2 inTexCoord1; 
attribute vec2 inTexCoord2;
varying vec4 oPosition;     
varying vec2 oTexCoord0;        
varying vec2 oTexCoord1;        
varying vec2 oTexCoord2;
varying float oVignette;
vec2 TexCoord0 = vec2((inTexCoord0.x), (-inTexCoord0.y));
vec2 TexCoord1 = vec2((inTexCoord1.x), (-inTexCoord1.y));
vec2 TexCoord2 = vec2((inTexCoord2.x), (-inTexCoord2.y));
float Vignette = inT;

vec2 normalizeTexCoord( in vec2 TexCoord )
{   
   return ( EyeToSourceUVScale*TexCoord) + EyeToSourceUVOffset;
}       

void main(){
    oTexCoord0 = normalizeTexCoord( TexCoord0);     
    oTexCoord1 = normalizeTexCoord( TexCoord1);     
    oTexCoord2 = normalizeTexCoord( TexCoord2); 
    oVignette = Vignette;
    gl_Position.xyzw = vec4( Position.xy , 0.500000, 1.00000);
}

That Works! But if i rename "inT" in the vertex shader into "inV" nothing works anymore. Any ideas why it doesn't work?

Let me know if you need more information about the context to give an answer.

----SOLUTION----

Before the main loop :

UINT stride = sizeof(ovrDistortionVertex);
   const VertexAttribDesc VertexDesc[] =
       //Name, Size, Type, Normalized, Offset
   {   {"Position", 2, GL_FLOAT, false, offsetof(ovrDistortionVertex, ScreenPosNDC)},
   {"inVignette",   1, GL_FLOAT, false, offsetof(ovrDistortionVertex, VignetteFactor)},
   {"inTexCoord0", 2, GL_FLOAT, false, offsetof(ovrDistortionVertex, TanEyeAnglesR)},
   {"inTexCoord1", 2, GL_FLOAT, false, offsetof(ovrDistortionVertex, TanEyeAnglesG)},
   {"inTexCoord2", 2, GL_FLOAT, false, offsetof(ovrDistortionVertex, TanEyeAnglesB)}    };
   for (int i = 0;i<5;i++)
   {
       VertexAttribDesc vad = VertexDesc[i];
       glBindAttribLocation (shader_programm, i, vad.Name);
   }
   glLinkProgram(shader_programm);

In the main loop remained:

glBindBuffer(GL_ARRAY_BUFFER, MeshVBs[eyeNum]->GLBuffer);
for (size_t i = 0; i < 5; i++)
{
    VertexAttribDesc vad = VertexDesc[i];
    glEnableVertexAttribArray((GLuint)i);   
    glVertexAttribPointer((GLuint)i, vad.Size, vad.Type, vad.Normalized, stride, reinterpret_cast<char*>(vad.Offset));
}

and the new vertex shader (fragment shader has also changed varying -> in)

#version 330 core
uniform vec2 EyeToSourceUVScale;
uniform vec2 EyeToSourceUVOffset;
in vec2 Position;       
in float inVignette;                
in vec2 inTexCoord0;    
in vec2 inTexCoord1;    
in vec2 inTexCoord2;
out vec4 oPosition;     
out vec2 oTexCoord0;        
out vec2 oTexCoord1;        
out vec2 oTexCoord2;
out float oVignette;
vec2 TexCoord0 = vec2((inTexCoord0.x), (-inTexCoord0.y));
vec2 TexCoord1 = vec2((inTexCoord1.x), (-inTexCoord1.y));
vec2 TexCoord2 = vec2((inTexCoord2.x), (-inTexCoord2.y));
float Vignette = inVignette;

vec2 normalizeTexCoord( in vec2 TexCoord )
{   
   return ( EyeToSourceUVScale*TexCoord) + EyeToSourceUVOffset;
}       

void main(){
 oTexCoord0 = normalizeTexCoord( TexCoord0);        
 oTexCoord1 = normalizeTexCoord( TexCoord1);        
 oTexCoord2 = normalizeTexCoord( TexCoord2);    

    oVignette = Vignette;
    gl_Position.xyzw = vec4( Position.xy , 0.500000, 1.00000);
}

That syntax is not valid for a 330 core shader. You need to use the new in and out syntax that GLSL 130 introduced. That means in the vertex shader, inputs ( attribute ) all become in and outputs ( varying ) all become out . In the matching fragment shader, the outputs ( varying ) from the vertex shader are inputs and thus are declared using in .

With all that said, I see nowhere that you actually query the location of your vertex attributes. Many drivers will automatically assign attributes locations in alphabetical order, so changing inT to inV is going to change its location to come after inTexCoord2 .

Since this is a GLSL 3.30 shader, you have the option of binding the attribute locations explicitly in the shader, binding them from the API before linking, or querying them after the fact.

To make this work as simply as possible, I would do something like this:

const VertexAttribDesc VertexDesc[] =
//Name, Size, Type, Normalized, Offset
{   {"Position",    2, GL_FLOAT, false, offsetof(ovrDistortionVertex, ScreenPosNDC)},
    {"inV", 1, GL_FLOAT, false, offsetof(ovrDistortionVertex, VignetteFactor)},
    {"inTexCoord0", 2, GL_FLOAT, false, offsetof(ovrDistortionVertex, TanEyeAnglesR)},
    {"inTexCoord1", 2, GL_FLOAT, false, offsetof(ovrDistortionVertex, TanEyeAnglesG)},
    {"inTexCoord2", 2, GL_FLOAT, false, offsetof(ovrDistortionVertex, TanEyeAnglesB)}    };

for (size_t i = 0; i < 5; i++)
{
    VertexAttribDesc vad = VertexDesc[i];
    glBindAttribLocation (prog, i, vad.Name);
}

Keep in mind, that has to be done before you link your GLSL program. Attribute locations are assigned during the link operation. You can change them later, but you have to re-link the program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM