我有一个问题,弄清楚我最简单的着色器中发生了什么。这是我的顶点着色器:
#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的颜色变量确实没有做任何事情。这些着色器给出了预期的结果,如下图所示:
现在,如果我只是使用颜色变量将 textureCoords 从VS传递到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;
}
这会产生意想不到的后续渲染(三角形向右上角移动):
我不明白为什么会发生这种情况,因为我甚至没有使用FS中的颜色变量。
为什么会这样?我只是想把vec2传给FS!我在这里看不到什么?
以下是一些其他信息: GL信息: 支持的GL版本:4.1 NVIDIA-10.16.34 355.10.05.35f05 支持的着色语言:4.10
此处填写完整代码: complete code 入口点是tests.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;
}
这是用于将数据实际存储到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);
}
对不起,很长的帖子。感谢名单!
答案 0 :(得分:-1)
经过实验,经历了大量的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中参数的位置。我想这与顶点着色器的版本(v4.10)有关,但我不确定。在任何情况下,通过在类型var_name
中声明类似布局(位置= X)的内部变量来指定attr位置