你好我一直想弄清楚为什么这个三角形不会画!这是我的代码。 它只是使用VBO和VAO的基本LWJGL OpenGL代码,我无法弄清楚为什么它不是绘图。我已经确认着色器正在正确编译
//bindings available for use.
GL.createCapabilities();
// Set the clear color
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
float vertices[] = {
0.0f, 0.5f, // Vertex 1 (X, Y)
0.5f, -0.5f, // Vertex 2 (X, Y)
-0.5f, -0.5f // Vertex 3 (X, Y)
};
int vbo;
vbo = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo);
FloatBuffer vboBuff = BufferUtils.createFloatBuffer(vertices.length);
vboBuff.put(vertices);
vboBuff.flip();
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vboBuff, GL15.GL_STATIC_DRAW);
int vertShade = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertShade,Reader.read("shaders/main.frag"));
glCompileShader(vertShade);
int fragShade = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragShade,Reader.read("shaders/main.frag"));
glCompileShader(fragShade);
String failed = glGetShaderInfoLog(fragShade);
System.err.println(failed);
int shaderprogram = glCreateProgram();
glAttachShader(shaderprogram,vertShade);
glAttachShader(shaderprogram,fragShade);
glLinkProgram(shaderprogram);
glUseProgram(shaderprogram);
int posAttrib = glGetAttribLocation(shaderprogram,"position");
glVertexAttribPointer(posAttrib,2,GL_FLOAT,false,0,0);
glEnableVertexAttribArray(posAttrib);
int vao;
vao = glGenVertexArrays();
// Run the rendering loop until the user has attempted to close
// the window or has pressed the ESCAPE key.
while ( glfwWindowShouldClose(window) == GL_FALSE ) {;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window); // swap the color buffers
// Poll for window events. The key callback above will only be
// invoked during this call.
glfwPollEvents();
}
SHADERS:///这些是单独的文件 片段着色器:
#version 150
out vec4 outColor;
void main()
{
outColor = vec4(1.0, 1.0, 1.0, 1.0);
}
顶点
#version 150
in vec2 position;
void main()
{
gl_Position = vec4(position, 0.0, 1.0);
}
编辑while循环之前的最后几行现在看起来像这样:
int shaderprogram = glCreateProgram();
glAttachShader(shaderprogram,vertShade);
glAttachShader(shaderprogram,fragShade);
glLinkProgram(shaderprogram);
glUseProgram(shaderprogram);
int vao;
vao = glGenVertexArrays();
glBindVertexArray(vbo);
int posAttrib = glGetAttribLocation(shaderprogram,"position");
glVertexAttribPointer(posAttrib,2,GL_FLOAT,false,0,0);
glEnableVertexAttribArray(posAttrib);
答案 0 :(得分:0)
查看自己的代码应该会使问题变得非常明显:
int vertShade = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertShade,Reader.read("shaders/main.frag"));
glCompileShader(vertShade);
int fragShade = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragShade,Reader.read("shaders/main.frag"));
glCompileShader(fragShade);
由于在读取两个着色器时使用相同的文件名,因此您使用顶点着色器的片段着色器代码,并且从不加载顶点着色器代码。
如果您修复了文件名,事情就应该开始变得更好了。确保在开发期间构建着色器后始终检查编译和链接状态。然后,您将能够快速识别这些类型的问题。