使用OpenGL绘图时JavaVM崩溃

时间:2015-10-23 16:54:12

标签: java opengl libgdx

我已经得到了以下代码:

package test.opengl;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.utils.BufferUtils;

import java.io.IOException;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.file.Files;
import java.nio.file.Paths;

import static com.badlogic.gdx.graphics.GL20.*;

public class GLClass extends ApplicationAdapter
{
    private int shaderProgram;
    private GL30 gl;

    FloatBuffer vertices;
    IntBuffer vao = BufferUtils.newIntBuffer(1);
    IntBuffer vbo = BufferUtils.newIntBuffer(1);

    @Override
    public void create()
    {
        gl = Gdx.gl30;
        compileShaders();

        vertices = BufferUtils.newFloatBuffer(9);
        vertices.put(new float[]
                {
                        -0.5f, -0.5f, 0.0f,
                        0.5f, -0.5f, 0.0f,
                        0.0f, 0.5f, 0.0f
                });

        gl.glEnable(GL_DEPTH_TEST);
        gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

        gl.glGenVertexArrays(1, vao);
        gl.glGenBuffers(1, vbo);

        gl.glBindVertexArray(vao.get(0));
        gl.glBindBuffer(GL_ARRAY_BUFFER, vbo.get(0));
        gl.glBufferData(GL_ARRAY_BUFFER, 9, vertices, GL_STATIC_DRAW);
        gl.glVertexAttribPointer(0, 3, GL_FLOAT, false, 3, 0);
        gl.glEnableVertexAttribArray(0);
        gl.glBindVertexArray(0);
    }

    @Override
    public void render()
    {
        gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
        gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        gl.glUseProgram(shaderProgram);

        gl.glBindVertexArray(vao.get(0));
        gl.glDrawArrays(GL_TRIANGLES, 0, 3);
        gl.glBindVertexArray(0);

        int err = gl.glGetError();
        if (err != 0)
            System.err.println("ERROR: " + err);
    }

    @Override
    public void resize(int width, int height)
    {
        gl.glViewport(0, 0, width, height);
    }

    private void compileShaders()
    {
        String vsSource;
        String fsSource;

        try
        {
            vsSource = new String(Files.readAllBytes(Paths.get("shaders/shader.vert")));
            fsSource = new String(Files.readAllBytes(Paths.get("shaders/shader.frag")));
        } catch (IOException e)
        {
            throw new RuntimeException(e);
        }

        shaderProgram = gl.glCreateProgram();

        int vertexShader = gl.glCreateShader(GL_VERTEX_SHADER);
        gl.glShaderSource(vertexShader, vsSource);
        gl.glCompileShader(vertexShader);
        System.err.println(gl.glGetShaderInfoLog(vertexShader));
        gl.glAttachShader(shaderProgram, vertexShader);

        int fragmentShader = gl.glCreateShader(GL_FRAGMENT_SHADER);
        gl.glShaderSource(fragmentShader, fsSource);
        gl.glCompileShader(fragmentShader);
        System.err.println(gl.glGetShaderInfoLog(fragmentShader));
        gl.glAttachShader(shaderProgram, fragmentShader);

        gl.glLinkProgram(shaderProgram);
        gl.glDeleteShader(vertexShader);
        gl.glDeleteShader(fragmentShader);
    }
}

shader.vert的代码:

#version 330 core

layout (location = 0) in vec3 position;

void main()
{
    gl_Position = vec4(position.x, position.y, position.z, 1.0);
}

shader.frag的代码:

#version 330
out vec4 out_Color;
void main()
{
    out_Color = vec4(1.0f, 0.5f, 0.2f, 1.0f);
}

它在桌面上运行,一旦第一次调用render()完成,程序就会崩溃,没有堆栈跟踪和段错误日志。 config.useGL30设置为true。

为什么会崩溃?我做错了吗?

0 个答案:

没有答案