【发布时间】:2021-04-25 08:06:06
【问题描述】:
我正在尝试让一个三角形显示在窗口中。我让窗口出现,但窗口是黑色的,窗口中没有出现三角形。着色器文件可以打开(如果不能打开,我会得到 FileNotFoundException),但是三角形不会显示在屏幕上。这是我的代码:
public class GoodbyeSimpleWorld implements GLEventListener, KeyListener {
private static GLWindow window;
private static Animator animator;
float vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
int shaderProgram;
IntBuffer VAO;
IntBuffer VBO;
int iVBO, iVAO;
public static void main(String[] args) {
new GoodbyeSimpleWorld().setup();
}
private void setup() {
GLProfile glProfile = GLProfile.get(GLProfile.GL3);
GLCapabilities glCapabilities = new GLCapabilities(glProfile);
window = GLWindow.create(glCapabilities);
window.setTitle("Goodbye Simple World");
window.setSize(1024, 768);
window.addGLEventListener(this);
window.addKeyListener(this);
window.setVisible(true);
window.addGLEventListener(this);
window.addKeyListener(this);
window.addWindowListener(new WindowAdapter(){
@Override
public void windowDestroyed(WindowEvent e) {
animator.stop();
System.exit(0);
}
});
animator = new Animator();
animator.start();
}
@Override
public void init(GLAutoDrawable drawable) {
GL3 gl = drawable.getGL().getGL3();
ShaderCode vertexShader = ShaderCode.create(gl, GL_VERTEX_SHADER, this.getClass(),
"shaders/gl3", null, "goodbye", "vert", null, true);
vertexShader.compile(gl);
ShaderCode fragmentShader = ShaderCode.create(gl, GL_FRAGMENT_SHADER, this.getClass(),
"shaders/gl3", null, "goodbye", "frag", null, true);
fragmentShader.compile(gl);
shaderProgram = gl.glCreateProgram();
gl.glAttachShader(shaderProgram, vertexShader.id());
gl.glAttachShader(shaderProgram, fragmentShader.id());
gl.glLinkProgram(shaderProgram);
gl.glDeleteShader(vertexShader.id());
gl.glDeleteShader(fragmentShader.id());
VBO = GLBuffers.newDirectIntBuffer(1);
VAO = GLBuffers.newDirectIntBuffer(1);
gl.glGenVertexArrays(1, VAO);
gl.glGenBuffers(1, VBO);
iVBO = VBO.get();
iVAO = VAO.get();
gl.glBindVertexArray(iVAO);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, iVBO);
gl.glBufferData(GL.GL_ARRAY_BUFFER, vertices.length, FloatBuffer.wrap(vertices), gl.GL_STATIC_DRAW);
gl.glVertexAttribPointer(0, 3, GL.GL_FLOAT, false, 3 * Float.BYTES, 0);
gl.glEnableVertexAttribArray(0);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
gl.glBindVertexArray(0);
}
@Override
public void display(GLAutoDrawable drawable) {
GL3 gl = drawable.getGL().getGL3();
drawable.setAutoSwapBufferMode(true);
gl.glUseProgram(shaderProgram);
gl.glBindVertexArray(iVAO);
gl.glDrawArrays(GL_TRIANGLES, 0, 3);
gl.glBindVertexArray(iVAO);
}
...
}
【问题讨论】: