OpenGL4着色器(#version 410)意外的行为

问题描述:

我有一个问题,弄清楚我最简单的着色器是怎么回事。这里是我的顶点着色器:OpenGL4着色器(#version 410)意外的行为

#version 410 core 

in vec3 position; 
in vec2 textureCoords; 
out vec2 color; 

void main(void) 
{ 

    gl_Position = vec4(position.x, position.y, position.z, 1.0); 
    color = vec2(1.0,0.5); 
} 

而片段着色器:

#version 410 core 

in vec2 color; 
out vec4 out_color; 

void main(void) 
{ 
    out_color = vec4(0.0, 1.0, 0.0, 1.0); 
} 

注意,颜色可变正从VS传递出FS并没有真正做任何事情。这些着色器给我预期的结果是下面的图片:

Render from the original shaders above

现在,如果我仅仅是从VS使用颜色变量传递textureCoords的FS没有任何改变它改变了FS我的形象在右边。因此,修改后的VS(和唯一的修改着色器)是:

#version 410 core 

in vec3 position; 
in vec2 textureCoords; 
out vec2 color; 

void main(void) 
{ 
    gl_Position = vec4(position.x, position.y, position.z, 1.0); 
    color = textureCoords; 
} 

这给了意想不到以下渲染(三角形转移到右上角):

After the modification to the VS

我不明白为什么这是因为我甚至没有在FS中使用颜色变量。

这是怎么发生的?我只是想把vec2传给FS!我在这里没有看到什么?

下面是一些额外的信息: GL信息: 支持GL版本:4.1 NVIDIA - 34年10月16日355.10.05.35f05 支持阴影郎咸平:4.10

完整的代码在这里: complete code 切入点是测试的.cpp

这里是我当前的顶点数组:

GLfloat PossibleVertices[] = { 
    // Left bottom triangle 
    0.0f, 0.5f, 0.0f, // v0 
    -0.5f, -0.5f, 0.0f, // v1 
    0.5f, -0.5f, 0.0f 
}; 

GLuint Indices[] = { 
    0, 1, 2 
}; 

GLfloat TextureCoords[] = 
{ 
    0.5f, 1.0f, // v0 
    0.0f, 0.0f, // v1 
    1.0f, 0.0f 
}; 

// Load data to VAO 
LoadToVAO(PossibleVertices, 9, Indices, 3, TextureCoords, 6); 

... 

LoadToVAO 
(
    GLfloat Positions[], GLuint PosArrySize, 
    GLuint Indices[], GLuint IndArrySize, 
    GLfloat TexCoords[], GLuint TCArrySize 
) 
{ 
    GLuint VaoID = CreateVAO(); 
    BindIndicesBufferVBO(Indices, IndArrySize); // Buffer Index - optimization 
    StoreDataInAttrList(0, 3, Positions, PosArrySize); 
    StoreDataInAttrList(1, 2, TexCoords, TCArrySize); 
    UnbindVAO(); 
    CGCore::RawModel* ret = new CGCore::RawModel(VaoID, IndArrySize); 
    return ret; 
} 

这里是FUNC要求实际存储数据到VAO。所谓的两个顶点数组:

void CGCore::Loader::StoreDataInAttrList(GLuint AttrNumber, GLuint AttrSize, GLfloat Data[], GLuint DataSize) 
{ 
    GLuint VboID; 
    glGenBuffers(1,&VboID); 
    VBOContainer.push_back(VboID); 
    glBindBuffer(GL_ARRAY_BUFFER, VboID); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*DataSize, Data, GL_STATIC_DRAW); 
    glVertexAttribPointer(AttrNumber, AttrSize, GL_FLOAT, GL_FALSE, 0, (void*) 0); // write to VAO 
    glBindBuffer(GL_ARRAY_BUFFER, 0); // unbind current VBO 
} 

这里是ATTR绑定代码:

void BindAttributes() 
{ 
    BindAttribute(0, "position"); 
    BindAttribute(1, "textureCoords"); 
} 

// and also ... 

void BindAttribute(int Attrib, const GLchar* VarName) 
{ 
    glBindAttribLocation(ProgramID, Attrib, VarName); 
} 

最后渲染代码:

void CGCore::Renderer::Render(CGCore::TexturedModel* TexturedModelObj) 
{ 
    CGCore::RawModel* Model = TexturedModelObj->GetModel(); 
    glBindVertexArray(Model->GetVaoID()); 
    glEnableVertexAttribArray(0); 
    glEnableVertexAttribArray(1); 
    glActiveTexture(GL_TEXTURE0); 
    glDrawElements(GL_TRIANGLES, Model->GetVertexCount(), GL_UNSIGNED_INT, (void*) 0); 
    glBindTexture(GL_TEXTURE_2D, TexturedModelObj->GetTexture()->GetTextureID()); 
    glDisableVertexAttribArray(0); 
    glDisableVertexAttribArray(1); 
    glBindVertexArray(0); 
} 

很抱歉的长期职位。感谢名单!

+1

请显示图纸代码。我的猜测是你搞砸了数据或属性指针。 – BDL

+0

我编辑github链接到绘图代码。但是,如果vec2没有以任何方式被使用,那么如何将vec2赋值给out vec2 shift顶点?为什么downvote它是一个合法的q? –

+1

例如,可能存在属性位置的问题。如果你在VS中不使用'textureCoords',那么它是不活动的,并且会被优化掉。现在,当您使用它时,它将处于活动状态并将获得指定位置。如果你没有在你的代码中正确处理这些位置,你可以画一些其他的东西。 – BDL

经过试验,经历了大量的OpenGL文档等,我终于解决了我的问题。与一些评论相反,问题在于顶点着色器。即变量的声明。在VS下面的变化给我的预期输出:

#version 410 core 
layout (location = 0) in vec3 position; 
layout (location = 1) in vec2 textureCoords; 

out vec2 TexCoord; 

void main() 
{ 
    gl_Position = vec4(position, 1.0f); 
    TexCoord = textureCoords; 
} 

所以我在做什么是明确指定的位置在VOA PARAMS PARAMS中声明的时候。我想它与顶点着色器(v4.10)的版本有关,但我不确定。在任何情况下,通过声明类似var 布局(位置= X)来指定attr位置var_name

+0

您正在得出错误的结论。 @ BDL的评论是重点。事实上,通过使用另一种方式来指定这些位置,可以解决错误地使用'glBindAttribLocation'这一事实并不意味着使用'glBindAttribLocation'是错误的,或者GLSL 4.10在这方面的行为不同。 – derhass