简体   繁体   English

LWJGL不渲染任何东西

[英]LWJGL doesn't render anything

I am trying to learn how to use LWJGL3 and I just got to a state where I want to render something (a test quad for now). 我正在尝试学习如何使用LWJGL3,我刚刚进入一个我想渲染的状态(现在是一个测试四边形)。 I have a class that represents a mesh where I set up the VAO with vertex, colour and indices buffers and another object later takes the mesh instance, retrieves its VAO ID and attempts to render it. 我有一个表示网格的类,我用顶点,颜色和索引缓冲区设置VAO,另一个对象稍后获取网格实例,检索其VAO ID并尝试渲染它。

The problem I have is that no matter what I try, nothing renders in the window. 我遇到的问题是无论我尝试什么,都不会在窗口中呈现。 I can change the background colour through the glClearColor() method but the quad never shows up. 我可以通过glClearColor()方法更改背景颜色,但四边形从不显示。

The VAO set up: VAO成立:

vertexCount = indices.length;

vaoID = glGenVertexArrays();
glBindVertexArray(vaoID);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);

//Vertices
FloatBuffer verticesBuffer = BufferUtils.createFloatBuffer(positions.length);
verticesBuffer.put(positions).flip();

vboID = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, verticesBuffer, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 3 * 4, 0);

//Colours
FloatBuffer colorsBuffer = BufferUtils.createFloatBuffer(colors.length);
colorsBuffer.put(colors).flip();

colVboID = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, colVboID);
glBufferData(GL_ARRAY_BUFFER, colorsBuffer, GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, false, 3 * 4, 0);

//Indices
IntBuffer indicesBuffer = BufferUtils.createIntBuffer(indices.length);
indicesBuffer.put(indices).flip();

idxVboID = glGenBuffers();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, idxVboID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW);

//Unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

The rendering code: 渲染代码:

//Bind the shader
shaderProgram.bind();

//Bind the VAO
glBindVertexArray(mesh.getVaoID());

//Draw
glDrawElements(GL_TRIANGLES, mesh.getVertexCount(), GL_UNSIGNED_INT, 0);

//Restore
glBindVertexArray(0);
shaderProgram.unbind();

Vertex shader: 顶点着色器:

#version 330

layout (location=0) in vec3 pos;
layout (location=1) in vec3 inColor;

out vec3 exColor;

void main()
{
    gl_Position = vec4(pos, 1.0);
    exColor = inColor;
}

Fragment shader: 片段着色器:

#version 330

in vec3 exColor;
out vec4 fragColor;

void main()
{
    fragColor = vec4(exColor, 1.0);
}

What am I doing wrong? 我究竟做错了什么?

The problem was not in the parts of code shown, but in the main loop that I copied from a book without thoroughly thinking through what it did. 问题不在于所显示的代码部分,而是在主循环中,我从一本书中复制而没有仔细思考它做了什么。 I ended up with a glClear call right before glfwSwapBuffers call, which cleared the buffer right before showing it. 我结束了一个glClear通话权之前glfwSwapBuffers打电话来清除缓冲区正确显示之前。

Lesson of the day: don't just copy from a book, think thoroughly about what you're doing 每日课程:不要只是从书中复制,彻底思考你在做什么

(Thank you to the people of LWJGL formus for helping me discover this mistake) (感谢LWJGL formus帮助我发现这个错误的人)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM