Search in sources :

Example 1 with GL3

use of javax.media.opengl.GL3 in project spaceships-demo by puniverse.

the class GLPort method dispose.

@Override
public void dispose(GLAutoDrawable drawable) {
    final GL3 gl = drawable.getGL().getGL3();
    shaderState.bind(gl);
    shaderState.destroy(gl);
    vertices.destroy(gl);
}
Also used : GL3(javax.media.opengl.GL3) DebugGL3(javax.media.opengl.DebugGL3)

Example 2 with GL3

use of javax.media.opengl.GL3 in project spaceships-demo by puniverse.

the class GLPort method display.

@Override
public void display(GLAutoDrawable drawable) {
    try {
        final GL3 gl = drawable.getGL().getGL3();
        shaderState.bind(gl);
        vao.bind(gl);
        int spaceshipLoc = gl.glGetUniformLocation(shaderProgram.program(), "spaceshipTex");
        gl.glUniform1i(spaceshipLoc, 0);
        gl.glActiveTexture(GL.GL_TEXTURE0);
        gl.glBindTexture(GL.GL_TEXTURE_2D, spaceshipTex.getTextureObject(gl));
        int explosionLoc = gl.glGetUniformLocation(shaderProgram.program(), "explosionTex");
        gl.glUniform1i(explosionLoc, 1);
        gl.glActiveTexture(GL.GL_TEXTURE0 + 1);
        gl.glBindTexture(GL.GL_TEXTURE_2D, explosionTex.getTextureObject(gl));
        vertices.clear();
        colors.clear();
        final FloatBuffer verticesb = (FloatBuffer) vertices.getBuffer();
        final FloatBuffer colorsb = (FloatBuffer) colors.getBuffer();
        long now = global.now();
        long clock = System.currentTimeMillis();
        fixPort(clock, true);
        MutableAABB currentPort = getCurrentPort(clock);
        portToMvMatrix(currentPort);
        double margins = WIDTH_MARGINS;
        final int n;
        if (clock - lastQueryTime > SB_QUERY_RATE) {
            n = query(now, SpatialQueries.contained(AABB.create(currentPort.min(X) - margins, currentPort.max(X) + margins, currentPort.min(Y) - margins, currentPort.max(Y) + margins)));
            lastQueryTime = clock;
        } else {
            n = indexGen.get();
            lastDispTime = clock;
        }
        int countInPort = 0;
        for (Record<SpaceshipState> s : ships.slice(0, n)) {
            long extrapolationTime = global.extrapolate ? Math.min(now, s.get($lastMoved) + MAX_EXTRAPOLATION_DURATION) : s.get($lastMoved);
            Spaceship.getCurrentLocation(s, extrapolationTime, verticesb);
            if (// 0.01 - start blow animation, 1.0 - end of animation
            s.get($blowTime) > 0)
                colorsb.put(Math.min(1.0f, (now - s.get($blowTime)) / EXPLOSION_DURATION));
            else
                // ship isn't blowing up
                colorsb.put(0);
            colorsb.put((float) Spaceship.getCurrentHeading(s, extrapolationTime));
            // put the shotLength (0 for ship that's not firing)
            colorsb.put(now - s.get($timeFired) < SHOOT_DURATION ? (float) s.get($shotLength) : 0f);
            if (portContains(s.get($x), s.get($y)))
                countInPort++;
        }
        setTitle((glxNode >= 0 ? "Node " + glxNode + ": " : "") + countInPort + " Spaceships " + (int) (port.max(X) - port.min(X)) + "x" + (int) (port.max(Y) - port.min(Y)));
        vertices.flip();
        colors.flip();
        int numElems = verticesb.limit() / 2;
        vertices.write(gl, 0, numElems);
        colors.write(gl, 0, numElems);
        shaderState.setUniform(gl, "in_Matrix", 4, 4, pmv.glGetMvMatrixf());
        gl.glClear(GL3.GL_COLOR_BUFFER_BIT);
        gl.glDrawArrays(GL3.GL_POINTS, 0, numElems);
        vao.unbind(gl);
        shaderState.unbind(gl);
    } catch (Throwable t) {
        System.err.println("XXXXXX");
        t.printStackTrace();
        throw t;
    }
}
Also used : FloatBuffer(java.nio.FloatBuffer) MutableAABB(co.paralleluniverse.spacebase.MutableAABB) SpaceshipState(co.paralleluniverse.spaceships.SpaceshipState) GL3(javax.media.opengl.GL3) DebugGL3(javax.media.opengl.DebugGL3)

Example 3 with GL3

use of javax.media.opengl.GL3 in project spaceships-demo by puniverse.

the class GLPort method init.

@Override
public void init(GLAutoDrawable drawable) {
    drawable.setGL(new DebugGL3(drawable.getGL().getGL3()));
    final GL3 gl = drawable.getGL().getGL3();
    try {
        spaceshipTex = TextureIO.newTexture(TextureIO.newTextureData(GLProfile.get(GLProfile.GL3), ClassLoader.getSystemResourceAsStream("spaceship.png"), false, "png"));
        explosionTex = TextureIO.newTexture(TextureIO.newTextureData(GLProfile.get(GLProfile.GL3), ClassLoader.getSystemResourceAsStream("explosion.png"), false, "png"));
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
    drawableWidth = drawable.getSurfaceWidth();
    drawableHeight = drawable.getSurfaceHeight();
    port.min(X, -drawable.getSurfaceWidth() / 2);
    port.max(X, drawable.getSurfaceWidth() / 2);
    port.min(Y, -drawable.getSurfaceHeight() / 2);
    port.max(Y, drawable.getSurfaceHeight() / 2);
    // gl.glEnable(gl.GL_VERTEX_PROGRAM_POINT_SIZE);
    gl.glViewport(0, 0, (int) (port.max(X) - port.min(X)), (int) (port.max(Y) - port.min(Y)));
    gl.glClearColor(0, 0, 0, 1);
    gl.glEnable(GL.GL_BLEND);
    gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE_MINUS_SRC_ALPHA);
    ShaderCode vertexShader = ShaderCode.create(gl, GL3.GL_VERTEX_SHADER, this.getClass(), "shader", null, "vertex", false);
    ShaderCode geometryShader = ShaderCode.create(gl, GL3.GL_GEOMETRY_SHADER, this.getClass(), "shader", null, "geometry", false);
    ShaderCode fragmentShader = ShaderCode.create(gl, GL3.GL_FRAGMENT_SHADER, this.getClass(), "shader", null, "fragment", false);
    if (!vertexShader.compile(gl, System.err))
        throw new GLException("Couldn't compile shader: " + vertexShader);
    if (!geometryShader.compile(gl, System.err))
        throw new GLException("Couldn't compile shader: " + geometryShader);
    if (!fragmentShader.compile(gl, System.err))
        throw new GLException("Couldn't compile shader: " + fragmentShader);
    shaderProgram = new ShaderProgram();
    shaderProgram.add(gl, vertexShader, System.err);
    shaderProgram.add(gl, geometryShader, System.err);
    shaderProgram.add(gl, fragmentShader, System.err);
    if (!shaderProgram.link(gl, System.err))
        throw new GLException("Couldn't link program: " + shaderProgram);
    this.shaderState = new ProgramState(gl, shaderProgram);
    shaderState.bind(gl);
    pmv.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
    pmv.glLoadIdentity();
    this.vao = shaderState.createVAO(gl);
    vao.bind(gl);
    this.vertices = new VBO(gl, 2, GL3.GL_FLOAT, false, maxItems, GL3.GL_DYNAMIC_DRAW);
    this.colors = new VBO(gl, 3, GL3.GL_FLOAT, false, maxItems, GL3.GL_DYNAMIC_DRAW);
    vao.setVertex(gl, "in_Position", vertices);
    vao.setVertex(gl, "in_Vertex", colors);
    shaderState.unbind(gl);
}
Also used : ShaderProgram(com.jogamp.opengl.util.glsl.ShaderProgram) GLException(javax.media.opengl.GLException) DebugGL3(javax.media.opengl.DebugGL3) IOException(java.io.IOException) GL3(javax.media.opengl.GL3) DebugGL3(javax.media.opengl.DebugGL3) ShaderCode(com.jogamp.opengl.util.glsl.ShaderCode)

Aggregations

DebugGL3 (javax.media.opengl.DebugGL3)3 GL3 (javax.media.opengl.GL3)3 MutableAABB (co.paralleluniverse.spacebase.MutableAABB)1 SpaceshipState (co.paralleluniverse.spaceships.SpaceshipState)1 ShaderCode (com.jogamp.opengl.util.glsl.ShaderCode)1 ShaderProgram (com.jogamp.opengl.util.glsl.ShaderProgram)1 IOException (java.io.IOException)1 FloatBuffer (java.nio.FloatBuffer)1 GLException (javax.media.opengl.GLException)1