com.rfdickerson.openworld.RenderSurface.java Source code

Java tutorial

Introduction

Here is the source code for com.rfdickerson.openworld.RenderSurface.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rfdickerson.openworld;

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import org.lwjgl.util.glu.GLU;
import org.lwjgl.util.vector.Matrix4f;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Robert F. Dickerson
 */
public class RenderSurface extends GameAsset {

    private static final Logger log = LoggerFactory.getLogger(RenderSurface.class);

    public double width = 20;
    public double height = 20;
    public int subdivisions = 128;
    // private int vertexCount = 8;
    private int indicesCount = 0;

    private int vboId = 0;
    private int vaoId = 0;
    private int vboiId = 0;

    @Override
    void init() {

        float[] vertices = { -0.5f, 0f, -0.5f, -0.5f, 0f, 0.5f, 0.5f, 0f, 0.5f, 0.5f, 0f, -0.5f

        };

        byte[] indices = { 0, 1, 3, 1, 2, 3 };

        indicesCount = indices.length;
        ByteBuffer indicesBuffer = BufferUtils.createByteBuffer(indicesCount);
        indicesBuffer.put(indices);
        indicesBuffer.flip();

        FloatBuffer verticesBuffer = BufferUtils.createFloatBuffer(vertices.length);
        verticesBuffer.put(vertices);
        verticesBuffer.flip();

        //vertexCount = 6;

        vaoId = GL30.glGenVertexArrays();
        GL30.glBindVertexArray(vaoId);

        vboId = GL15.glGenBuffers();
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId);
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticesBuffer, GL15.GL_STATIC_DRAW);

        GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

        GL30.glBindVertexArray(0);

        vboiId = GL15.glGenBuffers();
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboiId);
        GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL15.GL_STATIC_DRAW);
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);

        this.isLoaded = true;

        exitOnGLError("init terrain patch");

    }

    @Override
    void draw(SceneNode node) {

        if (!this.isLoaded) {
            this.init();
        } else {

            GLSLShaderService shaderService = GLSLShaderService.getInstance();
            // use shader
            Shader basic = shaderService.findShader("basic");
            if (basic == null) {
                log.error("Could not find shader");
            } else {
                //shaderService.useShader(basic);
                //basic.setUniform4f("projectionMatrix", c.getProjectionMatrix());
                //basic.setUniform4f("viewMatrix", c.getViewMatrix());
                //basic.setUniform4f("modelMatrix", t);

                shaderService.useShader(basic);

                GL20.glBindAttribLocation(basic.getProgramID(), 0, "in_Position");
            }

            GL30.glBindVertexArray(vaoId);
            GL20.glEnableVertexAttribArray(0);

            GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboiId);

            GL11.glDrawElements(GL11.GL_TRIANGLES, indicesCount, GL11.GL_UNSIGNED_BYTE, 0);

            GL20.glDisableVertexAttribArray(0);
            GL30.glBindVertexArray(0);
        }

        exitOnGLError("draw terrain patch");

    }

    @Override
    void cleanup() {

        log.info("Cleaning up the SimpleTerrain");

        GL20.glDisableVertexAttribArray(0);

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
        GL15.glDeleteBuffers(vboId);

        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
        GL15.glDeleteBuffers(vboiId);

        GL30.glBindVertexArray(0);
        GL30.glDeleteVertexArrays(vaoId);

        this.isLoaded = false;

    }

    private void exitOnGLError(String errorMessage) {

        int errorValue = GL11.glGetError();

        if (errorValue != GL11.GL_NO_ERROR) {

            String errorString = GLU.gluErrorString(errorValue);

            log.error("ERROR - " + errorMessage + ": " + errorString);

        }

    }

}