Java tutorial
/* * Copyright (C) 2012 Robert 'Bobby' Zenz * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package org.bonsaimind.badgersvoyage.opengl; 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.vector.Matrix4f; import org.lwjgl.util.vector.Vector3f; /** * An Object which can be rendered. * @author Robert 'Bobby' Zenz */ public abstract class RenderObject { protected Vector3f position = new Vector3f(0f, 0f, 0f); protected Vector3f scale = new Vector3f(1f, 1f, 1f); protected Vector3f rotation = new Vector3f(0f, 0f, 0f); protected float vertices[]; private int verticesCount; protected Matrix4f modelMatrix; protected FloatBuffer modelMatrixBuffer; protected int modelMatrixLocation; private int vaoId; private int vboId; private FloatBuffer verticesBuffer; public Vector3f getPosition() { return position; } public void setPosition(Vector3f position) { this.position = position; } public Vector3f getRotation() { return rotation; } public void setRotation(Vector3f rotation) { this.rotation = rotation; } public Vector3f getScale() { return scale; } public void setScale(Vector3f scale) { this.scale = scale; } public abstract void create(int programId); public void render() { GL30.glBindVertexArray(vaoId); GL20.glEnableVertexAttribArray(0); GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, verticesCount); } public void translate() { modelMatrix = new Matrix4f(); Matrix4f.scale(scale, modelMatrix, modelMatrix); Matrix4f.translate(position, modelMatrix, modelMatrix); Matrix4f.rotate((float) Math.toRadians(rotation.z), new Vector3f(0, 0, 1), modelMatrix, modelMatrix); Matrix4f.rotate((float) Math.toRadians(rotation.y), new Vector3f(0, 1, 0), modelMatrix, modelMatrix); Matrix4f.rotate((float) Math.toRadians(rotation.x), new Vector3f(1, 0, 0), modelMatrix, modelMatrix); modelMatrix.store(modelMatrixBuffer); modelMatrixBuffer.flip(); GL20.glUniformMatrix4(modelMatrixLocation, false, modelMatrixBuffer); } protected void createOpenGL(int programId) { modelMatrix = new Matrix4f(); modelMatrixLocation = GL20.glGetUniformLocation(programId, "modelMatrix"); modelMatrixBuffer = BufferUtils.createFloatBuffer(16); verticesCount = vertices.length / 3; verticesBuffer = BufferUtils.createFloatBuffer(vertices.length); verticesBuffer.put(vertices); verticesBuffer.flip(); 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); } }