我的片段着色器无法正常工作,它只是用白色绘制的。 我认为着色器一起不起作用。我尝试多次更改colorO并且没有运气。
#include <glew.h>
#include <glfw3.h>
GLuint compileshaders() {
GLuint vshader, fshader, program;
static const GLchar * vshadersource[] = {
"#version 450 core \n"
" layout (location=0) in vec3 position;\n"
" layout (location=1) in vec3 color;\n"
"void main() \n"
"{ \n"
"gl_Position = position; \n"
"} \n"
};
static const GLchar * fshadersource[] = {
"#version 450 core \n"
"out vec4 colorO; \n"
"void main() \n"
"{ \n"
" colorO = vec4(1.0,1.0,0.0,1.0); \n"
"} \n"
};
vshader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vshader, 1, vshadersource, NULL);
glCompileShader(vshader);
fshader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fshader, 1, fshadersource, NULL);
glCompileShader(fshader);
program = glCreateProgram();
glAttachShader(program, vshader);
glAttachShader(program, fshader);
glLinkProgram(program);
glDeleteShader(vshader);
glDeleteShader(fshader);
return program;
}
int main(void)
{
GLFWwindow* window;
GLuint program;
GLuint vertex_array_object;
GLuint buffers[2];
if (!glfwInit())
return -1;
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewInit();
static const GLfloat positions[] = {0.25,-0.25,0.5,0.25,0.25,0.5,-0.25,-0.25,0.5};
static const GLfloat colors[] = {1.0,0.0,1.0,1.0};
program = compileshaders();
glUseProgram(program);
glCreateVertexArrays(1, &vertex_array_object);
glCreateBuffers(2, &buffers[0]);
glNamedBufferStorage(buffers[0], sizeof(positions), positions, 0);
glVertexArrayVertexBuffer(vertex_array_object, 0, buffers[0], 0, sizeof(GLfloat) * 3);
glVertexArrayAttribFormat(vertex_array_object, 0, 3, GL_FLOAT, GL_FALSE, 0);
glVertexArrayAttribBinding(vertex_array_object, 0, 0);
glEnableVertexArrayAttrib(vertex_array_object, 0);
glNamedBufferStorage(buffers[1], sizeof(colors), colors, 0);
glVertexArrayVertexBuffer(vertex_array_object, 1, buffers[1], 0, sizeof(GLfloat) * 3);
glVertexArrayAttribFormat(vertex_array_object, 1, 3, GL_FLOAT, GL_FALSE, 0);
glVertexArrayAttribBinding(vertex_array_object, 1, 1);
glEnableVertexArrayAttrib(vertex_array_object, 1);
glBindVertexArray(vertex_array_object);
GLfloat clearcolor[4] = { 0.0,0.0,0.4,1.0 };
glPointSize(40);
while (!glfwWindowShouldClose(window))
{
glClearBufferfv(GL_COLOR, 0, clearcolor);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1,&vertex_array_object);
glDeleteProgram(program);
glfwTerminate();
return 0;
}
有人可以帮助我吗?我仍然试图掌握opengl。
答案 0 :(得分:1)
问题在于您的顶点着色器,您无法直接将position
分配给gl_Position
,因为position
是vec3
而gl_Position
是{ {1}}。
你可以这样做:
vec4
或者您可以将gl_Position = vec4(position, 1.0f);
定义为position
,即使您的排名为vec4
,vec3
的最后一个组成部分也会自动设置为position
那种情况:
1.0f
您可以在layout (location=0) in vec4 position;
调用后检测编译错误,因为您需要以glCompileShader
作为第二个参数调用glGetShaderiv
,第三个参数是指向结果的变量的指针应该存储。这会给GL_COMPILE_STATUS
(0
- 有一些编译错误)或GL_FALSE
(1
- 编译成功)。如果出现错误,您可以通过调用GL_TRUE
来获取错误日志,您也需要使用glGetShaderInfoLog
获取信息日志长度(使用glGetShaderiv
作为第二个参数)。您可以对链接器错误执行相同的操作,但是您需要调用GL_INFO_LOG_LENGTH
变体而不是glGetProgram*
变体。
答案 1 :(得分:0)
gl_Position = position;
需要。
gl_Position = vec4(position,1.0);
因为gl_Position是vec4而position是vec3。 感谢FamZ帮我发现错误。