OpenGL - 无法让着色器显示顶点颜色
OpenGL - Can't get shader to display vertex colors
所以我终于正确设置了 opengl window 并使用 vaos、vbos 和 glDrawElements() 等显示了三角形
但现在我遇到了新问题 - 无法让着色器工作。我还不太了解他们。我希望片段着色器显示我放入单独缓冲区对象中的顶点颜色。
示例代码:
const char* vertexShaderSource =
"#program 330\n"
"layout(location = 0) in vec2 position;\n"
"layout(location = 1) in vec3 color;\n"
"out vec4 fragColor;\n"
"void main(void){\n"
"gl_Position = vec4(position, 0.0, 1.0);\n"
"fragColor = vec4(color, 1.0);\n"
"}";
const char* fragShaderSource =
"#program 330\n"
"in vec4 fragColor;\n"
"out vec4 outColor;\n"
"void main(void){\n"
"outColor = fragColor;\n"
"}";
GLuint programID = glCreateProgram();
GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShaderID, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShaderID);
GLuint fragShaderID = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragShaderID, 1, &fragShaderSource, NULL);
glCompileShader(fragShaderID);
glAttachShader(programID, vertexShaderID);
glAttachShader(programID, fragShaderID);
glLinkProgram(programID);
glValidateProgram(programID);
GLfloat vertexPositions[8] =
{
-0.5f, 0.5f,
0.5f, 0.5f,
-0.5f, -0.5f,
0.5f, -0.5f,
};
GLfloat colors[18] =
{
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f,
1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f,
};
GLushort indices[6] =
{
0, 1, 2,
1, 3, 2
};
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLuint vbo; //Vertices
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 8, vertexPositions, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
GLuint cbo; //Colors
glGenBuffers(1, &cbo);
glBindBuffer(GL_ARRAY_BUFFER, cbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 18, colors, GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
GLuint ibo; //Indexes
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort) * 6, indices, GL_STATIC_DRAW);
并渲染:
while(..)
{
..clear
glBindVertexArray(vao);
glUseProgram(programID);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)(0));
..update
}
除了着色器之外,一切似乎都运行良好。任何帮助表示赞赏:)
谢谢
在每个着色器中,将 "#program 330\n"
更改为 "#version 330\n"
。这就是您指定要使用哪个 GLSL 版本的方式。
所以我终于正确设置了 opengl window 并使用 vaos、vbos 和 glDrawElements() 等显示了三角形
但现在我遇到了新问题 - 无法让着色器工作。我还不太了解他们。我希望片段着色器显示我放入单独缓冲区对象中的顶点颜色。
示例代码:
const char* vertexShaderSource =
"#program 330\n"
"layout(location = 0) in vec2 position;\n"
"layout(location = 1) in vec3 color;\n"
"out vec4 fragColor;\n"
"void main(void){\n"
"gl_Position = vec4(position, 0.0, 1.0);\n"
"fragColor = vec4(color, 1.0);\n"
"}";
const char* fragShaderSource =
"#program 330\n"
"in vec4 fragColor;\n"
"out vec4 outColor;\n"
"void main(void){\n"
"outColor = fragColor;\n"
"}";
GLuint programID = glCreateProgram();
GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShaderID, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShaderID);
GLuint fragShaderID = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragShaderID, 1, &fragShaderSource, NULL);
glCompileShader(fragShaderID);
glAttachShader(programID, vertexShaderID);
glAttachShader(programID, fragShaderID);
glLinkProgram(programID);
glValidateProgram(programID);
GLfloat vertexPositions[8] =
{
-0.5f, 0.5f,
0.5f, 0.5f,
-0.5f, -0.5f,
0.5f, -0.5f,
};
GLfloat colors[18] =
{
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f,
1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f,
};
GLushort indices[6] =
{
0, 1, 2,
1, 3, 2
};
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLuint vbo; //Vertices
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 8, vertexPositions, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
GLuint cbo; //Colors
glGenBuffers(1, &cbo);
glBindBuffer(GL_ARRAY_BUFFER, cbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 18, colors, GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
GLuint ibo; //Indexes
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort) * 6, indices, GL_STATIC_DRAW);
并渲染:
while(..)
{
..clear
glBindVertexArray(vao);
glUseProgram(programID);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)(0));
..update
}
除了着色器之外,一切似乎都运行良好。任何帮助表示赞赏:) 谢谢
在每个着色器中,将 "#program 330\n"
更改为 "#version 330\n"
。这就是您指定要使用哪个 GLSL 版本的方式。