我无法将任何内容渲染到屏幕上。
private void initVBO() {
vao = GL30.glGenVertexArrays();
GL30.glBindVertexArray(vao);
float[] vertices = {
-0.5f, 0.5f, 0f,
0.5f, 0.5f, 0f,
0.5f, -0.5f, 0f,
-0.5f, -0.5f, 0f
};
FloatBuffer vertBuffer = BufferUtils.createFloatBuffer(vertices.length);
vertBuffer.put(vertices);
vertBuffer.flip();
this.vboID = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, this.vboID);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertBuffer, GL15.GL_STATIC_DRAW);
positionAttribute = GL20.glGetAttribLocation(getMaterial().getShader()
.getProgram(), "position");
GL20.glEnableVertexAttribArray(positionAttribute);
GL20.glVertexAttribPointer(positionAttribute, 3, GL11.GL_FLOAT, false, 0,
0);
Color c = getMaterial().getColor();
float[] colors = {
c.red, c.green, c.blue, c.alpha,
c.red, c.green, c.blue, c.alpha,
c.red, c.green, c.blue, c.alpha,
};
FloatBuffer colorBuffer = BufferUtils.createFloatBuffer(colors.length);
colorBuffer.put(colors);
colorBuffer.flip();
this.vbocID = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, this.vbocID);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, colorBuffer, GL15.GL_STATIC_DRAW);
colorAttribute = GL20.glGetAttribLocation(getMaterial().getShader().getProgram
(), "color");
GL20.glEnableVertexAttribArray(colorAttribute);
GL20.glVertexAttribPointer(colorAttribute, 4, GL11.GL_FLOAT, false, 0, 0);
byte[] indices = {
0, 1, 2,
2, 3, 0
};
ByteBuffer indicesBuffer = BufferUtils.createByteBuffer(indices.length);
indicesBuffer.put(indices);
indicesBuffer.flip();
this.vboiID = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, this.vboiID);
GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer,
GL15.GL_STATIC_DRAW);
GL30.glBindVertexArray(0);
GL20.glDisableVertexAttribArray(positionAttribute);
GL20.glDisableVertexAttribArray(colorAttribute);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
}
public void render() {
GL20.glUseProgram(getMaterial().getShader().getProgram());
GL30.glBindVertexArray(this.vao);
GL20.glEnableVertexAttribArray(positionAttribute);
GL20.glEnableVertexAttribArray(colorAttribute);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, this.vboiID);
GL11.glDrawElements(GL11.GL_TRIANGLES, 6, GL11.GL_BYTE, 0);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
GL20.glDisableVertexAttribArray(colorAttribute);
GL20.glDisableVertexAttribArray(positionAttribute);
GL30.glBindVertexArray(0);
GL20.glUseProgram(0);
}
着色器正在正确编译,我正在绑定位置和颜色的属性。我只是使用glOrtho,它设置正确,因为通过立即模式绘图工作正常。我不确定问题是否可能来自创建索引或者它是否存在于glVertexAttribPointer中的问题。我只是试图得到一个彩色绘制到屏幕的矩形。
更新: 顶点着色器:
#version 150
in vec3 position;
in vec4 color;
out vec4 passColor;
void main(void) {
gl_Position = vec4(position, 1.0);
passColor = color;
}
Fragment Shader:
#version 150
in vec4 passColor;
out vec4 outColor;
void main(void) {
outColor = vec4(1.0, 1.0, 1.0, 1.0);
}
我还更新了上面的init和渲染代码。这里还有着色器的加载代码:
private void load(File vertex, File fragment) {
this.vertexID = FileUtility.loadShader(vertex, GL20.GL_VERTEX_SHADER);
this.fragmentID = FileUtility.loadShader(fragment, GL20.GL_FRAGMENT_SHADER);
programID = GL20.glCreateProgram();
GL20.glAttachShader(programID, vertexID);
GL20.glAttachShader(programID, fragmentID);
GL20.glBindAttribLocation(programID, 0, "position");
GL20.glBindAttribLocation(programID, 1, "color");
GL30.glBindFragDataLocation(programID, 0, "outColor");
GL20.glLinkProgram(programID);
GL20.glValidateProgram(programID);
int status = GL20.glGetProgrami(programID, GL20.GL_LINK_STATUS);
if (status != GL11.GL_TRUE) {
Log.print(LogLevel.Error, GL20.glGetProgramInfoLog(programID));
}
}
答案 0 :(得分:0)
这里有很多问题,主要是你的VBO实际上没有问题。
您创建,绑定,然后立即取消绑定您的VAO。因此,请移除对glBindVertexArray(0)
的通话,或将其向下推至initVBO
的底部。在取消绑定VAO之前,也不要取消绑定你的VBO,否则你的VAO不会被绑定到VBO。
您没有发布着色器,但您正在使用VAO,这意味着您至少可以访问GLSL 1.30。带有着色器的glOrtho
仅受到短暂支持,使用GLSL 1.20进行折旧并在核心配置文件中删除。我强烈建议尽快远离固定功能矩阵堆栈。 LWJGL有自己的矩阵类,开关应该非常简单。
除了着色器中的任何其他问题,首先应该在屏幕上显示某些内容